2021-10-14 11:49:27 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
class UserMyModulesController < ApplicationController
|
2021-10-14 11:49:27 +02:00
|
|
|
include InputSanitizeHelper
|
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
before_action :load_vars
|
2021-09-22 11:28:32 +02:00
|
|
|
before_action :check_view_permissions, except: %i(create destroy)
|
2020-06-30 09:20:53 +02:00
|
|
|
before_action :check_manage_permissions, only: %i(create destroy)
|
2016-11-22 15:16:26 +01:00
|
|
|
|
2020-04-14 17:25:36 +02:00
|
|
|
def index_old
|
2016-11-22 15:16:26 +01:00
|
|
|
@user_my_modules = @my_module.user_my_modules
|
|
|
|
|
|
|
|
respond_to do |format|
|
2016-11-22 15:19:12 +01:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2020-04-14 17:25:36 +02:00
|
|
|
partial: 'index_old.html.erb'
|
2017-05-08 17:32:55 +02:00
|
|
|
),
|
|
|
|
my_module_id: @my_module.id,
|
2021-10-13 10:10:14 +02:00
|
|
|
counter: @my_module.designated_users.count # Used for counter badge
|
2016-11-22 15:16:26 +01:00
|
|
|
}
|
2016-11-22 15:19:12 +01:00
|
|
|
end
|
2016-11-22 15:16:26 +01:00
|
|
|
end
|
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
|
2020-04-14 17:25:36 +02:00
|
|
|
def index
|
2020-04-08 16:24:59 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2020-04-14 17:25:36 +02:00
|
|
|
partial: 'index.html.erb'
|
2020-04-08 16:24:59 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
def index_edit
|
|
|
|
@user_my_modules = @my_module.user_my_modules
|
2021-10-13 09:50:06 +02:00
|
|
|
@undesignated_users = @my_module.undesignated_users.order(full_name: :asc)
|
2016-02-12 16:52:43 +01:00
|
|
|
@new_um = UserMyModule.new(my_module: @my_module)
|
|
|
|
|
|
|
|
respond_to do |format|
|
2017-05-08 17:32:55 +02:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
my_module: @my_module,
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'index_edit.html.erb'
|
|
|
|
)
|
2016-02-12 16:52:43 +01:00
|
|
|
}
|
2017-05-08 17:32:55 +02:00
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@um = UserMyModule.new(um_params.merge(my_module: @my_module))
|
|
|
|
@um.assigned_by = current_user
|
2021-10-14 11:49:27 +02:00
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
if @um.save
|
2022-06-03 10:09:42 +02:00
|
|
|
@um.log_activity(:designate_user_to_my_module, current_user)
|
2019-03-18 15:03:14 +01:00
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
respond_to do |format|
|
2017-12-20 13:39:35 +01:00
|
|
|
format.json do
|
2021-10-14 11:49:27 +02:00
|
|
|
render json: {
|
|
|
|
user: {
|
|
|
|
id: @um.user.id,
|
|
|
|
full_name: @um.user.full_name,
|
|
|
|
avatar_url: avatar_path(@um.user, :icon_small),
|
|
|
|
user_module_id: @um.id
|
|
|
|
}, status: :ok
|
|
|
|
}
|
2017-12-20 13:39:35 +01:00
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
2018-11-20 14:29:33 +01:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
errors: @um.errors
|
2021-10-14 11:49:27 +02:00
|
|
|
}, status: :unprocessable_entity
|
2018-11-20 14:29:33 +01:00
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
if @um.destroy
|
2022-06-03 10:09:42 +02:00
|
|
|
@um.log_activity(:undesignate_user_from_my_module, current_user)
|
2016-02-12 16:52:43 +01:00
|
|
|
|
|
|
|
respond_to do |format|
|
2017-11-29 16:13:28 +01:00
|
|
|
format.json do
|
2021-10-14 11:49:27 +02:00
|
|
|
render json: {}, status: :ok
|
2017-11-29 16:13:28 +01:00
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
2018-11-20 14:29:33 +01:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
errors: @um.errors
|
2021-10-14 11:49:27 +02:00
|
|
|
}, status: :unprocessable_entity
|
2018-11-20 14:29:33 +01:00
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-14 11:49:27 +02:00
|
|
|
def search
|
|
|
|
users = @my_module.users
|
2022-11-23 17:20:14 +01:00
|
|
|
.joins("LEFT OUTER JOIN user_my_modules ON user_my_modules.user_id = users.id "\
|
|
|
|
"AND user_my_modules.my_module_id = #{@my_module.id}")
|
2021-10-14 11:49:27 +02:00
|
|
|
.search(false, params[:query])
|
|
|
|
.limit(Constants::SEARCH_LIMIT)
|
2022-11-23 17:20:14 +01:00
|
|
|
.select('users.*')
|
|
|
|
.select('CASE WHEN user_my_modules.id IS NOT NULL '\
|
|
|
|
'THEN true ELSE false END as designated')
|
2021-10-14 11:49:27 +02:00
|
|
|
|
|
|
|
users = users.map do |user|
|
|
|
|
{
|
|
|
|
value: user.id,
|
|
|
|
label: sanitize_input(user.full_name),
|
2022-11-23 17:20:14 +01:00
|
|
|
params: { avatar_url: avatar_path(user, :icon_small), designated: user.designated }
|
2021-10-14 11:49:27 +02:00
|
|
|
}
|
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
|
2021-10-14 11:49:27 +02:00
|
|
|
render json: users
|
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
|
2021-10-14 11:49:27 +02:00
|
|
|
private
|
2016-02-12 16:52:43 +01:00
|
|
|
|
2021-10-14 11:49:27 +02:00
|
|
|
def load_vars
|
|
|
|
@my_module = MyModule.find(params[:my_module_id])
|
|
|
|
@project = @my_module.experiment.project
|
|
|
|
@um = UserMyModule.find(params[:id]) if action_name == 'destroy'
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
|
2016-11-22 15:16:26 +01:00
|
|
|
def check_view_permissions
|
2021-09-22 11:28:32 +02:00
|
|
|
render_403 unless can_read_my_module?(@my_module)
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
|
2018-02-09 16:14:40 +01:00
|
|
|
def check_manage_permissions
|
2021-11-19 13:24:57 +01:00
|
|
|
render_403 unless can_manage_my_module_designated_users?(@my_module)
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def um_params
|
|
|
|
params.require(:user_my_module).permit(:user_id, :my_module_id)
|
|
|
|
end
|
|
|
|
end
|