2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-23 12:47:06 -05:00
|
|
|
class Admin::ImpersonationTokensController < Admin::ApplicationController
|
2017-03-01 11:59:03 -05:00
|
|
|
before_action :user
|
2017-02-23 12:47:06 -05:00
|
|
|
|
2020-10-05 17:08:47 -04:00
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2017-02-23 12:47:06 -05:00
|
|
|
def index
|
|
|
|
set_index_vars
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-03-01 11:59:03 -05:00
|
|
|
@impersonation_token = finder.build(impersonation_token_params)
|
2017-02-23 12:47:06 -05:00
|
|
|
|
|
|
|
if @impersonation_token.save
|
2018-11-08 10:03:56 -05:00
|
|
|
PersonalAccessToken.redis_store!(current_user.id, @impersonation_token.token)
|
2019-03-21 09:31:05 -04:00
|
|
|
redirect_to admin_user_impersonation_tokens_path, notice: _("A new impersonation token has been created.")
|
2017-02-23 12:47:06 -05:00
|
|
|
else
|
|
|
|
set_index_vars
|
|
|
|
render :index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke
|
2017-03-01 11:59:03 -05:00
|
|
|
@impersonation_token = finder.find(params[:id])
|
2017-02-23 12:47:06 -05:00
|
|
|
|
|
|
|
if @impersonation_token.revoke!
|
2019-03-21 09:31:05 -04:00
|
|
|
flash[:notice] = _("Revoked impersonation token %{token_name}!") % { token_name: @impersonation_token.name }
|
2017-02-23 12:47:06 -05:00
|
|
|
else
|
2019-03-21 09:31:05 -04:00
|
|
|
flash[:alert] = _("Could not revoke impersonation token %{token_name}.") % { token_name: @impersonation_token.name }
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to admin_user_impersonation_tokens_path
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-23 12:47:06 -05:00
|
|
|
def user
|
|
|
|
@user ||= User.find_by!(username: params[:user_id])
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-23 12:47:06 -05:00
|
|
|
|
2017-03-01 11:59:03 -05:00
|
|
|
def finder(options = {})
|
|
|
|
PersonalAccessTokensFinder.new({ user: user, impersonation: true }.merge(options))
|
2017-02-27 13:56:54 -05:00
|
|
|
end
|
|
|
|
|
2017-02-23 12:47:06 -05:00
|
|
|
def impersonation_token_params
|
|
|
|
params.require(:personal_access_token).permit(:name, :expires_at, :impersonation, scopes: [])
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-23 12:47:06 -05:00
|
|
|
def set_index_vars
|
2019-04-15 09:05:55 -04:00
|
|
|
@scopes = Gitlab::Auth.available_scopes_for(current_user)
|
2017-03-01 11:59:03 -05:00
|
|
|
|
|
|
|
@impersonation_token ||= finder.build
|
|
|
|
@inactive_impersonation_tokens = finder(state: 'inactive').execute
|
|
|
|
@active_impersonation_tokens = finder(state: 'active').execute.order(:expires_at)
|
2018-11-08 10:03:56 -05:00
|
|
|
|
|
|
|
@new_impersonation_token = PersonalAccessToken.redis_getdel(current_user.id)
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-23 12:47:06 -05:00
|
|
|
end
|