Class: Caboose::ApplicationController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- Caboose::ApplicationController
- Defined in:
- app/controllers/caboose/application_controller.rb
Direct Known Subclasses
ApplicationController, AbOptionsController, AbVariantsController, AdminController, BlockTypeCategoriesController, BlockTypeSourcesController, BlockTypesController, BlocksController, LoginController, LogoutController, ModalController, PagePermissionsController, PagesController, PermissionsController, PostsController, RegisterController, RolesController, SettingsController, StationController, UsersController
Instance Method Summary collapse
-
#before_action ⇒ Object
To be overridden by the child controllers.
- #before_before_action ⇒ Object
-
#logged_in? ⇒ Boolean
Returns whether or not a user is logged in.
-
#logged_in_user ⇒ Object
Returns the currently logged in user.
-
#login_user(user, remember = false) ⇒ Object
Logs in a user.
-
#logout_user ⇒ Object
Logs a user out.
-
#parse_url_params ⇒ Object
Parses any parameters in the URL and adds them to the params.
-
#reject_param(url, param) ⇒ Object
Removes a given parameter from a URL querystring.
-
#user_is_allowed(resource, action) ⇒ Object
DEPRECATED: Use user_is_allowed_to(action, resource).
-
#user_is_allowed_to(action, resource) ⇒ Object
Checks to see if a user has permission to perform action on resource.
-
#validate_cookie ⇒ Object
Checks to see if a remember me cookie value is present.
-
#validate_token ⇒ Object
Checks to see if a token is given.
-
#var(name) ⇒ Object
def auth_or_error(message) if (!logged_in?) redirect_to “/login?return_url=#requestrequest.request_uri” and return false end redirect_to “/error?message=#message” end.
-
#verify_logged_in ⇒ Object
Redirects to login if not logged in.
Instance Method Details
#before_action ⇒ Object
To be overridden by the child controllers
66 67 |
# File 'app/controllers/caboose/application_controller.rb', line 66 def before_action end |
#before_before_action ⇒ 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 |
# File 'app/controllers/caboose/application_controller.rb', line 10 def before_before_action # Modify the built-in params array with URL params if necessary parse_url_params if Caboose.use_url_params # Make sure someone is logged in if !logged_in? elo = User.find(User::LOGGED_OUT_USER_ID) login_user(elo) end session['use_redirect_urls'] = true if session['use_redirect_urls'].nil? # Initialize AB Testing AbTesting.init(request.[:id]) if Caboose.use_ab_testing # Try to find the page @page = Page.new @crumb_trail = [] @subnav = {} @actions = {} @tasks = {} @page_tasks = {} @is_real_page = false #if @find_page @page = Page.page_with_uri(request.fullpath) @crumb_trail = Caboose::Page.crumb_trail(@page) #end # 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
83 84 85 86 87 88 |
# File 'app/controllers/caboose/application_controller.rb', line 83 def logged_in? validate_token return true if !session["app_user"].nil? && session["app_user"] != false && session["app_user"].id != -1 && session["app_user"].id != User::LOGGED_OUT_USER_ID return false end |
#logged_in_user ⇒ Object
Returns the currently logged in user
114 115 116 117 118 119 120 |
# File 'app/controllers/caboose/application_controller.rb', line 114 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
77 78 79 80 |
# File 'app/controllers/caboose/application_controller.rb', line 77 def login_user(user, remember = false) session["app_user"] = user .permanent[:caboose_user_id] = user.id if remember end |
#logout_user ⇒ Object
Logs a user out
70 71 72 73 74 |
# File 'app/controllers/caboose/application_controller.rb', line 70 def logout_user .delete(:caboose_user_id) session["app_user"] = nil reset_session end |
#parse_url_params ⇒ Object
Parses any parameters in the URL and adds them to the params
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/controllers/caboose/application_controller.rb', line 47 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] if v && v.length > 0 v = v.gsub('%20', ' ') params[k] = v end i = i-2 end end |
#reject_param(url, param) ⇒ Object
Removes a given parameter from a URL querystring
180 181 182 183 184 185 186 187 |
# File 'app/controllers/caboose/application_controller.rb', line 180 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
DEPRECATED: Use user_is_allowed_to(action, resource)
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.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/controllers/caboose/application_controller.rb', line 128 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 @return_url = request.fullpath render :template => "caboose/extras/error" return false end return true end |
#user_is_allowed_to(action, resource) ⇒ Object
Checks to see if a user has permission to perform action on resource
Redirects to login if not logged in Redirects to error page with message if not allowed
useful for creating super-readable code, for example:
> return unless user_is_allowed_to 'edit', 'pages'
Even your mom could read that code.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'app/controllers/caboose/application_controller.rb', line 154 def user_is_allowed_to(action, resource) unless logged_in? redirect_to "/login?return_url=" + URI.encode(request.fullpath) return false end @user = logged_in_user unless @user.is_allowed(resource, action) @error = "You don't have permission to #{action} #{resource}" @return_url = request.fullpath render :template => "caboose/extras/error" return false end return true end |
#validate_cookie ⇒ Object
Checks to see if a remember me cookie value is present.
104 105 106 107 108 109 110 111 |
# File 'app/controllers/caboose/application_controller.rb', line 104 def if [:caboose_user_id] && User.exists?([:caboose_user_id]) user = User.find([:caboose_user_id]) login_user(user) return true end return false end |
#validate_token ⇒ Object
Checks to see if a token is given. If so, it tries to validate the token and log the user in.
92 93 94 95 96 97 98 99 100 101 |
# File 'app/controllers/caboose/application_controller.rb', line 92 def validate_token token = params[:token] return false if token.nil? user = User.validate_token(token) return false if user.nil? login_user(user) return true end |
#var(name) ⇒ 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=#{}"
end
196 197 198 199 200 |
# File 'app/controllers/caboose/application_controller.rb', line 196 def var(name) s = Setting.where(:name => name).first return "" if s.nil? return s.value end |
#verify_logged_in ⇒ Object
Redirects to login if not logged in.
171 172 173 174 175 176 177 |
# File 'app/controllers/caboose/application_controller.rb', line 171 def verify_logged_in if !logged_in? redirect_to "/modal/login?return_url=" + URI.encode(request.fullpath) return false end return true end |