Esempio n. 1
0
 /**
  * save user
  *
  * @param  array $recordData data of Tinebase_Model_FullUser
  * @return array  
  */
 public function saveUser($recordData)
 {
     $password = isset($recordData['accountPassword']) ? $recordData['accountPassword'] : '';
     $account = new Tinebase_Model_FullUser();
     // always re-evaluate fullname
     unset($recordData['accountFullName']);
     try {
         $account->setFromArray($recordData);
         if (isset($recordData['sambaSAM'])) {
             $account->sambaSAM = new Tinebase_Model_SAMUser($recordData['sambaSAM']);
         }
         if (isset($recordData['emailUser'])) {
             $account->emailUser = new Tinebase_Model_EmailUser($recordData['emailUser']);
             $account->imapUser = new Tinebase_Model_EmailUser($recordData['emailUser']);
             $account->smtpUser = new Tinebase_Model_EmailUser($recordData['emailUser']);
         }
     } catch (Tinebase_Exception_Record_Validation $e) {
         // invalid data in some fields sent from client
         $result = array('errors' => $account->getValidationErrors(), 'errorMessage' => 'invalid data for some fields', 'status' => 'failure');
         return $result;
     }
     if ($account->getId() == NULL) {
         if (!Tinebase_User_Registration::getInstance()->checkUniqueUsername($account->accountLoginName)) {
             $result = array('errors' => 'invalid username', 'errorMessage' => 'Username already used.', 'status' => 'failure');
             return $result;
         }
         $account = Admin_Controller_User::getInstance()->create($account, $password, $password);
     } else {
         $account = Admin_Controller_User::getInstance()->update($account, $password, $password);
     }
     // after user update or creation add user to selected roles
     if (isset($recordData['accountRoles']) && $recordData['accountRoles']) {
         Tinebase_Acl_Roles::getInstance()->setRoleMemberships(array('id' => $account->accountId, 'type' => Tinebase_Acl_Rights::ACCOUNT_TYPE_USER), $recordData['accountRoles']);
     }
     $result = $this->_recordToJson($account);
     // add primary group to account for the group selection combo box
     $group = Tinebase_Group::getInstance()->getGroupById($account->accountPrimaryGroup);
     // add user groups
     $userGroups = Tinebase_Group::getInstance()->getMultiple(Tinebase_Group::getInstance()->getGroupMemberships($account->accountId))->toArray();
     // add user roles
     $userRoles = Tinebase_Acl_Roles::getInstance()->getMultiple(Tinebase_Acl_Roles::getInstance()->getRoleMemberships($account->accountId))->toArray();
     // encode the account array
     $result['accountPrimaryGroup'] = $group;
     // encode the groups array
     $result['groups'] = array('results' => $userGroups, 'totalcount' => count($userGroups));
     // encode the roles array
     $result['accountRoles'] = array('results' => $userRoles, 'totalcount' => count($userRoles));
     return $result;
 }