diff --git a/app/controllers/access_permissions/experiments_controller.rb b/app/controllers/access_permissions/experiments_controller.rb index b18b128c0..3b902c20b 100644 --- a/app/controllers/access_permissions/experiments_controller.rb +++ b/app/controllers/access_permissions/experiments_controller.rb @@ -2,8 +2,8 @@ module AccessPermissions class ExperimentsController < ApplicationController - before_action :set_project before_action :set_experiment + before_action :set_project before_action :check_read_permissions, only: %i(show) before_action :check_manage_permissions, only: %i(edit update) @@ -59,13 +59,11 @@ module AccessPermissions end def set_project - @project = current_team.projects.find_by(id: params[:project_id]) - - render_404 unless @project + @project = @experiment.project end def set_experiment - @experiment = @project.experiments.includes(user_assignments: %i(user user_role)).find_by(id: params[:id]) + @experiment = Experiment.includes(user_assignments: %i(user user_role)).find_by(id: params[:id]) render_404 unless @experiment end diff --git a/app/controllers/access_permissions/my_modules_controller.rb b/app/controllers/access_permissions/my_modules_controller.rb index 783533355..1bed4d660 100644 --- a/app/controllers/access_permissions/my_modules_controller.rb +++ b/app/controllers/access_permissions/my_modules_controller.rb @@ -2,9 +2,9 @@ module AccessPermissions class MyModulesController < ApplicationController - before_action :set_project - before_action :set_experiment before_action :set_my_module + before_action :set_experiment + before_action :set_project before_action :check_read_permissions, only: %i(show) before_action :check_manage_permissions, only: %i(edit update) @@ -53,19 +53,15 @@ module AccessPermissions end def set_project - @project = current_team.projects.find_by(id: params[:project_id]) - - render_404 unless @project + @project = @experiment.project end def set_experiment - @experiment = @project.experiments.find_by(id: params[:experiment_id]) - - render_404 unless @experiment + @experiment = @my_module.experiment end def set_my_module - @my_module = @experiment.my_modules.includes(user_assignments: %i(user user_role)).find_by(id: params[:id]) + @my_module = MyModule.includes(user_assignments: %i(user user_role)).find_by(id: params[:id]) render_404 unless @my_module end diff --git a/app/controllers/access_permissions/projects_controller.rb b/app/controllers/access_permissions/projects_controller.rb index 54a2da719..57ae09775 100644 --- a/app/controllers/access_permissions/projects_controller.rb +++ b/app/controllers/access_permissions/projects_controller.rb @@ -49,6 +49,7 @@ module AccessPermissions def create ActiveRecord::Base.transaction do + created_count = 0 permitted_create_params[:resource_members].each do |_k, user_assignment_params| next unless user_assignment_params[:assign] == '1' @@ -65,11 +66,12 @@ module AccessPermissions ) log_activity(:assign_user_to_project, user_assignment) + created_count += 1 propagate_job(user_assignment) end respond_to do |format| - @message = t('access_permissions.create.success', count: @project.user_assignments.count) + @message = t('access_permissions.create.success', count: created_count) format.json { render :edit } end rescue ActiveRecord::RecordInvalid @@ -104,8 +106,13 @@ module AccessPermissions end def update_default_public_user_role - @project.update!(permitted_default_public_user_role_params) - UserAssignments::ProjectGroupAssignmentJob.perform_later(current_team, @project, current_user) + Project.transaction do + @project.visibility = :hidden if permitted_default_public_user_role_params[:default_public_user_role_id].blank? + @project.assign_attributes(permitted_default_public_user_role_params) + @project.save! + + UserAssignments::ProjectGroupAssignmentJob.perform_later(current_team, @project, current_user) + end end private diff --git a/app/controllers/access_permissions/protocols_controller.rb b/app/controllers/access_permissions/protocols_controller.rb index 2c2684115..0a78bfdbb 100644 --- a/app/controllers/access_permissions/protocols_controller.rb +++ b/app/controllers/access_permissions/protocols_controller.rb @@ -47,19 +47,22 @@ module AccessPermissions def create ActiveRecord::Base.transaction do + created_count = 0 permitted_create_params[:resource_members].each do |_k, user_assignment_params| next unless user_assignment_params[:assign] == '1' user_assignment = UserAssignment.new(user_assignment_params) user_assignment.assignable = @protocol + user_assignment.assigned = :manually user_assignment.team = current_team user_assignment.assigned_by = current_user user_assignment.save! + created_count += 1 log_activity(:protocol_template_access_granted, user_assignment) end respond_to do |format| - @message = t('access_permissions.create.success', count: @protocol.user_assignments.count) + @message = t('access_permissions.create.success', count: created_count) format.json { render :edit } end rescue ActiveRecord::RecordInvalid @@ -90,8 +93,22 @@ module AccessPermissions end end + def update_default_public_user_role + Protocol.transaction do + @protocol.visibility = :hidden if permitted_default_public_user_role_params[:default_public_user_role_id].blank? + @protocol.assign_attributes(permitted_default_public_user_role_params) + @protocol.save! + + UserAssignments::ProjectGroupAssignmentJob.perform_later(current_team, @project, current_user) + end + end + private + def permitted_default_public_user_role_params + params.require(:protocol).permit(:default_public_user_role_id) + end + def permitted_update_params params.require(:user_assignment) .permit(%i(user_role_id user_id)) diff --git a/app/models/concerns/assignable.rb b/app/models/concerns/assignable.rb index a85797ecc..54fc2c390 100644 --- a/app/models/concerns/assignable.rb +++ b/app/models/concerns/assignable.rb @@ -54,12 +54,16 @@ module Assignable end end + def top_level_assignable? + self.class.name.in?(Extends::TOP_LEVEL_ASSIGNABLES) + end + private def create_users_assignments return if skip_user_assignments - role = if is_a?(Project) || is_a?(Team) + role = if top_level_assignable? UserRole.find_predefined_owner_role else permission_parent.user_assignments.find_by(user: created_by).user_role @@ -68,7 +72,7 @@ module Assignable UserAssignment.create!( user: created_by, assignable: self, - assigned: is_a?(Project) ? :manually : :automatically, + assigned: top_level_assignable? ? :manually : :automatically, user_role: role ) diff --git a/app/models/protocol.rb b/app/models/protocol.rb index ba347d7f8..7f2fbe3cc 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -16,10 +16,10 @@ class Protocol < ApplicationRecord include TinyMceImages before_validation :assign_version_number, on: :update, if: -> { protocol_type_changed? && in_repository_published? } - after_update :update_user_assignments, if: -> { saved_change_to_protocol_type? && in_repository? } - after_destroy :decrement_linked_children - after_save :update_linked_children after_create :auto_assign_protocol_members, if: :visible? + after_destroy :decrement_linked_children + after_save :update_user_assignments, if: -> { saved_change_to_visibility? && in_repository? } + after_save :update_linked_children skip_callback :create, :after, :create_users_assignments, if: -> { in_module? } enum visibility: { hidden: 0, visible: 1 } @@ -40,7 +40,9 @@ class Protocol < ApplicationRecord validate :prevent_update, on: :update, if: lambda { - in_repository_published? && !protocol_type_changed?(from: 'in_repository_draft') && !archived_changed? + # skip check if only public role of visibility changed + (changes.keys | %w(default_public_user_role_id visibility)).length != 2 && + in_repository_published? && !protocol_type_changed?(from: 'in_repository_draft') && !archived_changed? } with_options if: :in_module? do diff --git a/app/services/experiments/table_view_service.rb b/app/services/experiments/table_view_service.rb index 3dba492e1..c147e0cce 100644 --- a/app/services/experiments/table_view_service.rb +++ b/app/services/experiments/table_view_service.rb @@ -68,7 +68,6 @@ module Experiments end experiment = my_module.experiment - project = experiment.project result.push({ id: my_module.id, columns: prepared_my_module, @@ -81,7 +80,7 @@ module Experiments provisioning_status: my_module.provisioning_status == 'in_progress' && provisioning_status_my_module_url(my_module), - access: access_url(project, experiment, my_module) + access: access_url(my_module) } }) end @@ -93,11 +92,11 @@ module Experiments private - def access_url(project, experiment, my_module) + def access_url(my_module) if can_manage_my_module_users?(@user, my_module) - edit_access_permissions_project_experiment_my_module_path(project, experiment, my_module) + edit_access_permissions_my_module_path(my_module) else - access_permissions_project_experiment_my_module_path(project, experiment, my_module) + access_permissions_my_module_path(my_module) end end diff --git a/app/services/user_assignments/create_team_user_assignments_service.rb b/app/services/user_assignments/create_team_user_assignments_service.rb index 97ce5503a..f4dfc60e9 100644 --- a/app/services/user_assignments/create_team_user_assignments_service.rb +++ b/app/services/user_assignments/create_team_user_assignments_service.rb @@ -56,7 +56,7 @@ module UserAssignments end def create_protocols_assignments - @team.repository_protocols.in_repository_public.find_each do |protocol| + @team.repository_protocols.visible.find_each do |protocol| create_or_update_user_assignment(protocol, @viewer_role) end end diff --git a/app/views/access_permissions/experiments/edit.json.jbuilder b/app/views/access_permissions/experiments/edit.json.jbuilder index ca2068eed..2bef57ca5 100644 --- a/app/views/access_permissions/experiments/edit.json.jbuilder +++ b/app/views/access_permissions/experiments/edit.json.jbuilder @@ -1,13 +1,12 @@ # frozen_string_literal: true json.modal controller.render_to_string( - partial: 'access_permissions/experiments/modals/edit_modal', + partial: 'access_permissions/modals/edit_modal', formats: [:html], locals: { - experiment: @experiment, - project: @project, - users: @project.manually_assigned_users, - project_path: project_path(@project) + assignable: @experiment, + top_level_assignable: @project, + manually_assigned_users: @project.manually_assigned_users }, layout: false ) diff --git a/app/views/access_permissions/experiments/experiment_member.json.jbuilder b/app/views/access_permissions/experiments/experiment_member.json.jbuilder index 6656bd2ab..65f20cbb6 100644 --- a/app/views/access_permissions/experiments/experiment_member.json.jbuilder +++ b/app/views/access_permissions/experiments/experiment_member.json.jbuilder @@ -5,9 +5,9 @@ json.form controller.render_to_string( formats: [:html], locals: { user: @user_assignment.user, - object: @experiment, + assignable: @experiment, with_inherit: true, - update_path: access_permissions_project_experiment_path(@project, @experiment) + update_path: access_permissions_experiment_path(@experiment) }, layout: false ) diff --git a/app/views/access_permissions/experiments/modals/_edit_modal.html.erb b/app/views/access_permissions/experiments/modals/_edit_modal.html.erb deleted file mode 100644 index 07ea17bf9..000000000 --- a/app/views/access_permissions/experiments/modals/_edit_modal.html.erb +++ /dev/null @@ -1,28 +0,0 @@ -<% # frozen_string_literal: true %> - -