From 1f4f2d16bebeec118d28bb8189b2d846f3f49cce Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 27 Nov 2020 11:20:30 -0500 Subject: [PATCH] API: Implement mutation { deleteACL } --- api/graph/api/generated.go | 25 ++++++++----------------- api/graph/schema.graphqls | 2 +- api/graph/schema.resolvers.go | 23 +++++++++++++++++++++-- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/api/graph/api/generated.go b/api/graph/api/generated.go index 60938d4..8714f70 100644 --- a/api/graph/api/generated.go +++ b/api/graph/api/generated.go @@ -110,7 +110,7 @@ type ComplexityRoot struct { Mutation struct { CreateRepository func(childComplexity int, name string, visibility model.Visibility, description *string) int - DeleteACL func(childComplexity int, repoID int, entity string) int + DeleteACL func(childComplexity int, id int) int DeleteArtifact func(childComplexity int, id int) int DeleteRepository func(childComplexity int, id int) int UpdateACL func(childComplexity int, repoID int, mode model.AccessMode, entity string) int @@ -245,7 +245,7 @@ type MutationResolver interface { UpdateRepository(ctx context.Context, id int, input map[string]interface{}) (*model.Repository, error) DeleteRepository(ctx context.Context, id int) (*model.Repository, error) UpdateACL(ctx context.Context, repoID int, mode model.AccessMode, entity string) (*model.ACL, error) - DeleteACL(ctx context.Context, repoID int, entity string) (*model.ACL, error) + DeleteACL(ctx context.Context, id int) (*model.ACL, error) UploadArtifact(ctx context.Context, repoID int, revspec string, file graphql.Upload) (*model.Artifact, error) DeleteArtifact(ctx context.Context, id int) (*model.Artifact, error) } @@ -540,7 +540,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return 0, false } - return e.complexity.Mutation.DeleteACL(childComplexity, args["repoId"].(int), args["entity"].(string)), true + return e.complexity.Mutation.DeleteACL(childComplexity, args["id"].(int)), true case "Mutation.deleteArtifact": if e.complexity.Mutation.DeleteArtifact == nil { @@ -1599,7 +1599,7 @@ type Mutation { updateACL(repoId: Int!, mode: AccessMode!, entity: ID!): ACL! @access(scope: ACLS, kind: RW) # Deletes an entry from the access control list - deleteACL(repoId: Int!, entity: ID!): ACL! @access(scope: ACLS, kind: RW) + deleteACL(id: Int!): ACL! @access(scope: ACLS, kind: RW) # Uploads an artifact. revspec must match a specific git tag, and the # filename must be unique among artifacts for this repository. @@ -1692,23 +1692,14 @@ func (ec *executionContext) field_Mutation_deleteACL_args(ctx context.Context, r var err error args := map[string]interface{}{} var arg0 int - if tmp, ok := rawArgs["repoId"]; ok { - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("repoId")) + if tmp, ok := rawArgs["id"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id")) arg0, err = ec.unmarshalNInt2int(ctx, tmp) if err != nil { return nil, err } } - args["repoId"] = arg0 - var arg1 string - if tmp, ok := rawArgs["entity"]; ok { - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("entity")) - arg1, err = ec.unmarshalNID2string(ctx, tmp) - if err != nil { - return nil, err - } - } - args["entity"] = arg1 + args["id"] = arg0 return args, nil } @@ -3597,7 +3588,7 @@ func (ec *executionContext) _Mutation_deleteACL(ctx context.Context, field graph resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { directive0 := func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Mutation().DeleteACL(rctx, args["repoId"].(int), args["entity"].(string)) + return ec.resolvers.Mutation().DeleteACL(rctx, args["id"].(int)) } directive1 := func(ctx context.Context) (interface{}, error) { scope, err := ec.unmarshalNAccessScope2gitᚗsrᚗhtᚋאsircmpwnᚋgitᚗsrᚗhtᚋapiᚋgraphᚋmodelᚐAccessScope(ctx, "ACLS") diff --git a/api/graph/schema.graphqls b/api/graph/schema.graphqls index 58f0fd6..260b70e 100644 --- a/api/graph/schema.graphqls +++ b/api/graph/schema.graphqls @@ -352,7 +352,7 @@ type Mutation { updateACL(repoId: Int!, mode: AccessMode!, entity: ID!): ACL! @access(scope: ACLS, kind: RW) # Deletes an entry from the access control list - deleteACL(repoId: Int!, entity: ID!): ACL! @access(scope: ACLS, kind: RW) + deleteACL(id: Int!): ACL! @access(scope: ACLS, kind: RW) # Uploads an artifact. revspec must match a specific git tag, and the # filename must be unique among artifacts for this repository. diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go index 0d78a71..3f73054 100644 --- a/api/graph/schema.resolvers.go +++ b/api/graph/schema.resolvers.go @@ -361,8 +361,27 @@ func (r *mutationResolver) UpdateACL(ctx context.Context, repoID int, mode model return &acl, nil } -func (r *mutationResolver) DeleteACL(ctx context.Context, repoID int, entity string) (*model.ACL, error) { - panic(fmt.Errorf("deleteACL: not implemented")) +func (r *mutationResolver) DeleteACL(ctx context.Context, id int) (*model.ACL, error) { + var acl model.ACL + if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error { + row := tx.QueryRowContext(ctx, ` + DELETE FROM access + USING repository repo + WHERE repo_id = repo.id AND repo.owner_id = $1 AND access.id = $2 + RETURNING access.id, access.created, mode, repo_id, user_id; + `, auth.ForContext(ctx).UserID, id) + if err := row.Scan(&acl.ID, &acl.Created, &acl.RawAccessMode, + &acl.RepoID, &acl.UserID); err != nil { + if err == sql.ErrNoRows { + return fmt.Errorf("No such repository or ACL entry found") + } + return err + } + return nil + }); err != nil { + return nil, err + } + return &acl, nil } func (r *mutationResolver) UploadArtifact(ctx context.Context, repoID int, revspec string, file graphql.Upload) (*model.Artifact, error) { -- 2.38.4