2021-10-14 17:49:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
class UserMyModulesController < ApplicationController
|
2021-10-14 17:49:27 +08:00
|
|
|
include InputSanitizeHelper
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
before_action :load_vars
|
2021-09-22 17:28:32 +08:00
|
|
|
before_action :check_view_permissions, except: %i(create destroy)
|
2020-06-30 15:20:53 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(create destroy)
|
2016-11-22 22:16:26 +08:00
|
|
|
|
2023-03-21 18:29:23 +08:00
|
|
|
def designated_users
|
2016-11-22 22:16:26 +08:00
|
|
|
@user_my_modules = @my_module.user_my_modules
|
|
|
|
|
|
|
|
respond_to do |format|
|
2016-11-22 22:19:12 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2023-03-21 18:29:23 +08:00
|
|
|
partial: 'designated_users.html.erb'
|
2017-05-08 23:32:55 +08:00
|
|
|
),
|
|
|
|
my_module_id: @my_module.id,
|
2021-10-13 16:10:14 +08:00
|
|
|
counter: @my_module.designated_users.count # Used for counter badge
|
2016-11-22 22:16:26 +08:00
|
|
|
}
|
2016-11-22 22:19:12 +08:00
|
|
|
end
|
2016-11-22 22:16:26 +08:00
|
|
|
end
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2020-04-14 23:25:36 +08:00
|
|
|
def index
|
2020-04-08 22:24:59 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2020-04-14 23:25:36 +08:00
|
|
|
partial: 'index.html.erb'
|
2020-04-08 22:24:59 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def index_edit
|
|
|
|
@user_my_modules = @my_module.user_my_modules
|
2021-10-13 15:50:06 +08:00
|
|
|
@undesignated_users = @my_module.undesignated_users.order(full_name: :asc)
|
2016-02-12 23:52:43 +08:00
|
|
|
@new_um = UserMyModule.new(my_module: @my_module)
|
|
|
|
|
|
|
|
respond_to do |format|
|
2017-05-08 23:32:55 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
my_module: @my_module,
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'index_edit.html.erb'
|
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2017-05-08 23:32:55 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@um = UserMyModule.new(um_params.merge(my_module: @my_module))
|
|
|
|
@um.assigned_by = current_user
|
2021-10-14 17:49:27 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
if @um.save
|
2022-06-03 16:09:42 +08:00
|
|
|
@um.log_activity(:designate_user_to_my_module, current_user)
|
2019-03-18 22:03:14 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
respond_to do |format|
|
2017-12-20 20:39:35 +08:00
|
|
|
format.json do
|
2022-11-30 21:56:19 +08:00
|
|
|
if params[:table]
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'experiments/assigned_users.html.erb',
|
2023-01-05 19:00:12 +08:00
|
|
|
locals: { my_module: @my_module, user: current_user, skip_unassigned: false }),
|
2022-11-30 21:56:19 +08:00
|
|
|
unassign_url: my_module_user_my_module_path(@my_module, @um)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
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
|
|
|
|
}
|
|
|
|
end
|
2017-12-20 20:39:35 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
2018-11-20 21:29:33 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
errors: @um.errors
|
2021-10-14 17:49:27 +08:00
|
|
|
}, status: :unprocessable_entity
|
2018-11-20 21:29:33 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
if @um.destroy
|
2022-06-03 16:09:42 +08:00
|
|
|
@um.log_activity(:undesignate_user_from_my_module, current_user)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
2017-11-29 23:13:28 +08:00
|
|
|
format.json do
|
2022-11-30 21:56:19 +08:00
|
|
|
if params[:table]
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'experiments/assigned_users.html.erb',
|
2023-01-05 19:00:12 +08:00
|
|
|
locals: { my_module: @my_module, user: current_user, skip_unassigned: false })
|
2022-11-30 21:56:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
2017-11-29 23:13:28 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
2018-11-20 21:29:33 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
errors: @um.errors
|
2021-10-14 17:49:27 +08:00
|
|
|
}, status: :unprocessable_entity
|
2018-11-20 21:29:33 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-14 17:49:27 +08:00
|
|
|
def search
|
|
|
|
users = @my_module.users
|
2022-11-24 00:20:14 +08: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 17:49:27 +08:00
|
|
|
.search(false, params[:query])
|
2022-11-30 21:56:19 +08:00
|
|
|
.order(:full_name)
|
2021-10-14 17:49:27 +08:00
|
|
|
.limit(Constants::SEARCH_LIMIT)
|
2022-11-30 21:56:19 +08:00
|
|
|
.select('users.*', 'user_my_modules.id as user_my_module_id')
|
2022-11-24 00:20:14 +08:00
|
|
|
.select('CASE WHEN user_my_modules.id IS NOT NULL '\
|
|
|
|
'THEN true ELSE false END as designated')
|
2021-10-14 17:49:27 +08:00
|
|
|
|
|
|
|
users = users.map do |user|
|
2022-11-30 21:56:19 +08:00
|
|
|
next if params[:skip_assigned] && user.designated
|
2023-01-05 19:00:12 +08:00
|
|
|
next if ActiveModel::Type::Boolean.new.cast(params[:skip_unassigned]) && !user.designated
|
2022-11-30 21:56:19 +08:00
|
|
|
|
|
|
|
user_hash = {
|
2021-10-14 17:49:27 +08:00
|
|
|
value: user.id,
|
2023-03-14 01:00:30 +08:00
|
|
|
label: escape_input(user.full_name),
|
2022-11-30 21:56:19 +08:00
|
|
|
params: {
|
|
|
|
avatar_url: avatar_path(user, :icon_small),
|
|
|
|
designated: user.designated,
|
|
|
|
assign_url: my_module_user_my_modules_path(@my_module)
|
|
|
|
}
|
2021-10-14 17:49:27 +08:00
|
|
|
}
|
2022-11-30 21:56:19 +08:00
|
|
|
|
|
|
|
if user.designated
|
|
|
|
user_hash[:params][:unassign_url] = my_module_user_my_module_path(@my_module, user.user_my_module_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
user_hash
|
2021-10-14 17:49:27 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2022-11-30 21:56:19 +08:00
|
|
|
render json: users.compact
|
2021-10-14 17:49:27 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2021-10-14 17:49:27 +08:00
|
|
|
private
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2021-10-14 17:49:27 +08: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 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2016-11-22 22:16:26 +08:00
|
|
|
def check_view_permissions
|
2021-09-22 17:28:32 +08:00
|
|
|
render_403 unless can_read_my_module?(@my_module)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2018-02-09 23:14:40 +08:00
|
|
|
def check_manage_permissions
|
2021-11-19 20:24:57 +08:00
|
|
|
render_403 unless can_manage_my_module_designated_users?(@my_module)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def um_params
|
|
|
|
params.require(:user_my_module).permit(:user_id, :my_module_id)
|
|
|
|
end
|
|
|
|
end
|