Module: Croods::Resource::Authorization

Defined in:
lib/croods/resource/authorization.rb

Instance Method Summary collapse

Instance Method Details

#apply_authorization_roles!Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/croods/resource/authorization.rb', line 18

def apply_authorization_roles!
  authorization_roles.each do |authorization|
    (actions + additional_actions).each do |action|
      on = authorization[:on]
      roles = authorization[:roles]

      next if on && !on.include?(action.name)

      action.roles = roles
    end
  end
end

#authorization_rolesObject



14
15
16
# File 'lib/croods/resource/authorization.rb', line 14

def authorization_roles
  @authorization_roles ||= []
end

#authorize(*roles, on: nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/croods/resource/authorization.rb', line 6

def authorize(*roles, on: nil)
  return if roles.empty?

  on = [on] if on&.is_a?(Symbol)

  authorization_roles << { roles: roles, on: on }
end

#public_actions(*names) ⇒ Object Also known as: public_action



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/croods/resource/authorization.rb', line 31

def public_actions(*names)
  return unless names

  names = [names] if names&.is_a?(Symbol)

  extend_controller do
    skip_before_action :authenticate_user!, only: names
  end

  actions.each do |action|
    next unless names.include?(action.name)

    action.public = true
  end
end

#user_is_not_the_owner!Object



49
50
51
# File 'lib/croods/resource/authorization.rb', line 49

def user_is_not_the_owner!
  @user_is_the_owner = false
end

#user_is_the_owner?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/croods/resource/authorization.rb', line 53

def user_is_the_owner?
  return @user_is_the_owner unless @user_is_the_owner.nil?

  @user_is_the_owner = true
end