daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CreateAccountsClientsAndUsers < ActiveRecord::Migration
  2.   def self.up
  3.     create_table :accounts do |t|
  4.       t.column :crypted_password, :string, :limit => 255
  5.       t.column :email, :string, :null => false, :limit => 255
  6.       t.column :login, :string, :null => false, :limit => 255
  7.       t.column :name, :string, :null => false
  8.       t.column :remember_token_expires_at, :datetime
  9.       t.column :remember_token, :string, :limit => 255
  10.       t.column :salt, :string, :limit => 255
  11.       t.column :type, :string, :limit => 255, :null => false
  12.       t.timestamps
  13.     end
  14.     create_table :clients do |t|
  15.       t.column :user_id, :integer, :null => false
  16.       t.column :enable_report_access, :boolean, :default => false
  17.     end
  18.     create_table :users do |t|
  19.       t.column :admin, :boolean, :default => false, :null => false
  20.       t.column :company, :string, :limit => 255
  21.       t.column :country, :string, :limit => 255
  22.       t.column :credits, :float, :default => 0, :null => false
  23.       t.column :discount, :integer, :default => 0, :null => false
  24.       t.column :timezone, :string, :limit => 255
  25.       t.column :report_domain, :string, :limit => 255
  26.       t.column :report_logout_address, :string, :limit => 255
  27.       t.column :report_title, :string, :limit => 255
  28.     end
  29.   end
  30.  
  31.   def self.down
  32.     drop_table :accounts
  33.     drop_table :clients
  34.     drop_table :users
  35.   end
  36. end
  37.  
  38. require 'digest/sha1'
  39.  
  40. class Account < ActiveRecord::Base
  41.   acts_as_ferret :fields => [:name, :email, :login]
  42.   attr_accessible :login, :email, :password, :password_confirmation
  43.   attr_accessor :password
  44.   before_save :encrypt_password
  45.   validates_email_veracity_of :email, :timeout => 3
  46.   validates_presence_of :login, :email, :name
  47.   validates_presence_of :password_confirmation, :if => :password_required?
  48.   validates_length_of :password, :within => 4..40, :if => :password_required?
  49.   validates_confirmation_of :password, :if => :password_required?
  50.   validates_length_of :login, :within => 3..40
  51.   validates_length_of :email, :within => 3..100
  52.   validates_uniqueness_of :login, :email, :case_sensitive => false
  53.  
  54.   # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  55.   def self.authenticate(login, password)
  56.     u = find_by_login(login) # need to get the salt
  57.     u && u.authenticated?(password) ? u : nil
  58.   end
  59.  
  60.   # Encrypts some data with the salt.
  61.   def self.encrypt(password, salt)
  62.     Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  63.   end
  64.  
  65.   # Encrypts the password with the user salt
  66.   def encrypt(password)
  67.     self.class.encrypt(password, salt)
  68.   end
  69.  
  70.   def authenticated?(password)
  71.     crypted_password == encrypt(password)
  72.   end
  73.  
  74.   def remember_token?
  75.     remember_token_expires_at && Time.now.utc < remember_token_expires_at
  76.   end
  77.  
  78.   # These create and unset the fields required for remembering users between browser closes
  79.   def remember_me
  80.     remember_me_for 2.weeks
  81.   end
  82.  
  83.   def remember_me_for(time)
  84.     remember_me_until time.from_now.utc
  85.   end
  86.  
  87.   def remember_me_until(time)
  88.     self.remember_token_expires_at = time
  89.     self.remember_token            = encrypt("#{email}--#{remember_token_expires_at}")
  90.     save(false)
  91.   end
  92.  
  93.   def forget_me
  94.     self.remember_token_expires_at = nil
  95.     self.remember_token            = nil
  96.     save(false)
  97.   end
  98.  
  99.   protected
  100.  
  101.   # before filter
  102.   def encrypt_password
  103.     return if password.blank?
  104.     self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  105.     self.crypted_password = encrypt(password)
  106.   end
  107.    
  108.   def password_required?
  109.     crypted_password.blank? || !password.blank?
  110.   end
  111. end
  112.  
  113. class Client < Account
  114.   belongs_to :user
  115.   has_many :campaigns
  116.   has_many :lists
  117.   has_many :domains
  118.   validates_presence_of :login, :if => Proc.new { |thismodel| thismodel.enable_report_access == true }
  119.   validates_presence_of :password, :if => Proc.new { |thismodel| thismodel.enable_report_access == true }
  120.   validates_uniqueness_of :login, :if => Proc.new { |thismodel| thismodel.enable_report_access == true }
  121.   attr_accessible :enable_report_access
  122. end
  123.  
  124. class User < Account
  125.   attr_accessible :company, :country, :timezone
  126.   has_many :clients
  127.   has_many :campaigns, :through => :clients
  128.   has_many :lists, :through => :clients
  129.   validates_inclusion_of :discount, :in => 0..100
  130. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top