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
|
|
|
|
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
2023-07-05 18:43:23 +08:00
|
|
|
html: render_to_string(partial: 'designated_users', formats: :html),
|
2023-06-21 20:13:20 +08:00
|
|
|
my_module_id: @my_module.id,
|
|
|
|
counter: @my_module.designated_users.count # Used for counter badge
|
|
|
|
}
|
2016-11-22 22:16:26 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2020-04-14 23:25:36 +08:00
|
|
|
def index
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
2023-07-05 18:43:23 +08:00
|
|
|
html: render_to_string(partial: 'index', formats: :html)
|
2023-06-21 20:13:20 +08:00
|
|
|
}
|
2020-04-08 22:24:59 +08:00
|
|
|
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)
|
|
|
|
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
|
|
|
my_module: @my_module,
|
|
|
|
html: render_to_string(
|
2023-07-05 18:43:23 +08:00
|
|
|
partial: 'index_edit', formats: :html
|
2023-06-21 20:13:20 +08:00
|
|
|
)
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
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
|
|
|
|
2023-06-21 20:13:20 +08:00
|
|
|
if params[:table]
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'experiments/assigned_users',
|
2023-07-05 18:43:23 +08:00
|
|
|
locals: { my_module: @my_module, user: current_user, skip_unassigned: false },
|
|
|
|
formats: :html),
|
2023-06-21 20:13:20 +08:00
|
|
|
unassign_url: my_module_user_my_module_path(@my_module, @um)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
render json: {
|
|
|
|
user: {
|
|
|
|
id: @um.user.id,
|
2023-07-21 17:48:19 +08:00
|
|
|
full_name: escape_input(@um.user.full_name),
|
2023-06-21 20:13:20 +08:00
|
|
|
avatar_url: avatar_path(@um.user, :icon_small),
|
|
|
|
user_module_id: @um.id
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
|
|
|
errors: @um.errors
|
|
|
|
}, status: :unprocessable_entity
|
2016-02-12 23:52:43 +08:00
|
|
|
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
|
|
|
|
2023-06-21 20:13:20 +08:00
|
|
|
if params[:table]
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'experiments/assigned_users',
|
2023-07-05 18:43:23 +08:00
|
|
|
locals: { my_module: @my_module, user: current_user, skip_unassigned: false },
|
|
|
|
formats: :html)
|
2023-06-21 20:13:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
render json: {}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
|
|
|
errors: @um.errors
|
|
|
|
}, status: :unprocessable_entity
|
2016-02-12 23:52:43 +08:00
|
|
|
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
|
|
|
.select('users.*', 'user_my_modules.id as user_my_module_id')
|
2024-01-25 22:49:13 +08:00
|
|
|
.select('CASE WHEN user_my_modules.id IS NOT NULL THEN true ELSE false END as designated')
|
|
|
|
.order('designated DESC', :full_name)
|
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
|
|
|
|
2024-03-20 22:59:38 +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
|