Skip to content

Commit b2f7cc0

Browse files
authored
Merge pull request #654 from code0-tech/expose-abilities
Expose mutation abilities for each type
2 parents 1772252 + 004dace commit b2f7cc0

File tree

63 files changed

+270
-26
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+270
-26
lines changed

app/graphql/sagittarius_schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ def self.object_from_id(global_id, query_ctx = nil)
5757
# rubocop:enable GraphQL/MaxDepthSchema
5858
# rubocop:enable GraphQL/MaxComplexitySchema
5959

60-
Types::BaseObject.instance_variable_set(:@user_ability_types, nil) # release temporary type map
60+
if Types::BaseObject.instance_variable_defined?(:@user_ability_types)
61+
Types::BaseObject.remove_instance_variable(:@user_ability_types) # release temporary type map
62+
end

app/graphql/types/base_object.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def self.timestamps(entity_name = graphql_name)
2424
field :updated_at, Types::TimeType, null: false, description: "Time when this #{entity_name} was last updated"
2525
end
2626

27-
def self.expose_abilities(abilities, entity_name = graphql_name)
27+
def self.expose_abilities(abilities, entity_name: graphql_name, subject_resolver: nil)
2828
@user_ability_types ||= {}
2929

3030
type_class = @user_ability_types.fetch("#{entity_name}UserAbilities", nil)
@@ -43,10 +43,11 @@ def self.expose_abilities(abilities, entity_name = graphql_name)
4343
abilities.each do |ability|
4444
field ability, Boolean,
4545
null: false,
46-
description: "Shows if the current user can #{ability} in this #{entity_name}"
46+
description: "Shows if the current user has the `#{ability}` ability on this #{entity_name}"
4747

4848
define_method(ability) do
49-
Ability.allowed?(current_user, ability, object)
49+
subject = subject_resolver.nil? ? object : subject_resolver.call
50+
Ability.allowed?(current_authentication, ability, subject)
5051
end
5152
end
5253
end

app/graphql/types/flow_type.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class FlowType < Types::BaseObject
3030
description: 'Nodes of the flow',
3131
method: :collect_node_functions
3232

33+
expose_abilities %i[
34+
delete_flow
35+
]
36+
3337
id_field Flow
3438
timestamps
3539

app/graphql/types/namespace_member_type.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class NamespaceMemberType < Types::BaseObject
1212
field :member_roles, NamespaceMemberRoleType.connection_type, null: false, description: 'Memberroles of the member'
1313
field :roles, NamespaceRoleType.connection_type, null: false, description: 'Roles of the member'
1414

15+
expose_abilities %i[
16+
assign_member_roles
17+
delete_member
18+
]
19+
1520
id_field NamespaceMember
1621
timestamps
1722
end

app/graphql/types/namespace_project_type.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ class NamespaceProjectType < Types::BaseObject
2323

2424
field :flows, Types::FlowType.connection_type, null: true, description: 'Fetches all flows in this project'
2525

26+
expose_abilities %i[
27+
create_flow
28+
assign_project_runtimes
29+
delete_namespace_project
30+
update_namespace_project
31+
]
32+
2633
id_field NamespaceProject
2734
timestamps
2835

app/graphql/types/namespace_role_type.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ class NamespaceRoleType < BaseObject
1515
field :assigned_projects, Types::NamespaceProjectType.connection_type,
1616
description: 'The projects this role is assigned to'
1717

18+
expose_abilities %i[
19+
assign_role_abilities
20+
assign_role_projects
21+
delete_namespace_role
22+
update_namespace_role
23+
]
24+
1825
id_field ::NamespaceRole
1926
timestamps
2027

app/graphql/types/namespace_type.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class NamespaceType < Types::BaseObject
2222
lookahead_field :members, base_scope: ->(object) { object.namespace_members },
2323
conditional_lookaheads: { user: :user, namespace: :namespace }
2424

25+
expose_abilities %i[
26+
invite_member
27+
create_namespace_role
28+
create_namespace_project
29+
create_runtime
30+
]
31+
2532
id_field Namespace
2633
timestamps
2734
end

app/graphql/types/organization_type.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class OrganizationType < Types::BaseObject
1313
description: 'Namespace of this organization',
1414
method: :ensure_namespace
1515

16+
expose_abilities %i[
17+
delete_organization
18+
update_organization
19+
]
20+
1621
id_field Organization
1722
timestamps
1823
end

app/graphql/types/query_type.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ class QueryType < Types::BaseObject
4444

4545
field :global_runtimes, Types::RuntimeType.connection_type, null: false, description: 'Find runtimes'
4646

47+
expose_abilities %i[
48+
create_organization
49+
create_runtime
50+
delete_runtime
51+
update_runtime
52+
rotate_runtime_token
53+
update_application_setting
54+
], entity_name: 'Instance', subject_resolver: -> { :global }
55+
4756
def node(id:)
4857
context.schema.object_from_id(id, context)
4958
end

app/graphql/types/runtime_type.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ class RuntimeType < Types::BaseObject
1717

1818
field :token, String, null: true, description: 'Token belonging to the runtime, only present on creation'
1919

20+
expose_abilities %i[
21+
delete_runtime
22+
update_runtime
23+
rotate_runtime_token
24+
]
25+
2026
id_field Runtime
2127
timestamps
2228

0 commit comments

Comments
 (0)