scinote-web/app/controllers/user_projects_controller.rb

155 lines
4 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
class UserProjectsController < ApplicationController
2016-11-07 22:31:06 +08:00
include NotificationsHelper
include InputSanitizeHelper
2016-11-07 22:31:06 +08:00
2016-02-12 23:52:43 +08:00
before_action :load_vars
before_action :load_user_project, only: %i(update destroy)
before_action :check_view_permissions, only: :index
before_action :check_manage_users_permissions, only: :index_edit
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
@user_projects = @project.user_projects
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.json do
render json: {
html: render_to_string(partial: 'index.html.erb')
2016-02-12 23:52:43 +08:00
}
end
2016-02-12 23:52:43 +08:00
end
end
def index_edit
@user_projects = @project.user_projects
2016-02-12 23:52:43 +08:00
@unassigned_users = @project.unassigned_users
@new_user_project = UserProject.new(project: @project)
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.json do
render json: {
html_title: t('projects.index.modal_manage_users.modal_title', name: @project.name),
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
}
end
2016-02-12 23:52:43 +08:00
end
end
def create
@user_project = @project.user_projects.new(user_project_params)
@user_project.assigned_by = current_user
2016-02-12 23:52:43 +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|
format.json do
redirect_to edit_project_users_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')
error = t('user_projects.create.select_user_role') unless @user_project.role
2016-02-12 23:52:43 +08:00
respond_to do |format|
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
}
end
2016-02-12 23:52:43 +08:00
end
end
end
def update
@user_project.role = user_project_params[:role]
2016-02-12 23:52:43 +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|
format.json do
redirect_to edit_project_users_path(format: :json), turbolinks: false
end
2016-02-12 23:52:43 +08:00
end
else
respond_to do |format|
format.json do
render json: {
2016-02-12 23:52:43 +08:00
status: 'error',
errors: @user_project.errors
2016-02-12 23:52:43 +08:00
}
end
2016-02-12 23:52:43 +08:00
end
end
end
def destroy
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|
format.json do
redirect_to edit_project_users_path(format: :json),
turbolinks: false,
status: :see_other
end
2016-02-12 23:52:43 +08:00
end
else
respond_to do |format|
format.json do
render json: {
errors: @user_project.errors
2016-02-12 23:52:43 +08:00
}
end
2016-02-12 23:52:43 +08:00
end
end
end
private
def load_vars
@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
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
render_403 unless can_read_project?(@project)
2016-02-12 23:52:43 +08:00
end
def check_manage_users_permissions
render_403 unless can_manage_project?(@project)
end
2016-02-12 23:52:43 +08:00
def check_create_permissions
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
render_403 unless can_manage_project?(@project) && @user_project.user_id != current_user.id
2016-02-12 23:52:43 +08:00
end
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,
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