gitlab-org--gitlab-foss/app/controllers/profiles/personal_access_tokens_controller.rb

54 lines
1.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class Profiles::PersonalAccessTokensController < Profiles::ApplicationController
def index
set_index_vars
@personal_access_token = finder.build
end
def create
@personal_access_token = finder.build(personal_access_token_params)
if @personal_access_token.save
PersonalAccessToken.redis_store!(current_user.id, @personal_access_token.token)
redirect_to profile_personal_access_tokens_path, notice: _("Your new personal access token has been created.")
else
set_index_vars
render :index
end
end
2016-04-15 11:24:20 -04:00
def revoke
@personal_access_token = finder.find(params[:id])
2016-04-15 11:24:20 -04:00
if @personal_access_token.revoke!
flash[:notice] = _("Revoked personal access token %{personal_access_token_name}!") % { personal_access_token_name: @personal_access_token.name }
2016-04-15 11:24:20 -04:00
else
flash[:alert] = _("Could not revoke personal access token %{personal_access_token_name}.") % { personal_access_token_name: @personal_access_token.name }
2016-04-15 11:24:20 -04:00
end
redirect_to profile_personal_access_tokens_path
2016-04-15 11:24:20 -04:00
end
private
def finder(options = {})
PersonalAccessTokensFinder.new({ user: current_user, impersonation: false }.merge(options))
2017-02-27 13:56:54 -05:00
end
def personal_access_token_params
params.require(:personal_access_token).permit(:name, :expires_at, scopes: [])
end
# rubocop: disable CodeReuse/ActiveRecord
def set_index_vars
@scopes = Gitlab::Auth.available_scopes_for(current_user)
@inactive_personal_access_tokens = finder(state: 'inactive').execute
@active_personal_access_tokens = finder(state: 'active').execute.order(:expires_at)
@new_personal_access_token = PersonalAccessToken.redis_getdel(current_user.id)
end
# rubocop: enable CodeReuse/ActiveRecord
end