2021-12-14 08:19:02 +01:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2022-03-23 09:02:39 +01:00
|
|
|
"github.com/lib/pq"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
2021-12-14 08:19:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Memberships struct {
|
|
|
|
SearchResponse
|
|
|
|
Memberships []*Membership
|
|
|
|
}
|
|
|
|
|
|
|
|
type Membership struct {
|
|
|
|
UserID string
|
|
|
|
Roles []string
|
|
|
|
CreationDate time.Time
|
|
|
|
ChangeDate time.Time
|
|
|
|
Sequence uint64
|
|
|
|
ResourceOwner string
|
|
|
|
|
|
|
|
Org *OrgMembership
|
|
|
|
IAM *IAMMembership
|
|
|
|
Project *ProjectMembership
|
|
|
|
ProjectGrant *ProjectGrantMembership
|
|
|
|
}
|
|
|
|
|
|
|
|
type OrgMembership struct {
|
|
|
|
OrgID string
|
2021-12-16 14:25:38 +01:00
|
|
|
Name string
|
2021-12-14 08:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type IAMMembership struct {
|
|
|
|
IAMID string
|
2021-12-16 14:25:38 +01:00
|
|
|
Name string
|
2021-12-14 08:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectMembership struct {
|
|
|
|
ProjectID string
|
2021-12-16 14:25:38 +01:00
|
|
|
Name string
|
2021-12-14 08:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGrantMembership struct {
|
2021-12-16 14:25:38 +01:00
|
|
|
ProjectID string
|
|
|
|
ProjectName string
|
|
|
|
GrantID string
|
2021-12-14 08:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type MembershipSearchQuery struct {
|
|
|
|
SearchRequest
|
|
|
|
Queries []SearchQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipUserIDQuery(userID string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(membershipUserID.setTable(membershipAlias), userID, TextEquals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipResourceOwnerQuery(value string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(membershipResourceOwner.setTable(membershipAlias), value, TextEquals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipOrgIDQuery(value string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(membershipOrgID, value, TextEquals)
|
|
|
|
}
|
|
|
|
|
2022-03-24 14:00:24 +01:00
|
|
|
func NewMembershipResourceOwnersSearchQuery(ids ...string) (SearchQuery, error) {
|
|
|
|
list := make([]interface{}, len(ids))
|
|
|
|
for i, value := range ids {
|
|
|
|
list[i] = value
|
|
|
|
}
|
|
|
|
return NewListQuery(membershipResourceOwner, list, ListIn)
|
|
|
|
}
|
|
|
|
|
2021-12-14 08:19:02 +01:00
|
|
|
func NewMembershipProjectIDQuery(value string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(membershipProjectID, value, TextEquals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipProjectGrantIDQuery(value string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(membershipGrantID, value, TextEquals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMembershipIsIAMQuery() (SearchQuery, error) {
|
|
|
|
return NewNotNullQuery(membershipIAMID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *MembershipSearchQuery) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
|
|
|
query = q.SearchRequest.toQuery(query)
|
|
|
|
for _, q := range q.Queries {
|
|
|
|
query = q.toQuery(query)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) Memberships(ctx context.Context, queries *MembershipSearchQuery) (*Memberships, error) {
|
|
|
|
query, scan := prepareMembershipsQuery()
|
2022-03-23 09:02:39 +01:00
|
|
|
stmt, args, err := queries.toQuery(query).
|
|
|
|
Where(sq.Eq{
|
2022-03-29 11:53:19 +02:00
|
|
|
membershipInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
2022-03-23 09:02:39 +01:00
|
|
|
}).ToSql()
|
2021-12-14 08:19:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInvalidArgument(err, "QUERY-T84X9", "Errors.Query.InvalidRequest")
|
|
|
|
}
|
2022-03-24 17:21:34 +01:00
|
|
|
latestSequence, err := q.latestSequence(ctx, orgMemberTable, instanceMemberTable, projectMemberTable, projectGrantMemberTable)
|
2021-12-14 08:19:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := q.client.QueryContext(ctx, stmt, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-eAV2x", "Errors.Internal")
|
|
|
|
}
|
|
|
|
memberships, err := scan(rows)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
memberships.LatestSequence = latestSequence
|
|
|
|
return memberships, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
//membershipAlias is a hack to satisfy checks in the queries
|
|
|
|
membershipAlias = table{
|
|
|
|
name: "memberships",
|
|
|
|
}
|
|
|
|
membershipUserID = Column{
|
|
|
|
name: projection.MemberUserIDCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipRoles = Column{
|
|
|
|
name: projection.MemberRolesCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipCreationDate = Column{
|
|
|
|
name: projection.MemberCreationDate,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipChangeDate = Column{
|
|
|
|
name: projection.MemberChangeDate,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipSequence = Column{
|
|
|
|
name: projection.MemberSequence,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipResourceOwner = Column{
|
|
|
|
name: projection.MemberResourceOwner,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
2022-03-23 09:02:39 +01:00
|
|
|
membershipInstanceID = Column{
|
|
|
|
name: projection.MemberInstanceID,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
2021-12-14 08:19:02 +01:00
|
|
|
membershipOrgID = Column{
|
|
|
|
name: projection.OrgMemberOrgIDCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipIAMID = Column{
|
2022-03-24 17:21:34 +01:00
|
|
|
name: projection.InstanceMemberIAMIDCol,
|
2021-12-14 08:19:02 +01:00
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipProjectID = Column{
|
|
|
|
name: projection.ProjectMemberProjectIDCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
membershipGrantID = Column{
|
|
|
|
name: projection.ProjectGrantMemberGrantIDCol,
|
|
|
|
table: membershipAlias,
|
|
|
|
}
|
|
|
|
|
|
|
|
membershipFrom = "(" +
|
|
|
|
prepareOrgMember() +
|
|
|
|
" UNION ALL " +
|
|
|
|
prepareIAMMember() +
|
|
|
|
" UNION ALL " +
|
|
|
|
prepareProjectMember() +
|
|
|
|
" UNION ALL " +
|
|
|
|
prepareProjectGrantMember() +
|
|
|
|
") AS " + membershipAlias.identifier()
|
|
|
|
)
|
|
|
|
|
|
|
|
func prepareMembershipsQuery() (sq.SelectBuilder, func(*sql.Rows) (*Memberships, error)) {
|
|
|
|
return sq.Select(
|
|
|
|
membershipUserID.identifier(),
|
|
|
|
membershipRoles.identifier(),
|
|
|
|
membershipCreationDate.identifier(),
|
|
|
|
membershipChangeDate.identifier(),
|
|
|
|
membershipSequence.identifier(),
|
|
|
|
membershipResourceOwner.identifier(),
|
|
|
|
membershipOrgID.identifier(),
|
|
|
|
membershipIAMID.identifier(),
|
|
|
|
membershipProjectID.identifier(),
|
|
|
|
membershipGrantID.identifier(),
|
2021-12-16 14:25:38 +01:00
|
|
|
ProjectColumnName.identifier(),
|
|
|
|
OrgColumnName.identifier(),
|
2021-12-14 08:19:02 +01:00
|
|
|
countColumn.identifier(),
|
|
|
|
).From(membershipFrom).
|
2021-12-16 14:25:38 +01:00
|
|
|
LeftJoin(join(ProjectColumnID, membershipProjectID)).
|
|
|
|
LeftJoin(join(OrgColumnID, membershipOrgID)).
|
2021-12-14 08:19:02 +01:00
|
|
|
PlaceholderFormat(sq.Dollar),
|
|
|
|
func(rows *sql.Rows) (*Memberships, error) {
|
|
|
|
memberships := make([]*Membership, 0)
|
|
|
|
var count uint64
|
|
|
|
for rows.Next() {
|
|
|
|
|
|
|
|
var (
|
|
|
|
membership = new(Membership)
|
|
|
|
orgID = sql.NullString{}
|
|
|
|
iamID = sql.NullString{}
|
|
|
|
projectID = sql.NullString{}
|
|
|
|
grantID = sql.NullString{}
|
|
|
|
roles = pq.StringArray{}
|
2021-12-16 14:25:38 +01:00
|
|
|
projectName = sql.NullString{}
|
|
|
|
orgName = sql.NullString{}
|
2021-12-14 08:19:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
err := rows.Scan(
|
|
|
|
&membership.UserID,
|
|
|
|
&roles,
|
|
|
|
&membership.CreationDate,
|
|
|
|
&membership.ChangeDate,
|
|
|
|
&membership.Sequence,
|
|
|
|
&membership.ResourceOwner,
|
|
|
|
&orgID,
|
|
|
|
&iamID,
|
|
|
|
&projectID,
|
|
|
|
&grantID,
|
2021-12-16 14:25:38 +01:00
|
|
|
&projectName,
|
|
|
|
&orgName,
|
2021-12-14 08:19:02 +01:00
|
|
|
&count,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
membership.Roles = roles
|
|
|
|
|
|
|
|
if orgID.Valid {
|
|
|
|
membership.Org = &OrgMembership{
|
|
|
|
OrgID: orgID.String,
|
2021-12-16 14:25:38 +01:00
|
|
|
Name: orgName.String,
|
2021-12-14 08:19:02 +01:00
|
|
|
}
|
|
|
|
} else if iamID.Valid {
|
|
|
|
membership.IAM = &IAMMembership{
|
|
|
|
IAMID: iamID.String,
|
2021-12-16 14:25:38 +01:00
|
|
|
Name: iamID.String,
|
2021-12-14 08:19:02 +01:00
|
|
|
}
|
|
|
|
} else if projectID.Valid && grantID.Valid {
|
|
|
|
membership.ProjectGrant = &ProjectGrantMembership{
|
2021-12-16 14:25:38 +01:00
|
|
|
ProjectID: projectID.String,
|
|
|
|
ProjectName: projectName.String,
|
|
|
|
GrantID: grantID.String,
|
2021-12-14 08:19:02 +01:00
|
|
|
}
|
|
|
|
} else if projectID.Valid {
|
|
|
|
membership.Project = &ProjectMembership{
|
|
|
|
ProjectID: projectID.String,
|
2021-12-16 14:25:38 +01:00
|
|
|
Name: projectName.String,
|
2021-12-14 08:19:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memberships = append(memberships, membership)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-N34NV", "Errors.Query.CloseRows")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Memberships{
|
|
|
|
Memberships: memberships,
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: count,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareOrgMember() string {
|
|
|
|
stmt, _ := sq.Select(
|
|
|
|
OrgMemberUserID.identifier(),
|
|
|
|
OrgMemberRoles.identifier(),
|
|
|
|
OrgMemberCreationDate.identifier(),
|
|
|
|
OrgMemberChangeDate.identifier(),
|
|
|
|
OrgMemberSequence.identifier(),
|
|
|
|
OrgMemberResourceOwner.identifier(),
|
2022-03-29 11:53:19 +02:00
|
|
|
OrgMemberInstanceID.identifier(),
|
2021-12-14 08:19:02 +01:00
|
|
|
OrgMemberOrgID.identifier(),
|
|
|
|
"NULL::STRING AS "+membershipIAMID.name,
|
|
|
|
"NULL::STRING AS "+membershipProjectID.name,
|
|
|
|
"NULL::STRING AS "+membershipGrantID.name,
|
|
|
|
).From(orgMemberTable.identifier()).MustSql()
|
|
|
|
return stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareIAMMember() string {
|
|
|
|
stmt, _ := sq.Select(
|
2022-03-24 17:21:34 +01:00
|
|
|
InstanceMemberUserID.identifier(),
|
|
|
|
InstanceMemberRoles.identifier(),
|
|
|
|
InstanceMemberCreationDate.identifier(),
|
|
|
|
InstanceMemberChangeDate.identifier(),
|
|
|
|
InstanceMemberSequence.identifier(),
|
|
|
|
InstanceMemberResourceOwner.identifier(),
|
2022-03-29 11:53:19 +02:00
|
|
|
InstanceMemberInstanceID.identifier(),
|
2021-12-14 08:19:02 +01:00
|
|
|
"NULL::STRING AS "+membershipOrgID.name,
|
2022-03-24 17:21:34 +01:00
|
|
|
InstanceMemberIAMID.identifier(),
|
2021-12-14 08:19:02 +01:00
|
|
|
"NULL::STRING AS "+membershipProjectID.name,
|
|
|
|
"NULL::STRING AS "+membershipGrantID.name,
|
2022-03-24 17:21:34 +01:00
|
|
|
).From(instanceMemberTable.identifier()).MustSql()
|
2021-12-14 08:19:02 +01:00
|
|
|
return stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareProjectMember() string {
|
|
|
|
stmt, _ := sq.Select(
|
|
|
|
ProjectMemberUserID.identifier(),
|
|
|
|
ProjectMemberRoles.identifier(),
|
|
|
|
ProjectMemberCreationDate.identifier(),
|
|
|
|
ProjectMemberChangeDate.identifier(),
|
|
|
|
ProjectMemberSequence.identifier(),
|
|
|
|
ProjectMemberResourceOwner.identifier(),
|
2022-03-29 11:53:19 +02:00
|
|
|
ProjectMemberInstanceID.identifier(),
|
2021-12-14 08:19:02 +01:00
|
|
|
"NULL::STRING AS "+membershipOrgID.name,
|
|
|
|
"NULL::STRING AS "+membershipIAMID.name,
|
|
|
|
ProjectMemberProjectID.identifier(),
|
|
|
|
"NULL::STRING AS "+membershipGrantID.name,
|
|
|
|
).From(projectMemberTable.identifier()).MustSql()
|
|
|
|
|
|
|
|
return stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareProjectGrantMember() string {
|
|
|
|
stmt, _ := sq.Select(
|
|
|
|
ProjectGrantMemberUserID.identifier(),
|
|
|
|
ProjectGrantMemberRoles.identifier(),
|
|
|
|
ProjectGrantMemberCreationDate.identifier(),
|
|
|
|
ProjectGrantMemberChangeDate.identifier(),
|
|
|
|
ProjectGrantMemberSequence.identifier(),
|
|
|
|
ProjectGrantMemberResourceOwner.identifier(),
|
2022-03-29 11:53:19 +02:00
|
|
|
ProjectGrantMemberInstanceID.identifier(),
|
2021-12-14 08:19:02 +01:00
|
|
|
"NULL::STRING AS "+membershipOrgID.name,
|
|
|
|
"NULL::STRING AS "+membershipIAMID.name,
|
|
|
|
ProjectGrantMemberProjectID.identifier(),
|
|
|
|
ProjectGrantMemberGrantID.identifier(),
|
|
|
|
).From(projectGrantMemberTable.identifier()).MustSql()
|
|
|
|
|
|
|
|
return stmt
|
|
|
|
}
|