From 255a9526d205b8bc09e7957ad3feedbd8b07478f Mon Sep 17 00:00:00 2001 From: Marco Antonio Nina Mena Date: Tue, 6 Mar 2018 16:41:21 -0400 Subject: [PATCH] HOR-4391 Error when creating a role by End Point - Create rol Code Style Fix CR Fix CR Add validation Render format date --- rbac/engine/classes/model/Roles.php | 1134 ++++++++++------- .../src/ProcessMaker/BusinessModel/Role.php | 366 +++--- .../src/ProcessMaker/Services/Api/Role.php | 99 +- workflow/engine/templates/roles/rolesList.js | 7 +- 4 files changed, 959 insertions(+), 647 deletions(-) diff --git a/rbac/engine/classes/model/Roles.php b/rbac/engine/classes/model/Roles.php index c6adac3f0..871ace93a 100644 --- a/rbac/engine/classes/model/Roles.php +++ b/rbac/engine/classes/model/Roles.php @@ -1,31 +1,7 @@ . - * - * For more information, contact Colosa Inc, 2566 Le Jeune Rd., - * Coral Gables, FL, 33134, USA, or email info@colosa.com. - * + * @access public */ - /** - * @access public - */ require_once 'classes/model/Permissions.php'; require_once 'classes/model/Systems.php'; require_once 'classes/model/RolesPermissions.php'; @@ -48,87 +24,112 @@ require_once 'classes/model/Content.php'; * * @package rbac-classes-model */ -class Roles extends BaseRoles { +class Roles extends BaseRoles +{ public $rol_name; - /** - * Function load - * access public - */ - public function load($Uid) { + /** + * Function load role by uid + * + * @param string $rolUid + * + * @return array + * @throws Exception + */ + public function load($rolUid) + { try { - $oRow = RolesPeer::retrieveByPK($Uid); - if (! is_null($oRow)) { - $aFields = $oRow->toArray(BasePeer::TYPE_FIELDNAME); + $row = RolesPeer::retrieveByPK($rolUid); + if ($row) { + $aFields = $row->toArray(BasePeer::TYPE_FIELDNAME); $this->fromArray($aFields, BasePeer::TYPE_FIELDNAME); $this->setNew(false); $this->getRolName(); - $aFields['ROL_NAME'] = ($this->rol_name != '' ? $this->rol_name: $this->getRolCode()); + $aFields['ROL_NAME'] = !empty($this->rol_name) ? $this->rol_name : $this->getRolCode(); return $aFields; } else { - throw (new Exception("The '$Uid' row doesn't exist!")); + throw new Exception("The '$rolUid' row doesn't exist!"); } - } catch( exception $oError ) { - throw ($oError); + } catch (Exception $exception) { + throw $exception; } } - function loadByCode($sRolCode = '') { + /** + * Load role by Code + * + * @param string $rolCode + * + * @return array + * @throws Exception + */ + function loadByCode($rolCode = '') + { try { - $oCriteria = new Criteria('rbac'); - $oCriteria->add(RolesPeer::ROL_CODE, $sRolCode); - $oDataset = RolesPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - $aRow = $oDataset->getRow(); + $criteria = new Criteria('rbac'); + $criteria->add(RolesPeer::ROL_CODE, $rolCode); + $dataSet = RolesPeer::doSelectRS($criteria); + $dataSet->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $dataSet->next(); + $row = $dataSet->getRow(); $roles = new Roles(); - $roles->load($aRow['ROL_UID']); - $aRow['ROL_NAME'] = $roles->getRolName(); - if ($aRow['ROL_NAME'] == '') { - $aRow['ROL_NAME'] = $roles->getRolCode(); - } - - if (is_array($aRow)) { - return $aRow; - } else { - throw (new Exception("The role '$sRolCode' doesn\'t exist!")); + $roles->load($row['ROL_UID']); + $row['ROL_NAME'] = $roles->getRolName(); + if (empty($row['ROL_NAME'])) { + $row['ROL_NAME'] = $roles->getRolCode(); } - } catch( exception $oError ) { - throw ($oError); + + if (is_array($row)) { + return $row; + } else { + throw new Exception("The role '$rolCode' doesn\'t exist!"); + } + } catch (Exception $exception) { + throw $exception; } } - function listAllRoles($systemCode = 'PROCESSMAKER', $filter = '') { + /** + * Create criteria with all roles + * + * @param string $systemCode + * @param string $filter + * + * @return Criteria + * @throws Exception + */ + function listAllRoles($systemCode = 'PROCESSMAKER', $filter = '') + { try { - $oCriteria = new Criteria('rbac'); - $oCriteria->addSelectColumn(RolesPeer::ROL_UID); - $oCriteria->addSelectColumn(RolesPeer::ROL_PARENT); - $oCriteria->addSelectColumn(RolesPeer::ROL_SYSTEM); - $oCriteria->addSelectColumn(SystemsPeer::SYS_CODE); - $oCriteria->addSelectColumn(RolesPeer::ROL_CODE); - $oCriteria->addSelectColumn(RolesPeer::ROL_CREATE_DATE); - $oCriteria->addSelectColumn(RolesPeer::ROL_UPDATE_DATE); - $oCriteria->addSelectColumn(RolesPeer::ROL_STATUS); - $oCriteria->add(RolesPeer::ROL_UID, '', Criteria::NOT_EQUAL); - $oCriteria->add(RolesPeer::ROL_CODE, RBAC::PROCESSMAKER_GUEST, Criteria::NOT_EQUAL); - $oCriteria->add(SystemsPeer::SYS_CODE, $systemCode); - $oCriteria->add(RolesPeer::ROL_CREATE_DATE, '', Criteria::NOT_EQUAL); - $oCriteria->add(RolesPeer::ROL_UPDATE_DATE, '', Criteria::NOT_EQUAL); - //Added by QENNIX Jan 21th, 2011 - if ($filter != ''){ - $oCriteria->add(RolesPeer::ROL_CODE, '%'.$filter.'%', Criteria::LIKE); + $criteria = new Criteria('rbac'); + $criteria->addSelectColumn(RolesPeer::ROL_UID); + $criteria->addSelectColumn(RolesPeer::ROL_PARENT); + $criteria->addSelectColumn(RolesPeer::ROL_SYSTEM); + $criteria->addSelectColumn(SystemsPeer::SYS_CODE); + $criteria->addSelectColumn(RolesPeer::ROL_CODE); + $criteria->addSelectColumn(RolesPeer::ROL_CREATE_DATE); + $criteria->addSelectColumn(RolesPeer::ROL_UPDATE_DATE); + $criteria->addSelectColumn(RolesPeer::ROL_STATUS); + $criteria->add(RolesPeer::ROL_UID, '', Criteria::NOT_EQUAL); + $criteria->add(RolesPeer::ROL_CODE, RBAC::PROCESSMAKER_GUEST, Criteria::NOT_EQUAL); + $criteria->add(SystemsPeer::SYS_CODE, $systemCode); + $criteria->add(RolesPeer::ROL_CREATE_DATE, '', Criteria::NOT_EQUAL); + $criteria->add(RolesPeer::ROL_UPDATE_DATE, '', Criteria::NOT_EQUAL); + + if (!empty($filter)) { + $criteria->add(RolesPeer::ROL_CODE, '%' . $filter . '%', Criteria::LIKE); } - $oCriteria->addJoin(RolesPeer::ROL_SYSTEM, SystemsPeer::SYS_UID); + $criteria->addJoin(RolesPeer::ROL_SYSTEM, SystemsPeer::SYS_UID); - return $oCriteria; + return $criteria; - } catch( exception $oError ) { - throw (new Exception("Class ROLES::FATAL ERROR. Criteria with rbac Can't initialized ")); + } catch (Exception $exception) { + throw new Exception("Class ROLES::FATAL ERROR. Criteria with rbac Can't initialized "); } } @@ -145,23 +146,21 @@ class Roles extends BaseRoles { function getAllRolesFilter($start, $limit, $filter = '') { $systemCode = 'PROCESSMAKER'; - $criteriaCount = new Criteria('rbac'); + $criteria = new Criteria('rbac'); $result = []; - $criteriaCount->addSelectColumn('COUNT(*) AS CNT'); - $criteriaCount->add(RolesPeer::ROL_UID, '', Criteria::NOT_EQUAL); - $criteriaCount->add(SystemsPeer::SYS_CODE, $systemCode); - $criteriaCount->add(RolesPeer::ROL_CREATE_DATE, '', Criteria::NOT_EQUAL); - $criteriaCount->add(RolesPeer::ROL_UPDATE_DATE, '', Criteria::NOT_EQUAL); - $criteriaCount->add(RolesPeer::ROL_UID, ['', RBAC::PROCESSMAKER_GUEST_UID], Criteria::NOT_IN); - $criteriaCount->addJoin(RolesPeer::ROL_SYSTEM, SystemsPeer::SYS_UID); - if ($filter != '') { - $criteriaCount->add(RolesPeer::ROL_CODE, '%' . $filter . '%', Criteria::LIKE); - } - $result['COUNTER'] = $criteriaCount; + $criteria->addSelectColumn('COUNT(*) AS CNT'); + $criteria->add(RolesPeer::ROL_UID, '', Criteria::NOT_EQUAL); + $criteria->add(SystemsPeer::SYS_CODE, $systemCode); + $criteria->add(RolesPeer::ROL_UID, ['', RBAC::PROCESSMAKER_GUEST_UID], Criteria::NOT_IN); + $criteria->addJoin(RolesPeer::ROL_SYSTEM, SystemsPeer::SYS_UID); - $criteria = new Criteria('rbac'); - $criteria->clear(); + if (!empty($filter)) { + $criteria->add(RolesPeer::ROL_CODE, '%' . $filter . '%', Criteria::LIKE); + } + $result['COUNTER'] = clone $criteria; + + $criteria->clearSelectColumns(); $criteria->addSelectColumn(RolesPeer::ROL_UID); $criteria->addSelectColumn(RolesPeer::ROL_PARENT); $criteria->addSelectColumn(RolesPeer::ROL_SYSTEM); @@ -170,110 +169,174 @@ class Roles extends BaseRoles { $criteria->addSelectColumn(RolesPeer::ROL_CREATE_DATE); $criteria->addSelectColumn(RolesPeer::ROL_UPDATE_DATE); $criteria->addSelectColumn(RolesPeer::ROL_STATUS); - $criteria->add(RolesPeer::ROL_UID, ['', RBAC::PROCESSMAKER_GUEST_UID], Criteria::NOT_IN); - $criteria->add(SystemsPeer::SYS_CODE, $systemCode); - $criteria->add(RolesPeer::ROL_CREATE_DATE, '', Criteria::NOT_EQUAL); - $criteria->add(RolesPeer::ROL_UPDATE_DATE, '', Criteria::NOT_EQUAL); - $criteria->addJoin(RolesPeer::ROL_SYSTEM, SystemsPeer::SYS_UID); - - if ($filter != '') { - $criteria->add(RolesPeer::ROL_CODE, '%' . $filter . '%', Criteria::LIKE); - } $criteria->setOffset($start); $criteria->setLimit($limit); - $result['LIST'] = $criteria; + $result['LIST'] = $criteria; return $result; } - function getAllRoles($systemCode = 'PROCESSMAKER') { - $c = $this->listAllRoles($systemCode); - $rs = RolesPeer::DoSelectRs($c); - $rs->setFetchmode(ResultSet::FETCHMODE_ASSOC); - - $aRows = Array(); - while($rs->next()) { - $row = $rs->getRow(); - $o = new Roles(); - $o->load($row['ROL_UID']); - $row['ROL_NAME'] = $o->getRolName(); - if ($row['ROL_NAME'] == '') { - $row['ROL_NAME'] = $o->getRolCode(); - } - $aRows[] = $row; - } - return $aRows; - } - - function listAllPermissions($systemCode = 'PROCESSMAKER') { + /** + * Load roles by system code + * + * @param string $systemCode + * + * @return array + * @throws Exception + */ + function getAllRoles($systemCode = 'PROCESSMAKER') + { try { - $oCriteria = new Criteria('rbac'); - $oCriteria->addSelectColumn(PermissionsPeer::PER_UID); - $oCriteria->addSelectColumn(PermissionsPeer::PER_CODE); - $oCriteria->addSelectColumn(PermissionsPeer::PER_CREATE_DATE); - $oCriteria->addSelectColumn(PermissionsPeer::PER_UPDATE_DATE); - $oCriteria->addSelectColumn(PermissionsPeer::PER_STATUS); - $oCriteria->add(PermissionsPeer::PER_CODE, substr($systemCode, 0, 3) . '_%', Criteria::LIKE); + $criteria = $this->listAllRoles($systemCode); + $rs = RolesPeer::doSelectRS($criteria); + $rs->setFetchmode(ResultSet::FETCHMODE_ASSOC); - return $oCriteria; + $rows = []; + while ($rs->next()) { + $row = $rs->getRow(); + $o = new Roles(); + $o->load($row['ROL_UID']); + $row['ROL_NAME'] = $o->getRolName(); + if (empty($row['ROL_NAME'])) { + $row['ROL_NAME'] = $o->getRolCode(); + } + $rows[] = $row; + } + return $rows; - } catch( exception $oError ) { - throw (new Exception("Class ROLES::FATAL ERROR. Criteria with rbac Can't initialized ")); + } catch (Exception $exception) { + throw $exception; } } - function createRole($aData) { - $con = Propel::getConnection(RolesPeer::DATABASE_NAME); + /** + * Load all permissions by System Code + * + * @param string $systemCode + * + * @return Criteria + * @throws Exception + */ + function listAllPermissions($systemCode = 'PROCESSMAKER') + { try { - $sRolCode = $aData['ROL_CODE']; - $sRolSystem = $aData['ROL_SYSTEM']; + $criteria = new Criteria('rbac'); + $criteria->addSelectColumn(PermissionsPeer::PER_UID); + $criteria->addSelectColumn(PermissionsPeer::PER_CODE); + $criteria->addSelectColumn(PermissionsPeer::PER_CREATE_DATE); + $criteria->addSelectColumn(PermissionsPeer::PER_UPDATE_DATE); + $criteria->addSelectColumn(PermissionsPeer::PER_STATUS); + $criteria->add(PermissionsPeer::PER_CODE, substr($systemCode, 0, 3) . '_%', Criteria::LIKE); + + return $criteria; + + } catch (Exception $exception) { + throw new Exception("Class ROLES::FATAL ERROR. Criteria with rbac Can't initialized "); + } + } + + /** + * Check if exists role in system + * + * @param string $rolCode + * @param string $system + * + * @return array + * @throws Exception + */ + public function existsRolSystem($rolCode, $system) + { + try { + $criteria = new Criteria('rbac'); + $criteria->add(RolesPeer::ROL_CODE, $rolCode); + $criteria->add(RolesPeer::ROL_SYSTEM, $system); + $dataSet = RolesPeer::doSelectRS($criteria); + $dataSet->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $row = []; + if ($dataSet->next()) { + $row = $dataSet->getRow(); + } + return $row; + } catch (Exception $exception) { + throw $exception; + } + + } + + /** + * Create role in system + * + * @param array $data + * + * @return array|int + * @throws Exception + */ + function createRole($data) + { + try { + $con = Propel::getConnection(RolesPeer::DATABASE_NAME); + $rolCode = $data['ROL_CODE']; + $rolSystem = $data['ROL_SYSTEM']; $status = $fields['ROL_STATUS'] = 1 ? 'ACTIVE' : 'INACTIVE'; - $oCriteria = new Criteria('rbac'); - $oCriteria->add(RolesPeer::ROL_CODE, $sRolCode); - $oCriteria->add(RolesPeer::ROL_SYSTEM, $sRolSystem); - $oDataset = RolesPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - $aRow = $oDataset->getRow(); - if (is_array($aRow)) { - return $aRow; + $info = $this->existsRolSystem($rolCode, $rolSystem); + if ($info) { + return $info; } - if (!isset($aData['ROL_NAME'])) { - $aData['ROL_NAME'] = ''; + if (!isset($data['ROL_NAME'])) { + $data['ROL_NAME'] = ''; } - $rol_name = $aData['ROL_NAME']; - unset($aData['ROL_NAME']); + if (!isset($data['ROL_CREATE_DATE'])) { + $data['ROL_CREATE_DATE'] = date('Y-M-d H:i:s'); + } + if (!isset($data['ROL_UPDATE_DATE'])) { + $data['ROL_UPDATE_DATE'] = date('Y-M-d H:i:s'); + } + $rolName = $data['ROL_NAME']; + unset($data['ROL_NAME']); $obj = new Roles(); - $obj->fromArray($aData, BasePeer::TYPE_FIELDNAME); + $obj->fromArray($data, BasePeer::TYPE_FIELDNAME); if ($obj->validate()) { $con->begin(); $result = $obj->save(); $con->commit(); - $obj->setRolName($rol_name); - G::auditLog("CreateRole", "Role Name: ". $rol_name ." - Role Code: ".$aData['ROL_CODE']." - Role Status: ".$status); + $obj->setRolName($rolName); + G::auditLog('CreateRole', 'Role Name: ' . $rolName . ' - Role Code: ' . $data['ROL_CODE'] . ' - Role Status: ' . $status); } else { - $e = new Exception("Failed Validation in class " . get_class($this) . "."); - $e->aValidationFailures = $this->getValidationFailures(); - throw ($e); + $exception = new Exception('Failed Validation in class ' . get_class($this) . '.'); + $exception->aValidationFailures = $this->getValidationFailures(); + throw $exception; } return $result; - } catch( exception $e ) { + } catch (Exception $exception) { $con->rollback(); - throw ($e); + throw $exception; } } - public function updateRole($fields) { - $con = Propel::getConnection(RolesPeer::DATABASE_NAME); + /** + * Update role + * + * @param array $fields + * + * @return int + * @throws Exception + */ + public function updateRole($fields) + { try { + $con = Propel::getConnection(RolesPeer::DATABASE_NAME); $con->begin(); $this->load($fields['ROL_UID']); $rol_name = $fields['ROL_NAME']; unset($fields['ROL_NAME']); + if (!isset($data['ROL_UPDATE_DATE'])) { + $fields['ROL_UPDATE_DATE'] = date('Y-M-d H:i:s'); + } + $this->fromArray($fields, BasePeer::TYPE_FIELDNAME); if ($this->validate()) { $result = $this->save(); @@ -281,259 +344,374 @@ class Roles extends BaseRoles { $this->setRolName($rol_name); $status = $fields['ROL_STATUS'] = 1 ? 'ACTIVE' : 'INACTIVE'; - $rolCode = (isset($fields["ROL_CODE"]))? "- Role Code: " . $fields["ROL_CODE"] : ""; + $rolCode = isset($fields['ROL_CODE']) ? '- Role Code: ' . $fields['ROL_CODE'] : ''; - G::auditLog("UpdateRole", "Role Name: " . $rol_name . " - Role ID: (".$fields['ROL_UID'].") " . $rolCode . " - Role Status: ".$status); + G::auditLog('UpdateRole', 'Role Name: ' . $rol_name . ' - Role ID: (' . $fields['ROL_UID'] . ') ' . $rolCode . ' - Role Status: ' . $status); return $result; } else { $con->rollback(); - throw (new Exception("Failed Validation in class " . get_class($this) . ".")); + throw new Exception('Failed Validation in class ' . get_class($this) . '.'); } - } catch( exception $e ) { + } catch (Exception $exception) { $con->rollback(); - throw ($e); + throw $exception; } } - function removeRole($ROL_UID) { - $con = Propel::getConnection(RolesPeer::DATABASE_NAME); - try { - $con->begin(); - $this->setRolUid($ROL_UID); - $rol_name = $this->load($ROL_UID); - Content::removeContent('ROL_NAME', '', $this->getRolUid()); - $result = $this->delete(); - $con->commit(); - G::auditLog("DeleteRole", "Role Name: ".$rol_name['ROL_NAME']." Role UID: (".$ROL_UID.") "); - return $result; - } catch( exception $e ) { - $con->rollback(); - throw ($e); - } - } - - function verifyNewRole($code) { - $code = trim($code); - $oCriteria = new Criteria('rbac'); - $oCriteria->addSelectColumn(RolesPeer::ROL_UID); - $oCriteria->add(RolesPeer::ROL_CODE, $code); - $count = RolesPeer::doCount($oCriteria); - - if ($count == 0) { - return true; - } else { - return false; - } - } - - function loadById($ROL_UID) { - - $oCriteria = new Criteria('rbac'); - $oCriteria->addSelectColumn(RolesPeer::ROL_UID); - $oCriteria->addSelectColumn(RolesPeer::ROL_PARENT); - $oCriteria->addSelectColumn(RolesPeer::ROL_SYSTEM); - $oCriteria->addSelectColumn(RolesPeer::ROL_CODE); - $oCriteria->addSelectColumn(RolesPeer::ROL_CREATE_DATE); - $oCriteria->addSelectColumn(RolesPeer::ROL_UPDATE_DATE); - $oCriteria->addSelectColumn(RolesPeer::ROL_STATUS); - $oCriteria->add(RolesPeer::ROL_UID, $ROL_UID); - - $result = RolesPeer::doSelectRS($oCriteria); - $result->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $result->next(); - - $row = $result->getRow(); - if (is_array($row)) { - $o = RolesPeer::retrieveByPK($row['ROL_UID']); - $row['ROL_NAME'] = $o->getRolName(); - if ($row['ROL_NAME'] == '') { - $row['ROL_NAME'] = $o->getRolCode(); - } - return $row; - } else { - return null; - } - } - - function getRoleCode($ROL_UID) { - $oCriteria = new Criteria('rbac'); - $oCriteria->addSelectColumn(RolesPeer::ROL_UID); - $oCriteria->addSelectColumn(RolesPeer::ROL_CODE); - $oCriteria->add(RolesPeer::ROL_UID, $ROL_UID); - - $result = RolesPeer::doSelectRS($oCriteria); - $result->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $result->next(); - $row = $result->getRow(); - $ret = $row['ROL_CODE']; - - return $ret; - } - - //Added by Enrique at Feb 9th, 2011 - //Gets number of users by role - function getAllUsersByRole(){ - $oCriteria = new Criteria('rbac'); - $oCriteria->addSelectColumn(UsersRolesPeer::ROL_UID); - $oCriteria->addSelectColumn('COUNT(*) AS CNT'); - $oCriteria->addJoin(RbacUsersPeer::USR_UID,UsersRolesPeer::USR_UID,Criteria::INNER_JOIN); - $oCriteria->add(RbacUsersPeer::USR_STATUS,0,Criteria::NOT_EQUAL); - $oCriteria->addGroupByColumn(UsersRolesPeer::ROL_UID); - $oDataset = UsersRolesPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $aRoles = array(); - while ($oDataset->next()){ - $row = $oDataset->getRow(); - $aRoles[$row['ROL_UID']] = $row['CNT']; - } - return $aRoles; - } - - //Added by Enrique at Feb 10th, 2011 - //Gets number of users by department - function getAllUsersByDepartment(){ - $oCriteria = new Criteria('rbac'); - $oCriteria->addSelectColumn(UsersPeer::DEP_UID); - $oCriteria->addSelectColumn('COUNT(*) AS CNT'); - $oCriteria->add(UsersPeer::USR_STATUS, 'CLOSED', Criteria::NOT_EQUAL); - $oCriteria->addGroupByColumn(UsersPeer::DEP_UID); - $oDataset = UsersPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $aDepts = array(); - while ($oDataset->next()){ - $row = $oDataset->getRow(); - $aDepts[$row['DEP_UID']] = $row['CNT']; - } - return $aDepts; - } - - function getRoleUsers($ROL_UID, $filter='') { - throw new Exception(__METHOD__ . ': The method is deprecated'); - } - - function getAllUsers($ROL_UID, $filter='') { - throw new Exception(__METHOD__ . ': The method is deprecated'); - } - - function assignUserToRole($aData) { - /*find the system uid for this role */ - require_once 'classes/model/Users.php'; - $c = new Criteria(); - $c->add(RolesPeer::ROL_UID, $aData['ROL_UID']); - $result = RolesPeer::doSelectRS($c); - $result->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $result->next(); - $row = $result->getRow(); - $sSystemId = $row['ROL_SYSTEM']; - - //updating the role into users table - $oCriteria1 = new Criteria('workflow'); - $oCriteria1->add(UsersPeer::USR_UID , $aData['USR_UID'], Criteria::EQUAL); - $oCriteria2 = new Criteria('workflow'); - $oCriteria2->add(UsersPeer::USR_ROLE , $row['ROL_CODE']); - BasePeer::doUpdate($oCriteria1, $oCriteria2, Propel::getConnection('workflow')); - - //delete roles for the same System - $c = new Criteria(); - $c->addSelectColumn(UsersRolesPeer::USR_UID); - $c->addSelectColumn(RolesPeer::ROL_UID); - $c->addSelectColumn(RolesPeer::ROL_CODE); - $c->addSelectColumn(RolesPeer::ROL_SYSTEM); - $c->add(UsersRolesPeer::USR_UID, $aData['USR_UID']); - $c->add(RolesPeer::ROL_SYSTEM, $sSystemId); - $c->addJoin(RolesPeer::ROL_UID, UsersRolesPeer::ROL_UID); - $result = RolesPeer::doSelectRS($c); - $result->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $result->next(); - - while( $row = $result->getRow() ) { - $crit = new Criteria(); - $crit->add(UsersRolesPeer::USR_UID, $row['USR_UID']); - $crit->add(UsersRolesPeer::ROL_UID, $row['ROL_UID']); - UsersRolesPeer::doDelete($crit); - $result->next(); - } - - //save the unique role for this system - $oUsersRoles = new UsersRoles(); - $oUsersRoles->setUsrUid($aData['USR_UID']); - $oUsersRoles->setRolUid($aData['ROL_UID']); - $oUsersRoles->save(); - - $rol = $this->load($aData['ROL_UID']); - $oUsersRbac = new RbacUsers(); - $user = $oUsersRbac->load($aData['USR_UID']); - G::auditLog("AssignUserToRole", "Assign user ".$user['USR_USERNAME']." (".$aData['USR_UID'].") to Role ".$rol['ROL_NAME']." (".$aData['ROL_UID'].") "); - } - - function deleteUserRole($ROL_UID, $USR_UID) { - $crit = new Criteria(); - $crit->add(UsersRolesPeer::USR_UID, $USR_UID); - - if ($ROL_UID != '%') { - $crit->add(UsersRolesPeer::ROL_UID, $ROL_UID); - } - UsersRolesPeer::doDelete($crit); - $rol = $this->load($ROL_UID); - $oUsersRbac = new RbacUsers(); - $user = $oUsersRbac->load($USR_UID); - - G::auditLog("DeleteUserToRole", "Delete user ".$user['USR_USERNAME']." (".$USR_UID.") to Role ".$rol['ROL_NAME']." (".$ROL_UID.") "); - } - /** - * @param $roleUid + * Remove Role by Uid + * + * @param $rolUid + * @throws Exception + */ + function removeRole($rolUid) + { + try { + $con = Propel::getConnection(RolesPeer::DATABASE_NAME); + $con->begin(); + $this->setRolUid($rolUid); + $rol_name = $this->load($rolUid); + Content::removeContent('ROL_NAME', '', $this->getRolUid()); + $result = $this->delete(); + $con->commit(); + G::auditLog('DeleteRole', 'Role Name: ' . $rol_name['ROL_NAME'] . ' Role UID: (' . $rolUid . ') '); + return $result; + } catch (Exception $exception) { + $con->rollback(); + throw $exception; + } + } + + /** + * Check if exists Role Code + * + * @param string $code + * @return bool + */ + function verifyNewRole($code) + { + $code = trim($code); + $criteria = new Criteria('rbac'); + $criteria->addSelectColumn(RolesPeer::ROL_UID); + $criteria->add(RolesPeer::ROL_CODE, $code); + + if (RolesPeer::doCount($criteria) > 0) { + return true; + } + return false; + } + + /** + * Load role information by uid + * + * @param string $rolUid + * + * @return array + * @throws Exception + */ + function loadById($rolUid) + { + try { + $criteria = new Criteria('rbac'); + $criteria->addSelectColumn(RolesPeer::ROL_UID); + $criteria->addSelectColumn(RolesPeer::ROL_PARENT); + $criteria->addSelectColumn(RolesPeer::ROL_SYSTEM); + $criteria->addSelectColumn(RolesPeer::ROL_CODE); + $criteria->addSelectColumn(RolesPeer::ROL_CREATE_DATE); + $criteria->addSelectColumn(RolesPeer::ROL_UPDATE_DATE); + $criteria->addSelectColumn(RolesPeer::ROL_STATUS); + $criteria->add(RolesPeer::ROL_UID, $rolUid); + + $result = RolesPeer::doSelectRS($criteria); + $result->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $row = []; + + if ($result->next()) { + $row = $result->getRow(); + $o = RolesPeer::retrieveByPK($row['ROL_UID']); + $row['ROL_NAME'] = $o->getRolName(); + if (empty($row['ROL_NAME'])) { + $row['ROL_NAME'] = $o->getRolCode(); + } + } + return $row; + } catch (Exception $exception) { + throw $exception; + } + } + + /** + * Get Role code by Role Uid + * + * @param string $rolUid + * + * @return string + * @throws Exception + */ + function getRoleCode($rolUid) + { + try { + $criteria = new Criteria('rbac'); + $criteria->addSelectColumn(RolesPeer::ROL_UID); + $criteria->addSelectColumn(RolesPeer::ROL_CODE); + $criteria->add(RolesPeer::ROL_UID, $rolUid); + + $result = RolesPeer::doSelectRS($criteria); + $result->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $role = ''; + if ($result->next()) { + $row = $result->getRow(); + $role = $row['ROL_CODE']; + } + return $role; + } catch (Exception $exception) { + throw $exception; + } + } + + /** + * Gets number of users by role + * + * @return array + * @throws Exception + */ + function getAllUsersByRole() + { + try { + $criteria = new Criteria('rbac'); + $criteria->addSelectColumn(UsersRolesPeer::ROL_UID); + $criteria->addSelectColumn('COUNT(*) AS CNT'); + $criteria->addJoin(RbacUsersPeer::USR_UID, UsersRolesPeer::USR_UID, Criteria::INNER_JOIN); + $criteria->add(RbacUsersPeer::USR_STATUS, 0, Criteria::NOT_EQUAL); + $criteria->addGroupByColumn(UsersRolesPeer::ROL_UID); + $dataSet = UsersRolesPeer::doSelectRS($criteria); + $dataSet->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $roles = []; + while ($dataSet->next()) { + $row = $dataSet->getRow(); + $roles[$row['ROL_UID']] = $row['CNT']; + } + return $roles; + } catch (Exception $exception) { + throw $exception; + } + } + + /** + * Gets number of users by department + * + * @return array + * @throws Exception + */ + function getAllUsersByDepartment() + { + try { + $criteria = new Criteria('rbac'); + $criteria->addSelectColumn(UsersPeer::DEP_UID); + $criteria->addSelectColumn('COUNT(*) AS CNT'); + $criteria->add(UsersPeer::USR_STATUS, 'CLOSED', Criteria::NOT_EQUAL); + $criteria->addGroupByColumn(UsersPeer::DEP_UID); + $dataSet = UsersPeer::doSelectRS($criteria); + $dataSet->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $departments = []; + while ($dataSet->next()) { + $row = $dataSet->getRow(); + $departments[$row['DEP_UID']] = $row['CNT']; + } + return $departments; + } catch (Exception $exception) { + throw $exception; + } + } + + /** + * Get users by role + * + * @param $rolUid + * @param string $filter + * + * @throws Exception + * @deprecated + */ + function getRoleUsers($rolUid, $filter = '') + { + throw new Exception(__METHOD__ . ': The method is deprecated'); + } + + /** + * Get all users by role + * + * @param $rolUid + * @param string $filter + * + * @throws Exception + * @deprecated + */ + function getAllUsers($rolUid, $filter = '') + { + throw new Exception(__METHOD__ . ': The method is deprecated'); + } + + /** + * Assign User to Role + * + * @param array $data + * + * @throws Exception + */ + function assignUserToRole($data) + { + try { + /*find the system uid for this role */ + require_once 'classes/model/Users.php'; + $criteria = new Criteria(); + $criteria->add(RolesPeer::ROL_UID, $data['ROL_UID']); + $result = RolesPeer::doSelectRS($criteria); + $result->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $result->next(); + $row = $result->getRow(); + $systemId = $row['ROL_SYSTEM']; + + //updating the role into users table + $criteria1 = new Criteria('workflow'); + $criteria1->add(UsersPeer::USR_UID, $data['USR_UID'], Criteria::EQUAL); + $criteria2 = new Criteria('workflow'); + $criteria2->add(UsersPeer::USR_ROLE, $row['ROL_CODE']); + BasePeer::doUpdate($criteria1, $criteria2, Propel::getConnection('workflow')); + + //delete roles for the same System + $criteria = new Criteria(); + $criteria->addSelectColumn(UsersRolesPeer::USR_UID); + $criteria->addSelectColumn(RolesPeer::ROL_UID); + $criteria->addSelectColumn(RolesPeer::ROL_CODE); + $criteria->addSelectColumn(RolesPeer::ROL_SYSTEM); + $criteria->add(UsersRolesPeer::USR_UID, $data['USR_UID']); + $criteria->add(RolesPeer::ROL_SYSTEM, $systemId); + $criteria->addJoin(RolesPeer::ROL_UID, UsersRolesPeer::ROL_UID); + $result = RolesPeer::doSelectRS($criteria); + $result->setFetchmode(ResultSet::FETCHMODE_ASSOC); + + while ($result->next()) { + $row = $result->getRow(); + $criteriaDelete = new Criteria(); + $criteriaDelete->add(UsersRolesPeer::USR_UID, $row['USR_UID']); + $criteriaDelete->add(UsersRolesPeer::ROL_UID, $row['ROL_UID']); + UsersRolesPeer::doDelete($criteriaDelete); + } + + //save the unique role for this system + $oUsersRoles = new UsersRoles(); + $oUsersRoles->setUsrUid($data['USR_UID']); + $oUsersRoles->setRolUid($data['ROL_UID']); + $oUsersRoles->save(); + + $rol = $this->load($data['ROL_UID']); + $userRbac = new RbacUsers(); + $user = $userRbac->load($data['USR_UID']); + G::auditLog('AssignUserToRole', 'Assign user ' . $user['USR_USERNAME'] . ' (' . $data['USR_UID'] . ') to Role ' . $rol['ROL_NAME'] . ' (' . $data['ROL_UID'] . ') '); + } catch (Exception $exception) { + throw $exception; + } + } + + /** + * Delete a role from users + * + * @param string $rolUid + * @param string $userUid + * + * @throws Exception + */ + function deleteUserRole($rolUid, $userUid) + { + try { + $criteria = new Criteria(); + $criteria->add(UsersRolesPeer::USR_UID, $userUid); + + if ($rolUid !== '%') { + $criteria->add(UsersRolesPeer::ROL_UID, $rolUid); + } + UsersRolesPeer::doDelete($criteria); + $rol = $this->load($rolUid); + $userRbac = new RbacUsers(); + $user = $userRbac->load($userUid); + + G::auditLog('DeleteUserToRole', 'Delete user ' . $user['USR_USERNAME'] . ' (' . $userUid . ') to Role ' . $rol['ROL_NAME'] . ' (' . $rolUid . ') '); + } catch (Exception $exception) { + throw $exception; + } + } + + /** + * Get permissions by Role Uid + * + * @param string $roleUid + * * @return ResultSet * @throws Exception */ - function getRolePermissionsByPerUid($roleUid){ + function getRolePermissionsByPerUid($roleUid) + { try { $criteria = new Criteria(); $criteria->addSelectColumn(RolesPermissionsPeer::ROL_UID); $criteria->addSelectColumn(RolesPermissionsPeer::PER_UID); $criteria->add(RolesPermissionsPeer::PER_UID, $roleUid); - $oDataset = RolesPeer::doSelectRS($criteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $dataSet = RolesPeer::doSelectRS($criteria); + $dataSet->setFetchmode(ResultSet::FETCHMODE_ASSOC); - return $oDataset; + return $dataSet; - } catch( exception $e ) { - throw $e; + } catch (Exception $exception) { + throw $exception; } } /** - * Checks a permission is assigned to a Role - * @param $ROL_UID - * @param $PER_UID + * Checks if a permission is already assigned to a Role + * + * @param string $rolUid + * @param string $perUid + * * @return bool * @throws Exception */ - function getPermissionAssignedRole($ROL_UID, $PER_UID) + function getPermissionAssignedRole($rolUid, $perUid) { try { $criteria = new Criteria(); $criteria->addSelectColumn(RolesPermissionsPeer::ROL_UID); $criteria->addSelectColumn(RolesPermissionsPeer::PER_UID); - $criteria->add(RolesPermissionsPeer::ROL_UID, $ROL_UID, Criteria::EQUAL); - $criteria->add(RolesPermissionsPeer::PER_UID, $PER_UID, Criteria::EQUAL); + $criteria->add(RolesPermissionsPeer::ROL_UID, $rolUid, Criteria::EQUAL); + $criteria->add(RolesPermissionsPeer::PER_UID, $perUid, Criteria::EQUAL); - $oDataset = RolesPermissionsPeer::doSelectRS($criteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - if($aRowRP = $oDataset->getRow()){ + $dataSet = RolesPermissionsPeer::doSelectRS($criteria); + $dataSet->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $dataSet->next(); + if ($rowRP = $dataSet->getRow()) { return true; } return false; - } catch (exception $e) { - throw $e; + } catch (Exception $exception) { + throw $exception; } } - - function getRolePermissions($ROL_UID, $filter='', $status=null) { + + /** + * Get role information and permissions + * + * @param string $rolUid + * @param string $filter + * @param int $status + * + * @return ResultSet + * @throws Exception + */ + function getRolePermissions($rolUid, $filter = '', $status = null) + { try { $criteria = new Criteria(); $criteria->addSelectColumn(RolesPeer::ROL_UID); @@ -549,42 +727,53 @@ class Roles extends BaseRoles { $criteria->addSelectColumn(PermissionsPeer::PER_UPDATE_DATE); $criteria->addSelectColumn(PermissionsPeer::PER_STATUS); $criteria->add(RolesPeer::ROL_UID, "", Criteria::NOT_EQUAL); - $criteria->add(RolesPeer::ROL_UID, $ROL_UID); + $criteria->add(RolesPeer::ROL_UID, $rolUid); $criteria->addJoin(RolesPeer::ROL_UID, RolesPermissionsPeer::ROL_UID); $criteria->addJoin(RolesPermissionsPeer::PER_UID, PermissionsPeer::PER_UID); - if ($filter != '') { - $criteria->add(PermissionsPeer::PER_CODE, '%'.$filter.'%',Criteria::LIKE); + if (!empty($filter)) { + $criteria->add(PermissionsPeer::PER_CODE, '%' . $filter . '%', Criteria::LIKE); } - if (!is_null($status) && ($status == 1 || $status == 0)) { + if (!is_null($status) && ($status === 1 || $status === 0)) { $criteria->add(PermissionsPeer::PER_STATUS, $status); } - $oDataset = RolesPeer::doSelectRS($criteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $dataSet = RolesPeer::doSelectRS($criteria); + $dataSet->setFetchmode(ResultSet::FETCHMODE_ASSOC); - return $oDataset; - - } catch( exception $e ) { - throw $e; + return $dataSet; + } catch (Exception $exception) { + throw $exception; } } - function getAllPermissions($ROL_UID, $PER_SYSTEM = "", $filter='', $status=null) { + /** + * Get all permissions by Role Uid + * + * @param string $rolUid + * @param string $perSystem + * @param string $filter + * @param int $status + * + * @return ResultSet + * @throws Exception + */ + function getAllPermissions($rolUid, $perSystem = '', $filter = '', $status = null) + { try { - $c = new Criteria(); - $c->addSelectColumn(PermissionsPeer::PER_UID); - $c->add(RolesPeer::ROL_UID, $ROL_UID); - $c->addJoin(RolesPeer::ROL_UID, RolesPermissionsPeer::ROL_UID); - $c->addJoin(RolesPermissionsPeer::PER_UID, PermissionsPeer::PER_UID); + $criteria = new Criteria(); + $criteria->addSelectColumn(PermissionsPeer::PER_UID); + $criteria->add(RolesPeer::ROL_UID, $rolUid); + $criteria->addJoin(RolesPeer::ROL_UID, RolesPermissionsPeer::ROL_UID); + $criteria->addJoin(RolesPermissionsPeer::PER_UID, PermissionsPeer::PER_UID); - $result = PermissionsPeer::doSelectRS($c); + $result = PermissionsPeer::doSelectRS($criteria); $result->setFetchmode(ResultSet::FETCHMODE_ASSOC); $result->next(); $a = [RBAC::PM_GUEST_CASE_UID]; - while( $row = $result->getRow() ) { + while ($row = $result->getRow()) { $a[] = $row['PER_UID']; $result->next(); } @@ -598,107 +787,164 @@ class Roles extends BaseRoles { $criteria->addSelectColumn(PermissionsPeer::PER_SYSTEM); $criteria->addSelectColumn(SystemsPeer::SYS_CODE); $criteria->add(PermissionsPeer::PER_UID, $a, Criteria::NOT_IN); - if ($PER_SYSTEM != "") { - $criteria->add(SystemsPeer::SYS_CODE, $PER_SYSTEM); + if (!empty($perSystem)) { + $criteria->add(SystemsPeer::SYS_CODE, $perSystem); } $criteria->addJoin(PermissionsPeer::PER_SYSTEM, SystemsPeer::SYS_UID); - if ($filter != ''){ - $criteria->add(PermissionsPeer::PER_CODE, '%'.$filter.'%',Criteria::LIKE); + if (!empty($filter)) { + $criteria->add(PermissionsPeer::PER_CODE, '%' . $filter . '%', Criteria::LIKE); } if (!is_null($status) && ($status == 1 || $status == 0)) { $criteria->add(PermissionsPeer::PER_STATUS, $status); } - $oDataset = PermissionsPeer::doSelectRS($criteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - return $oDataset; - } catch( exception $e ) { - throw $e; + $dataSet = PermissionsPeer::doSelectRS($criteria); + $dataSet->setFetchmode(ResultSet::FETCHMODE_ASSOC); + return $dataSet; + } catch (Exception $exception) { + throw $exception; } } - function assignPermissionRole($sData) { - $o = new RolesPermissions(); - $o->setPerUid($sData['PER_UID']); - $o->setRolUid($sData['ROL_UID']); - if (isset($sData['PER_NAME'])) { - $o->setPermissionName($sData['PER_NAME']); + /** + * Assign Permission to role + * + * @param array $data + * + * @throws Exception + */ + function assignPermissionRole($data) + { + try { + $rolePermission = new RolesPermissions(); + $rolePermission->setPerUid($data['PER_UID']); + $rolePermission->setRolUid($data['ROL_UID']); + if (isset($data['PER_NAME'])) { + $rolePermission->setPermissionName($data['PER_NAME']); + } + $permission = $rolePermission->getPermissionName($data['PER_UID']); + $role = $this->load($data['ROL_UID']); + $rolePermission->save(); + G::auditLog('AddPermissionToRole', 'Add Permission ' . $permission . ' (' . $data['PER_UID'] . ') to Role ' . $role['ROL_NAME'] . ' (' . $data['ROL_UID'] . ') '); + } catch (Exception $exception) { + throw $exception; } - $permission = $o->getPermissionName($sData['PER_UID']); - $role = $this->load($sData['ROL_UID']); - $o->save(); - G::auditLog("AddPermissionToRole", "Add Permission ".$permission." (".$sData['PER_UID'].") to Role ".$role['ROL_NAME']." (".$sData['ROL_UID'].") "); } - function deletePermissionRole($ROL_UID, $PER_UID) { - $crit = new Criteria(); - $crit->add(RolesPermissionsPeer::ROL_UID, $ROL_UID); - $crit->add(RolesPermissionsPeer::PER_UID, $PER_UID); - RolesPermissionsPeer::doDelete($crit); + /** + * Delete permission from a role + * + * @param string $rolUid + * @param string $perUid + * + * @throws Exception + */ + function deletePermissionRole($rolUid, $perUid) + { + try { + $criteria = new Criteria(); + $criteria->add(RolesPermissionsPeer::ROL_UID, $rolUid); + $criteria->add(RolesPermissionsPeer::PER_UID, $perUid); + RolesPermissionsPeer::doDelete($criteria); - $o = new RolesPermissions(); - $o->setPerUid($PER_UID); - $permission = $o->getPermissionName($PER_UID); - $role = $this->load($ROL_UID); + $rolePermission = new RolesPermissions(); + $rolePermission->setPerUid($perUid); + $permission = $rolePermission->getPermissionName($perUid); + $role = $this->load($rolUid); - G::auditLog("DeletePermissionToRole", "Delete Permission ".$permission." (".$PER_UID.") from Role ".$role['ROL_NAME']." (".$ROL_UID.") "); + G::auditLog('DeletePermissionToRole', 'Delete Permission ' . $permission . ' (' . $perUid . ') from Role ' . $role['ROL_NAME'] . ' (' . $rolUid . ') '); + } catch (Exception $exception) { + throw $exception; + } } - function numUsersWithRole($ROL_UID) { + /** + * Count users with a specific role + * + * @param string $rolUid + * + * @return int + */ + function numUsersWithRole($rolUid) + { $criteria = new Criteria(); $criteria->addSelectColumn(RbacUsersPeer::USR_UID); - $criteria->add(RolesPeer::ROL_UID, "", Criteria::NOT_EQUAL); - $criteria->add(RolesPeer::ROL_UID, $ROL_UID); + $criteria->add(RolesPeer::ROL_UID, '', Criteria::NOT_EQUAL); + $criteria->add(RolesPeer::ROL_UID, $rolUid); $criteria->addJoin(RolesPeer::ROL_UID, UsersRolesPeer::ROL_UID); $criteria->addJoin(UsersRolesPeer::USR_UID, RbacUsersPeer::USR_UID); return RolesPeer::doCount($criteria); } - function verifyByCode($sRolCode = '') { + /** + * Check if already exists a Role Code + * + * @param string $rolCode + * + * @return int + * + * @throws Exception + */ + function verifyByCode($rolCode = '') + { try { - $oCriteria = new Criteria('rbac'); - $oCriteria->add(RolesPeer::ROL_CODE, $sRolCode); - $oDataset = RolesPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - $aRow = $oDataset->getRow(); - if (is_array($aRow)) { + $criteria = new Criteria('rbac'); + $criteria->add(RolesPeer::ROL_CODE, $rolCode); + $dataSet = RolesPeer::doSelectRS($criteria); + $dataSet->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $dataSet->next(); + $row = $dataSet->getRow(); + if ($row) { return 1; - } else { - return 0; } - } catch( exception $oError ) { - throw ($oError); + return 0; + } catch (Exception $exception) { + throw $exception; } } - public function getRolName() { - if ($this->getRolUid() == '') { - throw (new Exception("Error in getRolName, the ROL_UID can't be blank")); + /** + * Get Role name + * + * @return string + * + * @throws Exception + */ + public function getRolName() + { + if (empty($this->getRolUid())) { + throw new Exception("Error in getRolName, the ROL_UID can't be blank"); } $lang = defined('SYS_LANG') ? SYS_LANG : 'en'; $this->rol_name = Content::load('ROL_NAME', '', $this->getRolUid(), $lang); return $this->rol_name; } - public function setRolName($v) { + /** + * Save Role name in content + * + * @param string $roleName + * + * @throws Exception + */ + public function setRolName($roleName) + { if ($this->getRolUid() == '') { - throw (new Exception("Error in setProTitle, the PRO_UID can't be blank")); + throw new Exception("Error in setProTitle, the PRO_UID can't be blank"); } // Since the native PHP type for this column is string, // we will cast the input to a string (if it is not). - if ($v !== null && ! is_string($v)) { - $v = (string)$v; + if ($roleName !== null && !is_string($roleName)) { + $roleName = (string)$roleName; } - if ($this->rol_name !== $v || $v === '') { - $this->rol_name = $v; + if ($this->rol_name !== $roleName || $roleName === '') { + $this->rol_name = $roleName; $lang = defined('SYS_LANG') ? SYS_LANG : 'en'; $res = Content::addContent('ROL_NAME', '', $this->getRolUid(), $lang, $this->rol_name); } - } } // Roles diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Role.php b/workflow/engine/src/ProcessMaker/BusinessModel/Role.php index 46d391d34..ddda9b20b 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Role.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Role.php @@ -1,30 +1,47 @@ array("type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "roleUid"), + private $arrayFieldDefinition = [ + 'ROL_UID' => ['type' => 'string', 'required' => false, 'empty' => false, 'defaultValues' => [], 'fieldNameAux' => 'roleUid'], - "ROL_CODE" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "roleCode"), - "ROL_NAME" => array("type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "roleName"), - "ROL_STATUS" => array("type" => "string", "required" => false, "empty" => false, "defaultValues" => array("ACTIVE", "INACTIVE"), "fieldNameAux" => "roleStatus") - ); + 'ROL_CODE' => ['type' => 'string', 'required' => true, 'empty' => false, 'defaultValues' => [], 'fieldNameAux' => 'roleCode'], + 'ROL_NAME' => ['type' => 'string', 'required' => true, 'empty' => false, 'defaultValues' => [], 'fieldNameAux' => 'roleName'], + 'ROL_STATUS' => ['type' => 'string', 'required' => false, 'empty' => false, 'defaultValues' => ['ACTIVE', 'INACTIVE'], 'fieldNameAux' => 'roleStatus'] + ]; private $formatFieldNameInUppercase = true; - private $arrayFieldNameForException = array( - "filter" => "FILTER", - "start" => "START", - "limit" => "LIMIT" - ); + private $arrayFieldNameForException = [ + 'filter' => 'FILTER', + 'start' => 'START', + 'limit' => 'LIMIT' + ]; + + const SYSTEM_RBAC = '00000000000000000000000000000001'; + const SYSTEM_PROCESSMAKER = '00000000000000000000000000000002'; /** - * Constructor of the class + * Role constructor. * - * return void + * @throws Exception */ public function __construct() { @@ -32,7 +49,7 @@ class Role foreach ($this->arrayFieldDefinition as $key => $value) { $this->arrayFieldNameForException[$value["fieldNameAux"]] = $key; } - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -42,7 +59,8 @@ class Role * * @param bool $flag Value that set the format * - * return void + * @return void + * @throws Exception */ public function setFormatFieldNameInUppercase($flag) { @@ -50,7 +68,7 @@ class Role $this->formatFieldNameInUppercase = $flag; $this->setArrayFieldNameForException($this->arrayFieldNameForException); - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -60,7 +78,8 @@ class Role * * @param array $arrayData Data with the fields * - * return void + * @return void + * @throws Exception */ public function setArrayFieldNameForException(array $arrayData) { @@ -68,7 +87,7 @@ class Role foreach ($arrayData as $key => $value) { $this->arrayFieldNameForException[$key] = $this->getFieldNameByFormatFieldName($value); } - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -78,13 +97,14 @@ class Role * * @param string $fieldName Field name * - * return string Return the field name according the format + * @return string Return the field name according the format + * @throws Exception */ public function getFieldNameByFormatFieldName($fieldName) { try { - return ($this->formatFieldNameInUppercase)? strtoupper($fieldName) : strtolower($fieldName); - } catch (\Exception $e) { + return ($this->formatFieldNameInUppercase) ? strtoupper($fieldName) : strtolower($fieldName); + } catch (Exception $e) { throw $e; } } @@ -92,35 +112,37 @@ class Role /** * Verify if exists the code of a Role * - * @param string $roleCode Code - * @param string $roleSystemUid Unique id of System (00000000000000000000000000000001: RBAC, 00000000000000000000000000000002: PROCESSMAKER) + * @param string $roleCode Code + * @param string $roleSystemUid Unique id of System (00000000000000000000000000000001: RBAC, + * 00000000000000000000000000000002: PROCESSMAKER) * @param string $roleUidExclude Unique id of Role to exclude * - * return bool Return true if exists the code of a Role, false otherwise + * @return bool Return true if exists the code of a Role, false otherwise + * @throws Exception */ public function existsCode($roleCode, $roleSystemUid, $roleUidExclude = "") { try { - $criteria = new \Criteria("rbac"); + $criteria = new Criteria("rbac"); - $criteria->addSelectColumn(\RolesPeer::ROL_UID); + $criteria->addSelectColumn(RolesPeer::ROL_UID); - $criteria->add(\RolesPeer::ROL_SYSTEM, $roleSystemUid, \Criteria::EQUAL); + $criteria->add(RolesPeer::ROL_SYSTEM, $roleSystemUid, Criteria::EQUAL); - if ($roleUidExclude != "") { - $criteria->add(\RolesPeer::ROL_UID, $roleUidExclude, \Criteria::NOT_EQUAL); + if (!empty($roleUidExclude)) { + $criteria->add(RolesPeer::ROL_UID, $roleUidExclude, Criteria::NOT_EQUAL); } - $criteria->add(\RolesPeer::ROL_CODE, $roleCode, \Criteria::EQUAL); + $criteria->add(RolesPeer::ROL_CODE, $roleCode, Criteria::EQUAL); - $rsCriteria = \RolesPeer::doSelectRS($criteria); + $rsCriteria = RolesPeer::doSelectRS($criteria); if ($rsCriteria->next()) { return true; } else { return false; } - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -128,34 +150,36 @@ class Role /** * Verify if exists the name of a Role * - * @param string $roleName Name - * @param string $roleSystemUid Unique id of System (00000000000000000000000000000001: RBAC, 00000000000000000000000000000002: PROCESSMAKER) + * @param string $roleName Name + * @param string $roleSystemUid Unique id of System (00000000000000000000000000000001: RBAC, + * 00000000000000000000000000000002: PROCESSMAKER) * @param string $roleUidExclude Unique id of Role to exclude * - * return bool Return true if exists the name of a Role, false otherwise + * @return bool Return true if exists the name of a Role, false otherwise + * @throws Exception */ public function existsName($roleName, $roleSystemUid, $roleUidExclude = "") { try { //Set variables - $content = new \Content(); - $role = new \Roles(); + $content = new Content(); + $role = new ModelRoles(); $arrayContentByRole = $content->getAllContentsByRole(); //SQL - $criteria = new \Criteria("rbac"); + $criteria = new Criteria("rbac"); - $criteria->addSelectColumn(\RolesPeer::ROL_UID); + $criteria->addSelectColumn(RolesPeer::ROL_UID); - $criteria->add(\RolesPeer::ROL_SYSTEM, $roleSystemUid, \Criteria::EQUAL); + $criteria->add(RolesPeer::ROL_SYSTEM, $roleSystemUid, Criteria::EQUAL); if ($roleUidExclude != "") { - $criteria->add(\RolesPeer::ROL_UID, $roleUidExclude, \Criteria::NOT_EQUAL); + $criteria->add(RolesPeer::ROL_UID, $roleUidExclude, Criteria::NOT_EQUAL); } - $rsCriteria = \RolesPeer::doSelectRS($criteria); - $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); + $rsCriteria = RolesPeer::doSelectRS($criteria); + $rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC); while ($rsCriteria->next()) { $row = $rsCriteria->getRow(); @@ -175,7 +199,7 @@ class Role } return false; - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -183,20 +207,21 @@ class Role /** * Verify if does not exist the Role in table ROLES * - * @param string $roleUid Unique id of Role + * @param string $roleUid Unique id of Role * @param string $fieldNameForException Field name for the exception * - * return void Throw exception if does not exist the Role in table ROLES + * @return void Throw exception if does not exist the Role in table ROLES + * @throws Exception */ public function throwExceptionIfNotExistsRole($roleUid, $fieldNameForException) { try { - $obj = \RolesPeer::retrieveByPK($roleUid); + $obj = RolesPeer::retrieveByPK($roleUid); if (is_null($obj)) { - throw new \Exception(\G::LoadTranslation("ID_ROLE_DOES_NOT_EXIST", array($fieldNameForException, $roleUid))); + throw new Exception(G::LoadTranslation("ID_ROLE_DOES_NOT_EXIST", [$fieldNameForException, $roleUid])); } - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -204,20 +229,22 @@ class Role /** * Verify if exists the code of a Role * - * @param string $roleCode Code - * @param string $roleSystemUid Unique id of System (00000000000000000000000000000001: RBAC, 00000000000000000000000000000002: PROCESSMAKER) + * @param string $roleCode Code + * @param string $roleSystemUid Unique id of System (00000000000000000000000000000001: RBAC, + * 00000000000000000000000000000002: PROCESSMAKER) * @param string $fieldNameForException Field name for the exception - * @param string $roleUidExclude Unique id of Role to exclude + * @param string $roleUidExclude Unique id of Role to exclude * - * return void Throw exception if exists the code of a Role + * @return void Throw exception if exists the code of a Role + * @throws Exception */ public function throwExceptionIfExistsCode($roleCode, $roleSystemUid, $fieldNameForException, $roleUidExclude = "") { try { if ($this->existsCode($roleCode, $roleSystemUid, $roleUidExclude)) { - throw new \Exception(\G::LoadTranslation("ID_ROLE_CODE_ALREADY_EXISTS", array($fieldNameForException, $roleCode))); + throw new Exception(G::LoadTranslation("ID_ROLE_CODE_ALREADY_EXISTS", [$fieldNameForException, $roleCode])); } - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -225,20 +252,22 @@ class Role /** * Verify if exists the name of a Role * - * @param string $roleName Name - * @param string $roleSystemUid Unique id of System (00000000000000000000000000000001: RBAC, 00000000000000000000000000000002: PROCESSMAKER) + * @param string $roleName Name + * @param string $roleSystemUid Unique id of System (00000000000000000000000000000001: RBAC, + * 00000000000000000000000000000002: PROCESSMAKER) * @param string $fieldNameForException Field name for the exception - * @param string $roleUidExclude Unique id of Role to exclude + * @param string $roleUidExclude Unique id of Role to exclude * - * return void Throw exception if exists the name of a Role + * @return void Throw exception if exists the name of a Role + * @throws Exception */ public function throwExceptionIfExistsName($roleName, $roleSystemUid, $fieldNameForException, $roleUidExclude = "") { try { if ($this->existsName($roleName, $roleSystemUid, $roleUidExclude)) { - throw new \Exception(\G::LoadTranslation("ID_ROLE_NAME_ALREADY_EXISTS", array($fieldNameForException, $roleName))); + throw new Exception(G::LoadTranslation("ID_ROLE_NAME_ALREADY_EXISTS", [$fieldNameForException, $roleName])); } - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -246,28 +275,29 @@ class Role /** * Validate the data if they are invalid (INSERT and UPDATE) * - * @param string $roleUid Unique id of Role - * @param array $arrayData Data + * @param string $roleUid Unique id of Role + * @param array $arrayData Data * - * return void Throw exception if data has an invalid value + * @return void Throw exception if data has an invalid value + * @throws Exception */ public function throwExceptionIfDataIsInvalid($roleUid, array $arrayData) { try { //Set variables - $arrayRoleData = ($roleUid == "")? array() : $this->getRole($roleUid, true); - $flagInsert = ($roleUid == "")? true : false; + $arrayRoleData = ($roleUid == "") ? [] : $this->getRole($roleUid, true); + $flagInsert = ($roleUid == "") ? true : false; $arrayDataMain = array_merge($arrayRoleData, $arrayData); //Verify data - Field definition - $process = new \ProcessMaker\BusinessModel\Process(); + $process = new Process(); $process->throwExceptionIfDataNotMetFieldDefinition($arrayData, $this->arrayFieldDefinition, $this->arrayFieldNameForException, $flagInsert); //Verify data if (isset($arrayData["ROL_CODE"]) && !preg_match("/^\w+$/", $arrayData["ROL_CODE"])) { - throw new \Exception(\G::LoadTranslation("ID_ROLE_FIELD_CANNOT_CONTAIN_SPECIAL_CHARACTERS", array($this->arrayFieldNameForException["roleCode"]))); + throw new Exception(G::LoadTranslation("ID_ROLE_FIELD_CANNOT_CONTAIN_SPECIAL_CHARACTERS", [$this->arrayFieldNameForException["roleCode"]])); } if (isset($arrayData["ROL_CODE"])) { @@ -277,7 +307,7 @@ class Role if (isset($arrayData["ROL_NAME"])) { $this->throwExceptionIfExistsName($arrayData["ROL_NAME"], $arrayDataMain["ROL_SYSTEM"], $this->arrayFieldNameForException["roleName"], $roleUid); } - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -287,41 +317,42 @@ class Role * * @param array $arrayData Data * - * return array Return data of the new Role created + * @return array Return data of the new Role created + * @throws Exception */ public function create(array $arrayData) { try { //Verify data - $process = new \ProcessMaker\BusinessModel\Process(); - $validator = new \ProcessMaker\BusinessModel\Validator(); + $validator = new Validator(); $validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData"); //Set data $arrayData = array_change_key_case($arrayData, CASE_UPPER); - unset($arrayData["ROL_UID"]); + unset($arrayData['ROL_UID']); - $arrayData["ROL_SYSTEM"] = "00000000000000000000000000000002"; //PROCESSMAKER + $arrayData['ROL_SYSTEM'] = self::SYSTEM_PROCESSMAKER; //Verify data - $this->throwExceptionIfDataIsInvalid("", $arrayData); + $this->throwExceptionIfDataIsInvalid('', $arrayData); //Create - $role = new \Roles(); + $role = new ModelRoles(); - $roleUid = \ProcessMaker\Util\Common::generateUID(); + $roleUid = Common::generateUID(); - $arrayData["ROL_UID"] = $roleUid; - $arrayData["ROL_STATUS"] = (isset($arrayData["ROL_STATUS"]))? (($arrayData["ROL_STATUS"] == "ACTIVE")? 1 : 0) : 1; - $arrayData["ROL_CREATE_DATE"] = date("Y-M-d H:i:s"); + $arrayData['ROL_UID'] = $roleUid; + $arrayData['ROL_STATUS'] = isset($arrayData['ROL_STATUS']) ? $arrayData['ROL_STATUS'] === 'ACTIVE' ? 1 : 0 : 1; + $arrayData['ROL_CREATE_DATE'] = date('Y-M-d H:i:s'); + $arrayData['ROL_UPDATE_DATE'] = date('Y-M-d H:i:s'); - $result = $role->createRole($arrayData); + $role->createRole($arrayData); //Return return $this->getRole($roleUid); - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -329,17 +360,17 @@ class Role /** * Update Role * - * @param string $roleUid Unique id of Role - * @param array $arrayData Data + * @param string $roleUid Unique id of Role + * @param array $arrayData Data * - * return array Return data of the Role updated + * @return array Return data of the Role updated + * @throws Exception */ public function update($roleUid, array $arrayData) { try { //Verify data - $process = new \ProcessMaker\BusinessModel\Process(); - $validator = new \ProcessMaker\BusinessModel\Validator(); + $validator = new Validator(); $validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData"); @@ -350,29 +381,29 @@ class Role $arrayRoleData = $this->getRole($roleUid); //Verify data - $this->throwExceptionIfNotExistsRole($roleUid, $this->arrayFieldNameForException["roleUid"]); + $this->throwExceptionIfNotExistsRole($roleUid, $this->arrayFieldNameForException['roleUid']); - if ($roleUid == "00000000000000000000000000000002") { - throw new \Exception(\G::LoadTranslation("ID_ROLES_MSG")); + if ($roleUid === self::SYSTEM_PROCESSMAKER) { + throw new Exception(G::LoadTranslation('ID_ROLES_MSG')); } $this->throwExceptionIfDataIsInvalid($roleUid, $arrayData); //Update - $role = new \Roles(); + $role = new ModelRoles(); - $arrayData["ROL_UID"] = $roleUid; - $arrayData["ROL_UPDATE_DATE"] = date("Y-M-d H:i:s"); + $arrayData['ROL_UID'] = $roleUid; + $arrayData['ROL_UPDATE_DATE'] = date('Y-M-d H:i:s'); - if (!isset($arrayData["ROL_NAME"])) { - $arrayData["ROL_NAME"] = $arrayRoleData[$this->getFieldNameByFormatFieldName("ROL_NAME")]; + if (!isset($arrayData['ROL_NAME'])) { + $arrayData['ROL_NAME'] = $arrayRoleData[$this->getFieldNameByFormatFieldName('ROL_NAME')]; } - if (isset($arrayData["ROL_STATUS"])) { - $arrayData["ROL_STATUS"] = ($arrayData["ROL_STATUS"] == "ACTIVE")? 1 : 0; + if (isset($arrayData['ROL_STATUS'])) { + $arrayData['ROL_STATUS'] = $arrayData['ROL_STATUS'] === 'ACTIVE' ? 1 : 0; } - $result = $role->updateRole($arrayData); + $role->updateRole($arrayData); $arrayData = $arrayDataBackup; @@ -382,7 +413,7 @@ class Role } return $arrayData; - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -392,23 +423,24 @@ class Role * * @param string $roleUid Unique id of Role * - * return void + * @return void + * @throws Exception */ public function delete($roleUid) { try { - $role = new \Roles(); + $role = new ModelRoles(); //Verify data $this->throwExceptionIfNotExistsRole($roleUid, $this->arrayFieldNameForException["roleUid"]); if ($role->numUsersWithRole($roleUid) > 0) { - throw new \Exception(\G::LoadTranslation("ID_ROLES_CAN_NOT_DELETE")); + throw new Exception(G::LoadTranslation("ID_ROLES_CAN_NOT_DELETE")); } //Delete $result = $role->removeRole($roleUid); - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -416,25 +448,26 @@ class Role /** * Get criteria for Role * - * return object + * @return object + * @throws Exception */ public function getRoleCriteria() { try { - $criteria = new \Criteria("rbac"); + $criteria = new Criteria("rbac"); - $criteria->addSelectColumn(\RolesPeer::ROL_UID); - $criteria->addSelectColumn(\RolesPeer::ROL_PARENT); - $criteria->addSelectColumn(\RolesPeer::ROL_CODE); - $criteria->addSelectColumn(\RolesPeer::ROL_STATUS); - $criteria->addSelectColumn(\RolesPeer::ROL_SYSTEM); - $criteria->addSelectColumn(\RolesPeer::ROL_CREATE_DATE); - $criteria->addSelectColumn(\RolesPeer::ROL_UPDATE_DATE); + $criteria->addSelectColumn(RolesPeer::ROL_UID); + $criteria->addSelectColumn(RolesPeer::ROL_PARENT); + $criteria->addSelectColumn(RolesPeer::ROL_CODE); + $criteria->addSelectColumn(RolesPeer::ROL_STATUS); + $criteria->addSelectColumn(RolesPeer::ROL_SYSTEM); + $criteria->addSelectColumn(RolesPeer::ROL_CREATE_DATE); + $criteria->addSelectColumn(RolesPeer::ROL_UPDATE_DATE); - $criteria->add(\RolesPeer::ROL_SYSTEM, "00000000000000000000000000000002", \Criteria::EQUAL); //PROCESSMAKER + $criteria->add(RolesPeer::ROL_SYSTEM, self::SYSTEM_PROCESSMAKER, Criteria::EQUAL); //PROCESSMAKER return $criteria; - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -444,35 +477,36 @@ class Role * * @param array $record Record * - * return array Return an array with data Role + * @return array Return an array with data Role + * @throws Exception */ public function getRoleDataFromRecord(array $record) { try { - $conf = new \Configurations(); + $conf = new Configurations(); $confEnvSetting = $conf->getFormats(); - $dateTime = new \DateTime($record["ROL_CREATE_DATE"]); - $roleCreateDate = $dateTime->format($confEnvSetting["dateFormat"]); + $dateTime = new DateTime($record['ROL_CREATE_DATE']); + $roleCreateDate = $dateTime->format($confEnvSetting['dateFormat']); - $roleUpdateDate = ""; + $roleUpdateDate = ''; - if (!empty($record["ROL_UPDATE_DATE"])) { - $dateTime = new \DateTime($record["ROL_UPDATE_DATE"]); - $roleUpdateDate = $dateTime->format($confEnvSetting["dateFormat"]); + if (!empty($record['ROL_UPDATE_DATE'])) { + $dateTime = new DateTime($record['ROL_UPDATE_DATE']); + $roleUpdateDate = $dateTime->format($confEnvSetting['dateFormat']); } - return array( - $this->getFieldNameByFormatFieldName("ROL_UID") => $record["ROL_UID"], - $this->getFieldNameByFormatFieldName("ROL_CODE") => $record["ROL_CODE"], - $this->getFieldNameByFormatFieldName("ROL_NAME") => $record["ROL_NAME"], - $this->getFieldNameByFormatFieldName("ROL_STATUS") => ($record["ROL_STATUS"] . "" == "1")? "ACTIVE" : "INACTIVE", - $this->getFieldNameByFormatFieldName("ROL_SYSTEM") => $record["ROL_SYSTEM"], - $this->getFieldNameByFormatFieldName("ROL_CREATE_DATE") => $roleCreateDate, - $this->getFieldNameByFormatFieldName("ROL_UPDATE_DATE") => $roleUpdateDate, - $this->getFieldNameByFormatFieldName("ROL_TOTAL_USERS") => (int)($record["ROL_TOTAL_USERS"]) - ); - } catch (\Exception $e) { + return [ + $this->getFieldNameByFormatFieldName('ROL_UID') => $record['ROL_UID'], + $this->getFieldNameByFormatFieldName('ROL_CODE') => $record['ROL_CODE'], + $this->getFieldNameByFormatFieldName('ROL_NAME') => $record['ROL_NAME'], + $this->getFieldNameByFormatFieldName('ROL_STATUS') => $record['ROL_STATUS'] . '' === '1' ? 'ACTIVE' : 'INACTIVE', + $this->getFieldNameByFormatFieldName('ROL_SYSTEM') => $record['ROL_SYSTEM'], + $this->getFieldNameByFormatFieldName('ROL_CREATE_DATE') => $roleCreateDate, + $this->getFieldNameByFormatFieldName('ROL_UPDATE_DATE') => $roleUpdateDate, + $this->getFieldNameByFormatFieldName('ROL_TOTAL_USERS') => (int)$record['ROL_TOTAL_USERS'] + ]; + } catch (Exception $e) { throw $e; } } @@ -480,23 +514,24 @@ class Role /** * Get all Roles * - * @param array $arrayFilterData Data of the filters - * @param string $sortField Field name to sort - * @param string $sortDir Direction of sorting (ASC, DESC) - * @param int $start Start - * @param int $limit Limit + * @param array $arrayFilterData Data of the filters + * @param string $sortField Field name to sort + * @param string $sortDir Direction of sorting (ASC, DESC) + * @param int $start Start + * @param int $limit Limit * - * return array Return an array with all Roles + * @return array Return an array with all Roles + * @throws Exception */ public function getRoles(array $arrayFilterData = null, $sortField = null, $sortDir = null, $start = null, $limit = null) { try { - $arrayRole = array(); + $arrayRole = []; //Verify data - $process = new \ProcessMaker\BusinessModel\Process(); + $process = new Process(); - $process->throwExceptionIfDataNotMetPagerVarDefinition(array("start" => $start, "limit" => $limit), $this->arrayFieldNameForException); + $process->throwExceptionIfDataNotMetPagerVarDefinition(["start" => $start, "limit" => $limit], $this->arrayFieldNameForException); //Get data if (!is_null($limit) && $limit . "" == "0") { @@ -504,31 +539,31 @@ class Role } //Set variables - $content = new \Content(); - $role = new \Roles(); + $content = new Content(); + $role = new ModelRoles(); $arrayContentByRole = $content->getAllContentsByRole(); //SQL $criteria = $this->getRoleCriteria(); - $criteria->addAsColumn("ROL_TOTAL_USERS", "(SELECT COUNT(" . \UsersRolesPeer::ROL_UID . ") FROM " . \UsersRolesPeer::TABLE_NAME . " WHERE " . \UsersRolesPeer::ROL_UID . " = " . \RolesPeer::ROL_UID . ")"); + $criteria->addAsColumn("ROL_TOTAL_USERS", "(SELECT COUNT(" . UsersRolesPeer::ROL_UID . ") FROM " . UsersRolesPeer::TABLE_NAME . " WHERE " . UsersRolesPeer::ROL_UID . " = " . RolesPeer::ROL_UID . ")"); if (!is_null($arrayFilterData) && is_array($arrayFilterData) && isset($arrayFilterData["filter"]) && trim($arrayFilterData["filter"]) != "") { - $criteria->add(\RolesPeer::ROL_CODE, "%" . $arrayFilterData["filter"] . "%", \Criteria::LIKE); + $criteria->add(RolesPeer::ROL_CODE, "%" . $arrayFilterData["filter"] . "%", Criteria::LIKE); } //SQL if (!is_null($sortField) && trim($sortField) != "") { $sortField = strtoupper($sortField); - if (in_array($sortField, array("ROL_UID", "ROL_PARENT", "ROL_STATUS", "ROL_SYSTEM", "ROL_CREATE_DATE", "ROL_UPDATE_DATE"))) { - $sortField = \RolesPeer::TABLE_NAME . "." . $sortField; + if (in_array($sortField, ["ROL_UID", "ROL_PARENT", "ROL_STATUS", "ROL_SYSTEM", "ROL_CREATE_DATE", "ROL_UPDATE_DATE"])) { + $sortField = RolesPeer::TABLE_NAME . "." . $sortField; } else { - $sortField = \RolesPeer::ROL_CODE; + $sortField = RolesPeer::ROL_CODE; } } else { - $sortField = \RolesPeer::ROL_CODE; + $sortField = RolesPeer::ROL_CODE; } if (!is_null($sortDir) && trim($sortDir) != "" && strtoupper($sortDir) == "DESC") { @@ -545,8 +580,8 @@ class Role $criteria->setLimit((int)($limit)); } - $rsCriteria = \RolesPeer::doSelectRS($criteria); - $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); + $rsCriteria = RolesPeer::doSelectRS($criteria); + $rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC); while ($rsCriteria->next()) { $row = $rsCriteria->getRow(); @@ -567,7 +602,7 @@ class Role //Return return $arrayRole; - } catch (\Exception $e) { + } catch (Exception $e) { throw $e; } } @@ -575,10 +610,11 @@ class Role /** * Get data of a Role * - * @param string $roleUid Unique id of Role - * @param bool $flagGetRecord Value that set the getting + * @param string $roleUid Unique id of Role + * @param bool $flagGetRecord Value that set the getting * - * return array Return an array with data of a Role + * @return array Return an array with data of a Role + * @throws Exception */ public function getRole($roleUid, $flagGetRecord = false) { @@ -588,8 +624,8 @@ class Role //Set variables if (!$flagGetRecord) { - $content = new \Content(); - $role = new \Roles(); + $content = new Content(); + $role = new ModelRoles(); $arrayContentByRole = $content->getAllContentsByRole(); } @@ -599,13 +635,13 @@ class Role $criteria = $this->getRoleCriteria(); if (!$flagGetRecord) { - $criteria->addAsColumn("ROL_TOTAL_USERS", "(SELECT COUNT(" . \UsersRolesPeer::ROL_UID . ") FROM " . \UsersRolesPeer::TABLE_NAME . " WHERE " . \UsersRolesPeer::ROL_UID . " = " . \RolesPeer::ROL_UID . ")"); + $criteria->addAsColumn("ROL_TOTAL_USERS", "(SELECT COUNT(" . UsersRolesPeer::ROL_UID . ") FROM " . UsersRolesPeer::TABLE_NAME . " WHERE " . UsersRolesPeer::ROL_UID . " = " . RolesPeer::ROL_UID . ")"); } - $criteria->add(\RolesPeer::ROL_UID, $roleUid, \Criteria::EQUAL); + $criteria->add(RolesPeer::ROL_UID, $roleUid, Criteria::EQUAL); - $rsCriteria = \RolesPeer::doSelectRS($criteria); - $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); + $rsCriteria = RolesPeer::doSelectRS($criteria); + $rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC); $rsCriteria->next(); @@ -623,8 +659,8 @@ class Role } //Return - return (!$flagGetRecord)? $this->getRoleDataFromRecord($row) : $row; - } catch (\Exception $e) { + return (!$flagGetRecord) ? $this->getRoleDataFromRecord($row) : $row; + } catch (Exception $e) { throw $e; } } diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Role.php b/workflow/engine/src/ProcessMaker/Services/Api/Role.php index f16777593..42279d9ec 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Role.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Role.php @@ -1,8 +1,14 @@ getUserId(); - if (!$user->checkPermission($usrUid, "PM_USERS")) { - throw new \Exception(\G::LoadTranslation("ID_USER_NOT_HAVE_PERMISSION", array($usrUid))); + if (!$user->checkPermission($usrUid, 'PM_USERS')) { + throw new Exception(G::LoadTranslation('ID_USER_NOT_HAVE_PERMISSION', [$usrUid])); } - $this->role = new \ProcessMaker\BusinessModel\Role(); - + $this->role = new BmRole(); $this->role->setFormatFieldNameInUppercase(false); - } catch (\Exception $e) { + } catch (Exception $e) { throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } /** + * Load all roles + * + * @url GET + * + * @param string $filter + * @param int $start + * @param int $limit + * + * @return mixed + * @throws RestException + * * @access protected * @class AccessControl {@permission PM_USERS} - * @url GET */ public function index($filter = null, $start = null, $limit = null) { try { - $response = $this->role->getRoles(array("filter" => $filter), null, null, $start, $limit); + $response = $this->role->getRoles(['filter' => $filter], null, null, $start, $limit); - return \ProcessMaker\Util\DateTime::convertUtcToIso8601($response, $this->arrayFieldIso8601); - } catch (\Exception $e) { + return DateTime::convertUtcToIso8601($response, $this->arrayFieldIso8601); + } catch (Exception $e) { throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } /** - * @access protected - * @class AccessControl {@permission PM_USERS} + * load information role + * * @url GET /:rol_uid * * @param string $rol_uid {@min 32}{@max 32} + * + * @return mixed + * @throws RestException + * + * @access protected + * @class AccessControl {@permission PM_USERS} */ public function doGet($rol_uid) { try { $response = $this->role->getRole($rol_uid); - return \ProcessMaker\Util\DateTime::convertUtcToIso8601($response, $this->arrayFieldIso8601); - } catch (\Exception $e) { + return DateTime::convertUtcToIso8601($response, $this->arrayFieldIso8601); + } catch (Exception $e) { throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } /** - * @access protected - * @class AccessControl {@permission PM_USERS} + * Create rol + * * @url POST + * @status 201 * * @param array $request_data * - * @status 201 + * @return array + * @throws RestException + * + * @access protected + * @class AccessControl {@permission PM_USERS} */ public function doPost(array $request_data) { try { - $arrayData = $this->role->create($request_data); - - $response = $arrayData; - - return $response; - } catch (\Exception $e) { + return $this->role->create($request_data); + } catch (Exception $e) { throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } @@ -103,8 +125,8 @@ class Role extends Api * * @url PUT /:rol_uid * - * @param string $rol_uid {@min 32}{@max 32} - * @param array $request_data + * @param string $rol_uid {@min 32}{@max 32} + * @param array $request_data * * @throws RestException * @@ -114,24 +136,29 @@ class Role extends Api public function doPut($rol_uid, array $request_data) { try { - $arrayData = $this->role->update($rol_uid, $request_data); - } catch (\Exception $e) { + $this->role->update($rol_uid, $request_data); + } catch (Exception $e) { throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } /** - * @access protected - * @class AccessControl {@permission PM_USERS} + * Delete role + * * @url DELETE /:rol_uid * * @param string $rol_uid {@min 32}{@max 32} + * + * @throws RestException + * + * @access protected + * @class AccessControl {@permission PM_USERS} */ public function doDelete($rol_uid) { try { $this->role->delete($rol_uid); - } catch (\Exception $e) { + } catch (Exception $e) { throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } diff --git a/workflow/engine/templates/roles/rolesList.js b/workflow/engine/templates/roles/rolesList.js index f1637116c..00722432f 100644 --- a/workflow/engine/templates/roles/rolesList.js +++ b/workflow/engine/templates/roles/rolesList.js @@ -635,8 +635,11 @@ DoSearch = function(){ }; //Render Date Function -render_date = function(v){ - return _DF(v); +render_date = function(date){ + if (date != null) { + return _DF(date); + } + return date; }; //Update Page Size Configuration