Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2.  
  3. has_many :tels
  4. has_many :cars
  5. has_many :lines, through: :cars
  6. has_many :comments
  7. has_many :likes
  8.  
  9. enum type_user: [:societe, :user]
  10.  
  11. def self.signup(params)
  12. check_params = GlobalOperations.missing_params?(params["user"]["email"],
  13. params["user"]["password"],
  14. params["user"]["name"],
  15. params["user"]["username"],
  16. params["user"]["type_user"])
  17.  
  18. return {status:1} unless check_params
  19. return {status:1} unless GlobalOperations.email_valid?(params["user"]["email"])
  20. return {status:1} unless [0, 1].include? params["user"]["type_user"].to_i
  21.  
  22. check_user_exist = User.find_by(email: params["user"]["email"])
  23.  
  24. return {status:2} if check_user_exist
  25.  
  26. password_elms = User.create_hashed_password(params["user"]["password"])
  27.  
  28. user = User.create( name: params["user"]["name"],
  29. email: params["user"]["email"],
  30. username: params["user"]["username"],
  31. password: password_elms[:hashed_password],
  32. salt: password_elms[:salt],
  33. adresse: params["user"]["adresse"],
  34. type_user: params["user"]["type_user"].to_i)
  35.  
  36. params["user"]["tels"].each do |tel|
  37. Tel.create(number:tel,
  38. user_id: user.id)
  39. end if params["user"]["tels"]
  40.  
  41. UserMailer.welcome_mail(user).deliver_later
  42.  
  43. {status:0, user: user}
  44. end
  45.  
  46.  
  47. def self.create_hashed_password(password)
  48. salt = SecureRandom.base64(8)
  49. {hashed_password: Digest::SHA2.hexdigest(salt + password), salt: salt}
  50. end
  51.  
  52. def password_correct?(hashed_password, password_to_confirm, salt)
  53. hashed_password == Digest::SHA2.hexdigest(salt + password_to_confirm)
  54. end
  55.  
  56. def self.login(params)
  57. check_params = GlobalOperations.missing_params?(params["user"]["email"],
  58. params["user"]["password"])
  59. return {status:1} unless check_params
  60. return {status:1} unless GlobalOperations.email_valid?(params["user"]["email"])
  61. user = User.find_by(email: params["user"]["email"])
  62.  
  63. return {status:2} unless user
  64.  
  65. check_password = user.password_correct?(user.password, params["user"]["password"], user.salt)
  66. return {status:2} unless check_password
  67. return {status:0, user: user} if check_password
  68. end
  69.  
  70. def update_user(params)
  71. check_params = GlobalOperations.missing_params?(params["user"]["email"],
  72. params["user"]["name"],
  73. params["user"]["username"],
  74. params["user"]["image_link"])
  75.  
  76. return {status:1} unless check_params
  77. return {status:1} unless GlobalOperations.email_valid?(params["user"]["email"])
  78.  
  79. self.update(name: params["user"]["name"],
  80. username: params["user"]["username"],
  81. email: params["user"]["email"],
  82. adresse: params["user"]["adresse"]
  83. )
  84.  
  85. if DEFAULT_IMAGE != params["user"]["image_link"]
  86. image = Image.find_by(object_id: self.id, class_name:"User", image_type:0)
  87.  
  88. Image.create(object_id: self.id, class_name:"User", link:params["user"]["image_link"], image_type:0) unless image
  89. image.update(object_id: self.id, class_name:"User", link:params["user"]["image_link"], image_type:0) if image
  90. end
  91.  
  92. self.tels.destroy_all
  93. params["user"]["tel_numbers"].each_with_index do |tel|
  94. Tel.create(number:tel, user_id: self.id) if GlobalOperations.number?(tel)
  95. end if params["user"]["tel_numbers"]
  96. {status:0}
  97. end
  98.  
  99. def owner_of?(obj)
  100. obj.user_id == self.id
  101. end
  102.  
  103. def liked?(class_name, obj)
  104. !!self.likes.find_by(class_name: class_name, object_id: obj.id)
  105. end
  106. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement