2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-19 04:31:21 -04:00
|
|
|
class Profiles::PersonalAccessTokensController < Profiles::ApplicationController
|
2016-04-15 03:36:44 -04:00
|
|
|
def index
|
2016-11-22 03:57:31 -05:00
|
|
|
set_index_vars
|
2017-10-03 10:57:31 -04:00
|
|
|
@personal_access_token = finder.build
|
2016-04-15 03:36:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-03-01 11:59:03 -05:00
|
|
|
@personal_access_token = finder.build(personal_access_token_params)
|
2016-04-15 03:36:44 -04:00
|
|
|
|
|
|
|
if @personal_access_token.save
|
2017-12-03 23:01:18 -05:00
|
|
|
PersonalAccessToken.redis_store!(current_user.id, @personal_access_token.token)
|
2019-04-08 10:17:45 -04:00
|
|
|
redirect_to profile_personal_access_tokens_path, notice: _("Your new personal access token has been created.")
|
2016-04-15 03:36:44 -04:00
|
|
|
else
|
2016-11-22 03:57:31 -05:00
|
|
|
set_index_vars
|
2016-04-15 03:36:44 -04:00
|
|
|
render :index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-15 11:24:20 -04:00
|
|
|
def revoke
|
2017-03-01 11:59:03 -05:00
|
|
|
@personal_access_token = finder.find(params[:id])
|
2016-04-15 11:24:20 -04:00
|
|
|
|
|
|
|
if @personal_access_token.revoke!
|
2019-04-08 10:17:45 -04:00
|
|
|
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
|
2019-04-08 10:17:45 -04:00
|
|
|
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
|
2016-06-15 22:54:13 -04:00
|
|
|
|
|
|
|
redirect_to profile_personal_access_tokens_path
|
2016-04-15 11:24:20 -04:00
|
|
|
end
|
|
|
|
|
2016-04-15 03:36:44 -04:00
|
|
|
private
|
|
|
|
|
2017-03-01 11:59:03 -05:00
|
|
|
def finder(options = {})
|
|
|
|
PersonalAccessTokensFinder.new({ user: current_user, impersonation: false }.merge(options))
|
2017-02-27 13:56:54 -05:00
|
|
|
end
|
|
|
|
|
2016-04-15 03:36:44 -04:00
|
|
|
def personal_access_token_params
|
2016-11-22 03:57:31 -05:00
|
|
|
params.require(:personal_access_token).permit(:name, :expires_at, scopes: [])
|
2016-04-15 03:36:44 -04:00
|
|
|
end
|
2016-06-02 23:53:16 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-11-22 03:57:31 -05:00
|
|
|
def set_index_vars
|
2019-04-15 09:05:55 -04:00
|
|
|
@scopes = Gitlab::Auth.available_scopes_for(current_user)
|
2017-03-01 11:59:03 -05:00
|
|
|
|
|
|
|
@inactive_personal_access_tokens = finder(state: 'inactive').execute
|
|
|
|
@active_personal_access_tokens = finder(state: 'active').execute.order(:expires_at)
|
2017-12-03 23:01:18 -05:00
|
|
|
|
|
|
|
@new_personal_access_token = PersonalAccessToken.redis_getdel(current_user.id)
|
2016-06-02 23:53:16 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-04-15 03:36:44 -04:00
|
|
|
end
|