2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2019-02-01 19:40:24 +00:00
|
|
|
class User < ApplicationRecord
|
2019-08-06 14:08:51 +00:00
|
|
|
# :email, # str: balidated with Devise
|
|
|
|
# :password, # str: hashed with bcrypt
|
2019-07-30 21:29:24 +00:00
|
|
|
# :phone, # str
|
2019-08-06 14:08:51 +00:00
|
|
|
# :location,
|
|
|
|
# :city,
|
|
|
|
# :state_code,
|
|
|
|
# :password_confirmation, # accessor: used on registration
|
|
|
|
# :remember_me, # bool: don't sign user out for a while
|
|
|
|
# :provider, # str: OAuth provider
|
|
|
|
# :uid, # str: OAuth user ID
|
|
|
|
# :pending_password, # bool: User registered with oauth and did not set a password
|
|
|
|
# :name, # str: created with oauth
|
|
|
|
# :auto_generated, # bool: flag whether a password was auto-generated for this account
|
|
|
|
# :referer, # str: ID of the user who referred this account
|
|
|
|
# :reset_password_token,
|
|
|
|
# :reset_password_sent_at,
|
|
|
|
# :picture, # str: url for fb or twitter pic
|
|
|
|
# :current_password, # accessor: for updating pass
|
|
|
|
# :profile_attributes,
|
2019-07-30 21:29:24 +00:00
|
|
|
# :phone
|
|
|
|
|
|
|
|
devise :async, :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable
|
|
|
|
|
|
|
|
attr_accessor :offsite_donation_id, :current_password
|
|
|
|
|
|
|
|
validates :email,
|
|
|
|
presence: true,
|
|
|
|
uniqueness: { case_sensitive: false },
|
|
|
|
format: { with: Email::Regex }
|
|
|
|
|
|
|
|
has_many :donations, through: :profile
|
|
|
|
has_many :roles, dependent: :destroy
|
|
|
|
has_one :profile, dependent: :destroy
|
|
|
|
has_many :imports
|
2018-03-25 17:30:42 +00:00
|
|
|
has_many :email_settings
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
accepts_nested_attributes_for :profile
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
before_validation(on: :create) do
|
|
|
|
self.password = Devise.friendly_token.first(8) if auto_generated
|
|
|
|
build_profile if profile.nil?
|
|
|
|
self
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
# This creates the user in the normal way, but also sends the devise email confirmation email, which we don't want to send to np admins or anyone else
|
|
|
|
def self.register_donor!(params)
|
|
|
|
u = User.create!(params)
|
|
|
|
u.send_confirmation_instructions
|
2019-07-30 21:29:24 +00:00
|
|
|
u
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
def self.find_or_create_with_email(em)
|
2019-07-30 21:29:24 +00:00
|
|
|
user = where('lower(email) = ?', em.downcase).first
|
2018-03-25 17:30:42 +00:00
|
|
|
return user if user.present?
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
User.create!(email: em, auto_generated: true)
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def profile_picture(size)
|
|
|
|
profile.picture_url(size)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Required by Devise for Omniauth
|
|
|
|
# https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
|
|
|
|
def self.new_with_session(params, session)
|
|
|
|
super.tap do |user|
|
|
|
|
if data = session['devise.facebook_data'] && session['devise.facebook_data']['extra']['raw_info']
|
|
|
|
user.email = data['email'] if user.email.blank?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Don't require confirmation for new users -- they can still donate without confirmation
|
|
|
|
# https://github.com/plataformatec/devise/wiki/How-To:-Override-confirmations-so-users-can-pick-their-own-passwords-as-part-of-confirmation-activation
|
|
|
|
def confirmation_required?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
# This is useful for manually generating a Devise user confirmation token so that we can get the confirmation URL with the correct token from anywhere
|
|
|
|
def make_confirmation_token!
|
|
|
|
raw, db = Devise.token_generator.generate(User, :confirmation_token)
|
|
|
|
self.confirmation_token = db
|
|
|
|
self.confirmation_sent_at = Time.now
|
2019-07-30 21:29:24 +00:00
|
|
|
save!
|
|
|
|
raw
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2021-01-14 21:33:10 +00:00
|
|
|
def to_builder(*expand)
|
|
|
|
Jbuilder.new do |json|
|
|
|
|
json.object "user"
|
|
|
|
json.id id
|
|
|
|
end
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|