Class: Mongocore::Access

Inherits:
Object
  • Object
show all
Defined in:
lib/mongocore/access.rb

Constant Summary collapse

AL =

Access levels (7)

[:all, :user, :owner, :dev, :admin, :super, :app]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Access

The access control class



21
22
23
# File 'lib/mongocore/access.rb', line 21

def initialize(schema)
  @keys = schema.keys
end

Instance Attribute Details

#keysObject

Holds the keys from the model schema



18
19
20
# File 'lib/mongocore/access.rb', line 18

def keys
  @keys
end

Class Method Details

.getObject

Get the current access level



51
52
53
# File 'lib/mongocore/access.rb', line 51

def self.get
  RequestStore.store[:access]
end

.resetObject

Reset the access level



46
47
48
# File 'lib/mongocore/access.rb', line 46

def self.reset
  RequestStore.store[:access] = nil
end

.role(level, &block) ⇒ Object

Access block Run with Mongocore::Access.role(:user){ # Do something as :user}



67
68
69
# File 'lib/mongocore/access.rb', line 67

def self.role(level, &block)
  set(level); yield.tap{ set(nil)}
end

.set(level = nil) ⇒ Object

Set the current access level



56
57
58
# File 'lib/mongocore/access.rb', line 56

def self.set(level = nil)
  (level.nil? || set?(level)) ? RequestStore.store[:access] = level : get
end

.set?(level) ⇒ Boolean

Set?

Returns:

  • (Boolean)


61
62
63
# File 'lib/mongocore/access.rb', line 61

def self.set?(level)
  AL.index(level) >= AL.index(get || :all)
end

Instance Method Details

#ok?(level) ⇒ Boolean

Ok?

Returns:

  • (Boolean)


36
37
38
# File 'lib/mongocore/access.rb', line 36

def ok?(level)
  !Mongocore.access || (g = self.class.get).nil? || AL.index(level.to_sym) <= AL.index(g || :app)
end

#read?(key) ⇒ Boolean

Key readable?

Returns:

  • (Boolean)


26
27
28
# File 'lib/mongocore/access.rb', line 26

def read?(key)
  ok?(@keys[key][:read] || :all) rescue false
end

#write?(key) ⇒ Boolean

Key writable?

Returns:

  • (Boolean)


31
32
33
# File 'lib/mongocore/access.rb', line 31

def write?(key)
  ok?(@keys[key][:write] || :all) rescue false
end