class Mongocore::Access

Constants

AL

Access levels (7)

Attributes

keys[RW]

Holds the keys from the model schema

Public Class Methods

get() click to toggle source

Get the current access level

# File lib/mongocore/access.rb, line 51
def self.get
  RequestStore.store[:access]
end
new(schema) click to toggle source

The access control class

# File lib/mongocore/access.rb, line 21
def initialize(schema)
  @keys = schema.keys
end
reset() click to toggle source

Reset the access level

# File lib/mongocore/access.rb, line 46
def self.reset
  RequestStore.store[:access] = nil
end
role(level) { || ... } click to toggle source

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

# File lib/mongocore/access.rb, line 67
def self.role(level, &block)
  set(level); yield.tap{ set(nil)}
end
set(level = nil) click to toggle source

Set the current access level

# File lib/mongocore/access.rb, line 56
def self.set(level = nil)
  (level.nil? || set?(level)) ? RequestStore.store[:access] = level : get
end
set?(level) click to toggle source

Set?

# File lib/mongocore/access.rb, line 61
def self.set?(level)
  AL.index(level) >= AL.index(get || :all)
end

Public Instance Methods

ok?(level) click to toggle source

Ok?

# 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) click to toggle source

Key readable?

# File lib/mongocore/access.rb, line 26
def read?(key)
  ok?(@keys[key][:read] || :all) rescue false
end
write?(key) click to toggle source

Key writable?

# File lib/mongocore/access.rb, line 31
def write?(key)
  ok?(@keys[key][:write] || :all) rescue false
end