Class: Access::Role

Inherits:
Object
  • Object
show all
Includes:
Savable
Defined in:
lib/access/role.rb

Overview

Access::Role’s are a set of privileges with (optionally) an additional restriction (which is applied globally).

Defined Under Namespace

Modules: Base

Instance Attribute Summary collapse

Attributes included from Savable

#access, #base

Instance Method Summary collapse

Methods included from Savable

#delete, #save

Constructor Details

#initialize(role, description = nil, other = {}) ⇒ Role

Create a new Role role is a role-id, should be w+ description is a piece of text describing the role other: a hash that accepts the keys :privileges and :roles



63
64
65
66
67
68
# File 'lib/access/role.rb', line 63

def initialize(role, description=nil, other={})
	@id          = role
	@privileges  = Privileges.new(self, other[:privileges])
	@roles       = Roles.new(self, other[:roles])
	@description = (description || "No description").freeze
end

Instance Attribute Details

#descriptionObject

The description of the role



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

def description
  @description
end

#idObject (readonly)

The record-id



48
49
50
# File 'lib/access/role.rb', line 48

def id
  @id
end

#privilegesObject (readonly)

Privileges this role has granted



57
58
59
# File 'lib/access/role.rb', line 57

def privileges
  @privileges
end

#rolesObject (readonly)

The roles this role belongs to



54
55
56
# File 'lib/access/role.rb', line 54

def roles
  @roles
end

Instance Method Details

#allows?(privilege, condition = nil) ⇒ Boolean

recursively tests the role and its contained roles if any of them allows a given privilege under given conditions (may be nil to indicate no condition)

Returns:

  • (Boolean)


90
91
92
# File 'lib/access/role.rb', line 90

def allows?(privilege, condition=nil)
	@privileges.allow?(privilege, condition) || @roles.allow?(privilege, condition)
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


95
96
97
# File 'lib/access/role.rb', line 95

def eql?(other)
	self.class == other.class && @id.eql?(other.id)
end

#hashObject

:nodoc:



100
101
102
# File 'lib/access/role.rb', line 100

def hash
	@id.hash
end

#inspectObject

:nodoc:



104
105
106
107
108
109
110
111
112
# File 'lib/access/role.rb', line 104

def inspect # :nodoc:
	"#<%s:0x%08x description=%s privileges=%s roles=%s>" %  [
		self.class,
		object_id << 1,
		@description,
		@privileges.inspect,
		@roles.inspect,
	]
end

#storableObject

:nodoc: serialize to column => value for storage



78
79
80
81
82
83
84
85
# File 'lib/access/role.rb', line 78

def storable
	{
		:id          => @id,
		:description => @description,
		:privileges  => @privileges.storable,
		:roles       => @roles.storable,
	}
end