debian-mirror-gitlab/app/controllers/profiles/personal_access_tokens_controller.rb

54 lines
1.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-06-22 15:30:34 +05:30
class Profiles::PersonalAccessTokensController < Profiles::ApplicationController
def index
2017-08-17 22:00:37 +05:30
set_index_vars
2018-03-17 18:26:18 +05:30
@personal_access_token = finder.build
2016-06-22 15:30:34 +05:30
end
def create
2017-08-17 22:00:37 +05:30
@personal_access_token = finder.build(personal_access_token_params)
2016-06-22 15:30:34 +05:30
if @personal_access_token.save
2018-03-17 18:26:18 +05:30
PersonalAccessToken.redis_store!(current_user.id, @personal_access_token.token)
2019-07-31 22:56:46 +05:30
redirect_to profile_personal_access_tokens_path, notice: _("Your new personal access token has been created.")
2016-06-22 15:30:34 +05:30
else
2017-08-17 22:00:37 +05:30
set_index_vars
2016-06-22 15:30:34 +05:30
render :index
end
end
def revoke
2017-08-17 22:00:37 +05:30
@personal_access_token = finder.find(params[:id])
2016-06-22 15:30:34 +05:30
if @personal_access_token.revoke!
2019-07-31 22:56:46 +05:30
flash[:notice] = _("Revoked personal access token %{personal_access_token_name}!") % { personal_access_token_name: @personal_access_token.name }
2016-06-22 15:30:34 +05:30
else
2019-07-31 22:56:46 +05:30
flash[:alert] = _("Could not revoke personal access token %{personal_access_token_name}.") % { personal_access_token_name: @personal_access_token.name }
2016-06-22 15:30:34 +05:30
end
redirect_to profile_personal_access_tokens_path
end
private
2017-08-17 22:00:37 +05:30
def finder(options = {})
PersonalAccessTokensFinder.new({ user: current_user, impersonation: false }.merge(options))
end
2016-06-22 15:30:34 +05:30
def personal_access_token_params
2017-08-17 22:00:37 +05:30
params.require(:personal_access_token).permit(:name, :expires_at, scopes: [])
2016-06-22 15:30:34 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
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
@inactive_personal_access_tokens = finder(state: 'inactive').execute
@active_personal_access_tokens = finder(state: 'active').execute.order(:expires_at)
2018-03-17 18:26:18 +05:30
@new_personal_access_token = PersonalAccessToken.redis_getdel(current_user.id)
2016-06-22 15:30:34 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-06-22 15:30:34 +05:30
end