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
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2021-02-24 22:10:50 -05:00
|
|
|
before_action do
|
|
|
|
push_frontend_feature_flag(:personal_access_tokens_scoped_to_projects, current_user)
|
|
|
|
end
|
|
|
|
|
2016-04-15 03:36:44 -04:00
|
|
|
def index
|
2016-11-22 03:57:31 -05:00
|
|
|
set_index_vars
|
2021-06-30 08:07:58 -04:00
|
|
|
scopes = params[:scopes].split(',').map(&:squish).select(&:present?).map(&:to_sym) unless params[:scopes].nil?
|
|
|
|
@personal_access_token = finder.build(
|
|
|
|
name: params[:name],
|
|
|
|
scopes: scopes
|
|
|
|
)
|
2016-04-15 03:36:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-11-09 07:09:24 -05:00
|
|
|
result = ::PersonalAccessTokens::CreateService.new(
|
|
|
|
current_user: current_user, target_user: current_user, params: personal_access_token_params
|
|
|
|
).execute
|
2016-04-15 03:36:44 -04:00
|
|
|
|
2020-11-09 07:09:24 -05:00
|
|
|
@personal_access_token = result.payload[:personal_access_token]
|
|
|
|
|
|
|
|
if result.success?
|
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])
|
2020-08-07 11:10:17 -04:00
|
|
|
service = PersonalAccessTokens::RevokeService.new(current_user, token: @personal_access_token).execute
|
|
|
|
service.success? ? flash[:notice] = service.message : flash[:alert] = service.message
|
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
|
|
|
|
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
|
2020-06-18 08:09:25 -04:00
|
|
|
@active_personal_access_tokens = active_personal_access_tokens
|
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
|
2020-06-18 08:09:25 -04:00
|
|
|
|
|
|
|
def active_personal_access_tokens
|
|
|
|
finder(state: 'active', sort: 'expires_at_asc').execute
|
|
|
|
end
|
2016-04-15 03:36:44 -04:00
|
|
|
end
|
2020-06-18 08:09:25 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Profiles::PersonalAccessTokensController.prepend_mod_with('Profiles::PersonalAccessTokensController')
|