Guest User

Untitled

a guest
Mar 5th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. require 'digest/sha1'
  2.  
  3. class User < ActiveRecord::Base
  4. # Person.find(:all, :include => :addresses).map{|p| p.addresses}.flatten
  5. attr :file_data
  6. attr_accessor :password_confirmation, :file_data, :content_type
  7.  
  8. #...
  9. # joins
  10. #...
  11.  
  12. has_many :venues,
  13. :foreign_key => "created_by",
  14. :dependent => :destroy
  15. has_many :created_events,
  16. :class_name => "Event",
  17. :foreign_key => "owner_id"
  18. has_many :events,
  19. :through => :event_acts,
  20. :dependent => :destroy
  21. has_many :received_band_comments,
  22. :class_name => "UserComment",
  23. :dependent => :destroy
  24. has_many :posted_band_comments,
  25. :foreign_key => "owner_id",
  26. :class_name => "UserComment",
  27. :dependent => :destroy
  28. has_one :profile_stat,
  29. :class_name => "ProfileStat",
  30. :dependent => :destroy
  31. has_many :band_invites, :dependent => :destroy
  32. has_many :venue_comments, :dependent => :destroy
  33. has_many :venue_ratings, :dependent => :destroy
  34.  
  35.  
  36. # ...
  37. # validation
  38. # ...
  39.  
  40. validates_uniqueness_of :email, :message => "an account is already registered with this email address", :if => Proc.new {|user| !user.email.blank? }
  41. validates_presence_of :username
  42. validates_uniqueness_of :username, :if => Proc.new {|user| !user.username.blank? }
  43. validates_presence_of :name
  44. validates_presence_of :email
  45. validates_presence_of :password, :if => Proc.new {|user| (user.new_record? || !user.password.blank?) ==true }
  46. validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "looks kinda funny to me", :if => Proc.new {|user| !user.email.blank? }
  47. validates_confirmation_of :password, :if => Proc.new {|user| (user.new_record? || !user.password.blank?) ==true }
  48. validates_length_of :name, :maximum => 100, :if => Proc.new {|user| !user.name.blank? }
  49. validates_length_of :username, :within => 4..20, :if => Proc.new {|user| !user.username.blank? }
  50. validates_length_of :email, :maximum => 100, :if => Proc.new {|user| !user.email.blank? }
  51. validates_length_of :home_postcode, :maximum => 8
  52. validates_length_of :password, :within => 4..10, :if => Proc.new {|user| (user.new_record? || !user.password.blank?) ==true }
  53. validates_length_of :url, :maximum => 100, :if => Proc.new {|user| !user.url.blank? }
  54. validates_length_of :url_video, :maximum => 100, :if => Proc.new {|user| !user.url_video.blank? }
  55. validates_length_of :url_myspace, :maximum =>100, :if => Proc.new {|user| !user.url_myspace.blank? }
  56. validates_length_of :url_full_track, :maximum => 100, :if => Proc.new {|user| !user.url_full_track.blank? }
  57. validates_format_of :content_type, :with => /^image/, :message => "--- you can only upload pictures", :if => Proc.new {|user| user.file_data && user.file_data.length>0 }
  58.  
  59.  
  60. def validate
  61. end
  62.  
  63. #validates_format_of :content_type, :with => /^image/, :message => "--- you can only upload pictures", :if => Proc.new {|user| user.file_data && user.file_data.length>0 }
  64.  
  65.  
  66. # ...
  67. # trigger config
  68. # ..
  69. before_destroy :dont_destroy_infrid_or_jamie
  70. before_save :set_has_file
  71. after_save :process
  72. #after_destroy :cleanup
  73. before_create :before_create
  74. # ...
  75. # trigger methods
  76. # ...
  77.  
  78. def dont_destroy_infrid_or_jamie
  79. raise "can't destroy george or eoin" if self.username =="infrid" || self.username =="jamie"
  80. end
  81.  
  82. def before_create
  83. # generate my guid
  84. self.user_status = STATUS_WAITING_EMAIL_APPROVAL
  85. self.guid = UUID.new
  86. end
  87.  
  88. def before_update
  89.  
  90. end
  91.  
  92. def after_create
  93. @hashed_password = nil
  94. end
  95.  
  96. #...
  97. # accessors,
  98. #...
  99.  
  100. def password
  101. @password
  102. end
  103.  
  104.  
  105. def password=(pwd)
  106. return if pwd.blank?
  107. @password = pwd
  108. create_new_salt
  109. self.hashed_password = User.encrypted_password(self.password, self.salt)
  110. end
  111.  
  112. #...
  113. # data methods
  114. #...
  115.  
  116. def self.authenticate(name, password)
  117. user = self.find_by_username(name)
  118. if user
  119. expected_password = encrypted_password(password, user.salt)
  120. if user.hashed_password != expected_password
  121. user = nil
  122. end
  123. end
  124. user
  125. end
  126.  
  127. def self.get_user_name(u_id)
  128. return if !u_id
  129. User.connection.select_value("select username from users where id = " + u_id.to_s)
  130. end
  131.  
  132.  
  133.  
  134. def isAdmin?
  135. self.admin==5
  136. end
  137.  
  138. def get_user_type_text
  139. USER_TYPES.index(self.user_type)
  140. end
  141.  
  142. def get_user_status_text
  143. USER_STATUSES.index(self.user_status)
  144. end
  145.  
  146. # this method tells us if we have file data attached.. could be an mp3, could be a thumbnail
  147. def set_has_file
  148. if self.file_data && self.file_data.length>0
  149. self.has_thumb=1
  150. end
  151. end
  152.  
  153.  
  154. #...
  155. # image url methods
  156. #...
  157. def get_thumb
  158. if self.has_thumb==1 then
  159. THUMBNAIL_PATH + "#{self.id}-thumb.jpg"
  160. else
  161. THUMBNAIL_PATH + "none.jpg"
  162. end
  163. end
  164.  
  165. def thumbnail_url
  166. thumbnail_path.sub(/^public/, '' )
  167. end
  168.  
  169. def path
  170. File.join(IMAGE_PATH, "#{self.id}-full.jpg")
  171. end
  172.  
  173. def thumbnail_path
  174. File.join(THUMBNAIL_PATH, "#{self.id}-thumb.jpg")
  175. end
  176.  
  177. #...
  178. # mp3 url methods
  179. #...
  180. def get_mp3
  181. if self.has_mp3==1 then
  182. MP3_PREVIEW_PATH + "#{self.id}-thumb.jpg"
  183. else
  184. nil
  185. end
  186. end
  187.  
  188. def mp3_url
  189. mp3_path.sub(/^public/, '' )
  190. end
  191.  
  192. def mp3_path
  193. File.join(MP3_PREVIEW_PATH, "#{self.id}-thumb.jpg")
  194. end
  195.  
  196. #...
  197. #private
  198. #...
  199.  
  200.  
  201. private
  202.  
  203. def self.encrypted_password(password, salt)
  204. string_to_hash = password + "h4rd3r" + salt # 'h4rd3r' makes it harder to guess
  205. Digest::SHA1.hexdigest(string_to_hash)
  206. end
  207.  
  208. def create_new_salt
  209. self.salt = self.object_id.to_s + rand.to_s
  210. end
  211.  
  212.  
  213. def process
  214. if self.file_data && self.file_data.length>0 then
  215. create_directory
  216. cleanup
  217. #save_fullsize
  218. create_thumbnail
  219. self.file_data = nil
  220. end
  221. end
  222.  
  223. def save_fullsize
  224. img = Magick::Image.from_blob(self.file_data.read )[0]
  225. img.write FILE_PATH + path
  226. end
  227.  
  228. def create_thumbnail
  229. begin
  230. #img = Magick::Image.read(path).first
  231. img = Magick::Image.from_blob(self.file_data.read )[0]
  232. thumbnail = img.thumbnail(*THUMB_MAX_SIZE)
  233. thumbnail.write FILE_PATH + thumbnail_path
  234. rescue
  235. logger.error("create_thumnail", "couldn't save image")
  236. end
  237. end
  238.  
  239. def create_directory
  240. FileUtils.mkdir_p DIRECTORY
  241. end
  242.  
  243. def cleanup
  244. Dir[File.join(DIRECTORY, "#{self.id}-*")].each do |filename|
  245. File.unlink(filename) rescue nil end
  246. end
  247.  
  248.  
  249. end
Add Comment
Please, Sign In to add comment