create() public method

Creates a user.
public create ( array $credentials, Closure $callback = null ) : Cartalyst\Sentinel\Users\UserInterface
$credentials array
$callback Closure
return Cartalyst\Sentinel\Users\UserInterface
 /**
  * Processes the form.
  *
  * @param  string  $mode
  * @param  int  $id
  * @return \Illuminate\Http\RedirectResponse
  */
 protected function processForm($mode, $id = null)
 {
     $rules = ['email' => 'required|unique:users', 'password' => 'sometimes|required', 'password_confirm' => 'required_with:password|same:password'];
     if ($id) {
         $user = $this->users->createModel()->find($id);
         $rules['email'] .= ",email,{$user->email},email";
         $input = $this->prepareInput(Input::all(), $mode === 'update' ? true : false);
         $messages = $this->validateUser($input, $rules);
         if ($messages->isEmpty()) {
             try {
                 // Update the user
                 $this->users->update($user, array_except($input, 'roles'));
                 // Get the new user roles
                 $roles = array_get($input, 'roles', []);
                 // Get the user roles
                 $userRoles = $user->roles->lists('id');
                 // Prepare the roles to be added and removed
                 $toAdd = array_diff($roles, $userRoles);
                 $toDel = array_diff($userRoles, $roles);
                 // Detach the user roles
                 if (!empty($toDel)) {
                     $user->roles()->detach($toDel);
                 }
                 // Attach the user roles
                 if (!empty($toAdd)) {
                     $user->roles()->attach($toAdd);
                 }
             } catch (NotUniquePasswordException $e) {
                 return Redirect::back()->withInput()->withErrors('This password was used before. You must choose a unique password.');
             }
         }
     } else {
         $input = $this->prepareInput(Input::all(), true);
         $messages = $this->validateUser($input, $rules);
         if ($messages->isEmpty()) {
             $user = $this->users->create($input);
             $activation = Activation::create($user);
             Activation::complete($user, $activation->code);
         }
     }
     if ($messages->isEmpty()) {
         return Redirect::route('users.index')->withSuccess(trans("users/messages.success.{$mode}"));
     }
     return Redirect::back()->withInput()->withErrors($messages);
 }
Example #2
0
 /**
  * Registers a user. You may provide a callback to occur before the user
  * is saved, or provide a true boolean as a shortcut to activation.
  *
  * @param  array  $credentials
  * @param  \Closure|bool  $callback
  * @return \Cartalyst\Sentinel\Users\UserInteface|bool
  * @throws \InvalidArgumentException
  */
 public function register(array $credentials, $callback = null)
 {
     if ($callback !== null && !$callback instanceof Closure && !is_bool($callback)) {
         throw new InvalidArgumentException('You must provide a closure or a boolean.');
     }
     $this->fireEvent('sentinel.registering', $credentials);
     $valid = $this->users->validForCreation($credentials);
     if ($valid === false) {
         return false;
     }
     $argument = $callback instanceof Closure ? $callback : null;
     $user = $this->users->create($credentials, $argument);
     if ($callback === true) {
         $this->activate($user);
     }
     $this->fireEvent('sentinel.registered', $user);
     return $user;
 }