Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

app/models/user.rb

   1  
   2  require 'digest/sha1'
   3  
   4  # this model expects a certain database layout and its based on the name/login pattern.
   5  class User < ActiveRecord::Base
   6          has_and_belongs_to_many :groups,
   7                  :class_name => 'Group',
   8                  :join_table => 'users_groups'
   9  
  10          def self.authenticate(username, password)
  11                  @user = find(:first, :conditions => ["username = ? AND password = ? and confirmed = ?", username, sha1(password), true])
  12          end
  13  
  14          def remember_me
  15                  self.remember_token_expires = 2.weeks.from_now
  16                  self.remember_token = Digest::SHA1.hexdigest("GFDHDFUHFJI&&%ET%&*%^£FESER^&J&IJR%TXEYFGU(*I$R^%E&DU&-#{self.email}#{self.remember_token_expires}")
  17                  self.save_with_validation(false)
  18          end
  19  
  20          def forget_me
  21                  self.remember_token_expires = nil
  22                  self.remember_token = nil
  23                  self.save_with_validation(false)
  24          end
  25  
  26          def reset_password
  27                  tmppwd = self.generate_password
  28                  write_attribute("password", self.class.sha1(tmppwd))
  29                  self.save_with_validation(false)
  30                  tmppwd
  31          end
  32  
  33          protected
  34  
  35          def generate_password
  36                  chars = ("a".."z").to_a + ("1".."9").to_a
  37                  Array.new(6, '').collect{chars[rand(chars.size)]}.join
  38          end
  39  
  40          def self.sha1(pass)
  41                  Digest::SHA1.hexdigest(pass + "FSDT%^Y&JTFHY^&*IFY^H&&*(T&&RG%U&*I^HFGCDUI*TUF^HYU&*Y&T^F&*^&FUH")
  42          end
  43  
  44          before_create :crypt_password
  45  
  46          def crypt_password
  47                  write_attribute("password", self.class.sha1(password))
  48          end
  49  
  50          validates_length_of :username, :within => 4..24
  51          validates_length_of :password, :within => 6..32
  52          validates_presence_of :username, :password, :password_confirmation
  53          validates_uniqueness_of :username, :on => :create
  54          validates_confirmation_of :password, :on => :create
  55  
  56          validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
  57          validates_format_of :username, :with => /^(\w*)$/i
  58          validates_format_of :name, :with => /^([\w ]*)$/i
  59  
  60          validates_presence_of :email, :name
  61          validates_length_of :name, :within => 6..32
  62          validates_uniqueness_of :email, :on => :create
  63  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS