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, BillingAddressesController, BlockTypeCategoriesController, BlockTypeSourcesController, BlockTypeStoreController, BlockTypesController, BlocksController, CalendarsController, CartController, CategoriesController, CheckoutController, DomainsController, EventGroupsController, EventsController, FontsController, GiftCardsController, GoogleSpreadsheetsController, LineItemsController, LoginController, LoginLogsController, LogoutController, MediaCategoriesController, MediaController, ModalController, ModificationValueInputFieldsController, ModificationValuesController, ModificationsController, MyAccountController, MyAccountLineItemsController, MyAccountOrdersController, OrderPackagesController, OrdersController, PageCustomFieldValuesController, PageCustomFieldsController, PagePermissionsController, PagesController, PermissionsController, PostCategoriesController, PostCustomFieldValuesController, PostCustomFieldsController, PostsController, ProductImagesController, ProductsController, RedirectsController, RegisterController, RetargetingController, ReviewsController, RolesController, SettingsController, ShippingAddressesController, ShippingPackagesController, SitesController, SmtpController, SnsController, SocialController, StackableGroupsController, StationController, StoreController, UsersController, VariantsController, VendorsController
Instance Method Summary collapse
- #add_ga_event(cat, action, label = nil, value = nil) ⇒ Object
- #admin_add ⇒ Object
- #admin_bulk_add ⇒ Object
- #admin_bulk_delete ⇒ Object
- #admin_bulk_update ⇒ Object
- #admin_delete ⇒ Object
- #admin_edit ⇒ Object
-
#admin_index ⇒ Object
Standard methods used by model binder.
- #admin_json ⇒ Object
- #admin_json_single ⇒ Object
- #admin_update ⇒ Object
-
#before_action ⇒ Object
To be overridden by the child controllers.
- #before_before_action ⇒ Object
-
#hashify_query_string ⇒ Object
Redirects/Converts querystrings into hashes.
-
#init_cart ⇒ Object
Initialize the cart in the session.
-
#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.
-
#under_construction_or_forwarding_domain? ⇒ Boolean
Make sure we’re not under construction or on a forwarded domain.
-
#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
#add_ga_event(cat, action, label = nil, value = nil) ⇒ Object
90 91 92 93 94 95 96 |
# File 'app/controllers/caboose/application_controller.rb', line 90 def add_ga_event(cat, action, label = nil, value = nil) # Category String Required Typically the object that was interacted with (e.g. button) # Action String Required The type of interaction (e.g. click) # Label String Option Useful for categorizing events (e.g. nav buttons) # Value Number Option Values must be non-negative. Useful to pass counts (e.g. 4 times) @ga_events << [cat, action, label, value] end |
#admin_add ⇒ Object
279 |
# File 'app/controllers/caboose/application_controller.rb', line 279 def admin_add() raise 'This method should be overridden.' end |
#admin_bulk_add ⇒ Object
280 |
# File 'app/controllers/caboose/application_controller.rb', line 280 def admin_bulk_add() raise 'This method should be overridden.' end |
#admin_bulk_delete ⇒ Object
282 |
# File 'app/controllers/caboose/application_controller.rb', line 282 def admin_bulk_delete() raise 'This method should be overridden.' end |
#admin_bulk_update ⇒ Object
278 |
# File 'app/controllers/caboose/application_controller.rb', line 278 def admin_bulk_update() raise 'This method should be overridden.' end |
#admin_delete ⇒ Object
281 |
# File 'app/controllers/caboose/application_controller.rb', line 281 def admin_delete() raise 'This method should be overridden.' end |
#admin_edit ⇒ Object
276 |
# File 'app/controllers/caboose/application_controller.rb', line 276 def admin_edit() raise 'This method should be overridden.' end |
#admin_index ⇒ Object
Standard methods used by model binder
273 |
# File 'app/controllers/caboose/application_controller.rb', line 273 def admin_index() raise 'This method should be overridden.' end |
#admin_json ⇒ Object
274 |
# File 'app/controllers/caboose/application_controller.rb', line 274 def admin_json() raise 'This method should be overridden.' end |
#admin_json_single ⇒ Object
275 |
# File 'app/controllers/caboose/application_controller.rb', line 275 def admin_json_single() raise 'This method should be overridden.' end |
#admin_update ⇒ Object
277 |
# File 'app/controllers/caboose/application_controller.rb', line 277 def admin_update() raise 'This method should be overridden.' end |
#before_action ⇒ Object
To be overridden by the child controllers
118 119 |
# File 'app/controllers/caboose/application_controller.rb', line 118 def before_action end |
#before_before_action ⇒ Object
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 53 54 55 56 57 58 59 60 61 62 |
# File 'app/controllers/caboose/application_controller.rb', line 11 def before_before_action # Modify the built-in params array with URL params if necessary parse_url_params if Caboose.use_url_params @use_page_cache = !request.fullpath.starts_with?('/admin') # Get the site we're working with @domain = Domain.where(:domain => request.host_with_port).first @site = @domain ? @domain.site : nil if @domain.nil? || @site.nil? Caboose.log("Error: No site configured for #{request.host_with_port}") end # Set the site in any mailers CabooseMailer.site = @site # Make sure someone is logged in if !logged_in? && @site elo = User.logged_out_user(@site.id) login_user(elo) if 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 @request = request @page = Page.new @crumbtrail = Crumbtrail.new @subnav = {} @actions = {} @tasks = {} @page_tasks = {} @is_real_page = false @ga_events = [] #if @find_page @page = Page.page_with_uri(request.host_with_port, 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 # Initialize the card init_cart if @site && @site.use_store && !@domain.under_construction before_action end |
#hashify_query_string ⇒ Object
Redirects/Converts querystrings into hashes
264 265 266 267 268 269 270 |
# File 'app/controllers/caboose/application_controller.rb', line 264 def hashify_query_string if request.query_string && request.query_string.length > 0 redirect_to request.url.gsub('?', '#') return true end return false end |
#init_cart ⇒ Object
Initialize the cart in the session
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/controllers/caboose/application_controller.rb', line 65 def init_cart # Check if the cart ID is defined and that it exists in the database create_new_order = false if session[:cart_id] @order = Caboose::Order.where(:id => session[:cart_id]).first create_new_order = true if @order.nil? || @order.status != 'cart' else create_new_order = true end if create_new_order # Create an order to associate with the session @order = Caboose::Order.new @order.site_id = @site ? @site.id : nil @order.status = Caboose::Order::STATUS_CART @order.financial_status = Caboose::Order::STATUS_PENDING @order.date_created = DateTime.now @order.referring_site = request.env['HTTP_REFERER'] @order.landing_page = request.fullpath @order.landing_page_ref = params[:ref] || nil @order.save # Save the cart ID in the session session[:cart_id] = @order.id end end |
#logged_in? ⇒ Boolean
Returns whether or not a user is logged in
142 143 144 145 146 147 |
# File 'app/controllers/caboose/application_controller.rb', line 142 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(@site.id) return false end |
#logged_in_user ⇒ Object
Returns the currently logged in user
175 176 177 178 179 180 181 |
# File 'app/controllers/caboose/application_controller.rb', line 175 def logged_in_user if (!logged_in?) return User.logged_out_user(@site.id) end #return nil if !logged_in? return Caboose::User.where(:id => session["app_user"].id).first end |
#login_user(user, remember = false) ⇒ Object
Logs in a user
129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/controllers/caboose/application_controller.rb', line 129 def login_user(user, remember = false) session["app_user"] = Caboose::StdClass.new({ :id => user.id , :site_id => user.site_id , :first_name => user.first_name , :last_name => user.last_name , :username => user.username , :email => user.email }) .permanent[:caboose_user_id] = user.id if remember end |
#logout_user ⇒ Object
Logs a user out
122 123 124 125 126 |
# File 'app/controllers/caboose/application_controller.rb', line 122 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/controllers/caboose/application_controller.rb', line 99 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
241 242 243 244 245 246 247 248 |
# File 'app/controllers/caboose/application_controller.rb', line 241 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 |
#under_construction_or_forwarding_domain? ⇒ Boolean
Make sure we’re not under construction or on a forwarded domain
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'app/controllers/caboose/application_controller.rb', line 285 def under_construction_or_forwarding_domain? d = Caboose::Domain.where(:domain => request.host_with_port).first if d.nil? Caboose.log("Could not find domain for #{request.host_with_port}\nAdd this domain to the caboose site.") elsif d.under_construction == true if d.site.under_construction_html && d.site.under_construction_html.strip.length > 0 render :text => d.site.under_construction_html else render :file => 'caboose/application/under_construction', :layout => false end return true # See if we're on a forwarding domain elsif d.primary == false && d.forward_to_primary == true pd = d.site.primary_domain if pd && pd.domain != request.host url = "#{request.protocol}#{pd.domain}" if d.forward_to_uri && d.forward_to_uri.strip.length > 0 url << d.forward_to_uri elsif request.fullpath && request.fullpath.strip.length > 0 && request.fullpath.strip != '/' url << request.fullpath end redirect_to url return true end # Check for a 301 redirect else new_url = PermanentRedirect.match(@site.id, request.fullpath) if new_url redirect_to new_url, :status => 301 return true end end return false 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.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'app/controllers/caboose/application_controller.rb', line 189 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.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'app/controllers/caboose/application_controller.rb', line 215 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.
163 164 165 166 167 168 169 170 171 172 |
# File 'app/controllers/caboose/application_controller.rb', line 163 def if [:caboose_user_id] user = User.where(:id => [:caboose_user_id]).first if user login_user(user) return true end 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.
151 152 153 154 155 156 157 158 159 160 |
# File 'app/controllers/caboose/application_controller.rb', line 151 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
257 258 259 260 261 |
# File 'app/controllers/caboose/application_controller.rb', line 257 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.
232 233 234 235 236 237 238 |
# File 'app/controllers/caboose/application_controller.rb', line 232 def verify_logged_in if !logged_in? redirect_to "/modal/login?return_url=" + URI.encode(request.fullpath) return false end return true end |