Class: Caboose::ApplicationController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/caboose/application_controller.rb

Instance Method Summary collapse

Instance Method Details

#before_actionObject

To be overridden by the child controllers



47
48
# File 'app/controllers/caboose/application_controller.rb', line 47

def before_action      
end

#before_before_actionObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/caboose/application_controller.rb', line 7

def before_before_action
  
  # Modify the built-in params array with URL params if necessary 
  parse_url_params if Caboose.use_url_params
  
  # Try to find the page 
  @page = Page.page_with_uri(request.fullpath)
  
  session['use_redirect_urls'] = true if session['use_redirect_urls'].nil?
  
  @crumb_trail  = Caboose::Page.crumb_trail(@page)
@subnav       = {}
  @actions      = {}
  @tasks        = {}
  @page_tasks   = {}
  @is_real_page = false
  
  # Sets an instance variable of the logged in user
  @logged_in_user = logged_in_user
  
  before_action
end

#logged_in?Boolean

Returns whether or not a user is logged in

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'app/controllers/caboose/application_controller.rb', line 57

def logged_in?
  validate_token
  validate_cookie
  return true if !session["app_user"].nil? && session["app_user"] != false && session["app_user"].id != -1    
  return false
end

#logged_in_userObject

Returns the currently logged in user



88
89
90
91
92
93
94
# File 'app/controllers/caboose/application_controller.rb', line 88

def logged_in_user
  if (!logged_in?)
    return User.logged_out_user
  end
  #return nil if !logged_in?
  return session["app_user"]
end

#login_user(user, remember = false) ⇒ Object

Logs in a user



51
52
53
54
# File 'app/controllers/caboose/application_controller.rb', line 51

def (user, remember = false)
  session["app_user"] = user
  cookies.permanent[:caboose_user_id] = user.id if remember
end

#parse_url_paramsObject

Parses any parameters in the URL and adds them to the params



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/caboose/application_controller.rb', line 31

def parse_url_params      
  return if !Caboose.use_url_params      
  url = "#{request.fullpath}"
  url[0] = "" if url.starts_with?('/')      
  url = url.split('?')[0] if url.include?('?')      
  arr = url.split('/')      
  i = arr.count - 1
  while i >= 1 do
    k = arr[i-1]
    v = arr[i]
    params[k] = v if v && v.length > 0
    i = i-2
  end      
end

#reject_param(url, param) ⇒ Object

Removes a given parameter from a URL querystring



126
127
128
129
130
131
132
133
# File 'app/controllers/caboose/application_controller.rb', line 126

def reject_param(url, param)
  arr = url.split('?')
  return url if (arr.count == 1)
  qs = arr[1].split('&').reject { |pair| pair.split(/[=;]/).first == param }
  url2 = arr[0]
  url2 += "?" + qs.join('&') if qs.count > 0 
  return url2
end

#user_is_allowed(resource, action) ⇒ Object

Checks to see if a user has permission to perform the given action on the given resource. Redirects to login if not logged in. Redirects to error page with message if not allowed.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/caboose/application_controller.rb', line 100

def user_is_allowed(resource, action)
  if (!logged_in?)
    redirect_to "/login?return_url=" + URI.encode(request.fullpath)
    return false
  end
  
  @user = logged_in_user
  if (!@user.is_allowed(resource, action))
    @error = "You don't have permission to " + action + " " + resource
    render :template => "caboose/extras/error"
    return false
  end
  
  return true    
end

Checks to see if a remember me cookie value is present.



78
79
80
81
82
83
84
85
# File 'app/controllers/caboose/application_controller.rb', line 78

def validate_cookie
  if cookies[:caboose_user_id] && User.exists?(cookies[:caboose_user_id])
    user = User.find(cookies[:caboose_user_id])
    (user)
    return true
  end
  return false
end

#validate_tokenObject

Checks to see if a token is given. If so, it tries to validate the token and log the user in.



66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/caboose/application_controller.rb', line 66

def validate_token
  token = params[:token]
  return false if token.nil?
  
  user = User.validate_token(token)
  return false if user.nil?
 
  (user)
  return true
end

#var(key) ⇒ Object

def auth_or_error(message)

if (!logged_in?)
  redirect_to "/login?return_url=#{request.request_uri}" and return false
end
redirect_to "/error?message=#{message}"

end



142
143
144
145
146
# File 'app/controllers/caboose/application_controller.rb', line 142

def var(key)
  v = Var.where(:key => key).first
    return "" if v.nil?    
  return v.val
end

#verify_logged_inObject

Redirects to login if not logged in.



117
118
119
120
121
122
123
# File 'app/controllers/caboose/application_controller.rb', line 117

def verify_logged_in
  if (!logged_in?)
    redirect_to "/login?return_url=" + URI.encode(request.fullpath)
    return false
  end      
  return true    
end