Guest User

Untitled

a guest
May 2nd, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. require 'digest/sha1'
  2. class Usuari < ActiveRecord::Base
  3.  
  4. acts_as_mappable :default_units => :kms,
  5. :distance_field_name => :distance
  6.  
  7. #CONSTANTS
  8. CP_MAX_LENGTH = 5
  9.  
  10. acts_as_ferret :fields => {
  11. :email=>{:boost=>15},
  12. :nom=>{:boost=>13},
  13. :codi_postal=>{:boost=>10}
  14. }
  15.  
  16. # Virtual attribute for the unencrypted password
  17. attr_accessor :password
  18.  
  19. #VALIDATIONS
  20. validates_presence_of :email, :message=>'La dirección de correo no puede estar en blanco.'
  21. validates_presence_of :nom, :message=>'Debes introducir tu nombre.'
  22.  
  23. validates_presence_of :password,
  24. :message=>'La contraseña no puede estar en blanco',
  25. :if => :password_required?
  26.  
  27. validates_presence_of :password_confirmation,
  28. :message=>'La confirmación de contraseña no puede estar en blanco',
  29. :if => :password_required?
  30.  
  31. validates_length_of :password,
  32. :within => Con::STRING_RANGE_LENGTH,
  33. :if => :password_required?
  34.  
  35. validates_confirmation_of :password,
  36. :if => :password_required?
  37.  
  38. validates_length_of :nom,
  39. :within => Con::STRING_RANGE_LENGTH
  40.  
  41. validates_length_of :email,
  42. :within => Con::STRING_RANGE_LENGTH
  43.  
  44. validates_length_of :codi_postal,
  45. :is => CP_MAX_LENGTH
  46.  
  47. validates_uniqueness_of :email,
  48. :message => t('Ya existe un usuario registrado con este email'),
  49. :case_sensitive => false
  50.  
  51. validates_format_of :email,
  52. :with => /(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/i
  53.  
  54. validates_inclusion_of :enabled,
  55. :in => [true, false]
  56.  
  57. #RELATIONS
  58. belongs_to :rol
  59. has_many :anunciants
  60. has_many :ofertes, :order => 'id'
  61. has_many :demandes, :order => 'id'
  62.  
  63. has_and_belongs_to_many :favorits, :class_name => 'Anunciant', :join_table => "favorits"
  64.  
  65. #FUNCTIONS
  66. before_save :encrypt_password
  67. before_create :make_activation_code
  68.  
  69. # prevents a user from submitting a crafted form that bypasses activation
  70. # anything else you want your usuari to change should be added here.
  71. attr_accessible :nom, :email, :password, :password_confirmation, :codi_postal, :rol_id
  72.  
  73. class ActivationCodeNotFound < StandardError; end
  74. class AlreadyActivated < StandardError
  75. attr_reader :user, :message;
  76. def initialize(user, message=nil)
  77. @message, @user = message, user
  78. end
  79. end
  80.  
  81. # Finds the user with the corresponding activation code, activates their account and returns the user.
  82. # Raises:
  83. # +User::ActivationCodeNotFound+ if there is no user with the corresponding activation code
  84. # +User::AlreadyActivated+ if the user with the corresponding activation code has already activated their account
  85. def self.find_and_activate!(activation_code)
  86. raise ArgumentError if activation_code.nil?
  87. user = find_by_activation_code(activation_code)
  88. raise ActivationCodeNotFound if !user
  89. user.send(:activate!)
  90. user
  91. end
  92.  
  93. def active?
  94. # the presence of an activation date means they have activated
  95. !activated_at.nil?
  96. end
  97.  
  98. # Returns true if the user has just been activated.
  99. def pending?
  100. @activated
  101. end
  102.  
  103. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  104. def self.authenticate(email, password)
  105. u = find :first, :conditions => ['email = ? and enabled = ?', email, true] # need to get the salt
  106. u && u.authenticated?(password) ? u : nil
  107. end
  108.  
  109. # Encrypts some data with the salt.
  110. def self.encrypt(password, salt)
  111. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  112. end
  113.  
  114. # Encrypts the password with the user salt
  115. def encrypt(password)
  116. self.class.encrypt(password, salt)
  117. end
  118.  
  119. def authenticated?(password)
  120. crypted_password == encrypt(password)
  121. end
  122.  
  123. def remember_token?
  124. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  125. end
  126.  
  127. # These create and unset the fields required for remembering users between browser closes
  128. def remember_me
  129. remember_me_for 2.weeks
  130. end
  131.  
  132. def remember_me_for(time)
  133. remember_me_until time.from_now.utc
  134. end
  135.  
  136. def remember_me_until(time)
  137. self.remember_token_expires_at = time
  138. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  139. save(false)
  140. end
  141.  
  142. def forget_me
  143. self.remember_token_expires_at = nil
  144. self.remember_token = nil
  145. save(false)
  146. end
  147.  
  148. def forgot_password
  149. @forgotten_password = true
  150. self.make_password_reset_code
  151. end
  152.  
  153. def reset_password
  154. update_attribute(:password_reset_code, nil)
  155. @reset_password = true
  156. end
  157.  
  158. #used in user_observer
  159. def recently_forgot_password?
  160. @forgotten_password
  161. end
  162.  
  163. def recently_reset_password?
  164. @reset_password
  165. end
  166.  
  167. def self.find_for_forget(email)
  168. find :first, :conditions => ['email = ? AND activation_code IS NOT NULL', email]
  169. end
  170.  
  171. def has_rol?(rol)
  172. self.rol.nom == rol
  173. end
  174.  
  175. def is_anunciant?
  176. Anunciant.find(:first, :conditions => {:usuari_id => id})
  177. end
  178.  
  179. def geocode
  180. location = GeoKit::Geocoders::GoogleGeocoder.geocode(self[:codi_postal].to_s+', Spain')
  181. if location.success==true
  182. self[:lat]=location.lat
  183. self[:long]=location.lng
  184. end
  185. end
  186.  
  187. protected
  188. # before filter
  189. def encrypt_password
  190. return if password.blank?
  191. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{nom}--") if new_record?
  192. self.crypted_password = encrypt(password)
  193. end
  194.  
  195. def password_required?
  196. crypted_password.blank? || !password.blank?
  197. end
  198.  
  199. def make_activation_code
  200. self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  201. end
  202.  
  203. def make_password_reset_code
  204. self.password_reset_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  205. end
  206.  
  207. private
  208. def activate!
  209. @activated = true
  210. self.update_attribute(:activated_at, Time.now.utc)
  211. end
  212.  
  213. def before_save
  214. geocode
  215. end
  216. end
Add Comment
Please, Sign In to add comment