zitadel/internal/api/grpc/user/membership.go
Silvan d2ea9a1b8c
feat: member queries (#2796)
* refactor(domain): add user type

* fix(projections): start with login names

* fix(login_policy): correct handling of user domain claimed event

* fix(projections): add members

* refactor: simplify member projections

* add migration for members

* add metadata to member projections

* refactor: login name projection

* fix: set correct suffixes on login name projections

* test(projections): login name reduces

* fix: correct cols in reduce member

* test(projections): org, iam, project members

* member additional cols and conds as opt,
add project grant members

* fix(migration): members

* fix(migration): correct database name

* migration version

* migs

* better naming for member cond and col

* split project and project grant members

* prepare member columns

* feat(queries): membership query

* test(queries): membership prepare

* fix(queries): multiple projections for latest sequence

* fix(api): use query for membership queries in auth and management

* feat: org member queries

* fix(api): use query for iam member calls

* fix(queries): org members

* fix(queries): project members

* fix(queries): project grant members

* fix(query): member queries and user avatar column

* member cols

* fix(queries): membership stmt

* fix user test

* fix user test

* fix(membership): correct display name

* fix(projection): additional member manipulation events

* additional member tests

* fix(projections): additional events of idp links

* fix: use query for memberships (#2797)

* fix(api): use query for memberships

* remove comment

* handle err

* refactor(projections): idp user link user aggregate type

* fix(projections): handle old user events

* fix(api): add asset prefix

* no image for iam members
2021-12-16 13:25:38 +00:00

139 lines
4.2 KiB
Go

package user
import (
"github.com/caos/zitadel/internal/api/grpc/object"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/query"
user_model "github.com/caos/zitadel/internal/user/model"
user_pb "github.com/caos/zitadel/pkg/grpc/user"
)
func MembershipQueriesToQuery(queries []*user_pb.MembershipQuery) (_ []query.SearchQuery, err error) {
q := make([]query.SearchQuery, 0)
for _, query := range queries {
qs, err := MembershipQueryToQuery(query)
if err != nil {
return nil, err
}
q = append(q, qs)
}
return q, nil
}
func MembershipQueryToQuery(req *user_pb.MembershipQuery) (query.SearchQuery, error) {
switch q := req.Query.(type) {
case *user_pb.MembershipQuery_OrgQuery:
return query.NewMembershipOrgIDQuery(q.OrgQuery.OrgId)
case *user_pb.MembershipQuery_ProjectQuery:
return query.NewMembershipProjectIDQuery(q.ProjectQuery.ProjectId)
case *user_pb.MembershipQuery_ProjectGrantQuery:
return query.NewMembershipProjectGrantIDQuery(q.ProjectGrantQuery.ProjectGrantId)
case *user_pb.MembershipQuery_IamQuery:
return query.NewMembershipIsIAMQuery()
default:
return nil, errors.ThrowInvalidArgument(nil, "USER-dsg3z", "Errors.List.Query.Invalid")
}
}
func MembershipIAMQueryToModel(q *user_pb.MembershipIAMQuery) []*user_model.UserMembershipSearchQuery {
return []*user_model.UserMembershipSearchQuery{
{
Key: user_model.UserMembershipSearchKeyMemberType,
Method: domain.SearchMethodEquals,
Value: user_model.MemberTypeIam,
},
//TODO: q.IAM?
}
}
func MembershipOrgQueryToModel(q *user_pb.MembershipOrgQuery) []*user_model.UserMembershipSearchQuery {
return []*user_model.UserMembershipSearchQuery{
{
Key: user_model.UserMembershipSearchKeyMemberType,
Method: domain.SearchMethodEquals,
Value: user_model.MemberTypeOrganisation,
},
{
Key: user_model.UserMembershipSearchKeyObjectID,
Method: domain.SearchMethodEquals,
Value: q.OrgId,
},
}
}
func MembershipProjectQueryToModel(q *user_pb.MembershipProjectQuery) []*user_model.UserMembershipSearchQuery {
return []*user_model.UserMembershipSearchQuery{
{
Key: user_model.UserMembershipSearchKeyMemberType,
Method: domain.SearchMethodEquals,
Value: user_model.MemberTypeProject,
},
{
Key: user_model.UserMembershipSearchKeyObjectID,
Method: domain.SearchMethodEquals,
Value: q.ProjectId,
},
}
}
func MembershipProjectGrantQueryToModel(q *user_pb.MembershipProjectGrantQuery) []*user_model.UserMembershipSearchQuery {
return []*user_model.UserMembershipSearchQuery{
{
Key: user_model.UserMembershipSearchKeyMemberType,
Method: domain.SearchMethodEquals,
Value: user_model.MemberTypeProjectGrant,
},
{
Key: user_model.UserMembershipSearchKeyObjectID,
Method: domain.SearchMethodEquals,
Value: q.ProjectGrantId,
},
}
}
func MembershipsToMembershipsPb(memberships []*query.Membership) []*user_pb.Membership {
converted := make([]*user_pb.Membership, len(memberships))
for i, membership := range memberships {
converted[i] = MembershipToMembershipPb(membership)
}
return converted
}
func MembershipToMembershipPb(membership *query.Membership) *user_pb.Membership {
typ, name := memberTypeToPb(membership)
return &user_pb.Membership{
UserId: membership.UserID,
Type: typ,
DisplayName: name,
Roles: membership.Roles,
Details: object.ToViewDetailsPb(
membership.Sequence,
membership.CreationDate,
membership.ChangeDate,
membership.ResourceOwner,
),
}
}
func memberTypeToPb(membership *query.Membership) (user_pb.MembershipType, string) {
if membership.Org != nil {
return &user_pb.Membership_OrgId{
OrgId: membership.Org.OrgID,
}, membership.Org.Name
} else if membership.Project != nil {
return &user_pb.Membership_ProjectId{
ProjectId: membership.Project.ProjectID,
}, membership.Project.Name
} else if membership.ProjectGrant != nil {
return &user_pb.Membership_ProjectGrantId{
ProjectGrantId: membership.ProjectGrant.GrantID,
}, membership.ProjectGrant.ProjectName
} else if membership.IAM != nil {
return &user_pb.Membership_Iam{
Iam: true,
}, membership.IAM.Name
}
return nil, ""
}