Class: Access::User
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
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#privileges ⇒ Object
readonly
Returns the value of attribute privileges.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Attributes included from Savable
Instance Method Summary collapse
-
#activate ⇒ Object
Activate a user.
-
#active=(value) ⇒ Object
Set active state to value.
-
#active? ⇒ Boolean
Check if user is activated (deactivated users have no privileges).
-
#admin? ⇒ Boolean
Check if user is admin.
-
#authorized?(*args) ⇒ Boolean
Same as privileged? but also takes active? and logged? into consideration.
-
#deactivate ⇒ Object
Deactivate a user (deactivated users have no privileges).
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#inactive? ⇒ Boolean
Check if user is deactivated (deactivated users have no privileges).
-
#initialize(id, credentials, meta = nil, admin = false, other = {}) ⇒ User
constructor
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.
-
#inspect ⇒ Object
:nodoc:.
- #logged=(value) ⇒ Object
- #logged? ⇒ Boolean
- #login ⇒ Object
- #logout ⇒ Object
-
#privileged?(privilege, parameters = nil) ⇒ Boolean
Check if a user has sufficient privileges to be allowed for certain privilege with certain restriction parameters.
-
#storable ⇒ Object
The data needed to restore a user.
Methods included from Savable
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
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/access/user.rb', line 77 def initialize(id, credentials, =nil, admin=false, other={}) @id = id @credentials = credentials @admin = admin @active = other.has_key?(:active) ? other[:active] : false @meta = @roles = Roles.new(self, other[:role]) @privileges = Privileges.new(self, other[:privileges]) @logged = false extend(Admin) if @admin end |
Instance Attribute Details
#credentials ⇒ Object
Returns the value of attribute credentials.
52 53 54 |
# File 'lib/access/user.rb', line 52 def credentials @credentials end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
51 52 53 |
# File 'lib/access/user.rb', line 51 def id @id end |
#meta ⇒ Object
Returns the value of attribute meta.
56 57 58 |
# File 'lib/access/user.rb', line 56 def @meta end |
#privileges ⇒ Object (readonly)
Returns the value of attribute privileges.
53 54 55 |
# File 'lib/access/user.rb', line 53 def privileges @privileges end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
54 55 56 |
# File 'lib/access/user.rb', line 54 def roles @roles end |
Instance Method Details
#activate ⇒ Object
Activate a user
125 126 127 128 |
# File 'lib/access/user.rb', line 125 def activate @active = true save end |
#active=(value) ⇒ Object
Set active state to value
137 138 139 140 |
# File 'lib/access/user.rb', line 137 def active=(value) @active = value save end |
#active? ⇒ Boolean
Check if user is activated (deactivated users have no privileges)
115 116 117 |
# File 'lib/access/user.rb', line 115 def active? @active end |
#admin? ⇒ Boolean
Check if user is admin
110 111 112 |
# File 'lib/access/user.rb', line 110 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.
105 106 107 |
# File 'lib/access/user.rb', line 105 def (*args) @active && @logged && privileged?(*args) end |
#deactivate ⇒ Object
Deactivate a user (deactivated users have no privileges)
131 132 133 134 |
# File 'lib/access/user.rb', line 131 def deactivate @active = false save end |
#eql?(other) ⇒ Boolean Also known as: ==
158 159 160 |
# File 'lib/access/user.rb', line 158 def eql?(other) self.class == other.class && @id.eql?(other.id) end |
#hash ⇒ Object
163 164 165 |
# File 'lib/access/user.rb', line 163 def hash @id.hash end |
#inactive? ⇒ Boolean
Check if user is deactivated (deactivated users have no privileges)
120 121 122 |
# File 'lib/access/user.rb', line 120 def inactive? !@active end |
#inspect ⇒ Object
:nodoc:
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/access/user.rb', line 167 def inspect # :nodoc: "#<%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
150 151 152 |
# File 'lib/access/user.rb', line 150 def logged=(value) @logged = value end |
#logged? ⇒ Boolean
154 155 156 |
# File 'lib/access/user.rb', line 154 def logged? @logged end |
#login ⇒ Object
142 143 144 |
# File 'lib/access/user.rb', line 142 def login @logged = true end |
#logout ⇒ Object
146 147 148 |
# File 'lib/access/user.rb', line 146 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
99 100 101 |
# File 'lib/access/user.rb', line 99 def privileged?(privilege, parameters=nil) @privileges.allow?(privilege, parameters) || @roles.allow?(privilege, parameters) end |
#storable ⇒ Object
The data needed to restore a user. Simplified to hashes and scalar values.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/access/user.rb', line 60 def storable { :id => @id, :credentials => @credentials, :meta => @meta, :admin => @admin, :active => @active, :privileges => @privileges.storable, :roles => @roles.storable, } end |