Guest User

Untitled

a guest
Mar 15th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 KB | None | 0 0
  1. ## error
  2.  
  3. == 5 AddDefaultAdmin: migrating ===============================================
  4. rake aborted!
  5. uninitialized constant User::Locations
  6.  
  7. ## 005_add_default_admin.rb
  8.  
  9. class AddDefaultAdmin < ActiveRecord::Migration
  10. def self.up
  11. user = User.new
  12. user.login = "evan"
  13. user.email = "evan.alter@gmail.com"
  14. user.password = "test"
  15. user.password_confirmation = "test"
  16. user.default_location = Location.new(:zipcode => "78704", :name => "Default")
  17. user.profile = Profile.new
  18. user.roles << Role.find_by_name('admin')
  19. user.save(false)
  20. user.activate!
  21.  
  22. end
  23.  
  24. def self.down
  25. user = User.find_by_login('evan')
  26. user.default_location.destory
  27. user.profile.destroy
  28. user.roles.destory
  29. user.destory
  30. end
  31. end
  32.  
  33. ## location.rb
  34.  
  35. class Location < ActiveRecord::Base
  36. belongs_to :user
  37.  
  38. acts_as_mappable
  39. before_validation_on_create :geocode_address
  40.  
  41. validates_uniqueness_of :name, :message => "You already have a location with that same name", :scope => "user_id"
  42. #validates_presence_of :lat, :lng
  43.  
  44. private
  45.  
  46. def geocode_address
  47. field = :full_address
  48. if is_zipcode?
  49. address = self.zipcode
  50. field = :zipcode
  51. else
  52. return invalid_zipcode if self.full_address.nil?
  53. end
  54. geo=GeoKit::Geocoders::MultiGeocoder.geocode(address)
  55. if geo.success
  56. self.lat = geo.lat
  57. self.lng = geo.lng
  58. self.provider = geo.provider
  59. self.street_address ||= geo.street_address
  60. self.city ||= geo.city
  61. self.state ||= geo.state
  62. self.country_code ||= geo.country_code
  63. self.zipcode ||= geo.zip
  64. self.full_address = geo.full_address
  65. else
  66. errors.add(field, "could not be found")
  67. end
  68. geo.success
  69. end
  70.  
  71. def is_zipcode?
  72. self.zipcode =~ /^[0-9]{5}$/
  73. end
  74.  
  75. def invalid_zipcode
  76. errors.add(:zipcode, "is invalid")
  77. false
  78. end
  79.  
  80. end
  81.  
  82. ## user.rb
  83.  
  84. require 'digest/sha1'
  85. class User < ActiveRecord::Base
  86. #acts_as_ferret :fields => [:first_name, :last_name]
  87.  
  88. #Relationships
  89. has_and_belongs_to_many :roles
  90. has_many :locations
  91. has_one :default_location, :class_name => 'Locations'
  92. has_one :profile
  93. #TODO create online_method so users can view friends online
  94. has_many_friends #:online_method
  95.  
  96. #For values that must be used multiple places
  97. VALID_PROFILE_STATUSES = %w(public, logged_in_users, friends_only)
  98.  
  99. # Virtual attribute for the unencrypted password
  100. attr_accessor :password
  101.  
  102. validates_presence_of :login, :email
  103. validates_presence_of :password
  104. validates_presence_of :password_confirmation
  105. validates_length_of :password, :within => 4..40, :if => :password_present?
  106. validates_confirmation_of :password, :if => :password_present?
  107. validates_length_of :login, :within => 3..40, :if => :login_present?
  108. validates_length_of :email, :within => 3..100, :if => :email_present?
  109. validates_uniqueness_of :login, :email, :case_sensitive => false
  110. validates_inclusion_of :profile_status, :in => VALID_PROFILE_STATUSES
  111.  
  112. validates_each :birthdate do |record, attr, value|
  113. record.errors.add(attr,"You're not old enough.") if value > Date.new((Date.today.year - 21),(Date.today.month),(Date.today.day))
  114. end
  115.  
  116. before_save :encrypt_password
  117.  
  118. # prevents a user from submitting a crafted form that bypasses activation
  119. # anything else you want your user to change should be added here.
  120. attr_accessible :login, :email, :password, :password_confirmation, :birthdate, :profile, :default_location
  121.  
  122. # Not needed since attr_accessible prevents anything what is provided to be accessed
  123. #attr_protected :roles
  124.  
  125. acts_as_state_machine :initial => :pending
  126. state :passive
  127. state :pending, :enter => :make_activation_code
  128. state :active, :enter => :do_activate
  129. state :suspended
  130. state :deleted, :enter => :do_delete
  131.  
  132. event :register do
  133. transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| !(u.crypted_password.blank? && u.password.blank?) }
  134. end
  135.  
  136. event :activate do
  137. transitions :from => :pending, :to => :active
  138. end
  139.  
  140. event :suspend do
  141. transitions :from => [:passive, :pending, :active], :to => :suspended
  142. end
  143.  
  144. event :delete do
  145. transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
  146. end
  147.  
  148. event :unsuspend do
  149. transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| !u.activated_at.blank? }
  150. transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| !u.activation_code.blank? }
  151. transitions :from => :suspended, :to => :passive
  152. end
  153.  
  154. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  155. def self.authenticate(login, password)
  156. u = find_in_state :first, :active, :conditions => {:login => login} # need to get the salt
  157. u && u.authenticated?(password) ? u : nil
  158. end
  159.  
  160. # Encrypts some data with the salt.
  161. def self.encrypt(password, salt)
  162. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  163. end
  164.  
  165. # Encrypts the password with the user salt
  166. def encrypt(password)
  167. self.class.encrypt(password, salt)
  168. end
  169.  
  170. def authenticated?(password)
  171. crypted_password == encrypt(password)
  172. end
  173.  
  174. def remember_token?
  175. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  176. end
  177.  
  178. # These create and unset the fields required for remembering users between browser closes
  179. def remember_me
  180. remember_me_for 2.weeks
  181. end
  182.  
  183. def remember_me_for(time)
  184. remember_me_until time.from_now.utc
  185. end
  186.  
  187. def remember_me_until(time)
  188. self.remember_token_expires_at = time
  189. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  190. save(false)
  191. end
  192.  
  193. def forget_me
  194. self.remember_token_expires_at = nil
  195. self.remember_token = nil
  196. save(false)
  197. end
  198.  
  199. def forgot_password
  200. @forgotten_password = true
  201. self.make_password_reset_code
  202. end
  203.  
  204. def reset_password
  205. # First update the password_reset_code before setting the
  206. # reset_password flag to avoid duplicate email notifications.
  207. update_attribute(:password_reset_code, nil)
  208. @reset_password = true
  209. end
  210.  
  211. #used in user_observer
  212. def recently_forgot_password?
  213. @forgotten_password
  214. end
  215.  
  216. def recently_reset_password?
  217. @reset_password
  218. end
  219.  
  220. def self.find_for_forget(email)
  221. find_in_state :first, :active, :conditions => {:email => email}
  222. end
  223.  
  224. def self.find_active(id)
  225. find_in_state :first, :active, :conditions => {:id => id}
  226. end
  227.  
  228. # Returns true if the user has just been activated.
  229. def recently_activated?
  230. @activated
  231. end
  232.  
  233. # has_role? simply needs to return true or false whether a user has a role or not.
  234. # It may be a good idea to have "admin" roles return true always
  235. def has_role?(role_in_question)
  236. @_list ||= self.roles.collect(&:name)
  237. return true if @_list.include?("admin")
  238. (@_list.include?(role_in_question.to_s) )
  239. end
  240.  
  241. #
  242. def show_profile_to?(user)
  243. if self.active?
  244. return true if self.profile_status == 'public'
  245. return false if user.nil?
  246. if user.is_admin? || self == user
  247. return true
  248. else
  249. case self.profile_status
  250. when 'logged_in_users'
  251. return true
  252. when 'friends_only'
  253. return self.is_friends_with?(user)
  254. end
  255. end
  256. elsif user && user.is_admin?
  257. return true
  258. else
  259. return false
  260. end
  261. end
  262.  
  263. def is_admin?
  264. return self.has_role?('admin')
  265. end
  266.  
  267. # Checks that the user is an admin but the is not the user being passed in
  268. # Useful for things you don't want users doing to themselves like disabling
  269. # TODO This may need to be refactored to use is_current_user?(user)
  270. def is_admin_but_not?(user)
  271. self.is_admin? && self != user
  272. end
  273.  
  274. # This function extends has_many_friends and returns true if the user still
  275. # hasn't responded to the friendship request of the "friend"
  276. def pending_friend_for_me?(friend)
  277. self.pending_friends_for_me.include?(friend)
  278. end
  279.  
  280. # This function returns true if the user is still waiting
  281. # for an answer to the friendship request
  282. def pending_friend_by_me?(friend)
  283. self.pending_friends_by_me.include?(friend)
  284. end
  285.  
  286. def name
  287. self.login
  288. end
  289.  
  290. # TODO Add ablity for Users to block each other
  291.  
  292. protected
  293. # before filter
  294. def encrypt_password
  295. return if password.blank?
  296. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  297. self.crypted_password = encrypt(password)
  298. end
  299.  
  300. def password_present?
  301. !password.blank?
  302. end
  303.  
  304. def login_present?
  305. !login.blank?
  306. end
  307.  
  308. def email_present?
  309. !email.blank?
  310. end
  311.  
  312. def make_activation_code
  313. self.deleted_at = nil
  314. self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  315. end
  316.  
  317. def make_password_reset_code
  318. self.password_reset_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  319. end
  320.  
  321. def make_email_update_code
  322. self.email_update_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  323. end
  324.  
  325. def do_delete
  326. self.deleted_at = Time.now.utc
  327. end
  328.  
  329. def do_activate
  330. @activated = true
  331. self.activated_at = Time.now.utc
  332. self.deleted_at = self.activation_code = nil
  333. end
  334. end
Add Comment
Please, Sign In to add comment