Module: Petergate::ControllerMethods

Included in:
ActionController::Base
Defined in:
lib/petergate.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

AllRest =

Look into using Controller.action_methods instead

[:show, :index, :new, :edit, :update, :create, :destroy]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/petergate.rb', line 32

def self.included(base)
  base.extend(ClassMethods)
  base.helper_method :logged_in?
  base.before_filter do 
    unless logged_in?(:admin)
      message= defined?(check_access) ? check_access : true
      if message.is_a?(String) || message == false
        if user_signed_in?
          respond_to do |format|
            format.any(:js, :json, :xml) { render nothing: true, status: :forbidden }
            format.html do
              redirect_to (request.referrer || (current_user)), :notice => message || "Permission Denied"
            end
          end
        else
          authenticate_user!
        end
      end
    end
  end
end

Instance Method Details

#logged_in?(*roles) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/petergate.rb', line 69

def logged_in?(*roles)
  current_user && (roles & current_user.roles).any?
end

#permissions(rules = {all: [:index, :show], customer: [], wiring: []}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/petergate.rb', line 54

def permissions(rules = {all: [:index, :show], customer: [], wiring: []})
  # Allows Array's of keys for he same hash.
  rules = rules.inject({}){|h, (k, v)| k.class == Array ? h.merge(Hash[k.map{|kk| [kk, v]}]) : h.merge(k => v) }
  case params[:action].to_sym
  when *(rules[:all]) # checks where the action can be seen by :all
    true
  when *(rules[:user]) # checks if the action can be seen for all users
    user_signed_in?
  when *(rules[(user_signed_in? ? current_user.role.to_sym : :all)]) # checks if action can be seen by the  current_users role. If the user isn't logged in check if it can be seen by :all
    true
  else
    false
  end
end