From a22713143042bc052c48698dff70d2bd35511c13 Mon Sep 17 00:00:00 2001 From: Zach Inglis Date: Thu, 17 Apr 2008 08:43:06 -0400 Subject: [PATCH] Seperating my plugins --- README | 39 ++++++++++++++++ lib/dashboard_location.rb | 95 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 README create mode 100644 lib/dashboard_location.rb diff --git a/README b/README new file mode 100644 index 0000000..e9dcacb --- /dev/null +++ b/README @@ -0,0 +1,39 @@ +Dashboard Location +============================----version 1 beta +by Zach Inglis // zachinglis.com + +I was wanting to implement Basecamp-like urls to a project I was working on and found that account_location was only half of my soloution while dashboard_location was +only 3 quarters. Attached is a project-agnostic(no setting variables, do not worry) library for Rails. + +It is not the cleanest of libraries yet but I intend on improving it. It is only 1 day old so far. + + In controllers such as sessions_controller where you want people to be using the subdomain, use: + before_filter :should_add_subdomain_for_home_if_logged_in + before_filter :protect_controller_if_no_dashboard + + For controllers that should not be accessed through a subdomain, such as content controllers, use: + before_filter :redirect_dashboard_calls + + That is it! Just use methods like logged_in anywhere you like and it will work as you hope. + +Credits: + + While I hacked the hell out of it, credits are still due. + + David Heinemeier Hansson - for account_location - http://dev.rubyonrails.org/svn/rails/plugins/account_location/ + Derek Haynes - for his modifications - http://cleanair.highgroove.com/articles/2006/08/14/simplied-subdomain-authentication-in-ruby-on-rails + +Installation: + + Drag the dashboard_location.rb to your lib folder in your Rails directory. + + Perform: ruby ./script/generate model Home permalink:string name:string + + I use this in conjunction with Technoweenies permalink_fu to automatically generate the permalink. If you want to add the following code, else skip it: + has_permalink :name + + Add this to your signup process. Ryan address how to do similiar things here: http://railscasts.com/episodes/75 + +Cheers, hope your coding time is easier. Any suggestions or code changes you want to suggest, please email me by going to zachinglis.com/contact! + +Released under the MIT License as is account_dashboard \ No newline at end of file diff --git a/lib/dashboard_location.rb b/lib/dashboard_location.rb new file mode 100644 index 0000000..5d0b7d6 --- /dev/null +++ b/lib/dashboard_location.rb @@ -0,0 +1,95 @@ +module DashboardLocation + def self.included(controller) + controller.helper_method(:dashboard_domain, :dashboard_subdomain, + :dashboard_host, :dashboard_url, + :current_dashboard, :current_subscription, + :clean_root_url, :clean_dashboard_url) + end + +protected + def default_dashboard_subdomain + current_dashboard.permalink if current_dashboard + end + + def home_url(dashboard_subdomain = default_dashboard_subdomain, use_ssl = request.ssl?) + (use_ssl ? "https://" : "http://") + dashboard_host(dashboard_subdomain) + end + + def dashboard_host(dashboard_subdomain = default_dashboard_subdomain) + dashboard_subdomain + "." + dashboard_domain + end + + def dashboard_domain + dashboard_domain = "" + dashboard_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1 + dashboard_domain << request.domain + request.port_string + dashboard_domain + end + + def dashboard_subdomain + request.subdomains.first + end + + def current_dashboard + Home.find_by_permalink(dashboard_subdomain) + end + + def ensure_current_dashboard + return true if current_dashboard + flash[:notice] = "Sorry, but that is an invalid home." + redirect_to root_url and return false + end + + def should_add_subdomain_for_home_if_logged_in + redirect_to clean_dashboard_url if request.subdomains.empty? && logged_in? + end + + def should_redirect_if_no_subdomain + return unless request.subdomains.empty? + redirect_to logged_in? ? clean_dashboard_url : root_url + end + + def redirect_dashboard_calls + return if request.subdomains.empty? + if !logged_in? && !dashboard_subdomain.nil? && !current_dashboard.nil? + flash[:notice] = "Please login to access your account" + redirect_to login_url + elsif logged_in? && current_user.home.permalink != dashboard_subdomain + flash[:notice] = "Please only access your own home panel." + redirect_to clean_dashboard_url(current_user.home.permalink) + elsif dashboard_subdomain && current_dashboard.nil? + flash[:notice] = "We can't find where you are looking for." + redirect_to clean_root_url + elsif logged_in? && (request.path.nil? || request.path == "/") + redirect_to_dashboard_if_logged_in + end + end + + def redirect_to_dashboard_if_logged_in + redirect_to dashboard_url if logged_in? + end + + def protect_controller_if_no_dashboard + redirect_to clean_dashboard_url if dashboard_subdomain.nil? + end + + def account_domain + account_domain = "" + account_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1 + account_domain << request.domain + request.port_string + end + + def clean_dashboard_url(permalink=nil) + home_permalink = request.subdomains.empty? ? current_user.home.permalink : request.subdomains.first + home_permalink = permalink unless permalink.nil? # overwrite fu + "#{protocol}#{home_permalink}.#{account_domain}" + end + + def clean_root_url + "#{protocol}#{account_domain}" + end + + def protocol + request.ssl? ? "https://" : "http://" + end +end \ No newline at end of file