2018-09-23 19:44:14 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-19 08:31:21 +00:00
|
|
|
class Profiles::PersonalAccessTokensController < Profiles::ApplicationController
|
2020-10-08 18:08:32 +00:00
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2021-02-25 03:10:50 +00:00
|
|
|
before_action do
|
|
|
|
push_frontend_feature_flag(:personal_access_tokens_scoped_to_projects, current_user)
|
|
|
|
end
|
|
|
|
|
2016-04-15 07:36:44 +00:00
|
|
|
def index
|
2016-11-22 08:57:31 +00:00
|
|
|
set_index_vars
|
2017-10-03 14:57:31 +00:00
|
|
|
@personal_access_token = finder.build
|
2016-04-15 07:36:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-11-09 12:09:24 +00:00
|
|
|
result = ::PersonalAccessTokens::CreateService.new(
|
|
|
|
current_user: current_user, target_user: current_user, params: personal_access_token_params
|
|
|
|
).execute
|
2016-04-15 07:36:44 +00:00
|
|
|
|
2020-11-09 12:09:24 +00:00
|
|
|
@personal_access_token = result.payload[:personal_access_token]
|
|
|
|
|
|
|
|
if result.success?
|
2017-12-04 04:01:18 +00:00
|
|
|
PersonalAccessToken.redis_store!(current_user.id, @personal_access_token.token)
|
2019-04-08 14:17:45 +00:00
|
|
|
redirect_to profile_personal_access_tokens_path, notice: _("Your new personal access token has been created.")
|
2016-04-15 07:36:44 +00:00
|
|
|
else
|
2016-11-22 08:57:31 +00:00
|
|
|
set_index_vars
|
2016-04-15 07:36:44 +00:00
|
|
|
render :index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-15 15:24:20 +00:00
|
|
|
def revoke
|
2017-03-01 16:59:03 +00:00
|
|
|
@personal_access_token = finder.find(params[:id])
|
2020-08-07 15:10:17 +00:00
|
|
|
service = PersonalAccessTokens::RevokeService.new(current_user, token: @personal_access_token).execute
|
|
|
|
service.success? ? flash[:notice] = service.message : flash[:alert] = service.message
|
2016-06-16 02:54:13 +00:00
|
|
|
|
|
|
|
redirect_to profile_personal_access_tokens_path
|
2016-04-15 15:24:20 +00:00
|
|
|
end
|
|
|
|
|
2016-04-15 07:36:44 +00:00
|
|
|
private
|
|
|
|
|
2017-03-01 16:59:03 +00:00
|
|
|
def finder(options = {})
|
|
|
|
PersonalAccessTokensFinder.new({ user: current_user, impersonation: false }.merge(options))
|
2017-02-27 18:56:54 +00:00
|
|
|
end
|
|
|
|
|
2016-04-15 07:36:44 +00:00
|
|
|
def personal_access_token_params
|
2016-11-22 08:57:31 +00:00
|
|
|
params.require(:personal_access_token).permit(:name, :expires_at, scopes: [])
|
2016-04-15 07:36:44 +00:00
|
|
|
end
|
2016-06-03 03:53:16 +00:00
|
|
|
|
2016-11-22 08:57:31 +00:00
|
|
|
def set_index_vars
|
2019-04-15 13:05:55 +00:00
|
|
|
@scopes = Gitlab::Auth.available_scopes_for(current_user)
|
2017-03-01 16:59:03 +00:00
|
|
|
|
|
|
|
@inactive_personal_access_tokens = finder(state: 'inactive').execute
|
2020-06-18 12:09:25 +00:00
|
|
|
@active_personal_access_tokens = active_personal_access_tokens
|
2017-12-04 04:01:18 +00:00
|
|
|
|
|
|
|
@new_personal_access_token = PersonalAccessToken.redis_getdel(current_user.id)
|
2016-06-03 03:53:16 +00:00
|
|
|
end
|
2020-06-18 12:09:25 +00:00
|
|
|
|
|
|
|
def active_personal_access_tokens
|
|
|
|
finder(state: 'active', sort: 'expires_at_asc').execute
|
|
|
|
end
|
2016-04-15 07:36:44 +00:00
|
|
|
end
|
2020-06-18 12:09:25 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Profiles::PersonalAccessTokensController.prepend_mod_with('Profiles::PersonalAccessTokensController')
|