Esempio n. 1
0
 public function saveUser($sender, $params)
 {
     $results = $errors = array();
     try {
         Dao::beginTransaction();
         if (!isset($params->CallbackParameter->firstName) || ($firstName = trim($params->CallbackParameter->firstName)) === '') {
             throw new Exception('System Error: firstName is mandatory!');
         }
         if (!isset($params->CallbackParameter->lastName) || ($lastName = trim($params->CallbackParameter->lastName)) === '') {
             throw new Exception('System Error: lastName is mandatory!');
         }
         if (!isset($params->CallbackParameter->userName) || ($userName = trim($params->CallbackParameter->userName)) === '') {
             throw new Exception('System Error: userName is mandatory!');
         }
         if (!isset($params->CallbackParameter->roleid) || !($role = Role::get($params->CallbackParameter->roleid)) instanceof Role) {
             throw new Exception('System Error: role is mandatory!');
         }
         $newpassword = trim($params->CallbackParameter->newpassword);
         if (!isset($params->CallbackParameter->userid) || !($userAccount = UserAccount::get($params->CallbackParameter->userid)) instanceof UserAccount) {
             $userAccount = new UserAccount();
             $person = new Person();
             if ($newpassword === '') {
                 throw new Exception('System Error: new password is mandatory!');
             }
             $newpassword = sha1($newpassword);
         } else {
             $person = $userAccount->getPerson();
             if ($newpassword === '') {
                 $newpassword = $userAccount->getPassword();
             } else {
                 $newpassword = sha1($newpassword);
             }
         }
         //double check whether the username has been used
         $users = UserAccount::getAllByCriteria('username=? and id!=?', array($userName, $userAccount->getId()), false, 1, 1);
         if (count($users) > 0) {
             throw new Exception('Username(=' . $userName . ') has been used by another user, please choose another one!');
         }
         $person->setFirstName($firstName)->setLastName($lastName)->save();
         $userAccount->setUserName($userName)->setPassword($newpassword)->setPerson($person)->save();
         $results = $userAccount->clearRoles()->addRole($role)->getJson();
         Dao::commitTransaction();
     } catch (Exception $ex) {
         Dao::rollbackTransaction();
         $errors[] = $ex->getMessage();
     }
     $params->ResponseData = StringUtilsAbstract::getJson($results, $errors);
 }
Esempio n. 2
0
 function formatAccountResultSet($result)
 {
     $accountdata = array();
     if ($result != null) {
         $n = count($result);
         for ($i = 0; $i < $n; $i++) {
             $account = new UserAccount();
             $account->setUserID($result[$i]->userID);
             $account->setUserName($result[$i]->username);
             $account->setUserFullName($result[$i]->fullname);
             $accountdata[$i] = $account;
         }
         return $accountdata;
     } else {
         return false;
     }
 }