diff --git a/app/models/user_assignment.rb b/app/models/user_assignment.rb index b4a1f2fe9..bd0c728b5 100644 --- a/app/models/user_assignment.rb +++ b/app/models/user_assignment.rb @@ -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 diff --git a/app/models/user_role.rb b/app/models/user_role.rb index c9a121be5..5c9827e50 100644 --- a/app/models/user_role.rb +++ b/app/models/user_role.rb @@ -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 diff --git a/config/initializers/extends/permission_extends.rb b/config/initializers/extends/permission_extends.rb index 1fc307646..d5418cae6 100644 --- a/config/initializers/extends/permission_extends.rb +++ b/config/initializers/extends/permission_extends.rb @@ -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 diff --git a/db/migrate/20210202214508_create_user_roles_and_assignments.rb b/db/migrate/20210202214508_create_user_roles_and_assignments.rb index 2f9dbd5e3..3d6dc65d4 100644 --- a/db/migrate/20210202214508_create_user_roles_and_assignments.rb +++ b/db/migrate/20210202214508_create_user_roles_and_assignments.rb @@ -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 diff --git a/db/migrate/20210222123823_migrate_to_new_user_roles.rb b/db/migrate/20210222123823_migrate_to_new_user_roles.rb new file mode 100644 index 000000000..b7fccc161 --- /dev/null +++ b/db/migrate/20210222123823_migrate_to_new_user_roles.rb @@ -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 diff --git a/db/structure.sql b/db/structure.sql index 2c104a470..e8a29c461 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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');