Class: Garage::Permissions
- Inherits:
-
Object
- Object
- Garage::Permissions
- Defined in:
- lib/garage/permissions.rb
Instance Attribute Summary collapse
-
#resource_class ⇒ Object
Returns the value of attribute resource_class.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #authorize!(action) ⇒ Object
- #deleted! ⇒ Object
- #exists? ⇒ Boolean
- #for(action) ⇒ Object
- #forbids!(*actions) ⇒ Object
-
#initialize(user, resource_class, permissions = { read: :forbidden, write: :forbidden }) ⇒ Permissions
constructor
A new instance of Permissions.
- #permits!(*actions) ⇒ Object
- #permits?(action) ⇒ Boolean
- #readable? ⇒ Boolean
- #writable? ⇒ Boolean
Constructor Details
#initialize(user, resource_class, permissions = { read: :forbidden, write: :forbidden }) ⇒ Permissions
Returns a new instance of Permissions.
28 29 30 31 32 |
# File 'lib/garage/permissions.rb', line 28 def initialize(user, resource_class, = { read: :forbidden, write: :forbidden }) @user = user @resource_class = resource_class @perms = end |
Instance Attribute Details
#resource_class ⇒ Object
Returns the value of attribute resource_class.
26 27 28 |
# File 'lib/garage/permissions.rb', line 26 def resource_class @resource_class end |
#user ⇒ Object
Returns the value of attribute user.
26 27 28 |
# File 'lib/garage/permissions.rb', line 26 def user @user end |
Instance Method Details
#authorize!(action) ⇒ Object
34 35 36 37 |
# File 'lib/garage/permissions.rb', line 34 def (action) exists? or raise PermissionError.new(user, action, resource_class, :not_found) permits?(action) or raise PermissionError.new(user, action, resource_class, :forbidden) end |
#deleted! ⇒ Object
43 44 45 |
# File 'lib/garage/permissions.rb', line 43 def deleted! @perms[:deleted] = true end |
#exists? ⇒ Boolean
47 48 49 |
# File 'lib/garage/permissions.rb', line 47 def exists? !@perms[:deleted] end |
#for(action) ⇒ Object
39 40 41 |
# File 'lib/garage/permissions.rb', line 39 def for(action) Permission.new(@user, action, @perms[action]) end |
#forbids!(*actions) ⇒ Object
57 58 59 60 61 |
# File 'lib/garage/permissions.rb', line 57 def forbids!(*actions) actions.each do |action| @perms[action] = :forbidden end end |
#permits!(*actions) ⇒ Object
51 52 53 54 55 |
# File 'lib/garage/permissions.rb', line 51 def permits!(*actions) actions.each do |action| @perms[action] = :ok end end |
#permits?(action) ⇒ Boolean
63 64 65 |
# File 'lib/garage/permissions.rb', line 63 def permits?(action) self.for(action).allowed? end |
#readable? ⇒ Boolean
67 68 69 |
# File 'lib/garage/permissions.rb', line 67 def readable? permits? :read end |
#writable? ⇒ Boolean
71 72 73 |
# File 'lib/garage/permissions.rb', line 71 def writable? permits? :write end |