Class: PersonalAccessTokens::RotateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/personal_access_tokens/rotate_service.rb

Constant Summary collapse

EXPIRATION_PERIOD =
1.week

Instance Method Summary collapse

Constructor Details

#initialize(current_user, token) ⇒ RotateService

Returns a new instance of RotateService.



7
8
9
10
# File 'app/services/personal_access_tokens/rotate_service.rb', line 7

def initialize(current_user, token)
  @current_user = current_user
  @token = token
end

Instance Method Details

#executeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/personal_access_tokens/rotate_service.rb', line 12

def execute
  return ServiceResponse.error(message: _('token already revoked')) if token.revoked?

  response = ServiceResponse.success

  PersonalAccessToken.transaction do
    unless token.revoke!
      response = ServiceResponse.error(message: _('failed to revoke token'))
      raise ActiveRecord::Rollback
    end

    target_user = token.user
    new_token = target_user.personal_access_tokens.create(create_token_params(token))

    if new_token.persisted?
      response = ServiceResponse.success(payload: { personal_access_token: new_token })
    else
      response = ServiceResponse.error(message: new_token.errors.full_messages.to_sentence)

      raise ActiveRecord::Rollback
    end
  end

  response
end