BigW Consortium Gitlab

impersonation_tokens_controller.rb 1.42 KB
Newer Older
1
class Admin::ImpersonationTokensController < Admin::ApplicationController
2
  before_action :user
3 4 5 6 7 8

  def index
    set_index_vars
  end

  def create
9
    @impersonation_token = finder.build(impersonation_token_params)
10 11 12 13 14 15 16 17 18 19 20

    if @impersonation_token.save
      flash[:impersonation_token] = @impersonation_token.token
      redirect_to admin_user_impersonation_tokens_path, notice: "A new impersonation token has been created."
    else
      set_index_vars
      render :index
    end
  end

  def revoke
21
    @impersonation_token = finder.find(params[:id])
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

    if @impersonation_token.revoke!
      flash[:notice] = "Revoked impersonation token #{@impersonation_token.name}!"
    else
      flash[:alert] = "Could not revoke impersonation token #{@impersonation_token.name}."
    end

    redirect_to admin_user_impersonation_tokens_path
  end

  private

  def user
    @user ||= User.find_by!(username: params[:user_id])
  end

38 39
  def finder(options = {})
    PersonalAccessTokensFinder.new({ user: user, impersonation: true }.merge(options))
40 41
  end

42 43 44 45 46
  def impersonation_token_params
    params.require(:personal_access_token).permit(:name, :expires_at, :impersonation, scopes: [])
  end

  def set_index_vars
Sean McGivern committed
47
    @scopes = Gitlab::Auth::API_SCOPES
48 49 50 51

    @impersonation_token ||= finder.build
    @inactive_impersonation_tokens = finder(state: 'inactive').execute
    @active_impersonation_tokens = finder(state: 'active').execute.order(:expires_at)
52 53
  end
end