The code powering m.abunchtell.com https://m.abunchtell.com
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

228 rindas
6.2 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: users
  5. #
  6. # id :integer not null, primary key
  7. # email :string default(""), not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. # encrypted_password :string default(""), not null
  11. # reset_password_token :string
  12. # reset_password_sent_at :datetime
  13. # remember_created_at :datetime
  14. # sign_in_count :integer default(0), not null
  15. # current_sign_in_at :datetime
  16. # last_sign_in_at :datetime
  17. # current_sign_in_ip :inet
  18. # last_sign_in_ip :inet
  19. # admin :boolean default(FALSE), not null
  20. # confirmation_token :string
  21. # confirmed_at :datetime
  22. # confirmation_sent_at :datetime
  23. # unconfirmed_email :string
  24. # locale :string
  25. # encrypted_otp_secret :string
  26. # encrypted_otp_secret_iv :string
  27. # encrypted_otp_secret_salt :string
  28. # consumed_timestep :integer
  29. # otp_required_for_login :boolean default(FALSE), not null
  30. # last_emailed_at :datetime
  31. # otp_backup_codes :string is an Array
  32. # filtered_languages :string default([]), not null, is an Array
  33. # account_id :integer not null
  34. # disabled :boolean default(FALSE), not null
  35. # moderator :boolean default(FALSE), not null
  36. # invite_id :integer
  37. #
  38. class User < ApplicationRecord
  39. include Settings::Extend
  40. ACTIVE_DURATION = 14.days
  41. devise :two_factor_authenticatable,
  42. otp_secret_encryption_key: ENV['OTP_SECRET']
  43. devise :two_factor_backupable,
  44. otp_number_of_backup_codes: 10
  45. devise :registerable, :recoverable, :rememberable, :trackable, :validatable,
  46. :confirmable
  47. belongs_to :account, inverse_of: :user, required: true
  48. belongs_to :invite, counter_cache: :uses
  49. accepts_nested_attributes_for :account
  50. has_many :applications, class_name: 'Doorkeeper::Application', as: :owner
  51. validates :locale, inclusion: I18n.available_locales.map(&:to_s), if: :locale?
  52. validates_with BlacklistedEmailValidator, if: :email_changed?
  53. scope :recent, -> { order(id: :desc) }
  54. scope :admins, -> { where(admin: true) }
  55. scope :moderators, -> { where(moderator: true) }
  56. scope :staff, -> { admins.or(moderators) }
  57. scope :confirmed, -> { where.not(confirmed_at: nil) }
  58. scope :inactive, -> { where(arel_table[:current_sign_in_at].lt(ACTIVE_DURATION.ago)) }
  59. scope :active, -> { confirmed.where(arel_table[:current_sign_in_at].gteq(ACTIVE_DURATION.ago)).joins(:account).where(accounts: { suspended: false }) }
  60. scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) }
  61. scope :with_recent_ip_address, ->(value) { where(arel_table[:current_sign_in_ip].eq(value).or(arel_table[:last_sign_in_ip].eq(value))) }
  62. before_validation :sanitize_languages
  63. # This avoids a deprecation warning from Rails 5.1
  64. # It seems possible that a future release of devise-two-factor will
  65. # handle this itself, and this can be removed from our User class.
  66. attribute :otp_secret
  67. has_many :session_activations, dependent: :destroy
  68. delegate :auto_play_gif, :default_sensitive, :unfollow_modal, :boost_modal, :delete_modal,
  69. :reduce_motion, :system_font_ui, :noindex, :theme,
  70. to: :settings, prefix: :setting, allow_nil: false
  71. attr_accessor :invite_code
  72. def confirmed?
  73. confirmed_at.present?
  74. end
  75. def staff?
  76. admin? || moderator?
  77. end
  78. def role
  79. if admin?
  80. 'admin'
  81. elsif moderator?
  82. 'moderator'
  83. else
  84. 'user'
  85. end
  86. end
  87. def role?(role)
  88. case role
  89. when 'user'
  90. true
  91. when 'moderator'
  92. staff?
  93. when 'admin'
  94. admin?
  95. else
  96. false
  97. end
  98. end
  99. def disable!
  100. update!(disabled: true,
  101. last_sign_in_at: current_sign_in_at,
  102. current_sign_in_at: nil)
  103. end
  104. def enable!
  105. update!(disabled: false)
  106. end
  107. def confirm
  108. new_user = !confirmed?
  109. super
  110. update_statistics! if new_user
  111. end
  112. def confirm!
  113. new_user = !confirmed?
  114. skip_confirmation!
  115. save!
  116. update_statistics! if new_user
  117. end
  118. def promote!
  119. if moderator?
  120. update!(moderator: false, admin: true)
  121. elsif !admin?
  122. update!(moderator: true)
  123. end
  124. end
  125. def demote!
  126. if admin?
  127. update!(admin: false, moderator: true)
  128. elsif moderator?
  129. update!(moderator: false)
  130. end
  131. end
  132. def disable_two_factor!
  133. self.otp_required_for_login = false
  134. otp_backup_codes&.clear
  135. save!
  136. end
  137. def active_for_authentication?
  138. super && !disabled?
  139. end
  140. def setting_default_privacy
  141. settings.default_privacy || (account.locked? ? 'private' : 'public')
  142. end
  143. def allows_digest_emails?
  144. settings.notification_emails['digest']
  145. end
  146. def token_for_app(a)
  147. return nil if a.nil? || a.owner != self
  148. Doorkeeper::AccessToken
  149. .find_or_create_by(application_id: a.id, resource_owner_id: id) do |t|
  150. t.scopes = a.scopes
  151. t.expires_in = Doorkeeper.configuration.access_token_expires_in
  152. t.use_refresh_token = Doorkeeper.configuration.refresh_token_enabled?
  153. end
  154. end
  155. def activate_session(request)
  156. session_activations.activate(session_id: SecureRandom.hex,
  157. user_agent: request.user_agent,
  158. ip: request.remote_ip).session_id
  159. end
  160. def exclusive_session(id)
  161. session_activations.exclusive(id)
  162. end
  163. def session_active?(id)
  164. session_activations.active? id
  165. end
  166. def web_push_subscription(session)
  167. session.web_push_subscription.nil? ? nil : session.web_push_subscription.as_payload
  168. end
  169. def invite_code=(code)
  170. self.invite = Invite.find_by(code: code) unless code.blank?
  171. @invite_code = code
  172. end
  173. protected
  174. def send_devise_notification(notification, *args)
  175. devise_mailer.send(notification, self, *args).deliver_later
  176. end
  177. private
  178. def sanitize_languages
  179. filtered_languages.reject!(&:blank?)
  180. end
  181. def update_statistics!
  182. BootstrapTimelineWorker.perform_async(account_id)
  183. ActivityTracker.increment('activity:accounts:local')
  184. end
  185. end