2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
class Admin::ImpersonationTokensController < Admin::ApplicationController
|
|
|
|
before_action :user
|
|
|
|
|
|
|
|
def index
|
|
|
|
set_index_vars
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@impersonation_token = finder.build(impersonation_token_params)
|
|
|
|
|
|
|
|
if @impersonation_token.save
|
2018-12-05 23:21:45 +05:30
|
|
|
PersonalAccessToken.redis_store!(current_user.id, @impersonation_token.token)
|
2019-05-30 16:15:17 +05:30
|
|
|
redirect_to admin_user_impersonation_tokens_path, notice: "A new impersonation token has been created."
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
|
|
|
set_index_vars
|
|
|
|
render :index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke
|
|
|
|
@impersonation_token = finder.find(params[:id])
|
|
|
|
|
|
|
|
if @impersonation_token.revoke!
|
2019-05-30 16:15:17 +05:30
|
|
|
flash[:notice] = "Revoked impersonation token #{@impersonation_token.name}!"
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
2019-05-30 16:15:17 +05:30
|
|
|
flash[:alert] = "Could not revoke impersonation token #{@impersonation_token.name}."
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to admin_user_impersonation_tokens_path
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
def user
|
|
|
|
@user ||= User.find_by!(username: params[:user_id])
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def finder(options = {})
|
|
|
|
PersonalAccessTokensFinder.new({ user: user, impersonation: true }.merge(options))
|
|
|
|
end
|
|
|
|
|
|
|
|
def impersonation_token_params
|
|
|
|
params.require(:personal_access_token).permit(:name, :expires_at, :impersonation, scopes: [])
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
def set_index_vars
|
2018-03-17 18:26:18 +05:30
|
|
|
@scopes = Gitlab::Auth.available_scopes(current_user)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
@impersonation_token ||= finder.build
|
|
|
|
@inactive_impersonation_tokens = finder(state: 'inactive').execute
|
|
|
|
@active_impersonation_tokens = finder(state: 'active').execute.order(:expires_at)
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
@new_impersonation_token = PersonalAccessToken.redis_getdel(current_user.id)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|