Skip to content

An example Rails 3 app with basecamp like subdomains and authentication (using Devise), Monodb and Mongoid.

Notifications You must be signed in to change notification settings

millisami/rails3-subdomain-devise

 
 

Repository files navigation

Fork of Rails3-Subdomain-Devise modified for basecamp-like subdomaims, but using Mongoid and Mongodb.

Use this project as a starting point for a Rails 3 application that uses basecamp-like subdomains and authentication. User management and authentication is implemented using Devise.

This is a stab of modifying Rails3-Subdomain-Devise using Mongoid wit Mongodb.

The major changes are:

User and Subdomain documents


class Subdomain
  include Mongoid::Document
  include Mongoid::FindBy

  field :name

  references_many :users, :dependent => :destroy

  # has_friendly_id :name, :use_slug => true, :strip_non_ascii => true
  validates_uniqueness_of :name, :case_sensitive => false
  validates_presence_of :name
end

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name
  field :email

  referenced_in :subdomain
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates_presence_of :name, :subdomain_name
  validates_uniqueness_of  :email, :case_sensitive => false
  attr_accessor :subdomain_name
  attr_accessible :name, :subdomain_name, :email, :password, :password_confirmation, :remember_me
  # has_friendly_id :subdomain_name, :use_slug => true, :strip_non_ascii => true
  before_create :create_subdomain

  private
  def create_subdomain
    subdomain_exist = Subdomain.find_by_name(self.subdomain_name)
    if subdomain_exist
      self.subdomain = subdomain_exist
    else
      self.subdomain = Subdomain.find_or_create_by(:name => self.subdomain_name, :user_id => self.id)
    end
  end
end	

The majority of the subdomains controller has been stubbed in that “sign_up” will create a subdomain if it does not exist. Code was not removed in that an Admin user might want to delete subdomains and associated users.

The model names were not changed, but take on different roles. List of other changes:

  • Users#index probably should not be there, but it lists all users
  • Site would be the home for the site and currently list users for the subdomain
  • Subdomain still has user_id and indicates who created the subdomain (assume subdomain admin)
  • A helper method “current_subdomain” was added to controllers/application.rb to check if subdomain exists
  • Another helper method “check_my_domain(subdomain)” will check a passed subdomain against the current domain. Subdomain is kind of the root table, all major tables should be belong to subdomain and this check is there to prevent url modification (edit member not belonging to your domain). It will redirect to an “opps” action in the site controller.
  • Sign-in has been removed if no subdomain exists. If you modifiy the url and sign_in without a subdomain. It will log you in, but then immediately log you out and redirect to the subdomain sign_in form. I can’t seem to get the flash notice to work in this area.
    • I’ve made a few attempts to sign-in at the root level and redirect to the subdomain and create a session there, but failed! If anyone has any ideas on how to do this with devise, you are more than welcome to give it a try.
    • Send sign-in parameters to subdomain with a CURL post ofter sign-out of root domain?
  • If you register without a subdomain, it will be created when the user is created. If it exists, you are added as a user of that subdomain (TODO this should be fixed to reject adding user from sign_up without subdomain, if it exists)
  • If you register in a subdomain, you are added as a the users of that subdomain.

P.S. I am not a novice at Rails, but don’t consider myself experienced.

Installation (I think!)

  • git clone
  • bundle install
  • rake db:seed

Rails3-Subdomain-Devise

Please visit Rails3-Subdomain-Devise for the excellent step-by-step tutorial. You should still be able to follow the tutorial for this fork if you address the above changes.

License

Public Domain Dedication

This work is a compilation and derivation from other previously released works. With the exception of various included works, which may be restricted by other licenses, the author or authors of this code dedicate any and all copyright interest in this code to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this code under copyright law.

About

An example Rails 3 app with basecamp like subdomains and authentication (using Devise), Monodb and Mongoid.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • Ruby 90.0%
  • JavaScript 10.0%