mirror of
https://github.com/zitadel/zitadel.git
synced 2025-07-28 10:13:42 +00:00
fix: queries (#2548)
* fix(second_factors): correct query, rename method * fix(multi_factors): correct query, correct naming * fix(project_grant): add project_id to queries * fix: simplify project id query constructor
This commit is contained in:
parent
b1caef81da
commit
22cfad8cb0
@ -107,7 +107,7 @@ func (s *Server) RemoveIDPFromLoginPolicy(ctx context.Context, req *mgmt_pb.Remo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ListLoginPolicySecondFactors(ctx context.Context, req *mgmt_pb.ListLoginPolicySecondFactorsRequest) (*mgmt_pb.ListLoginPolicySecondFactorsResponse, error) {
|
func (s *Server) ListLoginPolicySecondFactors(ctx context.Context, req *mgmt_pb.ListLoginPolicySecondFactorsRequest) (*mgmt_pb.ListLoginPolicySecondFactorsResponse, error) {
|
||||||
result, err := s.query.SecondFactorsByID(ctx, authz.GetCtxData(ctx).OrgID)
|
result, err := s.query.SecondFactorsByOrg(ctx, authz.GetCtxData(ctx).OrgID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -138,7 +138,7 @@ func (s *Server) RemoveSecondFactorFromLoginPolicy(ctx context.Context, req *mgm
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ListLoginPolicyMultiFactors(ctx context.Context, req *mgmt_pb.ListLoginPolicyMultiFactorsRequest) (*mgmt_pb.ListLoginPolicyMultiFactorsResponse, error) {
|
func (s *Server) ListLoginPolicyMultiFactors(ctx context.Context, req *mgmt_pb.ListLoginPolicyMultiFactorsRequest) (*mgmt_pb.ListLoginPolicyMultiFactorsResponse, error) {
|
||||||
res, err := s.query.MultiFactorsByID(ctx, authz.GetCtxData(ctx).OrgID)
|
res, err := s.query.MultiFactorsByOrg(ctx, authz.GetCtxData(ctx).OrgID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -27,16 +27,16 @@ func (s *Server) ListProjectGrants(ctx context.Context, req *mgmt_pb.ListProject
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
queries.AppendMyResourceOwnerQuery(authz.GetCtxData(ctx).OrgID)
|
queries.AppendMyResourceOwnerQuery(authz.GetCtxData(ctx).OrgID)
|
||||||
domains, err := s.query.SearchProjectGrants(ctx, queries)
|
grants, err := s.query.SearchProjectGrants(ctx, queries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &mgmt_pb.ListProjectGrantsResponse{
|
return &mgmt_pb.ListProjectGrantsResponse{
|
||||||
Result: proj_grpc.GrantedProjectViewsToPb(domains.ProjectGrants),
|
Result: proj_grpc.GrantedProjectViewsToPb(grants.ProjectGrants),
|
||||||
Details: object_grpc.ToListDetails(
|
Details: object_grpc.ToListDetails(
|
||||||
domains.Count,
|
grants.Count,
|
||||||
domains.Sequence,
|
grants.Sequence,
|
||||||
domains.Timestamp,
|
grants.Timestamp,
|
||||||
),
|
),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -57,11 +57,11 @@ func (s *Server) AddProjectGrant(ctx context.Context, req *mgmt_pb.AddProjectGra
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) UpdateProjectGrant(ctx context.Context, req *mgmt_pb.UpdateProjectGrantRequest) (*mgmt_pb.UpdateProjectGrantResponse, error) {
|
func (s *Server) UpdateProjectGrant(ctx context.Context, req *mgmt_pb.UpdateProjectGrantRequest) (*mgmt_pb.UpdateProjectGrantResponse, error) {
|
||||||
userGrants, err := s.usergrant.UserGrantsByProjectAndGrantID(ctx, req.ProjectId, req.GrantId)
|
grants, err := s.usergrant.UserGrantsByProjectAndGrantID(ctx, req.ProjectId, req.GrantId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
grant, err := s.command.ChangeProjectGrant(ctx, UpdateProjectGrantRequestToDomain(req), authz.GetCtxData(ctx).OrgID, userGrantsToIDs(userGrants)...)
|
grant, err := s.command.ChangeProjectGrant(ctx, UpdateProjectGrantRequestToDomain(req), authz.GetCtxData(ctx).OrgID, userGrantsToIDs(grants)...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
|
|
||||||
func listProjectGrantsRequestToModel(req *mgmt_pb.ListProjectGrantsRequest) (*query.ProjectGrantSearchQueries, error) {
|
func listProjectGrantsRequestToModel(req *mgmt_pb.ListProjectGrantsRequest) (*query.ProjectGrantSearchQueries, error) {
|
||||||
offset, limit, asc := object.ListQueryToModel(req.Query)
|
offset, limit, asc := object.ListQueryToModel(req.Query)
|
||||||
queries, err := ProjectGrantQueriesToModel(req.Queries)
|
queries, err := ProjectGrantQueriesToModel(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -28,15 +28,22 @@ func listProjectGrantsRequestToModel(req *mgmt_pb.ListProjectGrantsRequest) (*qu
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProjectGrantQueriesToModel(queries []*proj_pb.ProjectGrantQuery) (_ []query.SearchQuery, err error) {
|
func ProjectGrantQueriesToModel(req *mgmt_pb.ListProjectGrantsRequest) (_ []query.SearchQuery, err error) {
|
||||||
q := make([]query.SearchQuery, len(queries))
|
queries := make([]query.SearchQuery, 0, len(req.Queries)+1)
|
||||||
for i, query := range queries {
|
for _, query := range req.Queries {
|
||||||
q[i], err = ProjectGrantQueryToModel(query)
|
q, err := ProjectGrantQueryToModel(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
queries = append(queries, q)
|
||||||
}
|
}
|
||||||
return q, nil
|
projectIDQuery, err := query.NewProjectGrantProjectIDSearchQuery(req.ProjectId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
queries = append(queries, projectIDQuery)
|
||||||
|
|
||||||
|
return queries, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProjectGrantQueryToModel(apiQuery *proj_pb.ProjectGrantQuery) (query.SearchQuery, error) {
|
func ProjectGrantQueryToModel(apiQuery *proj_pb.ProjectGrantQuery) (query.SearchQuery, error) {
|
||||||
|
@ -118,7 +118,7 @@ func (q *Queries) DefaultLoginPolicy(ctx context.Context) (*LoginPolicy, error)
|
|||||||
return scan(row)
|
return scan(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) SecondFactorsByID(ctx context.Context, orgID string) (*SecondFactors, error) {
|
func (q *Queries) SecondFactorsByOrg(ctx context.Context, orgID string) (*SecondFactors, error) {
|
||||||
query, scan := prepareLoginPolicy2FAsQuery()
|
query, scan := prepareLoginPolicy2FAsQuery()
|
||||||
stmt, args, err := query.Where(
|
stmt, args, err := query.Where(
|
||||||
sq.Or{
|
sq.Or{
|
||||||
@ -136,7 +136,12 @@ func (q *Queries) SecondFactorsByID(ctx context.Context, orgID string) (*SecondF
|
|||||||
}
|
}
|
||||||
|
|
||||||
row := q.client.QueryRowContext(ctx, stmt, args...)
|
row := q.client.QueryRowContext(ctx, stmt, args...)
|
||||||
return scan(row)
|
factors, err := scan(row)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
factors.LatestSequence, err = q.latestSequence(ctx, loginPolicyTable)
|
||||||
|
return factors, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) DefaultSecondFactors(ctx context.Context) (*SecondFactors, error) {
|
func (q *Queries) DefaultSecondFactors(ctx context.Context) (*SecondFactors, error) {
|
||||||
@ -149,10 +154,15 @@ func (q *Queries) DefaultSecondFactors(ctx context.Context) (*SecondFactors, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
row := q.client.QueryRowContext(ctx, stmt, args...)
|
row := q.client.QueryRowContext(ctx, stmt, args...)
|
||||||
return scan(row)
|
factors, err := scan(row)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
factors.LatestSequence, err = q.latestSequence(ctx, loginPolicyTable)
|
||||||
|
return factors, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) MultiFactorsByID(ctx context.Context, orgID string) (*MultiFactors, error) {
|
func (q *Queries) MultiFactorsByOrg(ctx context.Context, orgID string) (*MultiFactors, error) {
|
||||||
query, scan := prepareLoginPolicyMFAsQuery()
|
query, scan := prepareLoginPolicyMFAsQuery()
|
||||||
stmt, args, err := query.Where(
|
stmt, args, err := query.Where(
|
||||||
sq.Or{
|
sq.Or{
|
||||||
@ -170,7 +180,12 @@ func (q *Queries) MultiFactorsByID(ctx context.Context, orgID string) (*MultiFac
|
|||||||
}
|
}
|
||||||
|
|
||||||
row := q.client.QueryRowContext(ctx, stmt, args...)
|
row := q.client.QueryRowContext(ctx, stmt, args...)
|
||||||
return scan(row)
|
factors, err := scan(row)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
factors.LatestSequence, err = q.latestSequence(ctx, loginPolicyTable)
|
||||||
|
return factors, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) DefaultMultiFactors(ctx context.Context) (*MultiFactors, error) {
|
func (q *Queries) DefaultMultiFactors(ctx context.Context) (*MultiFactors, error) {
|
||||||
@ -183,7 +198,12 @@ func (q *Queries) DefaultMultiFactors(ctx context.Context) (*MultiFactors, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
row := q.client.QueryRowContext(ctx, stmt, args...)
|
row := q.client.QueryRowContext(ctx, stmt, args...)
|
||||||
return scan(row)
|
factors, err := scan(row)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
factors.LatestSequence, err = q.latestSequence(ctx, loginPolicyTable)
|
||||||
|
return factors, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareLoginPolicyQuery() (sq.SelectBuilder, func(*sql.Row) (*LoginPolicy, error)) {
|
func prepareLoginPolicyQuery() (sq.SelectBuilder, func(*sql.Row) (*LoginPolicy, error)) {
|
||||||
@ -242,14 +262,12 @@ func prepareLoginPolicyQuery() (sq.SelectBuilder, func(*sql.Row) (*LoginPolicy,
|
|||||||
|
|
||||||
func prepareLoginPolicy2FAsQuery() (sq.SelectBuilder, func(*sql.Row) (*SecondFactors, error)) {
|
func prepareLoginPolicy2FAsQuery() (sq.SelectBuilder, func(*sql.Row) (*SecondFactors, error)) {
|
||||||
return sq.Select(
|
return sq.Select(
|
||||||
LoginPolicyColumnSequence.identifier(),
|
|
||||||
LoginPolicyColumnSecondFactors.identifier(),
|
LoginPolicyColumnSecondFactors.identifier(),
|
||||||
).From(loginPolicyTable.identifier()).PlaceholderFormat(sq.Dollar),
|
).From(loginPolicyTable.identifier()).PlaceholderFormat(sq.Dollar),
|
||||||
func(row *sql.Row) (*SecondFactors, error) {
|
func(row *sql.Row) (*SecondFactors, error) {
|
||||||
p := new(SecondFactors)
|
p := new(SecondFactors)
|
||||||
secondFactors := pq.Int32Array{}
|
secondFactors := pq.Int32Array{}
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&p.Sequence,
|
|
||||||
&secondFactors,
|
&secondFactors,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -260,6 +278,7 @@ func prepareLoginPolicy2FAsQuery() (sq.SelectBuilder, func(*sql.Row) (*SecondFac
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.Factors = make([]domain.SecondFactorType, len(secondFactors))
|
p.Factors = make([]domain.SecondFactorType, len(secondFactors))
|
||||||
|
p.Count = uint64(len(secondFactors))
|
||||||
for i, mfa := range secondFactors {
|
for i, mfa := range secondFactors {
|
||||||
p.Factors[i] = domain.SecondFactorType(mfa)
|
p.Factors[i] = domain.SecondFactorType(mfa)
|
||||||
}
|
}
|
||||||
@ -269,14 +288,12 @@ func prepareLoginPolicy2FAsQuery() (sq.SelectBuilder, func(*sql.Row) (*SecondFac
|
|||||||
|
|
||||||
func prepareLoginPolicyMFAsQuery() (sq.SelectBuilder, func(*sql.Row) (*MultiFactors, error)) {
|
func prepareLoginPolicyMFAsQuery() (sq.SelectBuilder, func(*sql.Row) (*MultiFactors, error)) {
|
||||||
return sq.Select(
|
return sq.Select(
|
||||||
LoginPolicyColumnSequence.identifier(),
|
|
||||||
LoginPolicyColumnMultiFactors.identifier(),
|
LoginPolicyColumnMultiFactors.identifier(),
|
||||||
).From(loginPolicyTable.identifier()).PlaceholderFormat(sq.Dollar),
|
).From(loginPolicyTable.identifier()).PlaceholderFormat(sq.Dollar),
|
||||||
func(row *sql.Row) (*MultiFactors, error) {
|
func(row *sql.Row) (*MultiFactors, error) {
|
||||||
p := new(MultiFactors)
|
p := new(MultiFactors)
|
||||||
multiFactors := pq.Int32Array{}
|
multiFactors := pq.Int32Array{}
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&p.Sequence,
|
|
||||||
&multiFactors,
|
&multiFactors,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -287,6 +304,7 @@ func prepareLoginPolicyMFAsQuery() (sq.SelectBuilder, func(*sql.Row) (*MultiFact
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.Factors = make([]domain.MultiFactorType, len(multiFactors))
|
p.Factors = make([]domain.MultiFactorType, len(multiFactors))
|
||||||
|
p.Count = uint64(len(multiFactors))
|
||||||
for i, mfa := range multiFactors {
|
for i, mfa := range multiFactors {
|
||||||
p.Factors[i] = domain.MultiFactorType(mfa)
|
p.Factors[i] = domain.MultiFactorType(mfa)
|
||||||
}
|
}
|
||||||
|
@ -156,7 +156,7 @@ func (q *Queries) SearchProjectGrantsByProjectIDAndRoleKey(ctx context.Context,
|
|||||||
SearchRequest: SearchRequest{},
|
SearchRequest: SearchRequest{},
|
||||||
Queries: make([]SearchQuery, 2),
|
Queries: make([]SearchQuery, 2),
|
||||||
}
|
}
|
||||||
searchQuery.Queries[0], err = NewProjectGrantProjectIDSearchQuery(TextEquals, projectID)
|
searchQuery.Queries[0], err = NewProjectGrantProjectIDSearchQuery(projectID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -167,8 +167,8 @@ func (q *Queries) SearchProjectGrantsByProjectIDAndRoleKey(ctx context.Context,
|
|||||||
return q.SearchProjectGrants(ctx, searchQuery)
|
return q.SearchProjectGrants(ctx, searchQuery)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProjectGrantProjectIDSearchQuery(method TextComparison, value string) (SearchQuery, error) {
|
func NewProjectGrantProjectIDSearchQuery(value string) (SearchQuery, error) {
|
||||||
return NewTextQuery(ProjectGrantColumnProjectID, value, method)
|
return NewTextQuery(ProjectGrantColumnProjectID, value, TextEquals)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProjectGrantIDsSearchQuery(values []string) (SearchQuery, error) {
|
func NewProjectGrantIDsSearchQuery(values []string) (SearchQuery, error) {
|
||||||
@ -194,32 +194,32 @@ func NewProjectGrantGrantedOrgIDSearchQuery(value string) (SearchQuery, error) {
|
|||||||
return NewTextQuery(ProjectGrantColumnGrantedOrgID, value, TextEquals)
|
return NewTextQuery(ProjectGrantColumnGrantedOrgID, value, TextEquals)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ProjectGrantSearchQueries) AppendMyResourceOwnerQuery(orgID string) error {
|
func (q *ProjectGrantSearchQueries) AppendMyResourceOwnerQuery(orgID string) error {
|
||||||
query, err := NewProjectGrantResourceOwnerSearchQuery(orgID)
|
query, err := NewProjectGrantResourceOwnerSearchQuery(orgID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.Queries = append(r.Queries, query)
|
q.Queries = append(q.Queries, query)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *ProjectGrantSearchQueries) AppendGrantedOrgQuery(orgID string) error {
|
func (q *ProjectGrantSearchQueries) AppendGrantedOrgQuery(orgID string) error {
|
||||||
query, err := NewProjectGrantGrantedOrgIDSearchQuery(orgID)
|
query, err := NewProjectGrantGrantedOrgIDSearchQuery(orgID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.Queries = append(r.Queries, query)
|
q.Queries = append(q.Queries, query)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r ProjectGrantSearchQueries) AppendPermissionQueries(permissions []string) error {
|
func (q *ProjectGrantSearchQueries) AppendPermissionQueries(permissions []string) error {
|
||||||
if !authz.HasGlobalPermission(permissions) {
|
if !authz.HasGlobalPermission(permissions) {
|
||||||
ids := authz.GetAllPermissionCtxIDs(permissions)
|
ids := authz.GetAllPermissionCtxIDs(permissions)
|
||||||
query, err := NewProjectGrantIDsSearchQuery(ids)
|
query, err := NewProjectGrantIDsSearchQuery(ids)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.Queries = append(r.Queries, query)
|
q.Queries = append(q.Queries, query)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user