From c2b1cdef7e8cdaec35bd0844301ce8f06ed742b7 Mon Sep 17 00:00:00 2001 From: Simon Vocella Date: Fri, 6 Jan 2017 17:00:46 +0100 Subject: add admin panel for personal access tokens --- .../admin/personal_access_tokens_controller.rb | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/controllers/admin/personal_access_tokens_controller.rb (limited to 'app/controllers/admin') diff --git a/app/controllers/admin/personal_access_tokens_controller.rb b/app/controllers/admin/personal_access_tokens_controller.rb new file mode 100644 index 00000000000..7202d80ce1b --- /dev/null +++ b/app/controllers/admin/personal_access_tokens_controller.rb @@ -0,0 +1,48 @@ +class Admin::PersonalAccessTokensController < Admin::ApplicationController + before_action :user + + def index + set_index_vars + end + + def create + @personal_access_token = user.personal_access_tokens.generate(personal_access_token_params) + + if @personal_access_token.save + flash[:personal_access_token] = @personal_access_token.token + redirect_to admin_user_personal_access_tokens_path, notice: "A new personal access token has been created." + else + set_index_vars + render :index + end + end + + def revoke + @personal_access_token = user.personal_access_tokens.find(params[:id]) + + if @personal_access_token.revoke! + flash[:notice] = "Revoked personal access token #{@personal_access_token.name}!" + else + flash[:alert] = "Could not revoke personal access token #{@personal_access_token.name}." + end + + redirect_to admin_user_personal_access_tokens_path + end + + private + + def user + @user ||= User.find_by!(username: params[:user_id]) + end + + def personal_access_token_params + params.require(:personal_access_token).permit(:name, :expires_at, :impersonation, scopes: []) + end + + def set_index_vars + @personal_access_token ||= user.personal_access_tokens.build + @scopes = Gitlab::Auth::SCOPES + @active_personal_access_tokens = PersonalAccessToken.and_impersonation_tokens.where(user_id: user.id).active.order(:expires_at) + @inactive_personal_access_tokens = PersonalAccessToken.and_impersonation_tokens.where(user_id: user.id).inactive + end +end -- cgit v1.2.1