Add models for new permissions objects [SCI-5434]

This commit is contained in:
Oleksii Kriuchykhin 2021-02-04 14:08:20 +01:00
parent aa3944fea8
commit 1e3e701788
12 changed files with 299 additions and 6 deletions

View file

@ -22,6 +22,8 @@ class Experiment < ApplicationRecord
has_many :report_elements, inverse_of: :experiment, dependent: :destroy
# Associations for old activity type
has_many :activities, inverse_of: :experiment
has_many :user_assignments, as: :assignable, dependent: :destroy
has_many :users, through: :user_assignments
has_one_attached :workflowimg

View file

@ -52,6 +52,7 @@ class MyModule < ApplicationRecord
has_many :repository_snapshots, dependent: :destroy, inverse_of: :my_module
has_many :user_my_modules, inverse_of: :my_module, dependent: :destroy
has_many :users, through: :user_my_modules
has_many :user_assignments, as: :assignable, dependent: :destroy
has_many :report_elements, inverse_of: :my_module, dependent: :destroy
has_many :protocols, inverse_of: :my_module, dependent: :destroy
# Associations for old activity type

View file

@ -35,7 +35,8 @@ class Project < ApplicationRecord
belongs_to :team, inverse_of: :projects, touch: true
belongs_to :project_folder, inverse_of: :projects, optional: true, touch: true
has_many :user_projects, inverse_of: :project
has_many :users, through: :user_projects
has_many :user_assignments, as: :assignable, dependent: :destroy
has_many :users, through: :user_assignments
has_many :experiments, inverse_of: :project
has_many :active_experiments, -> { where(archived: false) },
class_name: 'Experiment'

View file

@ -59,6 +59,7 @@ class User < ApplicationRecord
has_many :user_identities, inverse_of: :user
has_many :user_teams, inverse_of: :user
has_many :teams, through: :user_teams
has_many :user_assignments, dependent: :destroy
has_many :user_projects, inverse_of: :user
has_many :projects, through: :user_projects
has_many :user_my_modules, inverse_of: :user

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
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'
end

14
app/models/user_role.rb Normal file
View file

@ -0,0 +1,14 @@
# frozen_string_literal: true
class UserRole < ApplicationRecord
validates :name,
presence: true,
length: { minimum: Constants::NAME_MIN_LENGTH,
maximum: Constants::NAME_MAX_LENGTH },
uniqueness: { case_sensitive: false }
validates :permissions, presence: true, length: { minimum: 1 }
belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User'
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User'
has_many :user_assignments, dependent: :destroy
end

View file

@ -0,0 +1,23 @@
# frozen_string_literal: true
class CreateUserRolesAndAssignments < ActiveRecord::Migration[6.1]
def change
create_table :user_roles do |t|
t.string :name
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.timestamps
end
create_table :user_assignments do |t|
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.timestamps
end
end
end

View file

