2010-07-28 00:42:33 +00:00
|
|
|
require 'digest'
|
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
class Account
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
include Locomotive::Mongoid::Document
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
# devise modules
|
2011-06-10 22:24:10 +00:00
|
|
|
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :encryptable, :encryptor => :sha1
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
## attributes ##
|
|
|
|
field :name
|
2011-07-11 11:13:58 +00:00
|
|
|
field :locale, :default => Locomotive.config.default_locale.to_s or 'en'
|
2010-07-28 00:42:33 +00:00
|
|
|
field :switch_site_token
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
## validations ##
|
|
|
|
validates_presence_of :name
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
## associations ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
## callbacks ##
|
2010-05-10 22:39:52 +00:00
|
|
|
before_destroy :remove_memberships!
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
## methods ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
def sites
|
2010-07-28 00:42:33 +00:00
|
|
|
@sites ||= Site.where({ 'memberships.account_id' => self._id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_switch_site_token!
|
|
|
|
self.switch_site_token = ActiveSupport::SecureRandom.base64(8).gsub("/", "_").gsub(/=+$/, "")
|
|
|
|
self.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_using_switch_site_token(token, age = 1.minute)
|
|
|
|
return if token.blank?
|
|
|
|
self.where(:switch_site_token => token, :updated_at.gt => age.ago.utc).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_using_switch_site_token!(token, age = 1.minute)
|
|
|
|
self.find_using_switch_site_token(token, age) || raise(Mongoid::Errors::DocumentNotFound.new(self, token))
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
protected
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def password_required?
|
|
|
|
!persisted? || !password.blank? || !password_confirmation.blank?
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def remove_memberships!
|
|
|
|
self.sites.each do |site|
|
|
|
|
site.memberships.delete_if { |m| m.account_id == self._id }
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
if site.admin_memberships.empty?
|
2010-07-23 20:09:54 +00:00
|
|
|
raise I18n.t('errors.messages.needs_admin_account')
|
2010-05-10 22:39:52 +00:00
|
|
|
else
|
|
|
|
site.save
|
|
|
|
end
|
|
|
|
end
|
2010-04-13 13:24:12 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
end
|