mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-29 11:12:01 +08:00
Add migration for new user assignments to projects [SCI-5478]
This commit is contained in:
parent
49194d1a45
commit
d8cffe53c7
6 changed files with 89 additions and 14 deletions
|
@ -4,5 +4,5 @@ class UserAssignment < ApplicationRecord
|
|||
belongs_to :assignable, polymorphic: true
|
||||
belongs_to :user_role
|
||||
belongs_to :user
|
||||
belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User'
|
||||
belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User', optional: true
|
||||
end
|
||||
|
|
|
@ -19,7 +19,9 @@ class UserRole < ApplicationRecord
|
|||
def self.owner_role
|
||||
new(
|
||||
name: I18n.t('user_roles.predefined.owner'),
|
||||
permissions: ProjectPermissions.constants + ExperimentPermissions.constants + MyModulePermissions.constants,
|
||||
permissions: ProjectPermissions.constants.map { |const| ProjectPermissions.const_get(const) } +
|
||||
ExperimentPermissions.constants.map { |const| ExperimentPermissions.const_get(const) } +
|
||||
MyModulePermissions.constants.map { |const| MyModulePermissions.const_get(const) },
|
||||
predefined: true
|
||||
)
|
||||
end
|
||||
|
@ -76,7 +78,8 @@ class UserRole < ApplicationRecord
|
|||
ProjectPermissions::READ,
|
||||
ExperimentPermissions::READ,
|
||||
MyModulePermissions::READ
|
||||
]
|
||||
],
|
||||
predefined: true
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ module PermissionExtends
|
|||
CREATE_COMMENTS
|
||||
MANAGE_COMMENTS
|
||||
MANAGE_TAGS
|
||||
).each { |permission| const_set(permission, permission.underscore) }
|
||||
).each { |permission| const_set(permission, "project_#{permission.underscore}") }
|
||||
end
|
||||
|
||||
module ExperimentPermissions
|
||||
|
@ -24,7 +24,7 @@ module PermissionExtends
|
|||
CLONE
|
||||
MOVE
|
||||
CREATE_TASKS
|
||||
).each { |permission| const_set(permission, permission.underscore) }
|
||||
).each { |permission| const_set(permission, "experiment_#{permission.underscore}") }
|
||||
end
|
||||
|
||||
module MyModulePermissions
|
||||
|
@ -41,7 +41,7 @@ module PermissionExtends
|
|||
MANAGE_COMMENTS
|
||||
CREATE_REPOSITORY_SNAPSHOT
|
||||
MANAGE_REPOSITORY_SNAPSHOT
|
||||
).each { |permission| const_set(permission, permission.underscore) }
|
||||
).each { |permission| const_set(permission, "task_#{permission.underscore}") }
|
||||
end
|
||||
|
||||
module RepositoryPermissions
|
||||
|
@ -59,6 +59,6 @@ module PermissionExtends
|
|||
CREATE_COLUMN
|
||||
UPDATE_COLUMN
|
||||
DELETE_COLUMN
|
||||
).each { |permission| const_set(permission, permission.underscore) }
|
||||
).each { |permission| const_set(permission, "inventory_#{permission.underscore}") }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,8 +6,8 @@ class CreateUserRolesAndAssignments < ActiveRecord::Migration[6.1]
|
|||
t.string :name
|
||||
t.boolean :predefined, default: false
|
||||
t.string :permissions, array: true, default: []
|
||||
t.references :created_by, foreign_key: { to_table: :users }, null: false
|
||||
t.references :last_modified_by, foreign_key: { to_table: :users }, null: false
|
||||
t.references :created_by, foreign_key: { to_table: :users }, null: true
|
||||
t.references :last_modified_by, foreign_key: { to_table: :users }, null: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
@ -16,7 +16,7 @@ class CreateUserRolesAndAssignments < ActiveRecord::Migration[6.1]
|
|||
t.references :assignable, polymorphic: true, null: false
|
||||
t.references :user, foreign_key: true, null: false
|
||||
t.references :user_role, foreign_key: true, null: false
|
||||
t.references :assigned_by, foreign_key: { to_table: :users }, null: false
|
||||
t.references :assigned_by, foreign_key: { to_table: :users }, null: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
|
71
db/migrate/20210222123823_migrate_to_new_user_roles.rb
Normal file
71
db/migrate/20210222123823_migrate_to_new_user_roles.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class MigrateToNewUserRoles < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
owner_role = UserRole.owner_role
|
||||
owner_role.save!
|
||||
normal_user_role = UserRole.normal_user_role
|
||||
normal_user_role.save!
|
||||
technician_role = UserRole.technician_role
|
||||
technician_role.save!
|
||||
viewer_role = UserRole.viewer_role
|
||||
viewer_role.save!
|
||||
|
||||
create_user_assignments(UserProject.owner, owner_role)
|
||||
create_user_assignments(UserProject.normal_user, normal_user_role)
|
||||
create_user_assignments(UserProject.technician, technician_role)
|
||||
create_user_assignments(UserProject.viewer, viewer_role)
|
||||
|
||||
UserProject.delete_all
|
||||
end
|
||||
dir.down do
|
||||
project_assignments = UserAssignment.joins(:user_role).where(assignable_type: 'Project')
|
||||
create_user_project(
|
||||
project_assignments.where(user_role: { name: I18n.t('user_roles.predefined.owner'), predefined: true }),
|
||||
'owner'
|
||||
)
|
||||
create_user_project(
|
||||
project_assignments.where(user_role: { name: I18n.t('user_roles.predefined.normal_user'), predefined: true }),
|
||||
'normal_user'
|
||||
)
|
||||
create_user_project(
|
||||
project_assignments.where(user_role: { name: I18n.t('user_roles.predefined.technician'), predefined: true }),
|
||||
'technician'
|
||||
)
|
||||
create_user_project(
|
||||
project_assignments.where(user_role: { name: I18n.t('user_roles.predefined.viewer'), predefined: true }),
|
||||
'viewer'
|
||||
)
|
||||
|
||||
UserAssignment.joins(:user_role).where(user_role: { predefined: true }).delete_all
|
||||
UserRole.where(predefined: true).delete_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_user_assignments(user_projects, user_role)
|
||||
user_projects.includes(:user, :project).find_in_batches(batch_size: 100) do |user_project_batch|
|
||||
user_assignments = []
|
||||
user_project_batch.each do |user_project|
|
||||
user_assignments << UserAssignment.new(user: user_project.user,
|
||||
assignable: user_project.project,
|
||||
user_role: user_role)
|
||||
end
|
||||
UserAssignment.import(user_assignments)
|
||||
end
|
||||
end
|
||||
|
||||
def create_user_project(user_roles, role)
|
||||
user_roles.includes(:user, :assignable).find_in_batches(batch_size: 100) do |user_role_batch|
|
||||
user_projects = []
|
||||
user_role_batch.each do |user_role|
|
||||
user_projects << UserProject.new(user: user_role.user, project: user_role.assignable, role: role)
|
||||
end
|
||||
UserProject.import(user_projects)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2440,7 +2440,7 @@ CREATE TABLE public.user_assignments (
|
|||
assignable_id bigint NOT NULL,
|
||||
user_id bigint NOT NULL,
|
||||
user_role_id bigint NOT NULL,
|
||||
assigned_by_id bigint NOT NULL,
|
||||
assigned_by_id bigint,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
updated_at timestamp(6) without time zone NOT NULL
|
||||
);
|
||||
|
@ -2611,8 +2611,8 @@ CREATE TABLE public.user_roles (
|
|||
name character varying,
|
||||
predefined boolean DEFAULT false,
|
||||
permissions character varying[] DEFAULT '{}'::character varying[],
|
||||
created_by_id bigint NOT NULL,
|
||||
last_modified_by_id bigint NOT NULL,
|
||||
created_by_id bigint,
|
||||
last_modified_by_id bigint,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
updated_at timestamp(6) without time zone NOT NULL
|
||||
);
|
||||
|
@ -7336,6 +7336,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20201209165626'),
|
||||
('20210128105457'),
|
||||
('20210128105458'),
|
||||
('20210202214508');
|
||||
('20210202214508'),
|
||||
('20210222123823');
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue