Fixed scopes and added permission checks to quick create services [SCI-6135] (#3584)

This commit is contained in:
artoscinote 2021-10-14 13:13:29 +02:00 committed by GitHub
parent 4a4c344a3a
commit c63090da8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 38 deletions

View file

@ -57,11 +57,14 @@ module Dashboard
end end
def load_project def load_project
@project = current_team.projects.find_by(id: params.dig(:project, :id)) @project = current_team.projects.managable_by_user(current_user).find_by(id: params.dig(:project, :id))
end end
def load_experiment def load_experiment
@experiment = @project.experiments.find_by(id: params.dig(:experiment, :id)) if @project return unless @project
@experiment =
@project.experiments.managable_by_user(current_user).find_by(id: params.dig(:experiment, :id))
end end
def check_task_create_permissions def check_task_create_permissions

View file

@ -22,7 +22,7 @@ module Assignable
.where('? = ANY(user_roles.permissions)', "::#{self.class.to_s.split('::').first}Permissions".constantize::MANAGE) .where('? = ANY(user_roles.permissions)', "::#{self.class.to_s.split('::').first}Permissions".constantize::MANAGE)
} }
after_create_commit do after_create do
UserAssignment.create!( UserAssignment.create!(
user: created_by, user: created_by,
assignable: self, assignable: self,

View file

@ -4,7 +4,7 @@ class Experiment < ApplicationRecord
ID_PREFIX = 'EX' ID_PREFIX = 'EX'
include PrefixedIdModel include PrefixedIdModel
SEARCHABLE_ATTRIBUTES = [:name, :description, PREFIXED_ID_SQL].freeze SEARCHABLE_ATTRIBUTES = ['experiments.name', 'experiments.description', PREFIXED_ID_SQL].freeze
include ArchivableModel include ArchivableModel
include SearchableModel include SearchableModel

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class CreateExperimentService class CreateExperimentService
include Canaid::Helpers::PermissionsHelper
def initialize(user, team, params) def initialize(user, team, params)
@params = params @params = params
@user = user @user = user
@ -8,26 +10,21 @@ class CreateExperimentService
end end
def call def call
new_experiment = nil
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
unless @params[:project].class == Project unless @params[:project].instance_of?(Project)
@params[:project] = CreateProjectService.new(@user, @team, @params[:project]).call @params[:project] = CreateProjectService.new(@user, @team, @params[:project]).call
end end
unless @params[:project]&.errors&.empty?
new_experiment = @params[:project] raise ActiveRecord::Rollback unless @params[:project]&.valid? &&
raise ActiveRecord::Rollback can_create_project_experiments?(@user, @params[:project])
end
@params[:created_by] = @user @params[:created_by] = @user
@params[:last_modified_by] = @user @params[:last_modified_by] = @user
@experiment = @params[:project].experiments.new(@params) @experiment = @params[:project].experiments.create!(@params)
create_experiment_activity
create_experiment_activity if @experiment.save
new_experiment = @experiment
end end
new_experiment @experiment
end end
private private

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class CreateMyModuleService class CreateMyModuleService
include Canaid::Helpers::PermissionsHelper
def initialize(user, team, params) def initialize(user, team, params)
@params = params @params = params
@my_module_params = params[:my_module] || {} @my_module_params = params[:my_module] || {}
@ -9,16 +11,14 @@ class CreateMyModuleService
end end
def call def call
new_my_module = nil
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
unless @params[:experiment].class == Experiment unless @params[:experiment].instance_of?(Experiment)
@params[:experiment][:project] = @params[:project] @params[:experiment][:project] = @params[:project]
@params[:experiment] = CreateExperimentService.new(@user, @team, @params[:experiment]).call @params[:experiment] = CreateExperimentService.new(@user, @team, @params[:experiment]).call
end end
unless @params[:experiment]&.errors&.empty?
new_my_module = @params[:experiment] raise ActiveRecord::Rollback unless @params[:experiment]&.valid? &&
raise ActiveRecord::Rollback can_manage_experiment_tasks?(@user, @params[:experiment])
end
@my_module_params[:x] ||= 0 @my_module_params[:x] ||= 0
@my_module_params[:y] ||= 0 @my_module_params[:y] ||= 0
@ -36,10 +36,9 @@ class CreateMyModuleService
create_my_module_activity create_my_module_activity
@my_module.assign_user(@user) @my_module.assign_user(@user)
new_my_module = @my_module
end end
new_my_module
@my_module
end end
private private

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class CreateProjectService class CreateProjectService
include Canaid::Helpers::PermissionsHelper
def initialize(user, team, params) def initialize(user, team, params)
@params = params @params = params
@user = user @user = user
@ -8,24 +10,16 @@ class CreateProjectService
end end
def call def call
new_project = nil return unless can_create_projects?(@user, @team)
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
@params[:created_by] = @user @params[:created_by] = @user
@params[:last_modified_by] = @user @params[:last_modified_by] = @user
@project = @team.projects.new(@params) @project = @team.projects.create!(@params)
create_project_activity
if @project.save
@project.user_projects.create!(role: :owner, user: @user)
create_project_activity
new_project = @project
else
new_project = @project
raise ActiveRecord::Rollback
end
end end
new_project @project
end end
private private