diff --git a/app/jobs/user_assignments/generate_user_assignments_job.rb b/app/jobs/user_assignments/generate_user_assignments_job.rb index d3632cadc..884a940c2 100644 --- a/app/jobs/user_assignments/generate_user_assignments_job.rb +++ b/app/jobs/user_assignments/generate_user_assignments_job.rb @@ -46,9 +46,9 @@ module UserAssignments end def assign_users_to_protocol(protocol) - return unless protocol.in_repository_public? + return unless protocol.visible? - protocol.add_team_users_as_viewers!(@assigned_by) + protocol.create_public_user_assignments!(@assigned_by) end def assign_users_to_report(report) diff --git a/app/models/protocol.rb b/app/models/protocol.rb index a2d76bea5..69857803e 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -18,6 +18,7 @@ class Protocol < ApplicationRecord after_save :update_linked_children skip_callback :create, :after, :create_users_assignments, if: -> { in_module? } + enum visibility: { hidden: 0, visible: 1 } enum protocol_type: { unlinked: 0, linked: 1, @@ -104,6 +105,9 @@ class Protocol < ApplicationRecord inverse_of: :protocols, optional: true belongs_to :team, inverse_of: :protocols + belongs_to :default_public_user_role, + class_name: 'UserRole', + optional: true belongs_to :previous_version, class_name: 'Protocol', inverse_of: :next_version, @@ -653,11 +657,13 @@ class Protocol < ApplicationRecord steps.map(&:can_destroy?).all? end - def add_team_users_as_viewers!(assigned_by) - viewer_role = UserRole.find_predefined_viewer_role - team.user_assignments.where.not(user: assigned_by).find_each do |user_assignment| - new_user_assignment = UserAssignment.find_or_initialize_by(user: user_assignment.user, assignable: self) - new_user_assignment.user_role = viewer_role + def create_public_user_assignments!(assigned_by) + public_role = default_public_user_role || UserRole.find_predefined_viewer_role + team.user_assignments.where.not(user: assigned_by).find_each do |team_user_assignment| + new_user_assignment = user_assignments.find_or_initialize_by(user: team_user_assignment.user) + next if new_user_assignment.manually_assigned? + + new_user_assignment.user_role = public_role new_user_assignment.assigned_by = assigned_by new_user_assignment.assigned = :automatically new_user_assignment.save! @@ -667,11 +673,10 @@ class Protocol < ApplicationRecord private def update_user_assignments - case protocol_type - when 'in_repository_public' - assigned_by = protocol_type_was == 'in_repository_archived' && restored_by || added_by - add_team_users_as_viewers!(assigned_by) - when 'in_repository_private', 'in_repository_archived' + case visibility + when 'visible' + create_public_user_assignments!(added_by) + when 'hidden' automatic_user_assignments.where.not(user: added_by).destroy_all end end diff --git a/db/migrate/20221125133611_add_protocol_versioning.rb b/db/migrate/20221125133611_add_protocol_versioning.rb index c40119bcd..0d4a1a03a 100644 --- a/db/migrate/20221125133611_add_protocol_versioning.rb +++ b/db/migrate/20221125133611_add_protocol_versioning.rb @@ -3,20 +3,23 @@ class AddProtocolVersioning < ActiveRecord::Migration[6.1] def up change_table :protocols, bulk: true do |t| + t.integer :visibility, index: true, default: 0 t.boolean :archived, default: false, null: false, index: true t.integer :version_number, default: 1 t.string :version_comment + t.references :default_public_user_role, foreign_key: { to_table: :user_roles } t.references :previous_version, index: true, foreign_key: { to_table: :protocols } t.references :last_modified_by, index: true, foreign_key: { to_table: :users } t.references :published_by, index: true, foreign_key: { to_table: :users } end + execute( 'UPDATE "protocols" SET "published_on" = "created_at", "published_by_id" = "added_by_id" ' \ 'WHERE "protocols"."protocol_type" IN (2, 4) ' \ 'AND "protocols"."published_on" IS NULL;' ) execute( - 'UPDATE "protocols" SET "published_by_id" = "added_by_id" ' \ + 'UPDATE "protocols" SET "published_by_id" = "added_by_id", "visibility" = 1 ' \ 'WHERE "protocols"."protocol_type" = 3;' ) execute( @@ -36,9 +39,11 @@ class AddProtocolVersioning < ActiveRecord::Migration[6.1] t.remove_references :published_by t.remove_references :last_modified_by t.remove_references :previous_version + t.remove_references :default_public_user_role t.remove :version_comment t.remove :version_number t.remove :archived + t.remove :visibility end end end diff --git a/db/structure.sql b/db/structure.sql index 75a507cb7..ab97630d7 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1372,9 +1372,11 @@ CREATE TABLE public.protocols ( updated_at timestamp without time zone NOT NULL, published_on timestamp without time zone, nr_of_linked_children integer DEFAULT 0, + visibility integer DEFAULT 0, archived boolean DEFAULT false NOT NULL, version_number integer DEFAULT 1, version_comment character varying, + default_public_user_role_id bigint, previous_version_id bigint, last_modified_by_id bigint, published_by_id bigint @@ -5585,6 +5587,13 @@ CREATE INDEX index_protocols_on_archived_by_id ON public.protocols USING btree ( CREATE INDEX index_protocols_on_authors ON public.protocols USING gin (public.trim_html_tags(authors) public.gin_trgm_ops); +-- +-- Name: index_protocols_on_default_public_user_role_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_protocols_on_default_public_user_role_id ON public.protocols USING btree (default_public_user_role_id); + + -- -- Name: index_protocols_on_description; Type: INDEX; Schema: public; Owner: - -- @@ -5655,6 +5664,13 @@ CREATE INDEX index_protocols_on_restored_by_id ON public.protocols USING btree ( CREATE INDEX index_protocols_on_team_id ON public.protocols USING btree (team_id); +-- +-- Name: index_protocols_on_visibility; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_protocols_on_visibility ON public.protocols USING btree (visibility); + + -- -- Name: index_report_elements_on_asset_id; Type: INDEX; Schema: public; Owner: - -- @@ -7298,6 +7314,14 @@ ALTER TABLE ONLY public.repository_stock_unit_items ADD CONSTRAINT fk_rails_4c20ff4c1f FOREIGN KEY (last_modified_by_id) REFERENCES public.users(id); +-- +-- Name: protocols fk_rails_4c4d4f815a; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.protocols + ADD CONSTRAINT fk_rails_4c4d4f815a FOREIGN KEY (default_public_user_role_id) REFERENCES public.user_roles(id); + + -- -- Name: repository_status_values fk_rails_4cf67f9f1e; Type: FK CONSTRAINT; Schema: public; Owner: - --