Class: Access::User

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

Overview

Access::User Use nil-id if you don’t want the object to be stored.

Defined Under Namespace

Modules: Base

Instance Attribute Summary collapse

Attributes included from Savable

#access, #base

Instance Method Summary collapse

Methods included from Savable

#save

Constructor Details

#initialize(id, credentials, meta = nil, admin = false, other = {}) ⇒ User

access: the access-instance the user is tied to (necessary for storing) id: a string/integer, identifying the user credentials: (if not subclassed) a string authenticating the user, will be hashed before storing. meta: meta data about the user admin: admin’s have all privileges granted



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/access/user.rb', line 85

def initialize(id, credentials, meta=nil, admin=false, other={})
	@id          = id
	@credentials = credentials
	@admin       = admin
	@active      = other.has_key?(:active) ? other[:active] : false
	@meta        = meta
	@roles       = Roles.new(self, other[:role])
	@privileges  = Privileges.new(self, other[:privileges])
	@logged      = false

	extend(Admin) if @admin
end

Instance Attribute Details

#credentialsObject

The credentials of the user (password)



55
56
57
# File 'lib/access/user.rb', line 55

def credentials
  @credentials
end

#idObject (readonly)

The record-id



52
53
54
# File 'lib/access/user.rb', line 52

def id
  @id
end

#metaObject

Metadata - anything you want



64
65
66
# File 'lib/access/user.rb', line 64

def meta
  @meta
end

#privilegesObject (readonly)

Privileges of the user (Privileges instance)



58
59
60
# File 'lib/access/user.rb', line 58

def privileges
  @privileges
end

#rolesObject (readonly)

Roles the user is in (Roles instance)



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

def roles
  @roles
end

Instance Method Details

#activateObject

Activate a user



137
138
139
140
# File 'lib/access/user.rb', line 137

def activate
	@active = true
	save
end

#active=(value) ⇒ Object

Set active state to value



149
150
151
152
# File 'lib/access/user.rb', line 149

def active=(value)
	@active = value
	save
end

#active?Boolean

Check if user is activated (deactivated users have no privileges)

Returns:

  • (Boolean)


127
128
129
# File 'lib/access/user.rb', line 127

def active?
	@active
end

#admin?Boolean

Check if user is admin

Returns:

  • (Boolean)


122
123
124
# File 'lib/access/user.rb', line 122

def admin?
	false
end

#authorized?(*args) ⇒ Boolean

Same as privileged? but also takes active? and logged? into consideration. I.e. if a user is inactive or not logged in, he is not authorized for anything.

Returns:

  • (Boolean)


117
118
119
# File 'lib/access/user.rb', line 117

def authorized?(*args)
	@active && @logged && privileged?(*args)
end

#deactivateObject

Deactivate a user (deactivated users have no privileges)



143
144
145
146
# File 'lib/access/user.rb', line 143

def deactivate
	@active = false
	save
end

#eql?(other) ⇒ Boolean Also known as: ==

:nodoc:

Returns:

  • (Boolean)


175
176
177
# File 'lib/access/user.rb', line 175

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

#hashObject

:nodoc:



181
182
183
# File 'lib/access/user.rb', line 181

def hash
	@id.hash
end

#inactive?Boolean

Check if user is deactivated (deactivated users have no privileges)

Returns:

  • (Boolean)


132
133
134
# File 'lib/access/user.rb', line 132

def inactive?
	!@active
end

#inspectObject

:nodoc:



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/access/user.rb', line 186

def inspect
	"#<%s:0x%08x base: %s id: %s credentials: %s %s%s%s>" %  [
		self.class,
		object_id << 1,
		"#{@base.class}(#{(class <<@base; self; end).ancestors.first})",
		@id.inspect,
		@credentials,
		@active ? 'active' : 'inactive',
		admin? ? ' admin' : '',
		logged? ? ' logged' : ''
	]
end

#logged=(value) ⇒ Object

Set the logged? status of the user



165
166
167
# File 'lib/access/user.rb', line 165

def logged=(value)
	@logged = !!value
end

#logged?Boolean

Whether the user is logged in or not

Returns:

  • (Boolean)


170
171
172
# File 'lib/access/user.rb', line 170

def logged?
	@logged
end

#loginObject

Set the logged? status of the user to true



155
156
157
# File 'lib/access/user.rb', line 155

def 
	@logged = true
end

#logoutObject

Set the logged? status of the user to false



160
161
162
# File 'lib/access/user.rb', line 160

def logout
	@logged = false
end

#privileged?(privilege, parameters = nil) ⇒ Boolean

Check if a user has sufficient privileges to be allowed for certain privilege with certain restriction parameters. WARNING! This method does not care about login- nor active-state. Use authorized? to do that

Returns:

  • (Boolean)


111
112
113
# File 'lib/access/user.rb', line 111

def privileged?(privilege, parameters=nil)
	@privileges.allow?(privilege, parameters) || @roles.allow?(privilege, parameters)
end

#storableObject

The data needed to restore a user. Simplified to hashes and scalar values.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/access/user.rb', line 68

def storable
	{
		:id          => @id,
		:credentials => @credentials,
		:meta        => @meta,
		:admin       => @admin,
		:active      => @active,
		:privileges  => @privileges.storable,
		:roles       => @roles.storable,
	}
end