70 lines
2.5 KiB
Ruby
70 lines
2.5 KiB
Ruby
module JamRuby
|
|
class UserManager < BaseManager
|
|
|
|
def initialize(options={})
|
|
super(options)
|
|
@log = Logging.logger[self]
|
|
end
|
|
|
|
# throws ActiveRecord::RecordNotFound if instrument is invalid
|
|
# throws an email delivery error if unable to connect out to SMTP
|
|
def signup(name, email, password, password_confirmation,
|
|
city, state, country, instruments, signup_confirm_url)
|
|
user = User.new
|
|
|
|
UserManager.active_record_transaction do |user_manager|
|
|
user.name = name
|
|
user.email = email
|
|
user.password = password
|
|
user.password_confirmation = password_confirmation
|
|
user.admin = false
|
|
user.email_confirmed = false
|
|
user.city = city
|
|
user.state = state
|
|
user.country = country
|
|
unless instruments.nil?
|
|
instruments.each do |musician_instrument_param|
|
|
instrument = Instrument.find(musician_instrument_param[:instrument_id])
|
|
musician_instrument = MusicianInstrument.new
|
|
musician_instrument.user = user
|
|
musician_instrument.instrument = instrument
|
|
musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level]
|
|
musician_instrument.priority = musician_instrument_param[:priority]
|
|
musician_instrument.save
|
|
user.musician_instruments << musician_instrument
|
|
end
|
|
end
|
|
user.signup_token = SecureRandom.urlsafe_base64
|
|
|
|
user.save
|
|
|
|
if user.errors.any?
|
|
raise ActiveRecord::Rollback
|
|
else
|
|
# any errors here should also rollback the transaction; that's OK. If emails aren't going to be delivered,
|
|
# it's already a really bad situation; make user signup again
|
|
UserMailer.welcome_message(user, signup_confirm_url + "/" + user.signup_token).deliver
|
|
end
|
|
end
|
|
|
|
return user
|
|
end
|
|
|
|
# throws RecordNotFound if signup token is invalid; i.e., if it's nil, empty string, or not belonging to a user
|
|
def signup_confirm(signup_token)
|
|
if signup_token.nil? || signup_token.empty?
|
|
# there are plenty of confirmed users with nil signup_tokens, so we can't look on it
|
|
raise ActiveRecord::RecordNotFound
|
|
else
|
|
UserManager.active_record_transaction do |user_manager|
|
|
# throws ActiveRecord::RecordNotFound if invalid
|
|
user = User.find_by_signup_token!(signup_token)
|
|
user.signup_token = nil
|
|
user.email_confirmed = true
|
|
user.save
|
|
return user
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end |