-
Notifications
You must be signed in to change notification settings - Fork 0
organisation user pins #953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1b8bfe6
65a8ca0
27b575c
169e976
cc62ab6
e3db55b
a787ca2
1708f57
ebe6cdc
ac56694
b4eb8bb
1a5f013
eb862f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Mutations | ||
| module Users | ||
| class UpdateOrganizationPins < BaseMutation | ||
| description 'Update pinned organizations for a user.' | ||
|
|
||
| field :user, Types::UserType, null: true, description: 'The updated user.' | ||
|
|
||
| argument :organization_ids, | ||
| [Types::GlobalIdType[::Organization]], | ||
| required: true, | ||
| description: 'Ordered list of organization IDs to pin for the user.' | ||
|
|
||
| def resolve(organization_ids:) | ||
| organizations = organization_ids.map { |id| SagittariusSchema.object_from_id(id) } | ||
| if organizations.any?(&:nil?) | ||
| return { user: nil, errors: [create_error(:organization_not_found, 'Invalid organization with provided id')] } | ||
| end | ||
|
|
||
| ::Users::UpdateOrganizationPinsService.new( | ||
| current_authentication, | ||
| organizations.map(&:id) | ||
| ).execute.to_mutation_response(success_key: :user) | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Types | ||
| class UserOrganizationPinType < Types::BaseObject | ||
| description 'Represents a pinned organization of a user' | ||
|
|
||
| authorize :read_user_organization_pin | ||
|
|
||
| field :organization, Types::OrganizationType, null: true, description: 'The pinned organization' | ||
| field :priority, Integer, null: false, description: 'Ordering priority of the pin' | ||
| field :user, Types::UserType, null: false, description: 'The user owning this pin' | ||
|
|
||
| id_field UserOrganizationPin | ||
| timestamps | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,6 +42,14 @@ class UserType < Types::BaseObject | |||||||
| description: 'Identities of this user', | ||||||||
| method: :user_identities | ||||||||
|
|
||||||||
| field :organization_pins, [Types::UserOrganizationPinType], | ||||||||
| null: false, | ||||||||
| description: 'Pinned organizations of this user with explicit priority' | ||||||||
|
|
||||||||
| field :pinned_organizations, [Types::OrganizationType], | ||||||||
| null: false, | ||||||||
| description: 'Pinned organizations of this user ordered by pin priority' | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
|
Taucher2003 marked this conversation as resolved.
|
||||||||
| field :mfa_status, Types::MfaStatusType, | ||||||||
| null: true, | ||||||||
| description: 'Multi-factor authentication status of this user' | ||||||||
|
|
@@ -72,5 +80,9 @@ def mfa_status | |||||||
| backup_codes_count: object.backup_codes.size, | ||||||||
| } | ||||||||
| end | ||||||||
|
|
||||||||
| def organization_pins | ||||||||
| object.user_organization_pins.order(priority: :asc) | ||||||||
| end | ||||||||
| end | ||||||||
| end | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,7 +41,8 @@ class AuditEvent < ApplicationRecord | |||||||||
| password_reset: 37, | ||||||||||
| user_deleted: 38, | ||||||||||
| user_created: 39, | ||||||||||
| project_module_configurations_updated: 40, | ||||||||||
| user_organization_pins_updated: 40, | ||||||||||
| project_module_configurations_updated: 41, | ||||||||||
|
Comment on lines
+44
to
+45
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| }.with_indifferent_access | ||||||||||
|
|
||||||||||
| # rubocop:disable Lint/StructNewOverride | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class UserOrganizationPin < ApplicationRecord | ||
| belongs_to :user, inverse_of: :user_organization_pins | ||
| belongs_to :organization, inverse_of: :user_organization_pins | ||
|
|
||
| validates :priority, presence: true, | ||
| numericality: { only_integer: true, greater_than_or_equal_to: 0 }, | ||
| uniqueness: { scope: :user_id } | ||
| validates :organization_id, uniqueness: { scope: :user_id } | ||
| end |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,9 @@ | ||||||||||
| # frozen_string_literal: true | ||||||||||
|
|
||||||||||
| class UserOrganizationPinPolicy < BasePolicy | ||||||||||
| delegate { subject.user } | ||||||||||
|
|
||||||||||
| condition(:user_is_self) { subject.user_id == user&.id } | ||||||||||
|
|
||||||||||
| rule { user_is_self }.enable :read_user_organization_pin | ||||||||||
|
Comment on lines
+5
to
+8
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| end | ||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,7 @@ class UserPolicy < BasePolicy | |||||||
| enable :read_user_identity | ||||||||
| enable :manage_mfa | ||||||||
| enable :update_user | ||||||||
| enable :update_user_organization_pin | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| enable :update_attachment_avatar | ||||||||
| enable :verify_email | ||||||||
| enable :send_verification_email | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Users | ||
| class UpdateOrganizationPinsService | ||
| include Sagittarius::Database::Transactional | ||
|
|
||
| attr_reader :current_authentication, :user, :organization_ids | ||
|
|
||
| def initialize(current_authentication, organization_ids) | ||
| @current_authentication = current_authentication | ||
| @user = current_authentication.user | ||
| @organization_ids = organization_ids.uniq | ||
| end | ||
|
|
||
| def execute | ||
| unless user && Ability.allowed?(current_authentication, :update_user_organization_pin, user) | ||
| return ServiceResponse.error(message: 'Missing permission', error_code: :missing_permission) | ||
| end | ||
|
|
||
| organizations = OrganizationsFinder.new(id: organization_ids, namespace_member_user: user).execute | ||
| if organizations.count != organization_ids.count | ||
| return ServiceResponse.error(message: 'Organization not found', error_code: :organization_not_found) | ||
| end | ||
|
|
||
| transactional do |t| | ||
| old_pins = user.user_organization_pins.map do |pin| | ||
| { organization_id: pin.organization_id, priority: pin.priority } | ||
| end | ||
|
|
||
| UserOrganizationPin.where(user: user).delete_all | ||
|
|
||
| organization_ids.each_with_index do |organization_id, priority| | ||
| pin = user.user_organization_pins.build(organization_id: organization_id, priority: priority) | ||
| next if pin.save | ||
|
|
||
| t.rollback_and_return! ServiceResponse.error( | ||
| message: 'Failed to update user organization pins', | ||
| error_code: :invalid_user_organization_pin, | ||
| details: pin.errors | ||
| ) | ||
| end | ||
|
|
||
| new_pins = user.user_organization_pins.reload.map do |pin| | ||
| { organization_id: pin.organization_id, priority: pin.priority } | ||
| end | ||
|
|
||
| AuditService.audit( | ||
| :user_organization_pins_updated, | ||
| author_id: current_authentication.user.id, | ||
| entity: user, | ||
| target: user, | ||
| details: { old_pins: old_pins, new_pins: new_pins } | ||
| ) | ||
|
|
||
| ServiceResponse.success(message: 'Updated user organization pins', payload: user) | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class CreateUserOrganizationPins < Code0::ZeroTrack::Database::Migration[1.0] | ||
| def change | ||
| create_table :user_organization_pins do |t| | ||
| t.references :user, null: false, foreign_key: { on_delete: :cascade }, index: false | ||
| t.references :organization, null: false, foreign_key: { on_delete: :cascade }, index: false | ||
| t.integer :priority, null: false | ||
|
|
||
| t.index %i[user_id organization_id], unique: true | ||
| t.index %i[user_id priority], unique: true | ||
|
|
||
| t.timestamps_with_timezone | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 7cf08006d8ab53c1cea495d14f91286debd633d327ea0dee7148ce805d61c32d |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --- | ||
| title: usersUpdateOrganizationPins | ||
| --- | ||
|
|
||
| Update pinned organizations for a user. | ||
|
|
||
| ## Arguments | ||
|
|
||
| | Name | Type | Description | | ||
| |------|------|-------------| | ||
| | `clientMutationId` | [`String`](../scalar/string.md) | A unique identifier for the client performing the mutation. | | ||
| | `organizationIds` | [`[OrganizationID!]!`](../scalar/organizationid.md) | Ordered list of organization IDs to pin for the user. | | ||
|
|
||
| ## Fields | ||
|
|
||
| | Name | Type | Description | | ||
| |------|------|-------------| | ||
| | `clientMutationId` | [`String`](../scalar/string.md) | A unique identifier for the client performing the mutation. | | ||
| | `errors` | [`[Error!]!`](../object/error.md) | Errors encountered during execution of the mutation. | | ||
| | `user` | [`User`](../object/user.md) | The updated user. | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| title: UserOrganizationPin | ||
| --- | ||
|
|
||
| Represents a pinned organization of a user | ||
|
|
||
| ## Fields without arguments | ||
|
|
||
| | Name | Type | Description | | ||
| |------|------|-------------| | ||
| | `createdAt` | [`Time!`](../scalar/time.md) | Time when this UserOrganizationPin was created | | ||
| | `id` | [`UserOrganizationPinID!`](../scalar/userorganizationpinid.md) | Global ID of this UserOrganizationPin | | ||
| | `organization` | [`Organization`](../object/organization.md) | The pinned organization | | ||
| | `priority` | [`Int!`](../scalar/int.md) | Ordering priority of the pin | | ||
| | `updatedAt` | [`Time!`](../scalar/time.md) | Time when this UserOrganizationPin was last updated | | ||
| | `user` | [`User!`](../object/user.md) | The user owning this pin | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| title: UserOrganizationPinID | ||
| --- | ||
|
|
||
| A unique identifier for all UserOrganizationPin entities of the application |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| FactoryBot.define do | ||
| sequence(:user_organization_pin_priority) | ||
|
|
||
| factory :user_organization_pin do | ||
| user | ||
| organization | ||
| priority { generate(:user_organization_pin_priority) } | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.