diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/User.php b/workflow/engine/src/ProcessMaker/BusinessModel/User.php index 3d61bf6d0..b48fa5ff7 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/User.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/User.php @@ -4,6 +4,744 @@ use \G; class User { + //--- --- + private $arrayFieldDefinition = array( + "USR_UID" => array("type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "usrUid"), + "USR_FIRSTNAME" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "usrFirstname"), + "USR_LASTNAME" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "usrLastname"), + "USR_USERNAME" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "usrUsername"), + "USR_EMAIL" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "usrEmail"), + "USR_ADDRESS" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrAddress"), + "USR_ZIP_CODE" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrZipCode"), + "USR_COUNTRY" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrCountry"), + "USR_CITY" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrCity"), + "USR_LOCATION" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrLocation"), + "USR_PHONE" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrPhone"), + "USR_POSITION" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrPosition"), + "USR_REPLACED_BY" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrReplacedBy"), + "USR_DUE_DATE" => array("type" => "date", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "usrDueDate"), + "USR_CALENDAR" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrCalendar"), + "USR_STATUS" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array("ACTIVE", "INACTIVE", "VACATION"), "fieldNameAux" => "usrStatus"), + "USR_ROLE" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "usrRole"), + "USR_NEW_PASS" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "usrNewPass"), + "USR_CNF_PASS" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "usrCnfPass"), + "USR_UX" => array("type" => "string", "required" => false, "empty" => false, "defaultValues" => array("NORMAL", "SIMPLIFIED", "SWITCHABLE", "SINGLE"), "fieldNameAux" => "usrUx"), + "DEP_UID" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "depUid"), + "USR_BIRTHDAY" => array("type" => "date", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrBirthday"), + "USR_FAX" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrFax"), + "USR_CELLULAR" => array("type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "usrCellular"), + "USR_LOGGED_NEXT_TIME" => array("type" => "int", "required" => false, "empty" => false, "defaultValues" => array(0, 1), "fieldNameAux" => "usrLoggedNextTime") + ); + + private $formatFieldNameInUppercase = true; + + private $arrayFieldNameForException = array( + "usrPhoto" => "USR_PHOTO" + ); + + /** + * Constructor of the class + * + * return void + */ + public function __construct() + { + try { + foreach ($this->arrayFieldDefinition as $key => $value) { + $this->arrayFieldNameForException[$value["fieldNameAux"]] = $key; + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Set the format of the fields name (uppercase, lowercase) + * + * @param bool $flag Value that set the format + * + * return void + */ + public function setFormatFieldNameInUppercase($flag) + { + try { + $this->formatFieldNameInUppercase = $flag; + + $this->setArrayFieldNameForException($this->arrayFieldNameForException); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Set exception users for fields + * + * @param array $arrayData Data with the fields + * + * return void + */ + public function setArrayFieldNameForException(array $arrayData) + { + try { + foreach ($arrayData as $key => $value) { + $this->arrayFieldNameForException[$key] = $this->getFieldNameByFormatFieldName($value); + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get the name of the field according to the format + * + * @param string $fieldName Field name + * + * return string Return the field name according the format + */ + public function getFieldNameByFormatFieldName($fieldName) + { + try { + return ($this->formatFieldNameInUppercase)? strtoupper($fieldName) : strtolower($fieldName); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Verify if exists the Name of a User + * + * @param string $userName Name + * @param string $userUidToExclude Unique id of User to exclude + * + * return bool Return true if exists the Name of a User, false otherwise + */ + public function existsName($userName, $userUidToExclude = "") + { + try { + $criteria = $this->getUserCriteria(); + + if ($userUidToExclude != "") { + $criteria->add(\UsersPeer::USR_UID, $userUidToExclude, \Criteria::NOT_EQUAL); + } + + $criteria->add(\UsersPeer::USR_USERNAME, $userName, \Criteria::EQUAL); + + //QUERY + $rsCriteria = \UsersPeer::doSelectRS($criteria); + + return ($rsCriteria->next())? true : false; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Verify if exists the Name of a User + * + * @param string $userName Name + * @param string $fieldNameForException Field name for the exception + * @param string $userUidToExclude Unique id of User to exclude + * + * return void Throw exception if exists the title of a User + */ + public function throwExceptionIfExistsName($userName, $fieldNameForException, $userUidToExclude = "") + { + try { + if ($this->existsName($userName, $userUidToExclude)) { + throw new \Exception(\G::LoadTranslation("ID_USER_NAME_ALREADY_EXISTS", array($fieldNameForException, $userName))); + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Verify password + * + * @param string $userPassword Password + * @param string $fieldNameForException Field name for the exception + * + * return void Throw exception if password is invalid + */ + public function throwExceptionIfPasswordIsInvalid($userPassword, $fieldNameForException) + { + try { + $result = $this->testPassword($userPassword); + + if (!$result["STATUS"]) { + throw new \Exception($fieldNameForException . ": " . $result["DESCRIPTION"]); + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Validate the data if they are invalid (INSERT and UPDATE) + * + * @param string $userUid Unique id of User + * @param array $arrayData Data + * + * return void Throw exception if data has an invalid value + */ + public function throwExceptionIfDataIsInvalid($userUid, array $arrayData) + { + try { + //Set variables + $arrayUserData = ($userUid == "")? array() : $this->getUser($userUid, true); + $flagInsert = ($userUid == "")? true : false; + + $arrayFinalData = array_merge($arrayUserData, $arrayData); + + //Verify data - Field definition. + $process = new \ProcessMaker\BusinessModel\Process(); + + $process->throwExceptionIfDataNotMetFieldDefinition($arrayData, $this->arrayFieldDefinition, $this->arrayFieldNameForException, $flagInsert); + + //Verify data + if (isset($arrayData["USR_USERNAME"])) { + $this->throwExceptionIfExistsName($arrayData["USR_USERNAME"], $this->arrayFieldNameForException["usrUsername"], $userUid); + } + + if (isset($arrayData["USR_EMAIL"])) { + if (!filter_var($arrayData["USR_EMAIL"], FILTER_VALIDATE_EMAIL)) { + throw new \Exception($this->arrayFieldNameForException["usrEmail"] . ": " . \G::LoadTranslation("ID_INCORRECT_EMAIL")); + } + } + + if (isset($arrayData["USR_NEW_PASS"])) { + $this->throwExceptionIfPasswordIsInvalid($arrayData["USR_NEW_PASS"], $this->arrayFieldNameForException["usrNewPass"]); + + if (!isset($arrayData["USR_CNF_PASS"])) { + throw new \Exception(\G::LoadTranslation("ID_UNDEFINED_VALUE_IS_REQUIRED", array($this->arrayFieldNameForException["usrCnfPass"]))); + } + + if ($arrayData["USR_NEW_PASS"] != $arrayData["USR_CNF_PASS"]) { + throw new \Exception($this->arrayFieldNameForException["usrNewPass"] . ", " . $this->arrayFieldNameForException["usrCnfPass"] . ": " . \G::LoadTranslation("ID_NEW_PASS_SAME_OLD_PASS")); + } + } + + if (isset($arrayData["USR_REPLACED_BY"]) && $arrayData["USR_REPLACED_BY"] != "") { + $obj = \UsersPeer::retrieveByPK($arrayData["USR_REPLACED_BY"]); + + if (is_null($obj)) { + throw new \Exception(\G::LoadTranslation("ID_USER_DOES_NOT_EXIST", array($this->arrayFieldNameForException["usrReplacedBy"], $arrayData["USR_REPLACED_BY"]))); + } + } + + if (isset($arrayData["USR_DUE_DATE"])) { + $arrayUserDueDate = explode("-", $arrayData["USR_DUE_DATE"]); + + if (ctype_digit($arrayUserDueDate[0])) { + if (!checkdate($arrayUserDueDate[1], $arrayUserDueDate[2], $arrayUserDueDate[0])) { + throw new \Exception($this->arrayFieldNameForException["usrDueDate"] . ": " . \G::LoadTranslation("ID_MSG_ERROR_DUE_DATE")); + } + } else { + throw new \Exception($this->arrayFieldNameForException["usrDueDate"] . ": " . \G::LoadTranslation("ID_MSG_ERROR_DUE_DATE")); + } + } + + if (isset($arrayData["USR_ROLE"])) { + require_once (PATH_RBAC_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "Roles.php"); + + $criteria = new \Criteria("rbac"); + + $criteria->add(\RolesPeer::ROL_CODE, $arrayData["USR_ROLE"]); + $rsCriteria = \RolesPeer::doSelectRS($criteria); + + if (!$rsCriteria->next()) { + throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array($this->arrayFieldNameForException["usrRole"]))); + } + } + + if (isset($arrayData["USR_COUNTRY"]) && $arrayData["USR_COUNTRY"] != "") { + $obj = \IsoCountryPeer::retrieveByPK($arrayData["USR_COUNTRY"]); + + if (is_null($obj)) { + throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array($this->arrayFieldNameForException["usrCountry"]))); + } + } + + if (isset($arrayData["USR_CITY"]) && $arrayData["USR_CITY"] != "") { + if (!isset($arrayFinalData["USR_COUNTRY"]) || $arrayFinalData["USR_COUNTRY"] == "") { + throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array($this->arrayFieldNameForException["usrCountry"]))); + } + + $obj = \IsoSubdivisionPeer::retrieveByPK($arrayFinalData["USR_COUNTRY"], $arrayData["USR_CITY"]); + + if (is_null($obj)) { + throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array($this->arrayFieldNameForException["usrCity"]))); + } + } + + if (isset($arrayData["USR_LOCATION"]) && $arrayData["USR_LOCATION"] != "") { + if (!isset($arrayFinalData["USR_COUNTRY"]) || $arrayFinalData["USR_COUNTRY"] == "") { + throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array($this->arrayFieldNameForException["usrCountry"]))); + } + + $obj = \IsoLocationPeer::retrieveByPK($arrayFinalData["USR_COUNTRY"], $arrayData["USR_LOCATION"]); + + if (is_null($obj)) { + throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array($this->arrayFieldNameForException["usrLocation"]))); + } + } + + if (isset($arrayData["USR_CALENDAR"]) && $arrayData["USR_CALENDAR"] != "") { + $obj = \CalendarDefinitionPeer::retrieveByPK($arrayData["USR_CALENDAR"]); + + if (is_null($obj)) { + throw new \Exception(\G::LoadTranslation("ID_CALENDAR_DOES_NOT_EXIST", array($this->arrayFieldNameForException["usrCalendar"], $arrayData["USR_CALENDAR"]))); + } + } + + if (isset($arrayData["DEP_UID"]) && $arrayData["DEP_UID"] != "") { + $department = new \Department(); + + if (!$department->existsDepartment($arrayData["DEP_UID"])) { + throw new \Exception(\G::LoadTranslation("ID_DEPARTMENT_NOT_EXIST", array($this->arrayFieldNameForException["depUid"], $arrayData["DEP_UID"]))); + } + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Verify if does not exist the User in table USERS + * + * @param string $userUid Unique id of Email Server + * @param string $fieldNameForException Field name for the exception + * + * return void Throw exception if does not exist the User in table USERS + */ + public function throwExceptionIfNotExistsUser($userUid, $fieldNameForException) + { + try { + $obj = \UsersPeer::retrieveByPK($userUid); + + if (is_null($obj) || $obj->getUsrUsername() == "") { + throw new \Exception(\G::LoadTranslation("ID_USER_DOES_NOT_EXIST", array($fieldNameForException, $userUid))); + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get data of a from a record + * + * @param array $record Record + * + * return array Return an array with data User + */ + public function getUserDataFromRecord(array $record) + { + try { + return array( + $this->getFieldNameByFormatFieldName("USR_UID") => $record["USR_UID"], + $this->getFieldNameByFormatFieldName("USR_USERNAME") => $record["USR_USERNAME"], + $this->getFieldNameByFormatFieldName("USR_PASSWORD") => $record["USR_PASSWORD"], + $this->getFieldNameByFormatFieldName("USR_FIRSTNAME") => $record["USR_FIRSTNAME"], + $this->getFieldNameByFormatFieldName("USR_LASTNAME") => $record["USR_LASTNAME"], + $this->getFieldNameByFormatFieldName("USR_EMAIL") => $record["USR_EMAIL"], + $this->getFieldNameByFormatFieldName("USR_DUE_DATE") => $record["USR_DUE_DATE"], + $this->getFieldNameByFormatFieldName("USR_CREATE_DATE") => $record["USR_CREATE_DATE"], + $this->getFieldNameByFormatFieldName("USR_UPDATE_DATE") => $record["USR_UPDATE_DATE"], + $this->getFieldNameByFormatFieldName("USR_STATUS") => $record["USR_STATUS"], + $this->getFieldNameByFormatFieldName("USR_COUNTRY") => $record["USR_COUNTRY"], + $this->getFieldNameByFormatFieldName("USR_CITY") => $record["USR_CITY"], + $this->getFieldNameByFormatFieldName("USR_LOCATION") => $record["USR_LOCATION"], + $this->getFieldNameByFormatFieldName("USR_ADDRESS") => $record["USR_ADDRESS"], + $this->getFieldNameByFormatFieldName("USR_PHONE") => $record["USR_PHONE"], + $this->getFieldNameByFormatFieldName("USR_FAX") => $record["USR_FAX"], + $this->getFieldNameByFormatFieldName("USR_CELLULAR") => $record["USR_CELLULAR"], + $this->getFieldNameByFormatFieldName("USR_ZIP_CODE") => $record["USR_ZIP_CODE"], + $this->getFieldNameByFormatFieldName("DEP_UID") => $record["DEP_UID"], + $this->getFieldNameByFormatFieldName("USR_POSITION") => $record["USR_POSITION"], + $this->getFieldNameByFormatFieldName("USR_RESUME") => $record["USR_RESUME"], + $this->getFieldNameByFormatFieldName("USR_BIRTHDAY") => $record["USR_BIRTHDAY"], + $this->getFieldNameByFormatFieldName("USR_ROLE") => $record["USR_ROLE"], + $this->getFieldNameByFormatFieldName("USR_REPORTS_TO") => $record["USR_REPORTS_TO"], + $this->getFieldNameByFormatFieldName("USR_REPLACED_BY") => $record["USR_REPLACED_BY"], + $this->getFieldNameByFormatFieldName("USR_UX") => $record["USR_UX"], + $this->getFieldNameByFormatFieldName("USR_TOTAL_INBOX") => $record["USR_TOTAL_INBOX"], + $this->getFieldNameByFormatFieldName("USR_TOTAL_DRAFT") => $record["USR_TOTAL_DRAFT"], + $this->getFieldNameByFormatFieldName("USR_TOTAL_CANCELLED") => $record["USR_TOTAL_CANCELLED"], + $this->getFieldNameByFormatFieldName("USR_TOTAL_PARTICIPATED") => $record["USR_TOTAL_PARTICIPATED"], + $this->getFieldNameByFormatFieldName("USR_TOTAL_PAUSED") => $record["USR_TOTAL_PAUSED"], + $this->getFieldNameByFormatFieldName("USR_TOTAL_COMPLETED") => $record["USR_TOTAL_COMPLETED"], + $this->getFieldNameByFormatFieldName("USR_TOTAL_UNASSIGNED") => $record["USR_TOTAL_UNASSIGNED"] + ); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get criteria for User + * + * return object + */ + public function getUserCriteria() + { + try { + $criteria = new \Criteria("workflow"); + + $criteria->addSelectColumn(\UsersPeer::USR_UID); + $criteria->addSelectColumn(\UsersPeer::USR_USERNAME); + $criteria->addSelectColumn(\UsersPeer::USR_PASSWORD); + $criteria->addSelectColumn(\UsersPeer::USR_FIRSTNAME); + $criteria->addSelectColumn(\UsersPeer::USR_LASTNAME); + $criteria->addSelectColumn(\UsersPeer::USR_EMAIL); + $criteria->addSelectColumn(\UsersPeer::USR_DUE_DATE); + $criteria->addSelectColumn(\UsersPeer::USR_CREATE_DATE); + $criteria->addSelectColumn(\UsersPeer::USR_UPDATE_DATE); + $criteria->addSelectColumn(\UsersPeer::USR_STATUS); + $criteria->addSelectColumn(\UsersPeer::USR_COUNTRY); + $criteria->addSelectColumn(\UsersPeer::USR_CITY); + $criteria->addSelectColumn(\UsersPeer::USR_LOCATION); + $criteria->addSelectColumn(\UsersPeer::USR_ADDRESS); + $criteria->addSelectColumn(\UsersPeer::USR_PHONE); + $criteria->addSelectColumn(\UsersPeer::USR_FAX); + $criteria->addSelectColumn(\UsersPeer::USR_CELLULAR); + $criteria->addSelectColumn(\UsersPeer::USR_ZIP_CODE); + $criteria->addSelectColumn(\UsersPeer::DEP_UID); + $criteria->addSelectColumn(\UsersPeer::USR_POSITION); + $criteria->addSelectColumn(\UsersPeer::USR_RESUME); + $criteria->addSelectColumn(\UsersPeer::USR_BIRTHDAY); + $criteria->addSelectColumn(\UsersPeer::USR_ROLE); + $criteria->addSelectColumn(\UsersPeer::USR_REPORTS_TO); + $criteria->addSelectColumn(\UsersPeer::USR_REPLACED_BY); + $criteria->addSelectColumn(\UsersPeer::USR_UX); + $criteria->addSelectColumn(\UsersPeer::USR_TOTAL_INBOX); + $criteria->addSelectColumn(\UsersPeer::USR_TOTAL_DRAFT); + $criteria->addSelectColumn(\UsersPeer::USR_TOTAL_CANCELLED); + $criteria->addSelectColumn(\UsersPeer::USR_TOTAL_PARTICIPATED); + $criteria->addSelectColumn(\UsersPeer::USR_TOTAL_PAUSED); + $criteria->addSelectColumn(\UsersPeer::USR_TOTAL_COMPLETED); + $criteria->addSelectColumn(\UsersPeer::USR_TOTAL_UNASSIGNED); + + return $criteria; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Create User + * + * @param array $arrayData Data + * + * return array Return data of the new User created + */ + public function create(array $arrayData) + { + try { + \G::LoadSystem("rbac"); + + //Verify data + $process = new \ProcessMaker\BusinessModel\Process(); + $validator = new \ProcessMaker\BusinessModel\Validator(); + + $validator->throwExceptionIfDataIsNotArray($arrayData, "\$arrayData"); + $validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData"); + + //Set data + $arrayData = array_change_key_case($arrayData, CASE_UPPER); + + unset($arrayData["USR_UID"]); + + $this->throwExceptionIfDataIsInvalid("", $arrayData); + + //Create + $cnn = \Propel::getConnection("workflow"); + + try { + $rbac = new \RBAC(); + $user = new \Users(); + + $rbac->initRBAC(); + + $arrayData["USR_PASSWORD"] = \Bootstrap::hashPassword($arrayData["USR_NEW_PASS"]); + + $arrayData["USR_BIRTHDAY"] = (isset($arrayData["USR_BIRTHDAY"]))? $arrayData["USR_BIRTHDAY"] : date("Y-m-d"); + $arrayData["USR_LOGGED_NEXT_TIME"] = (isset($arrayData["USR_LOGGED_NEXT_TIME"]))? $arrayData["USR_LOGGED_NEXT_TIME"] : 0; + $arrayData["USR_CREATE_DATE"] = date("Y-m-d H:i:s"); + $arrayData["USR_UPDATE_DATE"] = date("Y-m-d H:i:s"); + + //Create in rbac + //$userStatus = $arrayData["USR_STATUS"]; + // + //if ($arrayData["USR_STATUS"] == "ACTIVE") { + // $arrayData["USR_STATUS"] = 1; + //} + // + //if ($arrayData["USR_STATUS"] == "INACTIVE") { + // $arrayData["USR_STATUS"] = 0; + //} + // + //$userUid = $this->createUser($arrayData); + // + //if ($arrayData["USR_ROLE"] != "") { + // $this->assignRoleToUser($userUid, $arrayData["USR_ROLE"]); + //} + // + //$arrayData["USR_STATUS"] = $userStatus; + + $userUid = $rbac->createUser($arrayData, $arrayData["USR_ROLE"]); + + //Create in workflow + $arrayData["USR_UID"] = $userUid; + $arrayData["USR_PASSWORD"] = "00000000000000000000000000000000"; + + $result = $user->create($arrayData); + + //User Properties + $userProperty = new \UsersProperties(); + + $aUserProperty = $userProperty->loadOrCreateIfNotExists($arrayData["USR_UID"], array("USR_PASSWORD_HISTORY" => serialize(array(\Bootstrap::hashPassword($arrayData["USR_PASSWORD"]))))); + $aUserProperty["USR_LOGGED_NEXT_TIME"] = $arrayData["USR_LOGGED_NEXT_TIME"]; + + $userProperty->update($aUserProperty); + + //Save Calendar assigment + if (isset($arrayData["USR_CALENDAR"])) { + //Save Calendar ID for this user + \G::LoadClass("calendar"); + + $calendar = new \Calendar(); + $calendar->assignCalendarTo($arrayData["USR_UID"], $arrayData["USR_CALENDAR"], "USER"); + } + + //Return + return $this->getUser($userUid); + } catch (\Exception $e) { + $cnn->rollback(); + + throw $e; + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Update User + * + * @param string $userUid Unique id of User + * @param array $arrayData Data + * @param string $userUidLogged Unique id of User logged + * + * return array Return data of the User updated + */ + public function update($userUid, array $arrayData, $userUidLogged) + { + try { + \G::LoadSystem("rbac"); + + //Verify data + $process = new \ProcessMaker\BusinessModel\Process(); + $validator = new \ProcessMaker\BusinessModel\Validator(); + + $validator->throwExceptionIfDataIsNotArray($arrayData, "\$arrayData"); + $validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData"); + + //Set data + $arrayData = array_change_key_case($arrayData, CASE_UPPER); + $arrayDataBackup = $arrayData; + + //Verify data + $this->throwExceptionIfNotExistsUser($userUid, $this->arrayFieldNameForException["usrUid"]); + + $this->throwExceptionIfDataIsInvalid($userUid, $arrayData); + + //Permission Admin + $countPermission = 0; + + $permission = $this->loadUserRolePermission("PROCESSMAKER", $userUidLogged); + + foreach ($permission as $key => $value) { + if ($value["PER_CODE"] == "PM_USERS") { + $countPermission = $countPermission + 1; + } + } + + if ($countPermission != 1) { + throw new \Exception(\G::LoadTranslation("ID_USER_CAN_NOT_UPDATE", array($userUidLogged))); + } + + //Update + $cnn = \Propel::getConnection("workflow"); + + try { + $rbac = new \RBAC(); + $user = new \Users(); + + $rbac->initRBAC(); + + if (isset($arrayData["USR_NEW_PASS"])) { + $arrayData["USR_PASSWORD"] = \Bootstrap::hashPassword($arrayData["USR_NEW_PASS"]); + } + + $arrayData["USR_UID"] = $userUid; + $arrayData["USR_LOGGED_NEXT_TIME"] = (isset($arrayData["USR_LOGGED_NEXT_TIME"]))? $arrayData["USR_LOGGED_NEXT_TIME"] : 0; + $arrayData["USR_UPDATE_DATE"] = date("Y-m-d H:i:s"); + + $flagUserLoggedNextTime = false; + + if (isset($arrayData["USR_PASSWORD"])) { + if ($arrayData["USR_PASSWORD"] != "") { + //require_once 'classes/model/UsersProperties.php'; + + $userProperty = new \UsersProperties(); + $aUserProperty = $userProperty->loadOrCreateIfNotExists($userUid, array("USR_PASSWORD_HISTORY" => serialize(array(\Bootstrap::hashPassword($arrayData["USR_PASSWORD"]))))); + + //$memKey = "rbacSession" . session_id(); + //$memcache = & \PMmemcached::getSingleton(defined("SYS_SYS")? SYS_SYS : ""); + // + //if (($rbac->aUserInfo = $memcache->get($memKey)) == false) { + // $rbac->loadUserRolePermission("PROCESSMAKER", $userUidLogged); + // $memcache->set($memKey, $rbac->aUserInfo, \PMmemcached::EIGHT_HOURS); + //} + + if ($rbac->aUserInfo["PROCESSMAKER"]["ROLE"]["ROL_CODE"] == "PROCESSMAKER_ADMIN") { + $aUserProperty["USR_LAST_UPDATE_DATE"] = date("Y-m-d H:i:s"); + $aUserProperty["USR_LOGGED_NEXT_TIME"] = $arrayData["USR_LOGGED_NEXT_TIME"]; + $userProperty->update($aUserProperty); + } + + $aHistory = unserialize($aUserProperty["USR_PASSWORD_HISTORY"]); + + if (!is_array($aHistory)) { + $aHistory = array(); + } + + if (!defined("PPP_PASSWORD_HISTORY")) { + define("PPP_PASSWORD_HISTORY", 0); + } + + if (PPP_PASSWORD_HISTORY > 0) { + //it's looking a password igual into aHistory array that was send for post in md5 way + $c = 0; + $sw = 1; + + while (count($aHistory) >= 1 && count($aHistory) > $c && $sw) { + if (strcmp(trim($aHistory[$c]), trim($arrayData['USR_PASSWORD'])) == 0) { + $sw = 0; + } + + $c++; + } + + if ($sw == 0) { + $sDescription = G::LoadTranslation("ID_POLICY_ALERT") . ":\n\n"; + $sDescription = $sDescription . " - " . G::LoadTranslation("PASSWORD_HISTORY") . ": " . PPP_PASSWORD_HISTORY . "\n"; + $sDescription = $sDescription . "\n" . G::LoadTranslation("ID_PLEASE_CHANGE_PASSWORD_POLICY") . ""; + + throw new \Exception($this->arrayFieldNameForException["usrNewPass"] . ", " . $this->arrayFieldNameForException["usrCnfPass"] . ": " . $sDescription); + } + + if (count($aHistory) >= PPP_PASSWORD_HISTORY) { + $sLastPassw = array_shift($aHistory); + } + + $aHistory[] = $arrayData["USR_PASSWORD"]; + } + + $aUserProperty["USR_LAST_UPDATE_DATE"] = date("Y-m-d H:i:s"); + $aUserProperty["USR_LOGGED_NEXT_TIME"] = $arrayData["USR_LOGGED_NEXT_TIME"]; + $aUserProperty["USR_PASSWORD_HISTORY"] = serialize($aHistory); + $userProperty->update($aUserProperty); + } else { + $flagUserLoggedNextTime = true; + } + } else { + $flagUserLoggedNextTime = true; + } + + if ($flagUserLoggedNextTime) { + //require_once "classes/model/Users.php"; + $oUser = new \Users(); + $aUser = $oUser->load($userUid); + //require_once "classes/model/UsersProperties.php"; + $oUserProperty = new \UsersProperties(); + $aUserProperty = $oUserProperty->loadOrCreateIfNotExists($userUid, array("USR_PASSWORD_HISTORY" => serialize(array($aUser["USR_PASSWORD"])))); + $aUserProperty["USR_LOGGED_NEXT_TIME"] = $arrayData["USR_LOGGED_NEXT_TIME"]; + $oUserProperty->update($aUserProperty); + } + + //Update in rbac + if (isset($arrayData["USR_ROLE"])) { + $rbac->updateUser($arrayData, $arrayData["USR_ROLE"]); + } else { + $rbac->updateUser($arrayData); + } + + //Update in workflow + $result = $user->update($arrayData); + + //Save Calendar assigment + if (isset($arrayData["USR_CALENDAR"])) { + //Save Calendar ID for this user + \G::LoadClass("calendar"); + + $calendar = new \Calendar(); + $calendar->assignCalendarTo($userUid, $arrayData["USR_CALENDAR"], "USER"); + } + + //Return + $arrayData = $arrayDataBackup; + + if (!$this->formatFieldNameInUppercase) { + $arrayData = array_change_key_case($arrayData, CASE_LOWER); + } + + return $arrayData; + } catch (\Exception $e) { + $cnn->rollback(); + + throw $e; + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get data of a User + * + * @param string $userUid Unique id of User + * @param bool $flagGetRecord Value that set the getting + * + * return array Return an array with data of a User + */ + public function getUser($userUid, $flagGetRecord = false) + { + try { + //Verify data + $this->throwExceptionIfNotExistsUser($userUid, $this->arrayFieldNameForException["usrUid"]); + + //Get data + //SQL + $criteria = $this->getUserCriteria(); + + $criteria->add(\UsersPeer::USR_UID, $userUid, \Criteria::EQUAL); + + $rsCriteria = \UsersPeer::doSelectRS($criteria); + $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); + + $rsCriteria->next(); + + $row = $rsCriteria->getRow(); + + //Return + return (!$flagGetRecord)? $this->getUserDataFromRecord($row) : $row; + } catch (\Exception $e) { + throw $e; + } + } + //--- /--- + /** * Create User Uid * @@ -71,7 +809,7 @@ class User $oUserProperty = new \UsersProperties(); $aFields = array(); $dateNow = date('Y-m-d H:i:s'); - $aErrors = $oUserProperty->validatePassword($sPassword, $dateNow, $dateNow); + $aErrors = $oUserProperty->validatePassword($sPassword, $dateNow, 0); if (!empty($aErrors)) { if (!defined('NO_DISPLAY_USERNAME')) { define('NO_DISPLAY_USERNAME', 1); @@ -198,463 +936,6 @@ class User return $fieldsPermissions; } - /** - * Create User - * - * @param array $arrayUserData Data - * - * return array Return data of the new User created - */ - public function create($arrayUserData) - { - try { - require_once (PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "Users.php"); - $arrayUserData = array_change_key_case($arrayUserData, CASE_UPPER); - $form = $arrayUserData; - if (array_key_exists('USR_REPLACED_BY', $form)) { - if ($form['USR_REPLACED_BY'] != '') { - $oReplacedBy = \UsersPeer::retrieveByPK($form['USR_REPLACED_BY']); - if (is_null($oReplacedBy)) { - throw new \Exception(\G::LoadTranslation("ID_USER_DOES_NOT_EXIST", array(strtolower("USR_REPLACED_BY"), $form["USR_REPLACED_BY"]))); - } - } - } - if (array_key_exists('USR_COUNTRY', $form)) { - if ($form['USR_COUNTRY'] != '') { - $oCountry = \IsoCountryPeer::retrieveByPK($form['USR_COUNTRY']); - if (is_null($oCountry)) { - throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array('usr_country'))); - } - } - } - if (array_key_exists('USR_CITY', $form)) { - if ($form['USR_CITY'] != '') { - $oCity = \IsoSubdivisionPeer::retrieveByPK($form['USR_COUNTRY'], $form['USR_CITY']); - if (is_null($oCity)) { - throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array('usr_city'))); - } - } - } - if (array_key_exists('USR_LOCATION', $form)) { - if ($form['USR_LOCATION'] != '') { - $oLocation = \IsoLocationPeer::retrieveByPK($form['USR_COUNTRY'], $form['USR_LOCATION']); - if (is_null($oLocation)) { - throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array('usr_location'))); - } - } - } - if (isset($arrayUserData['USR_UID'])) { - $form['USR_UID'] = $arrayUserData['USR_UID']; - } else { - $form['USR_UID'] = ''; - } - if (array_key_exists('USR_NEW_PASS', $form)) { - $sConfirm = $this->testPassword($form['USR_NEW_PASS']); - if ($sConfirm['STATUS'] != 1) { - throw new \Exception('usr_new_pass. '.$sConfirm['DESCRIPTION']); - } - } - if (array_key_exists('USR_NEW_PASS', $form)) { - if ($form['USR_NEW_PASS'] != $form['USR_CNF_PASS']) { - throw new \Exception('usr_new_pass or usr_cnf_pass. '.\G::LoadTranslation('ID_NEW_PASS_SAME_OLD_PASS')); - } - } - if (array_key_exists('USR_NEW_PASS', $form)) { - $form['USR_PASSWORD'] = md5($form['USR_NEW_PASS']); - } - if (!isset($form['USR_CITY'])) { - $form['USR_CITY'] = ''; - } - if (!isset($form['USR_LOCATION'])) { - $form['USR_LOCATION'] = ''; - } - if (!isset($form['USR_AUTH_USER_DN'])) { - $form['USR_AUTH_USER_DN'] = ''; - } - $criteria = new \Criteria(); - $criteria->addSelectColumn(\UsersPeer::USR_USERNAME); - if (array_key_exists('USR_USERNAME', $form)) { - $criteria->add(\UsersPeer::USR_USERNAME, utf8_encode($arrayUserData['USR_USERNAME'])); - } else { - throw new \Exception('usr_name. '.\G::LoadTranslation('ID_MSG_ERROR_USR_USERNAME')); - } - if (\UsersPeer::doCount($criteria) > 0) { - throw new \Exception('usr_username. '.\G::LoadTranslation('ID_USERNAME_ALREADY_EXISTS', array('USER_ID' => $arrayUserData['USR_USERNAME']))); - } - if ($form['USR_USERNAME'] == '') { - throw new \Exception('usr_name. '.\G::LoadTranslation('ID_MSG_ERROR_USR_USERNAME')); - } else { - $userData['USR_USERNAME'] = $form['USR_USERNAME']; - } - $userData['USR_PASSWORD'] = $form['USR_PASSWORD']; - if ($form['USR_FIRSTNAME'] == '') { - throw new \Exception('usr_firstname. '.\G::LoadTranslation('ID_MSG_ERROR_USR_FIRSTNAME')); - } else { - $userData['USR_FIRSTNAME'] = $form['USR_FIRSTNAME']; - } - if ($form['USR_LASTNAME'] == '') { - throw new \Exception('usr_lastname. '.\G::LoadTranslation('ID_MSG_ERROR_USR_LASTNAME')); - } else { - $userData['USR_LASTNAME'] = $form['USR_LASTNAME']; - } - if ($form['USR_EMAIL'] == '') { - throw new \Exception(\G::LoadTranslation("ID_CAN_NOT_BE_NULL", array('usr_email'))); - } else { - if (!filter_var($form['USR_EMAIL'], FILTER_VALIDATE_EMAIL)) { - throw new \Exception('usr_email. '.\G::LoadTranslation('ID_INCORRECT_EMAIL')); - } else { - $userData['USR_EMAIL'] = $form['USR_EMAIL']; - } - } - if ($form['USR_DUE_DATE'] == '') { - throw new \Exception('usr_due_date. '.\G::LoadTranslation('ID_MSG_ERROR_DUE_DATE')); - } else { - $dueDate = explode("-", $form['USR_DUE_DATE']); - if (ctype_digit($dueDate[0])) { - if (checkdate($dueDate[1], $dueDate[2], $dueDate[0]) == false) { - throw new \Exception('usr_due_date. '.\G::LoadTranslation('ID_MSG_ERROR_DUE_DATE')); - } else { - $userData['USR_DUE_DATE'] = $form['USR_DUE_DATE']; - } - } else { - throw new \Exception('usr_due_date. '.\G::LoadTranslation('ID_MSG_ERROR_DUE_DATE')); - } - } - $userData['USR_CREATE_DATE'] = date('Y-m-d H:i:s'); - $userData['USR_UPDATE_DATE'] = date('Y-m-d H:i:s'); - $userData['USR_BIRTHDAY'] = date('Y-m-d'); - $userData['USR_AUTH_USER_DN'] = $form['USR_AUTH_USER_DN']; - $statusWF = $form['USR_STATUS']; - if ($form['USR_STATUS'] == '') { - throw new \Exception(\G::LoadTranslation("ID_CAN_NOT_BE_NULL", array('usr_status'))); - } else { - if ($form['USR_STATUS'] == 'ACTIVE' || $form['USR_STATUS'] == 'INACTIVE' || $form['USR_STATUS'] == 'VACATION') { - $userData['USR_STATUS'] = $form['USR_STATUS']; - } else { - throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array('usr_status'))); - } - } - if ($form['USR_ROLE'] == '') { - throw new \Exception(\G::LoadTranslation("ID_CAN_NOT_BE_NULL", array('usr_role'))); - } else { - require_once (PATH_RBAC_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "Roles.php"); - $oCriteria = new \Criteria('rbac'); - $oCriteria->add(\RolesPeer::ROL_CODE, $form['USR_ROLE']); - $oDataset = \RolesPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(\ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - if ($oDataset->getRow()) { - $userData['USR_ROLE'] = $form['USR_ROLE']; - } else { - throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array('usr_role'))); - } - } - try { - if ($userData['USR_STATUS'] == 'ACTIVE') { - $userData['USR_STATUS'] = 1; - } - if ($userData['USR_STATUS'] == 'INACTIVE') { - $userData['USR_STATUS'] = 0; - } - $sUserUID = $this->createUser($userData); - if ($form['USR_ROLE'] != '') { - $this->assignRoleToUser($sUserUID, $form['USR_ROLE']); - } - } catch(Exception $oError) { - throw new \Exception($oError->getMessage()); - } - $userData['USR_STATUS'] = $statusWF; - $userData['USR_UID'] = $sUserUID; - $userData['USR_COUNTRY'] = $form['USR_COUNTRY']; - $userData['USR_CITY'] = $form['USR_CITY']; - $userData['USR_LOCATION'] = $form['USR_LOCATION']; - $userData['USR_ADDRESS'] = $form['USR_ADDRESS']; - $userData['USR_PHONE'] = $form['USR_PHONE']; - $userData['USR_ZIP_CODE'] = $form['USR_ZIP_CODE']; - $userData['USR_POSITION'] = $form['USR_POSITION']; - $userData['USR_REPLACED_BY'] = $form['USR_REPLACED_BY']; - $oUser = new \Users(); - $oUser -> create( $userData ); - if ((isset($form['USR_CALENDAR']))) { - //Save Calendar ID for this user - \G::LoadClass("calendar"); - $calendarObj = new \Calendar(); - $calendarObj->assignCalendarTo($sUserUID, $form['USR_CALENDAR'], 'USER'); - } - $oCriteria = $this->getUser($sUserUID); - return $oCriteria; - } catch (\Exception $e) { - throw $e; - } - } - - /** - * Update User - * - * @param string $usrUid Unique id of User - * @param array $arrayUserData Data - * @param string $usrLoggedUid Unique id of User logged - * - * return array Return data of the User updated - */ - public function update($usrUid, $arrayUserData, $usrLoggedUid) - { - try { - global $RBAC; - $arrayUserData = array_change_key_case($arrayUserData, CASE_UPPER); - $form = $arrayUserData; - $countPermission = 0; - $permission = $this->loadUserRolePermission('PROCESSMAKER', $usrLoggedUid); - foreach ($permission as $key => $value) { - if ($value["PER_CODE"] == 'PM_USERS') { - $countPermission+=1; - } - } - if ($countPermission != 1) { - throw new \Exception(\G::LoadTranslation("ID_USER_CAN_NOT_UPDATE", array($usrLoggedUid))); - } - if (isset($arrayUserData['USR_USERNAME'])) { - $criteria = new \Criteria(); - $criteria->addSelectColumn(\UsersPeer::USR_USERNAME); - $criteria->add(\UsersPeer::USR_USERNAME, utf8_encode($arrayUserData['USR_USERNAME'])); - if (\UsersPeer::doCount($criteria) > 0) { - throw new \Exception('usr_username. '.\G::LoadTranslation('ID_USERNAME_ALREADY_EXISTS', array('USER_ID' => $arrayUserData['USR_USERNAME']))); - } - if ($form['USR_USERNAME'] != '') { - $userData['USR_USERNAME'] = $form['USR_USERNAME']; - } - } - if (isset($usrUid)) { - $form['USR_UID'] = $usrUid; - } else { - $form['USR_UID'] = ''; - } - if (!isset($form['USR_NEW_PASS'])) { - $form['USR_NEW_PASS'] = ''; - } - if ($form['USR_NEW_PASS'] != '') { - $form['USR_PASSWORD'] = md5($form['USR_NEW_PASS']); - } - if (!isset($form['USR_AUTH_USER_DN'])) { - $form['USR_AUTH_USER_DN'] = ''; - } - $userData['USR_UID'] = $form['USR_UID']; - - if (isset($form['USR_PASSWORD'])) { - if ($form['USR_PASSWORD'] != '') { - if ($form['USR_NEW_PASS'] != $form['USR_CNF_PASS']) { - throw new \Exception('usr_new_pass or usr_cnf_pass. '.\G::LoadTranslation('ID_NEW_PASS_SAME_OLD_PASS')); - } - $userData['USR_PASSWORD'] = $form['USR_PASSWORD']; - require_once (PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "UsersProperties.php"); - $oUserProperty = new \UsersProperties(); - $aUserProperty = $oUserProperty->loadOrCreateIfNotExists($form['USR_UID'], array('USR_PASSWORD_HISTORY' => serialize(array(md5($form['USR_PASSWORD']))))); - $memKey = 'rbacSession' . session_id(); - $memcache = & \PMmemcached::getSingleton(defined('SYS_SYS') ? SYS_SYS : '' ); - if (($RBAC->aUserInfo = $memcache->get($memKey)) === false) { - $this->loadUserRolePermission('PROCESSMAKER', $usrLoggedUid); - $memcache->set($memKey, $RBAC->aUserInfo, \PMmemcached::EIGHT_HOURS); - } - if ($RBAC->aUserInfo['PROCESSMAKER']['ROLE']['ROL_CODE'] == 'PROCESSMAKER_ADMIN') { - $aUserProperty['USR_LAST_UPDATE_DATE'] = date('Y-m-d H:i:s'); - $aUserProperty['USR_LOGGED_NEXT_TIME'] = 1; - $oUserProperty->update($aUserProperty); - } - $aErrors = $oUserProperty->validatePassword($form['USR_NEW_PASS'], $aUserProperty['USR_LAST_UPDATE_DATE'], 0); - if (count($aErrors) > 0) { - $sDescription = \G::LoadTranslation('ID_POLICY_ALERT') . ':,'; - foreach ($aErrors as $sError) { - switch ($sError) { - case 'ID_PPP_MINIMUN_LENGTH': - $sDescription .= ' - ' . \G::LoadTranslation($sError) . ': ' . PPP_MINIMUN_LENGTH . '. '; - break; - case 'ID_PPP_MAXIMUN_LENGTH': - $sDescription .= ' - ' . \G::LoadTranslation($sError) . ': ' . PPP_MAXIMUN_LENGTH . '. '; - break; - case 'ID_PPP_EXPIRATION_IN': - $sDescription .= ' - ' . \G::LoadTranslation($sError) . ' ' . PPP_EXPIRATION_IN . ' ' . G::LoadTranslation('ID_DAYS') . '. '; - break; - default: - $sDescription .= ' - ' . \G::LoadTranslation($sError) . ','; - break; - } - } - $sDescription .= '' . \G::LoadTranslation('ID_PLEASE_CHANGE_PASSWORD_POLICY'); - throw new \Exception('usr_new_pass or usr_cnf_pass. '.$sDescription); - } - $aHistory = unserialize($aUserProperty['USR_PASSWORD_HISTORY']); - if (!is_array($aHistory)) { - $aHistory = array(); - } - if (!defined('PPP_PASSWORD_HISTORY')) { - define('PPP_PASSWORD_HISTORY', 0); - } - if (PPP_PASSWORD_HISTORY > 0) { - //it's looking a password igual into aHistory array that was send for post in md5 way - $c = 0; - $sw = 1; - while (count($aHistory) >= 1 && count($aHistory) > $c && $sw) { - if (strcmp(trim($aHistory[$c]), trim($form['USR_PASSWORD'])) == 0) { - $sw = 0; - } - $c++; - } - if ($sw == 0) { - $sDescription = \G::LoadTranslation('ID_POLICY_ALERT') . ':

'; - $sDescription .= ' - ' . \G::LoadTranslation('PASSWORD_HISTORY') . ': ' . PPP_PASSWORD_HISTORY . '
'; - $sDescription .= '
' . \G::LoadTranslation('ID_PLEASE_CHANGE_PASSWORD_POLICY') . ''; - throw new \Exception('usr_new_pass or usr_cnf_pass. '.$sDescription); - } - if (count($aHistory) >= PPP_PASSWORD_HISTORY) { - $sLastPassw = array_shift($aHistory); - } - $aHistory[] = $form['USR_PASSWORD']; - } - $aUserProperty['USR_LAST_UPDATE_DATE'] = date('Y-m-d H:i:s'); - $aUserProperty['USR_LOGGED_NEXT_TIME'] = 1; - $aUserProperty['USR_PASSWORD_HISTORY'] = serialize($aHistory); - $oUserProperty->update($aUserProperty); - } - } - if (isset($form['USR_FIRSTNAME'])) { - if ($form['USR_FIRSTNAME'] != '') { - $userData['USR_FIRSTNAME'] = $form['USR_FIRSTNAME']; - } - } - if (isset($form['USR_LASTNAME'])) { - if ($form['USR_LASTNAME'] != '') { - $userData['USR_LASTNAME'] = $form['USR_LASTNAME']; - } - } - if (isset($form['USR_EMAIL'])) { - if ($form['USR_EMAIL'] != '') { - if (!filter_var($form['USR_EMAIL'], FILTER_VALIDATE_EMAIL)) { - throw new \Exception('usr_email. '.\G::LoadTranslation('ID_INCORRECT_EMAIL')); - } else { - $userData['USR_EMAIL'] = $form['USR_EMAIL']; - } - } - } - if (isset($form['USR_DUE_DATE'])) { - if ($form['USR_DUE_DATE'] != '') { - $dueDate = explode("-", $form['USR_DUE_DATE']); - if (ctype_digit($dueDate[0])) { - if (checkdate($dueDate[1], $dueDate[2], $dueDate[0]) == false) { - throw new \Exception('usr_due_date. '.\G::LoadTranslation('ID_MSG_ERROR_DUE_DATE')); - } else { - $userData['USR_DUE_DATE'] = $form['USR_DUE_DATE']; - } - } else { - throw new \Exception('usr_due_date. '.\G::LoadTranslation('ID_MSG_ERROR_DUE_DATE')); - } - } - } - $userData['USR_UPDATE_DATE'] = date('Y-m-d H:i:s'); - if (isset($form['USR_STATUS'])) { - if ($form['USR_STATUS'] != '') { - $userData['USR_STATUS'] = $form['USR_STATUS']; - } - } - if (isset($form['USR_ROLE'])) { - if ($form['USR_ROLE'] != '') { - require_once (PATH_RBAC_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "Roles.php"); - $oCriteria = new \Criteria('rbac'); - $oCriteria->add(\RolesPeer::ROL_CODE, $form['USR_ROLE']); - $oDataset = \RolesPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(\ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - if ($oDataset->getRow()) { - $userData['USR_ROLE'] = $form['USR_ROLE']; - } else { - throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array('usr_role'))); - } - $this->updateUser($userData, $form['USR_ROLE']); - } else { - $this->updateUser($userData); - } - } else { - $user = new \Users(); - $dataUser = $user->load($usrUid); - $this->updateUser($userData, $dataUser['USR_ROLE']); - } - if (isset($form['USR_COUNTRY'])) { - if ($form['USR_COUNTRY'] != '') { - $oReplacedBy = \IsoCountryPeer::retrieveByPK($form['USR_COUNTRY']); - if (is_null($oReplacedBy)) { - throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array($form['USR_COUNTRY']))); - } else { - $userData['USR_COUNTRY'] = $form['USR_COUNTRY']; - $userData['USR_CITY'] = ''; - $userData['USR_LOCATION'] = ''; - } - } - } - if (isset($form['USR_CITY'])) { - if ($form['USR_CITY'] != '') { - $oCity = \IsoSubdivisionPeer::retrieveByPK($form['USR_COUNTRY'], $form['USR_CITY']); - if (is_null($oCity)) { - throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array($form['USR_CITY']))); - } else { - $userData['USR_CITY'] = $form['USR_CITY']; - } - } - } - if (isset($form['USR_LOCATION'])) { - if ($form['USR_LOCATION'] != '') { - $oLocation = \IsoLocationPeer::retrieveByPK($form['USR_COUNTRY'], $form['USR_LOCATION']); - if (is_null($oLocation)) { - throw new \Exception(\G::LoadTranslation("ID_INVALID_VALUE_FOR", array($form['USR_LOCATION']))); - } else { - $userData['USR_LOCATION'] = $form['USR_LOCATION']; - } - } - } - if (isset($form['USR_ADDRESS'])) { - $userData['USR_ADDRESS'] = $form['USR_ADDRESS']; - } - if (isset($form['USR_PHONE'])) { - $userData['USR_PHONE'] = $form['USR_PHONE']; - } - if (isset($form['USR_FAX'])) { - $userData['USR_FAX'] = $form['USR_FAX']; - } - if (isset($form['USR_CELLULAR'])) { - $userData['USR_CELLULAR'] = $form['USR_CELLULAR']; - } - if (isset($form['USR_ZIP_CODE'])) { - $userData['USR_ZIP_CODE'] = $form['USR_ZIP_CODE']; - } - if (isset($form['USR_POSITION'])) { - $userData['USR_POSITION'] = $form['USR_POSITION']; - } - if (isset($form['USR_ROLE'])) { - if ($form['USR_ROLE'] != '') { - $userData['USR_ROLE'] = $form['USR_ROLE']; - } - } - if (isset($form['USR_REPLACED_BY'])) { - if ($form['USR_REPLACED_BY'] != '') { - $oReplacedBy = \UsersPeer::retrieveByPK($form['USR_REPLACED_BY']); - if (is_null($oReplacedBy)) { - throw new \Exception('usr_replaced_by:'.$form['USR_REPLACED_BY'].' '.\G::LoadTranslation('ID_AUTHENTICATION_SOURCE_INVALID')); - } else { - $userData['USR_REPLACED_BY'] = $form['USR_REPLACED_BY']; - } - } - } - if (isset($form['USR_AUTH_USER_DN'])) { - $userData['USR_AUTH_USER_DN'] = $form['USR_AUTH_USER_DN']; - } - require_once (PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "Users.php"); - $oUser = new \Users(); - $oUser->update($userData); - $oCriteria = $this->getUser($usrUid); - return $oCriteria; - } catch (\Exception $e) { - throw $e; - } - } - /** * Authenticate User * @@ -681,6 +962,9 @@ class User public function delete($usrUid) { try { + //Verify data + $this->throwExceptionIfNotExistsUser($usrUid, $this->arrayFieldNameForException["usrUid"]); + \G::LoadClass('case'); $oProcessMap = new \Cases(); $USR_UID = $usrUid; @@ -739,7 +1023,9 @@ class User try { $aUserInfo = array(); require_once (PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "Users.php"); - $oCriteria = new \Criteria(); + + $oCriteria = $this->getUserCriteria(); + if ($filter != '') { $oCriteria->add( $oCriteria->getNewCriterion( \UsersPeer::USR_USERNAME, "%$filter%", \Criteria::LIKE )->addOr( $oCriteria->getNewCriterion( \UsersPeer::USR_FIRSTNAME, "%$filter%", \Criteria::LIKE ) )->addOr( $oCriteria->getNewCriterion( \UsersPeer::USR_LASTNAME, "%$filter%", \Criteria::LIKE ) ) ); } @@ -776,40 +1062,6 @@ class User } } - /** - * Get data of a User - * - * @param string $userUid Unique id of User - * - * return array Return an array with data of a User - */ - public function getUser($userUid) - { - try { - $filter = ''; - $aUserInfo = array(); - Validator::usrUid($userUid, '$usr_uid'); - require_once (PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "Users.php"); - $oCriteria = new \Criteria(); - if ($filter != '') { - $oCriteria->add( $oCriteria->getNewCriterion( \UsersPeer::USR_USERNAME, "%$filter%", \Criteria::LIKE )->addOr( $oCriteria->getNewCriterion( \UsersPeer::USR_FIRSTNAME, "%$filter%", \Criteria::LIKE ) )->addOr( $oCriteria->getNewCriterion( \UsersPeer::USR_LASTNAME, "%$filter%", \Criteria::LIKE ) ) ); - } - $oCriteria->add(\UsersPeer::USR_UID, $userUid); - $oCriteria->add(\UsersPeer::USR_STATUS, 'CLOSED', \Criteria::ALT_NOT_EQUAL); - $oDataset = \UsersPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(\ResultSet::FETCHMODE_ASSOC); - while ($oDataset->next()) { - $aRow1 = $oDataset->getRow(); - $aRow1 = array_change_key_case($aRow1, CASE_LOWER); - $aUserInfo = $aRow1; - } - //Return - return $aUserInfo; - } catch (\Exception $e) { - throw $e; - } - } - /** * Upload image User * @@ -819,6 +1071,17 @@ class User public function uploadImage($userUid) { try { + //Verify data + $this->throwExceptionIfNotExistsUser($userUid, $this->arrayFieldNameForException["usrUid"]); + + if (!$_FILES) { + throw new \Exception(\G::LoadTranslation("ID_UPLOAD_ERR_NO_FILE")); + } + + if (!isset($_FILES["USR_PHOTO"])) { + throw new \Exception(\G::LoadTranslation("ID_UNDEFINED_VALUE_IS_REQUIRED", array($this->arrayFieldNameForException["usrPhoto"]))); + } + if ($_FILES['USR_PHOTO']['error'] != 1) { if ($_FILES['USR_PHOTO']['tmp_name'] != '') { $aAux = explode('.', $_FILES['USR_PHOTO']['name']);