Class: Garage::TokenScope::Ability

Inherits:
Object
  • Object
show all
Defined in:
lib/garage/token_scope.rb

Instance Method Summary collapse

Constructor Details

#initialize(user, scopes = []) ⇒ Ability

Returns a new instance of Ability.



35
36
37
38
39
# File 'lib/garage/token_scope.rb', line 35

def initialize(user, scopes = [])
  @user = user
  @access = []
  load_scopes(scopes)
end

Instance Method Details

#access!(klass, action) ⇒ Object



56
57
58
# File 'lib/garage/token_scope.rb', line 56

def access!(klass, action)
  allow?(klass, action) or raise MissingScopeError.new(@user, action, klass, :forbidden, missing_scopes(klass, action))
end

#allow?(klass, action) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/garage/token_scope.rb', line 60

def allow?(klass, action)
  @access.include?([klass.to_s, action])
end

#load_scope(scope) ⇒ Object



47
48
49
50
# File 'lib/garage/token_scope.rb', line 47

def load_scope(scope)
  scope = TokenScope.configuration.scopes[scope] if scope.is_a?(Symbol)
  @access.concat(scope.accessible_resources)
end

#load_scopes(scopes) ⇒ Object



41
42
43
44
45
# File 'lib/garage/token_scope.rb', line 41

def load_scopes(scopes)
  scopes.each do |scope|
    load_scope(scope)
  end
end

#missing_scopes(klass, action) ⇒ Object



52
53
54
# File 'lib/garage/token_scope.rb', line 52

def missing_scopes(klass, action)
  TokenScope.configuration.required_scopes(klass, action)
end