Project

General

Profile

Feature #9196 ยป 0001-Add-logging-to-create_user_from_email.-Force-limits-.patch

Alex Shulgin, 2011-09-06 20:51

View differences:

app/models/mail_handler.rb
65 65
      when 'accept'
66 66
        @user = User.anonymous
67 67
      when 'create'
68
        @user = MailHandler.create_user_from_email(email)
68
        @user = create_user_from_email(email)
69 69
        if @user
70 70
          logger.info "MailHandler: [#{@user.login}] account created"
71 71
          Mailer.deliver_account_information(@user, @user.password)
......
338 338
    @full_sanitizer ||= HTML::FullSanitizer.new
339 339
  end
340 340

  
341
  # Creates a user account for the +email+ sender
342
  def self.create_user_from_email(email)
341
  def self.limit_for(klass, attribute)
342
    klass.columns_hash[attribute.to_s].limit
343
  end
344

  
345
  def self.chop_on_limit(str, klass, attribute)
346
    str[0, limit_for(klass, attribute)]
347
  end
348

  
349
  def self.assign_attr_with_limit(model, attr, value)
350
    model.send("#{attr}=".to_sym, chop_on_limit(value, model.class, attr))
351
  end
352

  
353
  def self.new_user_from_email(email)
343 354
    addr = email.from_addrs.to_a.first
344 355
    if addr && !addr.spec.blank?
345 356
      user = User.new
346
      user.mail = addr.spec
357
      assign_attr_with_limit(user, 'mail', addr.spec)
347 358

  
348 359
      names = addr.name.blank? ? addr.spec.gsub(/@.*$/, '').split('.') : addr.name.split
349
      user.firstname = names.shift
350
      user.lastname = names.join(' ')
360
      assign_attr_with_limit(user, 'firstname', names.shift)
361
      assign_attr_with_limit(user, 'lastname', names.join(' '))
351 362
      user.lastname = '-' if user.lastname.blank?
352 363

  
353
      user.login = user.mail
364
      assign_attr_with_limit(user, 'login', user.mail)
354 365
      user.password = ActiveSupport::SecureRandom.hex(5)
355 366
      user.language = Setting.default_language
356
      user.save ? user : nil
367

  
368
      user
369
    end
370
  end
371

  
372
  # Creates a user account for the +email+ sender
373
  def create_user_from_email(email)
374
    user = MailHandler.new_user_from_email(email)
375
    if user.save
376
      user
377
    else
378
      logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger
379
      nil
357 380
    end
358 381
  end
359 382

  
    (1-1/1)