@ -2430,6 +2430,41 @@ CREATE SEQUENCE public.tokens_id_seq
ALTER SEQUENCE public.tokens_id_seq OWNED BY public.tokens.id;
--
-- Name: user_assignments; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.user_assignments (
id bigint NOT NULL,
assignable_type character varying NOT NULL,
assignable_id bigint NOT NULL,
user_id bigint NOT NULL,
user_role_id bigint NOT NULL,
assigned_by_id bigint NOT NULL,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);
--
-- Name: user_assignments_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.user_assignments_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: user_assignments_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.user_assignments_id_seq OWNED BY public.user_assignments.id;
--
-- Name: user_identities; Type: TABLE; Schema: public; Owner: -
--
@ -2567,6 +2602,40 @@ CREATE SEQUENCE public.user_projects_id_seq
ALTER SEQUENCE public.user_projects_id_seq OWNED BY public.user_projects.id;
--
-- Name: user_roles; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.user_roles (
id bigint NOT NULL,
name character varying,
permissions character varying[] DEFAULT '{}'::character varying[],
created_by_id bigint NOT NULL,
last_modified_by_id bigint NOT NULL,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);
--
-- Name: user_roles_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.user_roles_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: user_roles_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.user_roles_id_seq OWNED BY public.user_roles.id;
--
-- Name: user_system_notifications; Type: TABLE; Schema: public; Owner: -
--
@ -3307,6 +3376,13 @@ ALTER TABLE ONLY public.tiny_mce_assets ALTER COLUMN id SET DEFAULT nextval('pub
ALTER TABLE ONLY public.tokens ALTER COLUMN id SET DEFAULT nextval('public.tokens_id_seq'::regclass);
--
-- Name: user_assignments id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_assignments ALTER COLUMN id SET DEFAULT nextval('public.user_assignments_id_seq'::regclass);
--
-- Name: user_identities id; Type: DEFAULT; Schema: public; Owner: -
--
@ -3335,6 +3411,13 @@ ALTER TABLE ONLY public.user_notifications ALTER COLUMN id SET DEFAULT nextval('
ALTER TABLE ONLY public.user_projects ALTER COLUMN id SET DEFAULT nextval('public.user_projects_id_seq'::regclass);
--
-- Name: user_roles id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_roles ALTER COLUMN id SET DEFAULT nextval('public.user_roles_id_seq'::regclass);
--
-- Name: user_system_notifications id; Type: DEFAULT; Schema: public; Owner: -
--
@ -3927,6 +4010,14 @@ ALTER TABLE ONLY public.tokens
ADD CONSTRAINT tokens_pkey PRIMARY KEY (id);
--
-- Name: user_assignments user_assignments_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_assignments
ADD CONSTRAINT user_assignments_pkey PRIMARY KEY (id);
--
-- Name: user_identities user_identities_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@ -3959,6 +4050,14 @@ ALTER TABLE ONLY public.user_projects
ADD CONSTRAINT user_projects_pkey PRIMARY KEY (id);
--
-- Name: user_roles user_roles_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_roles
ADD CONSTRAINT user_roles_pkey PRIMARY KEY (id);
--
-- Name: user_system_notifications user_system_notifications_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@ -4927,10 +5026,10 @@ CREATE INDEX index_repository_cells_on_repository_row_id ON public.repository_ce
--
-- Name: index_repository_cells_on_value_type_and_value_id; Type: INDEX; Schema: public; Owner: -
-- Name: index_repository_cells_on_value; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_repository_cells_on_value_type_and_value_id ON public.repository_cells USING btree (value_type, value_id);
CREATE INDEX index_repository_cells_on_value ON public.repository_cells USING btree (value_type, value_id);
--
@ -5486,6 +5585,34 @@ CREATE INDEX index_tiny_mce_assets_on_object_type_and_object_id ON public.tiny_m
CREATE INDEX index_tiny_mce_assets_on_team_id ON public.tiny_mce_assets USING btree (team_id);
--
-- Name: index_user_assignments_on_assignable; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_user_assignments_on_assignable ON public.user_assignments USING btree (assignable_type, assignable_id);
--
-- Name: index_user_assignments_on_assigned_by_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_user_assignments_on_assigned_by_id ON public.user_assignments USING btree (assigned_by_id);
--
-- Name: index_user_assignments_on_user_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_user_assignments_on_user_id ON public.user_assignments USING btree (user_id);
--
-- Name: index_user_assignments_on_user_role_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_user_assignments_on_user_role_id ON public.user_assignments USING btree (user_role_id);
--
-- Name: index_user_identities_on_provider_and_uid; Type: INDEX; Schema: public; Owner: -
--
@ -5577,6 +5704,20 @@ CREATE INDEX index_user_projects_on_user_id ON public.user_projects USING btree
CREATE UNIQUE INDEX index_user_projects_on_user_id_and_project_id ON public.user_projects USING btree (user_id, project_id);
--
-- Name: index_user_roles_on_created_by_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_user_roles_on_created_by_id ON public.user_roles USING btree (created_by_id);
--
-- Name: index_user_roles_on_last_modified_by_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_user_roles_on_last_modified_by_id ON public.user_roles USING btree (last_modified_by_id);
--
-- Name: index_user_system_notifications_on_read_at; Type: INDEX; Schema: public; Owner: -
--
@ -5711,10 +5852,10 @@ CREATE INDEX index_view_states_on_user_id ON public.view_states USING btree (use
--
-- Name: index_view_states_on_viewable_type_and_viewable_id; Type: INDEX; Schema: public; Owner: -
-- Name: index_view_states_on_viewable; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_view_states_on_viewable_type_and_viewable_id ON public.view_states USING btree (viewable_type, viewable_id);
CREATE INDEX index_view_states_on_viewable ON public.view_states USING btree (viewable_type, viewable_id);
--
@ -5771,6 +5912,14 @@ ALTER TABLE ONLY public.assets
ADD CONSTRAINT fk_rails_0916329f9e FOREIGN KEY (created_by_id) REFERENCES public.users(id);
--
-- Name: user_assignments fk_rails_0b13c65ab0; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_assignments
ADD CONSTRAINT fk_rails_0b13c65ab0 FOREIGN KEY (user_id) REFERENCES public.users(id);
--
-- Name: my_modules fk_rails_0d264d93f8; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -5819,6 +5968,14 @@ ALTER TABLE ONLY public.zip_exports
ADD CONSTRAINT fk_rails_1952fc2261 FOREIGN KEY (user_id) REFERENCES public.users(id);
--
-- Name: user_assignments fk_rails_19dca62dfc; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_assignments
ADD CONSTRAINT fk_rails_19dca62dfc FOREIGN KEY (user_role_id) REFERENCES public.user_roles(id);
--
-- Name: user_system_notifications fk_rails_20d9487a3c; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -6059,6 +6216,14 @@ ALTER TABLE ONLY public.user_my_modules
ADD CONSTRAINT fk_rails_62fa90cb51 FOREIGN KEY (user_id) REFERENCES public.users(id);
--
-- Name: user_assignments fk_rails_6467e12fc3; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_assignments
ADD CONSTRAINT fk_rails_6467e12fc3 FOREIGN KEY (assigned_by_id) REFERENCES public.users(id);
--
-- Name: user_teams fk_rails_64c25f3fe6; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -6395,6 +6560,14 @@ ALTER TABLE ONLY public.user_teams
ADD CONSTRAINT fk_rails_978858c8ea FOREIGN KEY (user_id) REFERENCES public.users(id);
--
-- Name: user_roles fk_rails_983264fab9; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_roles
ADD CONSTRAINT fk_rails_983264fab9 FOREIGN KEY (created_by_id) REFERENCES public.users(id);
--
-- Name: repository_checklist_values fk_rails_98a7704432; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -6683,6 +6856,14 @@ ALTER TABLE ONLY public.checklists
ADD CONSTRAINT fk_rails_c89d59efd3 FOREIGN KEY (created_by_id) REFERENCES public.users(id);
--
-- Name: user_roles fk_rails_c958cec38d; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.user_roles
ADD CONSTRAINT fk_rails_c958cec38d FOREIGN KEY (last_modified_by_id) REFERENCES public.users(id);
--
-- Name: my_module_tags fk_rails_cb98be233f; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -7153,6 +7334,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20201126203713'),
('20201209165626'),
('20210128105457'),
('20210128105458');
('20210128105458'),
('20210202214508');

View file

@ -0,0 +1,5 @@
FactoryBot.define do
factory :user_assignment do
end
end

View file

@ -0,0 +1,5 @@
FactoryBot.define do
factory :user_role do
end
end

View file

@ -0,0 +1,24 @@
require 'rails_helper'
RSpec.describe UserAssignment, type: :model do
it 'should be of class UserAssignment' do
expect(subject.class).to eq UserAssignment
end
describe 'Database table' do
it { should have_db_column :assignable_type }
it { should have_db_column :assignable_id }
it { should have_db_column :user_id }
it { should have_db_column :user_role_id }
it { should have_db_column :assigned_by_id }
it { should have_db_column :created_at }
it { should have_db_column :updated_at }
end
describe 'Relations' do
it { should belong_to :assignable }
it { should belong_to :user }
it { should belong_to :user_role }
it { should belong_to(:assigned_by).class_name('User') }
end
end

View file

@ -0,0 +1,27 @@
require 'rails_helper'
RSpec.describe UserRole, type: :model do
it 'should be of class UserRole' do
expect(subject.class).to eq UserRole
end
describe 'Database table' do
it { should have_db_column :name }
it { should have_db_column :permissions }
it { should have_db_column :created_by_id }
it { should have_db_column :last_modified_by_id }
it { should have_db_column :created_at }
it { should have_db_column :updated_at }
end
describe 'Relations' do
it { should have_many :user_assignments }
it { should belong_to :created_by }
it { should belong_to :last_modified_by }
end
describe 'Should be a valid object' do
it { should validate_presence_of :name }
it { should validate_presence_of :permissions }
end
end