Guest User

Untitled

a guest
May 24th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. class Account < ActiveRecord::Base
  2.  
  3. has_many :users, :through => :groups, :dependent => :destroy
  4. has_many :changerequests, :dependent => :destroy
  5. has_many :tasks, :through => :changerequests, :dependent => :destroy
  6. has_many :comments, :through => :changerequests, :dependent => :destroy
  7. has_many :groups, :dependent => :destroy
  8.  
  9. validates_format_of :name, :with => /^[a-zA-Z0-9]*?$/, :message => 'accepts letters and numbers only.'
  10. validates_presence_of :name
  11. validates_uniqueness_of :name
  12. validates_associated :groups
  13.  
  14. def before_save
  15. # Transform all account names (subdomains) to lowercase
  16. self.name.downcase!
  17. end
  18. end
  19.  
  20. class Group < ActiveRecord::Base
  21. has_many :users, :dependent => :destroy
  22. belongs_to :account
  23.  
  24. validates_presence_of :name
  25. validates_uniqueness_of :name, :scope => :account_id
  26. validates_associated :users
  27.  
  28. end
  29.  
  30. class User < ActiveRecord::Base
  31.  
  32. has_many :changerequests
  33. has_many :tasks
  34. has_many :comments, :dependent => :destroy
  35. belongs_to :group
  36. has_one :account, :through => :groups
  37.  
  38. validates_presence_of :first, :last, :email, :username
  39. validate :uniqueness_of_username
  40. attr_accessor :password_confirmation
  41. validates_confirmation_of :password
  42. validate :password_non_blank
  43.  
  44. def self.authenticate(username, password)
  45. user = self.find_by_username(username.downcase)
  46. if user
  47. expected_password = encrypted_password(password, user.salt)
  48. if user.hashed_password != expected_password
  49. user = nil
  50. end
  51. end
  52. user
  53. end
  54.  
  55. # 'password' is a virtual attribute
  56. def password
  57. @password
  58. end
  59.  
  60. def password=(pwd)
  61. @password = pwd
  62. return if pwd.blank?
  63. create_new_salt
  64. self.hashed_password = User.encrypted_password(self.password, self.salt)
  65. end
  66.  
  67. def before_save
  68. # Transition all usernames to lowercase
  69. self.username.downcase!
  70. end
  71.  
  72. private
  73.  
  74. def password_non_blank
  75. errors.add_to_base("Missing password" ) if hashed_password.blank?
  76. end
  77.  
  78. def create_new_salt
  79. self.salt = self.object_id.to_s + rand.to_s
  80. end
  81.  
  82. def self.encrypted_password(password, salt)
  83. string_to_hash = password + "wibble" + salt
  84. Digest::SHA1.hexdigest(string_to_hash)
  85. end
  86.  
  87. protected
  88. def uniqueness_of_username
  89. # Ensure username is unique per account
  90. @account = Account.find(self.group.account_id) if self.group_id
  91. if @account
  92. errors.add(:username, ' is already taken.') if @account.users.find(:all, :conditions => { :username => :username })
  93. end
  94. end
  95. end
Add Comment
Please, Sign In to add comment