/** * Adds a new kuser and user_login_data records as needed * @param kuser $user * @param string $password * @param bool $checkPasswordStructure * @throws kUserException::USER_NOT_FOUND * @throws kUserException::USER_ALREADY_EXISTS * @throws kUserException::INVALID_EMAIL * @throws kUserException::INVALID_PARTNER * @throws kUserException::ADMIN_LOGIN_USERS_QUOTA_EXCEEDED * @throws kUserException::LOGIN_ID_ALREADY_USED * @throws kUserException::PASSWORD_STRUCTURE_INVALID * @throws kPermissionException::ROLE_ID_MISSING * @throws kPermissionException::ONLY_ONE_ROLE_PER_USER_ALLOWED */ public static function addUser(kuser $user, $password = null, $checkPasswordStructure = true, $sendEmail = null) { if (!$user->getPuserId()) { throw new kUserException('', kUserException::USER_ID_MISSING); } // check if user with the same partner and puserId already exists $existingUser = kuserPeer::getKuserByPartnerAndUid($user->getPartnerId(), $user->getPuserId()); if ($existingUser) { throw new kUserException('', kUserException::USER_ALREADY_EXISTS); } // check if roles are valid - may throw exceptions if (!$user->getRoleIds() && $user->getIsAdmin()) { // assign default role according to user type admin / normal $userRoleId = $user->getPartner()->getAdminSessionRoleId(); $user->setRoleIds($userRoleId); } UserRolePeer::testValidRolesForUser($user->getRoleIds(), $user->getPartnerId()); if ($user->getScreenName() === null) { $user->setScreenName($user->getPuserId()); } if ($user->getFullName() === null) { $user->setFirstName($user->getPuserId()); } if (is_null($user->getStatus())) { $user->setStatus(KuserStatus::ACTIVE); } // if password is set, user should be able to login to the system - add a user_login_data record if ($password || $user->getIsAdmin()) { // throws an action on error $user->enableLogin($user->getEmail(), $password, $checkPasswordStructure, $sendEmail); } $user->save(); return $user; }