Commit 98f688ac authored by Alex Kalderimis's avatar Alex Kalderimis

Refactor snippets create mutation to use declarative auth

This refactors the snippet creation mutation to use the declarative
authorization methods.
parent 31e5c252
...@@ -6,6 +6,8 @@ module Mutations ...@@ -6,6 +6,8 @@ module Mutations
include SpammableMutationFields include SpammableMutationFields
include ResolvesProject include ResolvesProject
authorize :create_snippet
graphql_name 'CreateSnippet' graphql_name 'CreateSnippet'
field :snippet, field :snippet,
...@@ -37,17 +39,15 @@ module Mutations ...@@ -37,17 +39,15 @@ module Mutations
description: 'Actions to perform over the snippet repository and blobs', description: 'Actions to perform over the snippet repository and blobs',
required: false required: false
def resolve(args) def resolve(project_path: nil, **args)
project_path = args.delete(:project_path)
if project_path.present? if project_path.present?
project = find_project!(project_path: project_path) project = authorized_find!(project_path)
elsif !can_create_personal_snippet? else
raise_resource_not_available_error! authorize!(:global)
end end
service_response = ::Snippets::CreateService.new(project, service_response = ::Snippets::CreateService.new(project,
context[:current_user], current_user,
create_params(args)).execute create_params(args)).execute
snippet = service_response.payload[:snippet] snippet = service_response.payload[:snippet]
...@@ -67,20 +67,8 @@ module Mutations ...@@ -67,20 +67,8 @@ module Mutations
private private
def find_project!(project_path:) def find_object(full_path)
authorized_find!(full_path: project_path) Project.find_by_full_path(full_path)
end
def find_object(full_path:)
resolve_project(full_path: full_path)
end
def authorized_resource?(project)
Ability.allowed?(context[:current_user], :create_snippet, project)
end
def can_create_personal_snippet?
Ability.allowed?(context[:current_user], :create_snippet)
end end
def create_params(args) def create_params(args)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment