Class: Lanes::User
- Defined in:
- lib/lanes/access/user.rb
Constant Summary collapse
- USER_EDITABLE_ATTRIBUTES =
We override the default implementation so that we can gaurantee that the current user can always update their own information
[:name, :email, :password]
Class Method Summary collapse
- .can_write_attributes?(attr, user) ⇒ Boolean
-
.current ⇒ User
If all that’s needed is the user’s id, see ‘current_id`, that method does not not attempt to instantiate a User Defaults to nil.
-
.current_id ⇒ Fixnum
Retrieve the current id of the user we’re proxying for.
-
.scoped_to(user) ⇒ UserProxy
sets the user for the duration of the block.
Instance Method Summary collapse
-
#can_delete?(model, id) ⇒ Boolean
Can the User delete the model?.
-
#can_read?(model, attribute = nil) ⇒ Boolean
Can the User view the model?.
-
#can_write?(model, attribute = nil) ⇒ Boolean
Can the User create and update the model?.
- #can_write_attributes?(attr, user) ⇒ Boolean
- #roles ⇒ Object
- #setting_attribute_is_allowed?(name, user) ⇒ Boolean
- #workspace_data ⇒ Object
Methods included from Concerns::ApiAttributeAccess
#_set_attribute_data_from_collection, #set_attribute_data
Class Method Details
.can_write_attributes?(attr, user) ⇒ Boolean
53 54 55 |
# File 'lib/lanes/access/user.rb', line 53 def self.can_write_attributes?( attr, user ) ( attr[:id] && attr[:id].to_i == user.id ) ? true : super end |
.current ⇒ User
If all that’s needed is the user’s id, see ‘current_id`, that method does not not attempt to instantiate a User Defaults to nil
63 64 65 66 67 68 69 70 71 |
# File 'lib/lanes/access/user.rb', line 63 def self.current uid = Thread.current[:lanes_current_user] if uid.is_a?(User) uid else user = Thread.current[:lanes_current_user] = User.find_by_id(uid) return user ? user.id : nil end end |
.current_id ⇒ Fixnum
Retrieve the current id of the user we’re proxying for. get’s a bit complicated since we can proxy both for a user object or just the user’s id
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lanes/access/user.rb', line 77 def self.current_id uid = Thread.current[:lanes_current_user] if uid.nil? 0 elsif uid.is_a?(User) uid.id else uid end end |
.scoped_to(user) ⇒ UserProxy
sets the user for the duration of the block
114 115 116 117 118 119 |
# File 'lib/lanes/access/user.rb', line 114 def self.scoped_to( user ) prev_user, Thread.current[:lanes_current_user] = self.current, user yield user ensure Thread.current[:lanes_current_user] = prev_user end |
Instance Method Details
#can_delete?(model, id) ⇒ Boolean
Returns Can the User delete the model?.
40 41 42 |
# File 'lib/lanes/access/user.rb', line 40 def can_delete?(model, id) roles.can_delete?(model, id) end |
#can_read?(model, attribute = nil) ⇒ Boolean
Returns Can the User view the model?.
26 27 28 |
# File 'lib/lanes/access/user.rb', line 26 def can_read?(model, attribute = nil) roles.can_read?(model, attribute) end |
#can_write?(model, attribute = nil) ⇒ Boolean
Returns Can the User create and update the model?.
33 34 35 |
# File 'lib/lanes/access/user.rb', line 33 def can_write?(model, attribute = nil) roles.can_write?(model, attribute) end |
#can_write_attributes?(attr, user) ⇒ Boolean
50 51 52 |
# File 'lib/lanes/access/user.rb', line 50 def can_write_attributes?( attr, user ) ( !new_record? && user.id == self.id ) ? true : super end |
#roles ⇒ Object
13 14 15 |
# File 'lib/lanes/access/user.rb', line 13 def roles @cached_roles ||= Access::RoleCollection.new(self) end |
#setting_attribute_is_allowed?(name, user) ⇒ Boolean
47 48 49 |
# File 'lib/lanes/access/user.rb', line 47 def setting_attribute_is_allowed?(name, user) ( !new_record? && user.id == self.id && USER_EDITABLE_ATTRIBUTES.include?(name) ) ? true : super end |