Example #1
0
 /**
  * Creates a new user
  *
  * @param  Array $params
  * @return The created user
  * @throws InvalidModelException
  * @throws ExistingUserException
  */
 public function createUser($params, $setAsAuthenticated = true, $role = User::ROLE_USER, $userType = null)
 {
     if ($userType == null) {
         $userType = $this->userClass;
     }
     // Check if there's a user with this email first.
     $select = $this->dbService->select();
     $select->from(strtolower($userType), '*')->where('username=?', $params['username'])->orWhere('email=?', $params['email']);
     $existing = $this->dbService->getObject($select, $userType);
     if ($existing) {
         throw new ExistingUserException($params['username'] . ' already exists');
     }
     $newPass = null;
     if (isset($params['password'])) {
         $newPass = $params['password'];
     }
     $params['role'] = $role;
     // Create a user with initial information
     $user = $userType;
     $user = new $user();
     $user->generateSaltedPassword($params['password']);
     unset($params['password']);
     $user->bind($params);
     $validator = new ModelValidator();
     if (!$validator->isValid($user)) {
         throw new InvalidModelException($validator->getMessages());
     }
     // so now we save the user, then reset their password which emails,
     // then we set them as the authenticated user.
     if ($this->dbService->createObject($user)) {
         $this->trackerService->track('create-user', $user->id);
         if ($setAsAuthenticated) {
             $this->authService->setAuthenticatedUser($user);
         }
         return $user;
     }
     return null;
 }