2017-08-25 14:54:32 +08:00
|
|
|
module ClientApi
|
|
|
|
module Users
|
|
|
|
class UserTeamsController < ApplicationController
|
2017-08-28 23:05:09 +08:00
|
|
|
include ClientApi::Users::UserTeamsHelper
|
|
|
|
|
2017-12-13 21:36:46 +08:00
|
|
|
before_action :check_leave_team_permission, only: :leave_team
|
|
|
|
before_action :check_manage_user_team_permission,
|
|
|
|
only: %i(update_role remove_user)
|
2017-11-28 22:41:52 +08:00
|
|
|
|
2017-08-25 14:54:32 +08:00
|
|
|
def leave_team
|
2017-09-12 23:30:13 +08:00
|
|
|
ut_service = ClientApi::UserTeamService.new(
|
|
|
|
user: current_user,
|
|
|
|
team_id: params[:team],
|
|
|
|
user_team_id: params[:user_team]
|
|
|
|
)
|
2017-09-01 23:10:43 +08:00
|
|
|
ut_service.destroy_user_team_and_assign_new_team_owner!
|
2017-09-12 23:30:13 +08:00
|
|
|
success_response('/client_api/teams/index', ut_service.teams_data)
|
2017-09-01 23:10:43 +08:00
|
|
|
rescue ClientApi::CustomUserTeamError
|
2017-09-12 23:30:13 +08:00
|
|
|
unsuccess_response(t('client_api.user_teams.leave_team_error'))
|
2017-08-28 23:05:09 +08:00
|
|
|
end
|
|
|
|
|
2017-08-31 21:56:55 +08:00
|
|
|
def update_role
|
2017-09-12 23:30:13 +08:00
|
|
|
ut_service = ClientApi::UserTeamService.new(
|
|
|
|
user: current_user,
|
|
|
|
team_id: params[:team],
|
|
|
|
user_team_id: params[:user_team],
|
|
|
|
role: params[:role]
|
|
|
|
)
|
|
|
|
ut_service.update_role!
|
|
|
|
success_response('/client_api/teams/team_users',
|
|
|
|
ut_service.team_users_data)
|
|
|
|
rescue ClientApi::CustomUserTeamError => error
|
|
|
|
unsuccess_response(error.to_s)
|
2017-08-28 23:05:09 +08:00
|
|
|
end
|
|
|
|
|
2017-09-13 23:11:51 +08:00
|
|
|
def remove_user
|
|
|
|
ut_service = ClientApi::UserTeamService.new(
|
|
|
|
user: current_user,
|
|
|
|
team_id: params[:team],
|
|
|
|
user_team_id: params[:user_team]
|
|
|
|
)
|
|
|
|
ut_service.destroy_user_team_and_assign_new_team_owner!
|
|
|
|
success_response('/client_api/teams/team_users',
|
|
|
|
ut_service.team_users_data)
|
|
|
|
rescue ClientApi::CustomUserTeamError => error
|
|
|
|
unsuccess_response(error.to_s)
|
|
|
|
end
|
|
|
|
|
2017-08-31 21:56:55 +08:00
|
|
|
private
|
2017-08-28 23:05:09 +08:00
|
|
|
|
2017-12-13 21:36:46 +08:00
|
|
|
def check_leave_team_permission
|
2017-12-14 17:52:13 +08:00
|
|
|
return unless params[:user_team]
|
2017-12-13 21:36:46 +08:00
|
|
|
user_team = UserTeam.find_by_id(params[:user_team])
|
|
|
|
unless current_user == user_team.user || can_read_team?(user_team.team)
|
|
|
|
respond_422(t('client_api.permission_error'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-28 22:41:52 +08:00
|
|
|
def check_manage_user_team_permission
|
2017-12-13 21:36:46 +08:00
|
|
|
user_team = UserTeam.find_by_id(params[:user_team])
|
2018-01-05 22:15:50 +08:00
|
|
|
unless can_manage_team_users?(user_team.team)
|
2017-12-04 18:12:35 +08:00
|
|
|
respond_422(t('client_api.user_teams.permission_error'))
|
2017-11-28 22:41:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-12 23:30:13 +08:00
|
|
|
def success_response(template, locals)
|
2017-08-25 14:54:32 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-09-12 23:30:13 +08:00
|
|
|
render template: template,
|
2017-08-25 14:54:32 +08:00
|
|
|
status: :ok,
|
2017-08-31 21:56:55 +08:00
|
|
|
locals: locals
|
2017-08-28 23:05:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-12 23:30:13 +08:00
|
|
|
def unsuccess_response(message)
|
2017-08-28 23:05:09 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-09-12 23:30:13 +08:00
|
|
|
render json: { message: message },
|
2017-08-28 23:05:09 +08:00
|
|
|
status: :unprocessable_entity
|
2017-08-25 14:54:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|