-
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
Open
raphael-goetz
wants to merge
13
commits into
main
Choose a base branch
from
#556-organisation-user-pins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1b8bfe6
feat: added user organisation pins migrations
raphael-goetz 65a8ca0
feat: added pin organisation user service
raphael-goetz 27b575c
docs: regen
raphael-goetz 169e976
feat: added specs for user organisation pins
raphael-goetz cc62ab6
ref: satisfied rubocop
raphael-goetz e3db55b
Apply suggestions from code review
raphael-goetz a787ca2
feat: removed user_id from mutation
raphael-goetz 1708f57
feat: implemented requested changes
raphael-goetz ebe6cdc
Merge branch 'main' into #556-organisation-user-pins
raphael-goetz ac56694
docs: regenerate graphql docs
raphael-goetz b4eb8bb
Merge branch 'main' into #556-organisation-user-pins
raphael-goetz 1a5f013
fix: correct structure sql
raphael-goetz eb862f4
Merge branch 'main' into #556-organisation-user-pins
raphael-goetz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,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 |
This file contains hidden or 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 hidden or 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,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 | ||
This file contains hidden or 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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,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 |
This file contains hidden or 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,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 | ||||||||||
This file contains hidden or 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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||
|
|
||||||||
This file contains hidden or 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 hidden or 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 hidden or 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,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 |
16 changes: 16 additions & 0 deletions
16
db/migrate/20260504153000_create_user_organization_pins.rb
This file contains hidden or 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,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 |
This file contains hidden or 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 @@ | ||
| 7cf08006d8ab53c1cea495d14f91286debd633d327ea0dee7148ce805d61c32d |
This file contains hidden or 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 hidden or 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 hidden or 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,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. | |
This file contains hidden or 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 hidden or 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,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 | |
This file contains hidden or 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,5 @@ | ||
| --- | ||
| title: UserOrganizationPinID | ||
| --- | ||
|
|
||
| A unique identifier for all UserOrganizationPin entities of the application |
This file contains hidden or 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,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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.