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_userObject



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
# File 'lib/isaca/rails/authorization.rb', line 10

def authorize_isaca_user
  if 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)
      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
      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
    end
  end
end

#claim_symbols(claim_params, state) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/isaca/rails/authorization.rb', line 58

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)


54
55
56
# File 'lib/isaca/rails/authorization.rb', line 54

def user_has_privilege?(user, privilege)
  user.claims.select {|c| c.privilege.to_sym == privilege}.any?
end