Class: Caboose::ApplicationController
- Inherits:
-
ActionController::Base
- Object
- ActionController::Base
- Caboose::ApplicationController
- Defined in:
- app/controllers/caboose/application_controller.rb
Direct Known Subclasses
AdminController, LoginController, LogoutController, 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.
-
#reject_param(url, param) ⇒ Object
Removes a given parameter from a URL querystring.
-
#user_is_allowed(resource, action) ⇒ Object
Checks to see if a user has permission to perform the given action on the given 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(key) ⇒ 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
28 29 |
# File 'app/controllers/caboose/application_controller.rb', line 28 def before_action end |
#before_before_action ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/controllers/caboose/application_controller.rb', line 7 def before_before_action # 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
38 39 40 41 42 43 |
# File 'app/controllers/caboose/application_controller.rb', line 38 def logged_in? validate_token return true if !session["app_user"].nil? && session["app_user"] != false && session["app_user"].id != -1 return false end |
#logged_in_user ⇒ Object
Returns the currently logged in user
69 70 71 72 73 74 75 |
# File 'app/controllers/caboose/application_controller.rb', line 69 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
32 33 34 35 |
# File 'app/controllers/caboose/application_controller.rb', line 32 def login_user(user, remember = false) session["app_user"] = user .permanent[:caboose_user_id] = user.id if remember end |
#reject_param(url, param) ⇒ Object
Removes a given parameter from a URL querystring
107 108 109 110 111 112 113 114 |
# File 'app/controllers/caboose/application_controller.rb', line 107 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.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/controllers/caboose/application_controller.rb', line 81 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 |
#validate_cookie ⇒ Object
Checks to see if a remember me cookie value is present.
59 60 61 62 63 64 65 66 |
# File 'app/controllers/caboose/application_controller.rb', line 59 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.
47 48 49 50 51 52 53 54 55 56 |
# File 'app/controllers/caboose/application_controller.rb', line 47 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(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=#{}"
end
123 124 125 126 127 |
# File 'app/controllers/caboose/application_controller.rb', line 123 def var(key) v = Var.where(:key => key).first return "" if v.nil? return v.val end |
#verify_logged_in ⇒ Object
Redirects to login if not logged in.
98 99 100 101 102 103 104 |
# File 'app/controllers/caboose/application_controller.rb', line 98 def verify_logged_in if (!logged_in?) redirect_to "/login?return_url=" + URI.encode(request.fullpath) return false end return true end |