Class: Doorkeeper::OAuth::Scopes

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/doorkeeper/oauth/scopes.rb

Constant Summary collapse

DYNAMIC_SCOPE_WILDCARD =
"*"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScopes



26
27
28
# File 'lib/doorkeeper/oauth/scopes.rb', line 26

def initialize
  @scopes = []
end

Class Method Details

.from_array(array) ⇒ Object



18
19
20
21
22
# File 'lib/doorkeeper/oauth/scopes.rb', line 18

def self.from_array(array)
  new.tap do |scope|
    scope.add(*array)
  end
end

.from_string(string) ⇒ Object



11
12
13
14
15
16
# File 'lib/doorkeeper/oauth/scopes.rb', line 11

def self.from_string(string)
  string ||= ""
  new.tap do |scope|
    scope.add(*string.split)
  end
end

Instance Method Details

#&(other) ⇒ Object

DEPRECATED: With dynamic scopes, #allowed should be called because A & B doesn’t really make sense with dynamic scopes.

For example, if A = user:* and B is user:1, A & B = []. If we modified this method to take dynamic scopes into an account, then order becomes important, and this would violate the principle that A & B = B & A.



79
80
81
82
83
# File 'lib/doorkeeper/oauth/scopes.rb', line 79

def &(other)
  return allowed(other) if dynamic_scopes_enabled?

  self.class.from_array(all & to_array(other))
end

#+(other) ⇒ Object



61
62
63
# File 'lib/doorkeeper/oauth/scopes.rb', line 61

def +(other)
  self.class.from_array(all + to_array(other))
end

#<=>(other) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/doorkeeper/oauth/scopes.rb', line 65

def <=>(other)
  if other.respond_to?(:map)
    map(&:to_s).sort <=> other.map(&:to_s).sort
  else
    super
  end
end

#add(*scopes) ⇒ Object



42
43
44
45
# File 'lib/doorkeeper/oauth/scopes.rb', line 42

def add(*scopes)
  @scopes.push(*scopes.map(&:to_s))
  @scopes.uniq!
end

#allObject



47
48
49
# File 'lib/doorkeeper/oauth/scopes.rb', line 47

def all
  @scopes
end

#allowed(other) ⇒ Object

Returns a set of scopes that are allowed, taking dynamic scopes into account. This instance’s scopes is taken as the allowed set, and the passed value is the set to filter.



90
91
92
93
# File 'lib/doorkeeper/oauth/scopes.rb', line 90

def allowed(other)
  filtered_scopes = other.select { |scope| self.exists?(scope) }
  self.class.from_array(filtered_scopes)
end

#exists?(scope) ⇒ Boolean



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/doorkeeper/oauth/scopes.rb', line 30

def exists?(scope)
  scope = scope.to_s

  @scopes.any? do |allowed_scope|
    if dynamic_scopes_enabled? && dynamic_scopes_present?(allowed_scope, scope)
      dynamic_scope_match?(allowed_scope, scope)
    else
      allowed_scope == scope
    end
  end
end

#scopes?(scopes) ⇒ Boolean Also known as: has_scopes?



55
56
57
# File 'lib/doorkeeper/oauth/scopes.rb', line 55

def scopes?(scopes)
  scopes.all? { |scope| exists?(scope) }
end

#to_sObject



51
52
53
# File 'lib/doorkeeper/oauth/scopes.rb', line 51

def to_s
  @scopes.join(" ")
end