debian-mirror-gitlab/app/controllers/admin/impersonation_tokens_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
1.9 KiB
Ruby
Raw Normal View History

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
2021-08-04 16:29:09 +05:30
before_action :verify_impersonation_enabled!
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
feature_category :authentication_and_authorization
2017-08-17 22:00:37 +05:30
def index
set_index_vars
end
def create
@impersonation_token = finder.build(impersonation_token_params)
if @impersonation_token.save
2022-11-25 23:54:43 +05:30
render json: { new_token: @impersonation_token.token,
active_access_tokens: active_impersonation_tokens }, status: :ok
2017-08-17 22:00:37 +05:30
else
2022-11-25 23:54:43 +05:30
render json: { errors: @impersonation_token.errors.full_messages }, status: :unprocessable_entity
2017-08-17 22:00:37 +05:30
end
end
def revoke
@impersonation_token = finder.find(params[:id])
if @impersonation_token.revoke!
2023-03-17 16:20:25 +05:30
flash[:notice] = format(_("Revoked impersonation token %{token_name}!"), token_name: @impersonation_token.name)
2017-08-17 22:00:37 +05:30
else
2023-03-17 16:20:25 +05:30
flash[:alert] = format(_("Could not revoke impersonation token %{token_name}."), token_name: @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
2021-08-04 16:29:09 +05:30
def verify_impersonation_enabled!
access_denied! unless helpers.impersonation_enabled?
end
2017-08-17 22:00:37 +05:30
def finder(options = {})
PersonalAccessTokensFinder.new({ user: user, impersonation: true }.merge(options))
end
2022-11-25 23:54:43 +05:30
def active_impersonation_tokens
tokens = finder(state: 'active', sort: 'expires_at_asc_id_desc').execute
::ImpersonationAccessTokenSerializer.new.represent(tokens)
end
2017-08-17 22:00:37 +05:30
def impersonation_token_params
params.require(:personal_access_token).permit(:name, :expires_at, :impersonation, scopes: [])
end
def set_index_vars
2019-07-31 22:56:46 +05:30
@scopes = Gitlab::Auth.available_scopes_for(current_user)
2017-08-17 22:00:37 +05:30
@impersonation_token ||= finder.build
2022-11-25 23:54:43 +05:30
@active_impersonation_tokens = active_impersonation_tokens
2017-08-17 22:00:37 +05:30
end
end