Class: Garage::Permissions

Inherits:
Object
  • Object
show all
Defined in:
lib/garage/permissions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, permissions = { read: :forbidden, write: :forbidden })
  @user = user
  @resource_class = resource_class
  @perms = permissions
end

Instance Attribute Details

#resource_classObject

Returns the value of attribute resource_class.



26
27
28
# File 'lib/garage/permissions.rb', line 26

def resource_class
  @resource_class
end

#userObject

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 authorize!(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

Returns:

  • (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

Returns:

  • (Boolean)


63
64
65
# File 'lib/garage/permissions.rb', line 63

def permits?(action)
  self.for(action).allowed?
end

#readable?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/garage/permissions.rb', line 67

def readable?
  permits? :read
end

#writable?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/garage/permissions.rb', line 71

def writable?
  permits? :write
end