user.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. class User < ApplicationRecord
  2. attr_accessor :remember_token
  3. validates :name, presence: true, length: { maximum: 255 }
  4. VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  5. validates :email, presence: true, length: { maximum: 255 },
  6. format: { with: VALID_EMAIL_REGEX },
  7. uniqueness: { case_sensitive: false }
  8. before_save { email.downcase! }
  9. validates :login, presence: true, length: { maximum: 50 },
  10. format: { with: /\A[a-zA-Z0-9_]+\Z/ },
  11. uniqueness: { case_sensitive: false }
  12. has_secure_password
  13. validates :password, presence: true, length: { minimum: 6 },
  14. if: lambda { new_record? || !password.blank? ||
  15. !password_confirmation.blank? }
  16. enum gender: [ :unspecified, :male, :female, :other ]
  17. VALID_PHONE_REGEX = /[0-9a-z\-+() .]*/i
  18. validates :phone, length: { maximum: 255 },
  19. format: { with: VALID_PHONE_REGEX }
  20. validates :birth_date, presence: true
  21. mount_uploader :picture, PictureUploader
  22. validate :picture_size
  23. has_many :messages
  24. has_many :conversation_participations, dependent: :destroy
  25. has_many :conversations, through: :conversation_participations
  26. has_many :administrations, dependent: :destroy
  27. has_many :schools_administering, through: :administrations,
  28. class_name: "School", source: :school
  29. belongs_to :school, optional: true
  30. has_many :course_participations
  31. has_many :courses, through: :course_participations
  32. has_many :assignments, through: :courses
  33. # Returns the hash digest of the given string.
  34. def User.digest(string)
  35. cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
  36. BCrypt::Engine.cost
  37. BCrypt::Password.create(string, cost: cost)
  38. end
  39. # Returns a random token.
  40. def User.new_token
  41. SecureRandom.urlsafe_base64
  42. end
  43. def remember
  44. self.remember_token = User.new_token
  45. update_attribute(:remember_digest, User.digest(remember_token))
  46. end
  47. # Returns true if the given token matches the digest.
  48. def authenticated?(attribute, token)
  49. digest = send("#{attribute}_digest")
  50. return false if digest.nil?
  51. BCrypt::Password.new(digest).is_password?(token)
  52. end
  53. # Forgets a user.
  54. def forget
  55. update_attribute(:remember_digest, nil)
  56. end
  57. def is_administrator_at?(school)
  58. school ? school.administrators.include?(self) : false
  59. end
  60. def is_course_teacher?(course)
  61. !course || course.users.merge(CourseParticipation.teachers).include?(self) || self.is_course_administrator?(course)
  62. end
  63. def is_course_administrator?(course)
  64. !course || self.is_administrator_at?(course.school) || self.admin?
  65. end
  66. private
  67. def picture_size
  68. if picture.size > 5.megabytes
  69. errors.add(:picture, "should be less than 5MB")
  70. end
  71. end
  72. end