Module: Isaca::Rails::Authorization

Extended by:
ActiveSupport::Concern
Included in:
Controller
Defined in:
lib/isaca/rails/authorization.rb

Instance Method Summary collapse

Instance Method Details

#authorize_isaca_user(user = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/isaca/rails/authorization.rb', line 10

def authorize_isaca_user(user = nil)
  # if current_isaca_user.admin?
  if (!user.nil? && user.admin?) || (!current_isaca_user.nil? && current_isaca_user.admin?)
    if %w(index new show create update destroy).include?(action_name)
      if %w(index show).include?(action_name)
        behavior = 'read'
      else
        behavior = 'write'
      end
    else
      if %w(GET).include?(request.method)
        behavior = 'read'
      else
        behavior = 'write'
      end
    end

    privilege = "#{behavior}_#{controller_name.underscore}".to_sym
    # unless user_has_privilege?(current_isaca_user, privilege)
    unless user_has_privilege?(user, privilege)
      respond_to do |format|
        message = "#{t('isaca.rails.claims.admin_required')} Missing claim: #{privilege}."

        format.html do
          redirect_to root_path, alert: message
        end

        format.json do
          render json: {error: message}.to_json, status: :forbidden
        end

        format.any do
          redirect_to root_path, alert: message
        end
      end
    end
  else
    respond_to do |format|
      format.html do
        redirect_to root_path, alert: t('isaca.rails.claims.admin_required')
      end

      format.json do
        render json: {error: t('isaca.rails.claims.admin_required')}.to_json, status: :forbidden
      end

      format.any do
        redirect_to root_path, alert: t('isaca.rails.claims.admin_required')
      end
    end
  end
end

#claim_symbols(claim_params, state) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/isaca/rails/authorization.rb', line 71

def claim_symbols(claim_params, state)
  case state
  when :destroyable
    claim_params.select {|k,v| v == '0'}.keys.map(&:to_sym)
  when :creatable
    claim_params.select {|k,v| v == '1'}.keys.map(&:to_sym)
  else
    nil
  end
end

#user_has_privilege?(user, privilege) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'lib/isaca/rails/authorization.rb', line 63

def user_has_privilege?(user, privilege)
  unless user.nil?
    user.claims.where(privilege: privilege).any?
  else
    current_isaca_user.has_privilege?(privilege)
  end
end