Ejemplo n.º 1
0
 /**
  * Creates a user on the panel. Returns the created user's ID.
  *
  * @param  string       $email
  * @param  string|null  $password An unhashed version of the user's password.
  * @return bool|integer
  */
 public function create($email, $password = null, $admin = false)
 {
     $validator = Validator::make(['email' => $email, 'password' => $password, 'root_admin' => $admin], ['email' => 'required|email|unique:users,email', 'password' => 'nullable|regex:((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,})', 'root_admin' => 'required|boolean']);
     // Run validator, throw catchable and displayable exception if it fails.
     // Exception includes a JSON result of failed validation rules.
     if ($validator->fails()) {
         throw new DisplayValidationException($validator->errors());
     }
     DB::beginTransaction();
     try {
         $user = new Models\User();
         $uuid = new UuidService();
         $user->uuid = $uuid->generate('users', 'uuid');
         $user->email = $email;
         $user->password = Hash::make(is_null($password) ? str_random(30) : $password);
         $user->language = 'en';
         $user->root_admin = $admin ? 1 : 0;
         $user->save();
         // Setup a Password Reset to use when they set a password.
         $token = str_random(32);
         DB::table('password_resets')->insert(['email' => $user->email, 'token' => $token, 'created_at' => Carbon::now()->toDateTimeString()]);
         $user->notify(new AccountCreated($token));
         DB::commit();
         return $user->id;
     } catch (\Exception $ex) {
         DB::rollBack();
         throw $ex;
     }
 }