-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow role permissions to be restricted to specific projects
- Loading branch information
1 parent
a4b3cb3
commit 9f77c25
Showing
27 changed files
with
476 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module NamespaceRoles | ||
class AssignProjects < BaseMutation | ||
description 'Update the project a role is assigned to.' | ||
|
||
field :projects, [Types::NamespaceProjectType], description: 'The now assigned projects' | ||
|
||
argument :project_ids, [Types::GlobalIdType[::NamespaceProject]], | ||
description: 'The projects that should be assigned to the role' | ||
argument :role_id, Types::GlobalIdType[::NamespaceRole], | ||
description: 'The id of the role which should be assigned to projects' | ||
|
||
def resolve(role_id:, project_ids:) | ||
role = SagittariusSchema.object_from_id(role_id) | ||
projects = project_ids.map { |id| SagittariusSchema.object_from_id(id) } | ||
|
||
return { projects: nil, errors: [create_message_error('Invalid role')] } if role.nil? | ||
return { projects: nil, errors: [create_message_error('Invalid project')] } if projects.any?(&:nil?) | ||
|
||
::NamespaceRoles::AssignProjectsService.new( | ||
current_user, | ||
role, | ||
projects | ||
).execute.to_mutation_response(success_key: :projects) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class NamespaceRoleProjectAssignment < ApplicationRecord | ||
belongs_to :role, class_name: 'NamespaceRole', inverse_of: :project_assignments | ||
belongs_to :project, class_name: 'NamespaceProject', inverse_of: :role_assignments | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
module NamespaceRoles | ||
class AssignProjectsService | ||
include Sagittarius::Database::Transactional | ||
|
||
attr_reader :current_user, :role, :projects | ||
|
||
def initialize(current_user, role, projects) | ||
@current_user = current_user | ||
@role = role | ||
@projects = projects | ||
end | ||
|
||
# rubocop:disable Metrics/CyclomaticComplexity | ||
# rubocop:disable Metrics/PerceivedComplexity | ||
def execute | ||
namespace = role.namespace | ||
unless Ability.allowed?(current_user, :assign_role_projects, namespace) | ||
return ServiceResponse.error(message: 'Missing permissions', payload: :missing_permission) | ||
end | ||
|
||
transactional do |t| | ||
current_projects = role.project_assignments | ||
old_projects_for_audit_event = current_projects.map(&:project).map do |project| | ||
{ name: project.name, id: project.id } | ||
end | ||
|
||
current_projects.where.not(project: projects).delete_all | ||
|
||
(projects - current_projects.map(&:project)).map do |projects| | ||
project_assignment = NamespaceRoleProjectAssignment.create(role: role, project: projects) | ||
|
||
next if project_assignment.persisted? | ||
|
||
t.rollback_and_return! ServiceResponse.error( | ||
message: 'Failed to save namespace role project assignment', | ||
payload: project_assignment.errors | ||
) | ||
end | ||
|
||
new_projects = role.reload.project_assignments.map(&:project) | ||
|
||
AuditService.audit( | ||
:namespace_role_projects_updated, | ||
author_id: current_user.id, | ||
entity: role, | ||
details: { | ||
old_projects: old_projects_for_audit_event, | ||
new_projects: new_projects.map { |project| { name: project.name, id: project.id } }, | ||
}, | ||
target: namespace | ||
) | ||
|
||
ServiceResponse.success(message: 'Role project assignments updated', payload: new_projects) | ||
end | ||
end | ||
# rubocop:enable Metrics/PerceivedComplexity | ||
# rubocop:enable Metrics/CyclomaticComplexity | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20240622182317_create_namespace_role_project_assignments.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateNamespaceRoleProjectAssignments < Sagittarius::Database::Migration[1.0] | ||
def change | ||
create_table :namespace_role_project_assignments do |t| | ||
t.references :role, null: false, index: false, foreign_key: { to_table: :namespace_roles } | ||
t.references :project, null: false, foreign_key: { to_table: :namespace_projects } | ||
|
||
t.index %i[role_id project_id], unique: true | ||
|
||
t.timestamps_with_timezone | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6e5cf40f7ab5a1e704fe918addc71ae146439d096a1183734b725b986ea5fdea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
docs/content/graphql/mutation/namespacerolesassignprojects.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
title: namespaceRolesAssignProjects | ||
--- | ||
|
||
Update the project a role is assigned to. | ||
|
||
## Arguments | ||
|
||
| Name | Type | Description | | ||
|------|------|-------------| | ||
| `clientMutationId` | [`String`](../scalar/string.md) | A unique identifier for the client performing the mutation. | | ||
| `projectIds` | [`[NamespaceProjectID!]!`](../scalar/namespaceprojectid.md) | The projects that should be assigned to the role | | ||
| `roleId` | [`NamespaceRoleID!`](../scalar/namespaceroleid.md) | The id of the role which should be assigned to projects | | ||
|
||
## Fields | ||
|
||
| Name | Type | Description | | ||
|------|------|-------------| | ||
| `clientMutationId` | [`String`](../scalar/string.md) | A unique identifier for the client performing the mutation. | | ||
| `errors` | [`[Error!]!`](../union/error.md) | Errors encountered during execution of the mutation. | | ||
| `projects` | [`[NamespaceProject!]`](../object/namespaceproject.md) | The now assigned projects | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :namespace_role_project_assignment do | ||
role factory: :namespace_role | ||
project factory: :namespace_project | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
spec/graphql/mutations/namespace_roles/assign_projects_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Mutations::NamespaceRoles::AssignProjects do | ||
it { expect(described_class.graphql_name).to eq('NamespaceRolesAssignProjects') } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
namespace | ||
name | ||
abilities | ||
assignedProjects | ||
createdAt | ||
updatedAt | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe NamespaceRoleProjectAssignment do | ||
subject { create(:namespace_role_project_assignment) } | ||
|
||
describe 'associations' do | ||
it { is_expected.to belong_to(:role).class_name('NamespaceRole').inverse_of(:project_assignments).required } | ||
it { is_expected.to belong_to(:project).class_name('NamespaceProject').inverse_of(:role_assignments).required } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.