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:
Silvan 2021-10-22 13:13:55 +02:00 committed by GitHub
parent b1caef81da
commit 22cfad8cb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 34 deletions

View File

@ -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) {
result, err := s.query.SecondFactorsByID(ctx, authz.GetCtxData(ctx).OrgID)
result, err := s.query.SecondFactorsByOrg(ctx, authz.GetCtxData(ctx).OrgID)
if err != nil {
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) {
res, err := s.query.MultiFactorsByID(ctx, authz.GetCtxData(ctx).OrgID)
res, err := s.query.MultiFactorsByOrg(ctx, authz.GetCtxData(ctx).OrgID)
if err != nil {
return nil, err
}

View File

@ -27,16 +27,16 @@ func (s *Server) ListProjectGrants(ctx context.Context, req *mgmt_pb.ListProject
return nil, err
}
queries.AppendMyResourceOwnerQuery(authz.GetCtxData(ctx).OrgID)
domains, err := s.query.SearchProjectGrants(ctx, queries)
grants, err := s.query.SearchProjectGrants(ctx, queries)
if err != nil {
return nil, err
}
return &mgmt_pb.ListProjectGrantsResponse{
Result: proj_grpc.GrantedProjectViewsToPb(domains.ProjectGrants),
Result: proj_grpc.GrantedProjectViewsToPb(grants.ProjectGrants),
Details: object_grpc.ToListDetails(
domains.Count,
domains.Sequence,
domains.Timestamp,
grants.Count,
grants.Sequence,
grants.Timestamp,
),
}, 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) {
userGrants, err := s.usergrant.UserGrantsByProjectAndGrantID(ctx, req.ProjectId, req.GrantId)
grants, err := s.usergrant.UserGrantsByProjectAndGrantID(ctx, req.ProjectId, req.GrantId)
if err != nil {
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 {
return nil, err
}

View File

@ -14,7 +14,7 @@ import (
func listProjectGrantsRequestToModel(req *mgmt_pb.ListProjectGrantsRequest) (*query.ProjectGrantSearchQueries, error) {
offset, limit, asc := object.ListQueryToModel(req.Query)
queries, err := ProjectGrantQueriesToModel(req.Queries)
queries, err := ProjectGrantQueriesToModel(req)
if err != nil {
return nil, err
}
@ -28,15 +28,22 @@ func listProjectGrantsRequestToModel(req *mgmt_pb.ListProjectGrantsRequest) (*qu
}, nil
}
func ProjectGrantQueriesToModel(queries []*proj_pb.ProjectGrantQuery) (_ []query.SearchQuery, err error) {
q := make([]query.SearchQuery, len(queries))
for i, query := range queries {
q[i], err = ProjectGrantQueryToModel(query)
func ProjectGrantQueriesToModel(req *mgmt_pb.ListProjectGrantsRequest) (_ []query.SearchQuery, err error) {
queries := make([]query.SearchQuery, 0, len(req.Queries)+1)
for _, query := range req.Queries {
q, err := ProjectGrantQueryToModel(query)
if err != nil {
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) {

View File

@ -118,7 +118,7 @@ func (q *Queries) DefaultLoginPolicy(ctx context.Context) (*LoginPolicy, error)
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()
stmt, args, err := query.Where(
sq.Or{
@ -136,7 +136,12 @@ func (q *Queries) SecondFactorsByID(ctx context.Context, orgID string) (*SecondF
}
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) {
@ -149,10 +154,15 @@ func (q *Queries) DefaultSecondFactors(ctx context.Context) (*SecondFactors, err
}
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()
stmt, args, err := query.Where(
sq.Or{
@ -170,7 +180,12 @@ func (q *Queries) MultiFactorsByID(ctx context.Context, orgID string) (*MultiFac
}
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) {
@ -183,7 +198,12 @@ func (q *Queries) DefaultMultiFactors(ctx context.Context) (*MultiFactors, error
}
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)) {
@ -242,14 +262,12 @@ func prepareLoginPolicyQuery() (sq.SelectBuilder, func(*sql.Row) (*LoginPolicy,
func prepareLoginPolicy2FAsQuery() (sq.SelectBuilder, func(*sql.Row) (*SecondFactors, error)) {
return sq.Select(
LoginPolicyColumnSequence.identifier(),
LoginPolicyColumnSecondFactors.identifier(),
).From(loginPolicyTable.identifier()).PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*SecondFactors, error) {
p := new(SecondFactors)
secondFactors := pq.Int32Array{}
err := row.Scan(
&p.Sequence,
&secondFactors,
)
if err != nil {
@ -260,6 +278,7 @@ func prepareLoginPolicy2FAsQuery() (sq.SelectBuilder, func(*sql.Row) (*SecondFac
}
p.Factors = make([]domain.SecondFactorType, len(secondFactors))
p.Count = uint64(len(secondFactors))
for i, mfa := range secondFactors {
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)) {
return sq.Select(
LoginPolicyColumnSequence.identifier(),
LoginPolicyColumnMultiFactors.identifier(),
).From(loginPolicyTable.identifier()).PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*MultiFactors, error) {
p := new(MultiFactors)
multiFactors := pq.Int32Array{}
err := row.Scan(
&p.Sequence,
&multiFactors,
)
if err != nil {
@ -287,6 +304,7 @@ func prepareLoginPolicyMFAsQuery() (sq.SelectBuilder, func(*sql.Row) (*MultiFact
}
p.Factors = make([]domain.MultiFactorType, len(multiFactors))
p.Count = uint64(len(multiFactors))
for i, mfa := range multiFactors {
p.Factors[i] = domain.MultiFactorType(mfa)
}

View File

@ -156,7 +156,7 @@ func (q *Queries) SearchProjectGrantsByProjectIDAndRoleKey(ctx context.Context,
SearchRequest: SearchRequest{},
Queries: make([]SearchQuery, 2),
}
searchQuery.Queries[0], err = NewProjectGrantProjectIDSearchQuery(TextEquals, projectID)
searchQuery.Queries[0], err = NewProjectGrantProjectIDSearchQuery(projectID)
if err != nil {
return nil, err
}
@ -167,8 +167,8 @@ func (q *Queries) SearchProjectGrantsByProjectIDAndRoleKey(ctx context.Context,
return q.SearchProjectGrants(ctx, searchQuery)
}
func NewProjectGrantProjectIDSearchQuery(method TextComparison, value string) (SearchQuery, error) {
return NewTextQuery(ProjectGrantColumnProjectID, value, method)
func NewProjectGrantProjectIDSearchQuery(value string) (SearchQuery, error) {
return NewTextQuery(ProjectGrantColumnProjectID, value, TextEquals)
}
func NewProjectGrantIDsSearchQuery(values []string) (SearchQuery, error) {
@ -194,32 +194,32 @@ func NewProjectGrantGrantedOrgIDSearchQuery(value string) (SearchQuery, error) {
return NewTextQuery(ProjectGrantColumnGrantedOrgID, value, TextEquals)
}
func (r *ProjectGrantSearchQueries) AppendMyResourceOwnerQuery(orgID string) error {
func (q *ProjectGrantSearchQueries) AppendMyResourceOwnerQuery(orgID string) error {
query, err := NewProjectGrantResourceOwnerSearchQuery(orgID)
if err != nil {
return err
}
r.Queries = append(r.Queries, query)
q.Queries = append(q.Queries, query)
return nil
}
func (r *ProjectGrantSearchQueries) AppendGrantedOrgQuery(orgID string) error {
func (q *ProjectGrantSearchQueries) AppendGrantedOrgQuery(orgID string) error {
query, err := NewProjectGrantGrantedOrgIDSearchQuery(orgID)
if err != nil {
return err
}
r.Queries = append(r.Queries, query)
q.Queries = append(q.Queries, query)
return nil
}
func (r ProjectGrantSearchQueries) AppendPermissionQueries(permissions []string) error {
func (q *ProjectGrantSearchQueries) AppendPermissionQueries(permissions []string) error {
if !authz.HasGlobalPermission(permissions) {
ids := authz.GetAllPermissionCtxIDs(permissions)
query, err := NewProjectGrantIDsSearchQuery(ids)
if err != nil {
return err
}
r.Queries = append(r.Queries, query)
q.Queries = append(q.Queries, query)
}
return nil
}