2016-02-12 23:52:43 +08:00
|
|
|
class UserProjectsController < ApplicationController
|
2016-11-07 22:31:06 +08:00
|
|
|
include NotificationsHelper
|
2017-05-12 22:18:53 +08:00
|
|
|
include InputSanitizeHelper
|
2016-11-07 22:31:06 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
before_action :load_vars
|
2020-09-22 17:56:12 +08:00
|
|
|
before_action :load_user_project, only: %i(update destroy)
|
2018-02-03 01:19:03 +08:00
|
|
|
before_action :check_view_permissions, only: :index
|
|
|
|
before_action :check_manage_users_permissions, only: :index_edit
|
2016-11-22 22:16:26 +08:00
|
|
|
before_action :check_create_permissions, only: :create
|
2018-11-09 18:36:44 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(update destroy)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
@users = @project.user_projects
|
|
|
|
|
|
|
|
respond_to do |format|
|
2017-05-08 23:32:55 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'index.html.erb'
|
|
|
|
),
|
|
|
|
project_id: @project.id,
|
2017-05-09 16:18:57 +08:00
|
|
|
counter: @project.users.count # Used for counter badge
|
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 index_edit
|
2020-09-22 17:56:12 +08:00
|
|
|
@user_projects = @project.user_projects
|
2016-02-12 23:52:43 +08:00
|
|
|
@unassigned_users = @project.unassigned_users
|
2020-09-22 17:56:12 +08:00
|
|
|
@new_user_project = UserProject.new(project: @project)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
2017-05-08 23:32:55 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
project: @project,
|
2018-06-01 22:13:22 +08:00
|
|
|
html_header: t('projects.index.modal_manage_users.modal_title_html',
|
|
|
|
name: @project.name),
|
2017-05-08 23:32:55 +08:00
|
|
|
html_body: render_to_string(
|
|
|
|
partial: 'index_edit.html.erb'
|
|
|
|
),
|
|
|
|
html_footer: render_to_string(
|
|
|
|
partial: 'index_edit_footer.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
|
2020-09-22 17:56:12 +08:00
|
|
|
@user_project = @project.user_projects.new(user_project_params)
|
|
|
|
@user_project.assigned_by = current_user
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2020-09-22 17:56:12 +08:00
|
|
|
if @user_project.save
|
2019-03-08 00:26:42 +08:00
|
|
|
log_activity(:assign_user_to_project)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
2017-11-30 05:55:10 +08:00
|
|
|
format.json do
|
|
|
|
redirect_to project_users_edit_path(format: :json), turbolinks: false
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
error = t('user_projects.create.can_add_user_to_project')
|
2020-09-22 17:56:12 +08:00
|
|
|
error = t('user_projects.create.select_user_role') unless @user_project.role
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
2020-09-22 17:56:12 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
2016-02-12 23:52:43 +08:00
|
|
|
status: 'error',
|
2016-11-23 20:19:10 +08:00
|
|
|
error: error
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2020-09-22 17:56:12 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2020-09-22 17:56:12 +08:00
|
|
|
@user_project.role = user_project_params[:role]
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2020-09-22 17:56:12 +08:00
|
|
|
if @user_project.save
|
2019-03-08 00:26:42 +08:00
|
|
|
log_activity(:change_user_role_on_project)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
2017-11-30 05:55:10 +08:00
|
|
|
format.json do
|
|
|
|
redirect_to project_users_edit_path(format: :json), turbolinks: false
|
|
|
|
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: {
|
2016-02-12 23:52:43 +08:00
|
|
|
status: 'error',
|
2020-09-22 17:56:12 +08:00
|
|
|
errors: @user_project.errors
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2018-11-20 21:29:33 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2020-09-22 17:56:12 +08:00
|
|
|
if @user_project.destroy
|
2019-03-08 00:26:42 +08:00
|
|
|
log_activity(:unassign_user_from_project)
|
2016-02-12 23:52:43 +08:00
|
|
|
respond_to do |format|
|
2017-11-30 05:55:10 +08:00
|
|
|
format.json do
|
|
|
|
redirect_to project_users_edit_path(format: :json),
|
|
|
|
turbolinks: false,
|
2020-09-22 17:56:12 +08:00
|
|
|
status: :see_other
|
2017-11-30 05:55:10 +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: {
|
2020-09-22 17:56:12 +08:00
|
|
|
errors: @user_project.errors
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2018-11-20 21:29:33 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars
|
2020-09-22 17:56:12 +08:00
|
|
|
@project = Project.find_by(id: params[:project_id])
|
2018-11-09 18:36:44 +08:00
|
|
|
render_404 unless @project
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2020-09-22 17:56:12 +08:00
|
|
|
def load_user_project
|
|
|
|
@user_project = @project.user_projects.find(params[:id])
|
|
|
|
render_404 unless @user_project
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_view_permissions
|
2018-01-25 19:55:57 +08:00
|
|
|
render_403 unless can_read_project?(@project)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2018-02-03 01:19:03 +08:00
|
|
|
def check_manage_users_permissions
|
2018-02-03 01:48:55 +08:00
|
|
|
render_403 unless can_manage_project?(@project)
|
2018-02-03 01:19:03 +08:00
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def check_create_permissions
|
2020-09-22 17:56:12 +08:00
|
|
|
render_403 unless can_manage_project?(@project)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2018-11-09 18:36:44 +08:00
|
|
|
def check_manage_permissions
|
2020-09-22 17:56:12 +08:00
|
|
|
render_403 unless can_manage_project?(@project) && @user_project.user_id != current_user.id
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2020-09-22 17:56:12 +08:00
|
|
|
def user_project_params
|
2016-02-12 23:52:43 +08:00
|
|
|
params.require(:user_project).permit(:user_id, :project_id, :role)
|
|
|
|
end
|
2019-03-08 00:26:42 +08:00
|
|
|
|
|
|
|
def log_activity(type_of)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @project,
|
|
|
|
team: @project.team,
|
|
|
|
project: @project,
|
|
|
|
message_items: { project: @project.id,
|
2020-09-22 17:56:12 +08:00
|
|
|
user_target: @user_project.user.id,
|
|
|
|
role: @user_project.role_str })
|
2019-03-08 00:26:42 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|