Class: PublishingPlatform::SSO::AuthoriseUser

Inherits:
Object
  • Object
show all
Defined in:
lib/publishing_platform_sso/authorise_user.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_user, permissions) ⇒ AuthoriseUser

Returns a new instance of AuthoriseUser.



6
7
8
9
# File 'lib/publishing_platform_sso/authorise_user.rb', line 6

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

Class Method Details

.callObject



4
# File 'lib/publishing_platform_sso/authorise_user.rb', line 4

def self.call(...) = new(...).call

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/publishing_platform_sso/authorise_user.rb', line 11

def call
  case permissions
  when String
    unless current_user.has_permission?(permissions)
      raise PublishingPlatform::SSO::PermissionDeniedError, "Sorry, you don't seem to have the #{permissions} permission for this app."
    end
  when Hash
    raise ArgumentError, "Must be either `any_of` or `all_of`" unless permissions.keys.size == 1

    if permissions[:any_of]
      authorise_user_with_at_least_one_of_permissions!(permissions[:any_of])
    elsif permissions[:all_of]
      authorise_user_with_all_permissions!(permissions[:all_of])
    else
      raise ArgumentError, "Must be either `any_of` or `all_of`"
    end
  end
end