コード例 #1
0
ファイル: AuthController.php プロジェクト: joaogl/base
 /**
  * Account sign up form processing.
  *
  * @return Redirect
  */
 public function postSignup()
 {
     /************TEMP VARIABLE************/
     /*
      * 0 - Disabled
      * 1 - Enabled and no activation
      * 2 - User activation
      * 3 - Admin activation
      */
     $signupStatus = \Base::getSetting('USER_REGISTRATION');
     /************TEMP VARIABLE************/
     $signupEnabled = $signupStatus != 0;
     $userActivation = $signupStatus == 2;
     $adminActivation = $signupStatus == 3;
     if (!$signupEnabled) {
         return Redirect::to(URL::previous())->withInput()->with('error', Lang::get('base.auth.account.registration_disabled'));
     }
     $rules = array();
     // Declare the rules for the form validation
     foreach (Sentinel::createModel()->getRegisterFields() as $fieldid => $field) {
         $rules[$fieldid] = $field['validator'];
     }
     $rules['g-recaptcha-response'] = 'required';
     // Create a new validator instance from our validation rules
     $validator = Validator::make(Input::all(), $rules);
     $err = false;
     // If validation fails, we'll exit the operation now.
     if ($validator->fails() || ($err = $this->captchaCheck()) == false) {
         if ($err) {
             return Redirect::to(URL::previous())->withInput()->withErrors(['g-recaptcha-response' => Lang::get('base.captcha.error')]);
         }
         // Ooops.. something went wrong
         return Redirect::to(URL::previous())->withInput()->withErrors($validator);
     }
     try {
         $data = array();
         // Set the data to the user from the User class
         foreach (Sentinel::createModel()->getRegisterFields() as $fieldid => $field) {
             $data[$fieldid] = Input::get($fieldid);
         }
         // Set the standard data to the user
         $data['ip'] = Request::getClientIP();
         $data['status'] = 0;
         $data['staff'] = 0;
         if ($data['birthday'] != null) {
             $data['birthday'] = \Carbon\Carbon::createFromFormat('d/m/Y', $data['birthday']);
         }
         // Find the user if it exists and needs to be created
         $user = Sentinel::getUserRepository()->findByCredentials(['email' => Input::get('email')]);
         if ($user != null) {
             // Update the temporary user to the new one
             if (Sentinel::validForUpdate($data, ['email' => Input::get('email')])) {
                 $testing = Sentinel::createModel()->getRegisterFields();
                 $user = Sentinel::findById($user->id);
                 foreach ($data as $fieldid => $field) {
                     if (!isset($testing[$fieldid]) || $testing[$fieldid]['save'] == true) {
                         $user[$fieldid] = $field;
                     }
                 }
                 $user['password'] = bcrypt($user['password']);
                 Sentinel::update($user, ['email' => Input::get('email')]);
             } else {
                 return Redirect::to(URL::previous())->withInput()->with('error', Lang::get('base.auth.account.registration_failed'));
             }
         } else {
             $user = Sentinel::register($data, false);
         }
         // If the user needs to activate the account send him an email
         if ($userActivation) {
             $activation = Activation::create($user);
             // Data to be used on the email view
             $data = array('user' => $user, 'activationUrl' => URL::route('activate', [$user->id, $activation->code]));
             // Send the activation code through email
             Mail::queue('emails.auth.register-activate', $data, function ($m) use($user) {
                 $m->to($user->email, $user->first_name . ' ' . $user->last_name);
                 $m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name);
             });
         }
         // If the administrator needs to activate the account send the user a warning saying that
         if ($adminActivation) {
             Mail::queue('emails.auth.register-admin-activate', ['user' => $user], function ($m) use($user) {
                 $m->to($user->email, $user->first_name . ' ' . $user->last_name);
                 $m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name);
             });
         }
         // Log the user in
         if (!$adminActivation && !$userActivation) {
             $activation = Activation::create($user);
             if (Activation::complete($user, $activation->code)) {
                 Sentinel::login($user, false);
                 Mail::queue('emails.auth.activated', ['user' => $user], function ($m) use($user) {
                     $m->to($user->email, $user->first_name . ' ' . $user->last_name);
                     $m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name);
                 });
             }
         }
         Base::Log('New account registred for ' . $user->username . ' (' . $user->first_name . ' ' . $user->last_name . ') from IP ' . Request::ip());
         // Redirect to the home page with success menu
         return Redirect::to("login")->with('success', Lang::get('base.auth.signup.success') . $adminActivation ? Lang::get('base.auth.signup.admin') : $userActivation ? Lang::get('base.auth.signup.self') : Lang::get('base.auth.signup.ready'));
     } catch (UserExistsException $e) {
         $this->messageBag->add('email', Lang::get('base.auth.account.already_exists'));
     }
     Base::Log('New account registration attempt from IP ' . Request::ip());
     // Ooops.. something went wrong
     return Redirect::back()->withInput()->withErrors($this->messageBag);
 }