scinote-web/app/controllers/users/settings_controller.rb

340 lines
7.8 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
class Users::SettingsController < ApplicationController
include UsersGenerator
2016-11-07 22:31:06 +08:00
include NotificationsHelper
include InputSanitizeHelper
2016-02-12 23:52:43 +08:00
before_action :load_user, only: [
:teams,
:team,
:create_team,
:teams_datatable,
:team_users_datatable,
:user_current_team,
:destroy_user_team
2016-02-12 23:52:43 +08:00
]
before_action :check_team_permission, only: [
:team,
:update_team,
:destroy_team,
:team_name,
:team_description,
:team_users_datatable
2016-02-12 23:52:43 +08:00
]
before_action :check_user_team_permission, only: [
:update_user_team,
:leave_user_team_html,
:destroy_user_team_html,
:destroy_user_team
2016-02-12 23:52:43 +08:00
]
def teams
@user_teams =
2016-02-12 23:52:43 +08:00
@user
.user_teams
.includes(team: :users)
2016-02-12 23:52:43 +08:00
.order(created_at: :asc)
@member_of = @user_teams.count
2016-02-12 23:52:43 +08:00
end
def team
@user_team = UserTeam.find_by(user: @user, team: @team)
2016-02-12 23:52:43 +08:00
end
def update_team
2016-02-12 23:52:43 +08:00
respond_to do |format|
if @team.update(update_team_params)
@team.update(last_modified_by: current_user)
2016-02-12 23:52:43 +08:00
format.json {
render json: {
status: :ok,
description_label: render_to_string(
partial: "users/settings/teams/description_label.html.erb",
locals: { team: @team }
2016-02-12 23:52:43 +08:00
)
}
}
else
format.json {
render json: @team.errors,
2016-02-12 23:52:43 +08:00
status: :unprocessable_entity
}
end
end
end
def team_name
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.json {
render json: {
html: render_to_string({
partial: "users/settings/teams/name_modal_body.html.erb",
locals: { team: @team }
2016-02-12 23:52:43 +08:00
})
}
}
end
end
def team_description
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.json {
render json: {
html: render_to_string({
partial: "users/settings/teams/description_modal_body.html.erb",
locals: { team: @team }
2016-02-12 23:52:43 +08:00
})
}
}
end
end
def teams_datatable
respond_to do |format|
format.json do
render json: ::TeamsDatatable.new(view_context, @user)
end
end
end
def team_users_datatable
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.json {
render json: ::TeamUsersDatatable.new(view_context, @team, @user)
2016-02-12 23:52:43 +08:00
}
end
end
def new_team
@new_team = Team.new
2016-02-12 23:52:43 +08:00
end
def create_team
@new_team = Team.new(create_team_params)
@new_team.created_by = @user
2016-02-12 23:52:43 +08:00
if @new_team.save
# Okay, team is created, now
2016-02-12 23:52:43 +08:00
# add the current user as admin
UserTeam.create(
2016-02-12 23:52:43 +08:00
user: @user,
team: @new_team,
2016-02-12 23:52:43 +08:00
role: 2
)
# Redirect to new team page
redirect_to action: :team, team_id: @new_team.id
2016-02-12 23:52:43 +08:00
else
render :new_team
2016-02-12 23:52:43 +08:00
end
end
def destroy_team
@team.destroy
2016-02-12 23:52:43 +08:00
flash[:notice] = I18n.t(
"users.settings.teams.edit.modal_destroy_team.flash_success",
team: @team.name
2016-02-12 23:52:43 +08:00
)
# Redirect back to all teams page
redirect_to action: :teams
2016-02-12 23:52:43 +08:00
end
def update_user_team
2016-02-12 23:52:43 +08:00
respond_to do |format|
if @user_team.update(update_user_team_params)
2016-02-12 23:52:43 +08:00
format.json {
render json: {
status: :ok
}
}
else
format.json {
render json: @user_team.errors,
2016-02-12 23:52:43 +08:00
status: :unprocessable_entity
}
end
end
end
def leave_user_team_html
2016-02-12 23:52:43 +08:00
respond_to do |format|
2017-01-31 20:33:55 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: {
2017-01-31 20:33:55 +08:00
html: render_to_string(
partial: 'users/settings/teams/leave_user_team_modal_body.html.erb',
locals: { user_team: @user_team }
2017-01-31 20:33:55 +08:00
),
2016-02-12 23:52:43 +08:00
heading: I18n.t(
2017-01-31 20:33:55 +08:00
'users.settings.teams.index.leave_uo_heading',
team: escape_input(@user_team.team.name)
2016-02-12 23:52:43 +08:00
)
}
2017-01-31 20:33:55 +08:00
end
2016-02-12 23:52:43 +08:00
end
end
def destroy_user_team_html
2016-02-12 23:52:43 +08:00
respond_to do |format|
2017-01-31 20:33:55 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: {
2017-01-31 20:33:55 +08:00
html: render_to_string(
partial: 'users/settings/teams/' \
'destroy_user_team_modal_body.html.erb',
locals: { user_team: @user_team }
2017-01-31 20:33:55 +08:00
),
2016-02-12 23:52:43 +08:00
heading: I18n.t(
2017-01-31 20:33:55 +08:00
'users.settings.teams.edit.destroy_uo_heading',
user: escape_input(@user_team.user.full_name),
team: escape_input(@user_team.team.name)
2016-02-12 23:52:43 +08:00
)
}
2017-01-31 20:33:55 +08:00
end
2016-02-12 23:52:43 +08:00
end
end
def destroy_user_team
2016-02-12 23:52:43 +08:00
respond_to do |format|
# If user is last administrator of team,
2016-02-12 23:52:43 +08:00
# he/she cannot be deleted from it.
invalid =
@user_team.admin? &&
@user_team
.team
.user_teams
2016-02-12 23:52:43 +08:00
.where(role: 2)
.count <= 1
2016-07-21 19:11:15 +08:00
if !invalid then
begin
UserTeam.transaction do
2016-07-21 19:11:15 +08:00
# If user leaves on his/her own accord,
# new owner for projects is the first
# administrator of team
2016-07-21 19:11:15 +08:00
if params[:leave]
new_owner =
@user_team
.team
.user_teams
2016-07-21 19:11:15 +08:00
.where(role: 2)
.where.not(id: @user_team.id)
2016-07-21 19:11:15 +08:00
.first
.user
else
# Otherwise, the new owner for projects is
# the current user (= an administrator removing
# the user from the team)
2016-07-21 19:11:15 +08:00
new_owner = current_user
end
reset_user_current_team(@user_team)
@user_team.destroy(new_owner)
2016-07-21 19:11:15 +08:00
end
rescue Exception
invalid = true
end
end
if !invalid
2016-02-12 23:52:43 +08:00
if params[:leave] then
flash[:notice] = I18n.t(
2017-01-31 20:33:55 +08:00
'users.settings.teams.index.leave_flash',
team: @user_team.team.name
2016-02-12 23:52:43 +08:00
)
flash.keep(:notice)
end
generate_notification(@user_team.user,
@user_team.user,
@user_team.team,
2016-11-07 22:31:06 +08:00
false,
2016-11-02 20:32:15 +08:00
false)
2016-02-12 23:52:43 +08:00
format.json {
render json: {
status: :ok
}
}
else
format.json {
render json: @user_team.errors,
2016-02-12 23:52:43 +08:00
status: :unprocessable_entity
}
end
end
end
def user_current_team
team_id = params[:user][:current_team_id].to_i
if @user.teams_ids.include?(team_id)
@user.current_team_id = team_id
@changed_team = Team.find_by_id(@user.current_team_id)
if @user.save
flash[:success] = t('users.settings.changed_team_flash',
team: @changed_team.name)
redirect_to root_path
return
end
end
flash[:alert] = t('users.settings.changed_team_error_flash')
redirect_to :back
end
2016-02-12 23:52:43 +08:00
private
def load_user
@user = current_user
end
def check_team_permission
@team = Team.find_by_id(params[:team_id])
unless is_admin_of_team(@team)
2016-02-12 23:52:43 +08:00
render_403
end
end
def check_user_team_permission
@user_team = UserTeam.find_by_id(params[:user_team_id])
@team = @user_team.team
# Don't allow the user to modify UserTeam-s if he's not admin,
# unless he/she is modifying his/her UserTeam
2017-01-31 20:33:55 +08:00
if current_user != @user_team.user &&
!is_admin_of_team(@user_team.team)
2016-02-12 23:52:43 +08:00
render_403
end
end
def create_team_params
params.require(:team).permit(
2016-02-12 23:52:43 +08:00
:name,
:description
)
end
def update_team_params
params.require(:team).permit(
2016-02-12 23:52:43 +08:00
:name,
:description
)
end
def create_user_params
params.require(:user).permit(
:full_name,
:email
)
end
def update_user_team_params
params.require(:user_team).permit(
2016-02-12 23:52:43 +08:00
:role
)
end
def reset_user_current_team(user_team)
ids = user_team.user.teams_ids
ids -= [user_team.team.id]
user_team.user.current_team_id = ids.first
user_team.user.save
2016-10-25 02:07:20 +08:00
end
2016-02-12 23:52:43 +08:00
end