[795] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /**************************************************************************\ |
---|
| 4 | * eGroupWare * |
---|
| 5 | * http://www.egroupware.org * |
---|
| 6 | * -------------------------------------------- * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 8 | * under the terms of the GNU General Public License as published by the * |
---|
| 9 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 10 | * option) any later version. * |
---|
| 11 | \**************************************************************************/ |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * Provê métodos para administrar os perfis do processo. |
---|
| 15 | * @author Sidnei Augusto Drovetto Junior - drovetto@gmail.com |
---|
| 16 | * @version 1.0 |
---|
| 17 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 18 | * @package Workflow |
---|
| 19 | * @subpackage local |
---|
| 20 | */ |
---|
| 21 | class wf_role |
---|
| 22 | { |
---|
| 23 | /** |
---|
| 24 | * @var int $processID o ID do processo onde a classe está sendo utilizada |
---|
| 25 | * @access private |
---|
| 26 | */ |
---|
| 27 | private $processID; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * @var object $db link com o banco de dados do Expresso |
---|
| 31 | * @access private |
---|
| 32 | */ |
---|
| 33 | private $db; |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * @var object $roleManager objeto da classe RoleManager |
---|
| 37 | * @access private |
---|
| 38 | */ |
---|
| 39 | private $roleManager; |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * @var object $activityManager objeto da classe ActivityManager |
---|
| 43 | * @access private |
---|
| 44 | */ |
---|
| 45 | private $activityManager; |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Construtor da wf_role |
---|
| 49 | * @return object |
---|
| 50 | * @access public |
---|
| 51 | */ |
---|
| 52 | function wf_role() |
---|
| 53 | { |
---|
| 54 | if (!is_null($GLOBALS['workflow']['wf_runtime']->activity)) |
---|
| 55 | $this->processID = (int) $GLOBALS['workflow']['wf_runtime']->activity->getProcessId(); |
---|
| 56 | if (isset($GLOBALS['workflow']['job'])) |
---|
| 57 | $this->processID = (int) $GLOBALS['workflow']['job']['processID']; |
---|
| 58 | |
---|
[3167] | 59 | $this->db = &Factory::getInstance('WorkflowObjects')->getDBGalaxia()->Link_ID; |
---|
| 60 | $this->roleManager = Factory::getInstance('workflow_rolemanager'); |
---|
| 61 | $this->activityManager = Factory::getInstance('workflow_activitymanager'); |
---|
[795] | 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * Busca os perfis pertencentes ao processo que chama o método. |
---|
| 66 | * @return array Lista dos perfis pertencentes ao processo. |
---|
| 67 | * @access public |
---|
| 68 | */ |
---|
| 69 | function getRoles() |
---|
| 70 | { |
---|
| 71 | $output = array(); |
---|
| 72 | $roles = $this->roleManager->list_roles($this->processID, 0, -1, 'wf_name__ASC', '', ''); |
---|
| 73 | foreach ($roles['data'] as $role) |
---|
| 74 | $output[] = array('id' => $role['wf_role_id'], 'name' => $role['wf_name'], 'description' => $role['wf_description']); |
---|
| 75 | return $output; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Busca os perfis associados a uma atividade do processo. |
---|
| 80 | * @param string $activityName O nome da atividade da qual se quer os perfis. |
---|
| 81 | * @return array Lista dos perfis pertencentes à atividade. |
---|
| 82 | * @access public |
---|
| 83 | */ |
---|
| 84 | function getActivityRoles($activityName) |
---|
| 85 | { |
---|
| 86 | /* check if the activity exists */ |
---|
| 87 | $output = array(); |
---|
| 88 | if (!$this->activityManager->activity_name_exists($this->processID, $activityName)) |
---|
| 89 | return $output; |
---|
| 90 | |
---|
| 91 | /* get the roles */ |
---|
| 92 | $activityID = $this->activityManager->_get_activity_id_by_name($this->processID, $activityName); |
---|
| 93 | $roles = $this->activityManager->get_activity_roles($activityID); |
---|
| 94 | foreach ($roles as $role) |
---|
| 95 | $output[] = array('id' => $role['wf_role_id'], 'name' => $role['wf_name'], 'read_only' => ($role['wf_readonly'] == 0) ? false : true); |
---|
| 96 | |
---|
| 97 | usort($output, create_function('$a,$b', 'return strcasecmp($a[\'name\'],$b[\'name\']);')); |
---|
| 98 | return $output; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | * Busca o id de uma atividade pelo seu nome |
---|
| 103 | * @param string $activityName O nome da atividade da qual se quer o id |
---|
| 104 | * @return int O id da atividade ou null se não encontrado |
---|
| 105 | * @access public |
---|
| 106 | */ |
---|
| 107 | function getActivityIdByName($activityName) |
---|
| 108 | { |
---|
| 109 | /* check if the activity exists */ |
---|
| 110 | if (!$this->activityManager->activity_name_exists($this->processID, $activityName)) |
---|
| 111 | return null; |
---|
| 112 | |
---|
| 113 | /* get the id */ |
---|
| 114 | $activityID = $this->activityManager->_get_activity_id_by_name($this->processID, $activityName); |
---|
| 115 | return $activityID; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Cria um perfil que poderá ser utilizado no processo atual. |
---|
| 120 | * @param string $roleName O nome do perfil que será criado. |
---|
| 121 | * @param string $description A descrição do perfil que será criado. |
---|
| 122 | * @return mixed O ID do perfil criado. Em caso de erro, será retornado false. |
---|
| 123 | * @access public |
---|
| 124 | */ |
---|
| 125 | function createRole($roleName, $description) |
---|
| 126 | { |
---|
| 127 | $values = array( |
---|
| 128 | 'wf_name' => $roleName, |
---|
| 129 | 'wf_description' => $description); |
---|
| 130 | return $this->roleManager->replace_role($this->processID, 0, $values); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * Remove um perfil. |
---|
| 135 | * @param string $roleName O nome do perfil que será removido. |
---|
| 136 | * @return bool Será retornado true em caso de sucesso e false caso contrário. |
---|
| 137 | * @access public |
---|
| 138 | */ |
---|
| 139 | function removeRole($roleName) |
---|
| 140 | { |
---|
| 141 | /* check if the role exists */ |
---|
| 142 | if ($this->roleManager->role_name_exists($this->processID, $roleName) == 0) |
---|
| 143 | return false; |
---|
| 144 | |
---|
| 145 | /* remove the role */ |
---|
| 146 | $roleID = $this->roleManager->get_role_id($this->processID, $roleName); |
---|
| 147 | return $this->roleManager->remove_role($this->processID, $roleID); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * Atualiza um perfil. |
---|
| 152 | * @param string $previousRoleName O nome do perfil que será atualizado. |
---|
| 153 | * @param string $roleName O novo nome do perfil que será atualizado. |
---|
| 154 | * @param string $description A nova descrição do perfil que será atualizado. |
---|
| 155 | * @return mixed O ID do perfil atualizado. Em caso de erro, será retornado false. |
---|
| 156 | * @access public |
---|
| 157 | */ |
---|
| 158 | function updateRole($previousRoleName, $newRoleName, $newDescription) |
---|
| 159 | { |
---|
| 160 | /* check if the role exists */ |
---|
| 161 | if ($this->roleManager->role_name_exists($this->processID, $previousRoleName) == 0) |
---|
| 162 | return false; |
---|
| 163 | |
---|
| 164 | /* update the role */ |
---|
| 165 | $roleID = $this->roleManager->get_role_id($this->processID, $previousRoleName); |
---|
| 166 | $values = array( |
---|
| 167 | 'wf_name' => $newRoleName, |
---|
| 168 | 'wf_description' => $newDescription); |
---|
| 169 | return $this->roleManager->replace_role($this->processID, $roleID, $values); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * Adiciona usuários/grupos a um perfil. |
---|
| 174 | * @param mixed $users Um inteiro ou um array de inteiros representando os IDs dos usuários/grupos que serão adicionados ao perfil. |
---|
| 175 | * @param string $roleName O nome do perfil que receberá os usuários. |
---|
| 176 | * @access public |
---|
| 177 | */ |
---|
| 178 | function addUsersToRole($users, $roleName) |
---|
| 179 | { |
---|
| 180 | /* check if the role exists */ |
---|
| 181 | if ($this->roleManager->role_name_exists($this->processID, $roleName) == 0) |
---|
| 182 | return false; |
---|
| 183 | |
---|
| 184 | /* add the user/group to the role */ |
---|
| 185 | if (!is_array($users)) |
---|
| 186 | $users = array((int) $users); |
---|
| 187 | $roleID = $this->roleManager->get_role_id($this->processID, $roleName); |
---|
[3167] | 188 | $ldap = Factory::getInstance('WorkflowLDAP'); |
---|
[795] | 189 | foreach ($users as $user) |
---|
| 190 | { |
---|
| 191 | $user = str_replace('u', '', str_replace('g', '', $user)); |
---|
| 192 | $accountType = $ldap->getEntityType($user); |
---|
| 193 | if (($accountType == 'u') || ($accountType == 'g')) |
---|
| 194 | $this->roleManager->map_user_to_role($this->processID, $user, $roleID, $accountType); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * Busca os usuários/grupos de um perfil. |
---|
| 200 | * @param string $roleName O nome do perfil do qual se quer os usuários/grupos. |
---|
| 201 | * @param boolean $expandGroups Valor booleano que indica se os grupos devem ser expandidos. |
---|
| 202 | * @return array Lista dos usuários/grupos (id, nome e tipo) pertencentes ao perfil especificado. |
---|
| 203 | * @access public |
---|
| 204 | */ |
---|
| 205 | function getUsersFromRole($roleName, $expandGroups = false) |
---|
| 206 | { |
---|
| 207 | /* check if the role exists */ |
---|
| 208 | if ($this->roleManager->role_name_exists($this->processID, $roleName) == 0) |
---|
| 209 | return false; |
---|
| 210 | |
---|
| 211 | /* get the user/group from the role */ |
---|
| 212 | $users = $this->roleManager->list_mapped_users($this->processID, false, array('wf_role_name' => $roleName)); |
---|
| 213 | |
---|
[3167] | 214 | $ldap = Factory::getInstance('WorkflowLDAP'); |
---|
[795] | 215 | $output = array(); |
---|
| 216 | foreach ($users as $id => $login) |
---|
| 217 | { |
---|
[1048] | 218 | $accountType = $ldap->getEntityType($id); |
---|
[795] | 219 | // if it must expand the group, get its users and put them into the tmp_output array using |
---|
| 220 | // their uidnumber as key (it avoids duplicated values) |
---|
| 221 | if($accountType == 'g' && $expandGroups) |
---|
| 222 | { |
---|
| 223 | $groupUsers = $ldap->getGroupUsers($id); |
---|
| 224 | foreach ($groupUsers as $groupUser) |
---|
| 225 | if ((!isset($output[$groupUser['account_id']])) && ($groupUser['account_name'] != '')) |
---|
| 226 | $output[$groupUser['account_id']] = array('id' => $groupUser['account_id'], 'name' => $groupUser['account_name'], 'type' => 'u'); |
---|
| 227 | } |
---|
| 228 | else |
---|
| 229 | { |
---|
| 230 | if (isset($output[$id])) |
---|
| 231 | continue; |
---|
| 232 | |
---|
| 233 | $name = $ldap->getName($id); |
---|
| 234 | if ($name != '') |
---|
| 235 | $output[$id] = array('id' => $id, 'name' => $name, 'type' => $accountType); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | // format the output array |
---|
| 240 | $output = array_values($output); |
---|
| 241 | |
---|
| 242 | usort($output, create_function('$a,$b', 'return strcasecmp($a[\'name\'],$b[\'name\']);')); |
---|
| 243 | return $output; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | /** |
---|
| 247 | * Remove os usuários/grupos um perfil. |
---|
| 248 | * @param mixed $users Um inteiro ou um array de inteiros representando os IDs dos usuários/grupos que serão removidos do perfil. |
---|
| 249 | * @param string $roleName O nome do perfil que terá os usuários/grupos removidos. |
---|
| 250 | * @access public |
---|
| 251 | */ |
---|
| 252 | function removeUsersFromRole($users, $roleName) |
---|
| 253 | { |
---|
| 254 | /* check if the role exists */ |
---|
| 255 | if ($this->roleManager->role_name_exists($this->processID, $roleName) == 0) |
---|
| 256 | return false; |
---|
| 257 | |
---|
| 258 | /* remove the users from the role */ |
---|
| 259 | if (!is_array($users)) |
---|
| 260 | $users = array((int) $users); |
---|
| 261 | $roleID = $this->roleManager->get_role_id($this->processID, $roleName); |
---|
| 262 | |
---|
| 263 | foreach ($users as $user) |
---|
| 264 | { |
---|
| 265 | $user = str_replace('u', '', str_replace('g', '', $user)); |
---|
| 266 | $this->roleManager->remove_mapping($user, $roleID); |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | /** |
---|
| 271 | * Associa um perfil a uma atividade. |
---|
| 272 | * @param string $roleName O nome do perfil que será associado à atividade. |
---|
| 273 | * @param string $activityName O nome da atividade que será associada ao perfil. |
---|
| 274 | * @return bool Será retornado true em caso de sucesso e false caso contrário. |
---|
| 275 | * @access public |
---|
| 276 | */ |
---|
| 277 | function mapRoleToActivity($roleName, $activityName, $readOnly = false) |
---|
| 278 | { |
---|
| 279 | /* check if the role and the activity exist */ |
---|
| 280 | if (($this->roleManager->role_name_exists($this->processID, $roleName) == 0) || (!$this->activityManager->activity_name_exists($this->processID, $activityName))) |
---|
| 281 | return false; |
---|
| 282 | |
---|
| 283 | /* create the new mapping */ |
---|
| 284 | $activityID = $this->activityManager->_get_activity_id_by_name($this->processID, $activityName); |
---|
| 285 | $roleID = $this->roleManager->get_role_id($this->processID, $roleName); |
---|
| 286 | return $this->activityManager->add_activity_role($activityID, $roleID, $readOnly); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | /** |
---|
| 290 | * Desassocia um perfil de uma atividade. |
---|
| 291 | * @param string $roleName O nome do perfil que será desassociado à atividade. |
---|
| 292 | * @param string $activityName O nome da atividade que será desassociada ao perfil. |
---|
| 293 | * @return bool Será retornado true em caso de sucesso e false caso contrário. |
---|
| 294 | * @access public |
---|
| 295 | */ |
---|
| 296 | function unmapRoleFromActivity($roleName, $activityName) |
---|
| 297 | { |
---|
| 298 | /* check if the role and the activity exist */ |
---|
| 299 | if (($this->roleManager->role_name_exists($this->processID, $roleName) == 0) || (!$this->activityManager->activity_name_exists($this->processID, $activityName))) |
---|
| 300 | return false; |
---|
| 301 | |
---|
| 302 | /* remove the mapping */ |
---|
| 303 | $activityID = $this->activityManager->_get_activity_id_by_name($this->processID, $activityName); |
---|
| 304 | $roleID = $this->roleManager->get_role_id($this->processID, $roleName); |
---|
| 305 | return $this->activityManager->remove_activity_role($activityID, $roleID); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | /** |
---|
| 309 | * Recupera o ID de um perfil através do seu nome |
---|
| 310 | * @param string $roleName O nome do perfil que será criado. |
---|
| 311 | * @return int O ID do perfil solicitado |
---|
| 312 | * @access public |
---|
| 313 | */ |
---|
| 314 | function getRoleIdByName($roleName) |
---|
| 315 | { |
---|
| 316 | /* check if the role exists */ |
---|
| 317 | if ($this->roleManager->role_name_exists($this->processID, $roleName) == 0) |
---|
| 318 | return false; |
---|
| 319 | |
---|
| 320 | /* get roleId by roleName */ |
---|
| 321 | return $this->roleManager->get_role_id($this->processID, $roleName); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | /** |
---|
| 325 | * Recupera informações sobre um perfil a partir de seu ID |
---|
| 326 | * @param int $roleID O ID do perfil solicitado. |
---|
| 327 | * @return mixed Uma array associativa contendo informações sobre o perfil. Em caso de erro, será retornado false. |
---|
| 328 | * @access public |
---|
| 329 | */ |
---|
| 330 | function getRoleByID($roleID) |
---|
| 331 | { |
---|
| 332 | /* get role information */ |
---|
| 333 | $data = $this->roleManager->get_role($this->processID, $roleID); |
---|
| 334 | if (!is_array($data)) |
---|
| 335 | return false; |
---|
| 336 | |
---|
| 337 | /* user friendly keys */ |
---|
| 338 | $output = array( |
---|
| 339 | 'id' => $data['wf_role_id'], |
---|
| 340 | 'name' => $data['wf_name'], |
---|
| 341 | 'description' => $data['wf_description'] |
---|
| 342 | ); |
---|
| 343 | |
---|
| 344 | return $output; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | /** |
---|
| 348 | * Busca os perfis de um usuário |
---|
| 349 | * @param int $userID O ID do usuário. |
---|
| 350 | * @param string $activityName O nome de uma atividade do processo (parâmetro opcional que se utilizado, verifica somente os perfis do usuário na atividade indicada). |
---|
| 351 | * @return array Uma array contendo os perfis do usuário. |
---|
| 352 | * @access public |
---|
| 353 | */ |
---|
| 354 | function getUserRoles($userID, $activityName = null) |
---|
| 355 | { |
---|
| 356 | /* get valid roles and initilize some variables */ |
---|
| 357 | $roles = (is_null($activityName)) ? $this->getRoles() : $this->getActivityRoles($activityName); |
---|
| 358 | $userID = (int) $userID; |
---|
| 359 | $output = array(); |
---|
| 360 | |
---|
| 361 | /* if no role found, return an empty array */ |
---|
| 362 | if (count($roles) < 1) |
---|
| 363 | return $output; |
---|
| 364 | |
---|
| 365 | /* get only the IDs of the roles */ |
---|
| 366 | $roleIDs = array_map(create_function('$a', 'return $a[\'id\'];'), $roles); |
---|
| 367 | |
---|
| 368 | /* get the roles */ |
---|
[3167] | 369 | $userGroups = Factory::getInstance('WorkflowLDAP')->getUserGroups($userID); |
---|
[795] | 370 | $query = 'SELECT DISTINCT role.wf_role_id, role.wf_name, role.wf_description FROM egw_wf_roles role, egw_wf_user_roles user_role WHERE (user_role.wf_role_id = role.wf_role_id) AND (role.wf_p_id = ?) AND (role.wf_role_id = ANY (?))'; |
---|
| 371 | $query .= ' AND (((user_role.wf_user = ?) AND (user_role.wf_account_type = ?))'; |
---|
| 372 | $values = array($this->processID, '{' . implode(', ', $roleIDs) . '}', $userID, 'u'); |
---|
| 373 | if (!empty($userGroups)) |
---|
| 374 | { |
---|
| 375 | $query .= ' OR ((user_role.wf_user = ANY (?)) AND (user_role.wf_account_type = ?))'; |
---|
| 376 | $values[] = '{' . implode(', ', $userGroups) . '}'; |
---|
| 377 | $values[] = 'g'; |
---|
| 378 | } |
---|
| 379 | $query .= ')'; |
---|
| 380 | $resultSet = $this->db->query($query, $values)->getArray(-1); |
---|
| 381 | |
---|
| 382 | /* format the output */ |
---|
| 383 | foreach ($resultSet as $row) |
---|
| 384 | $output[] = array( |
---|
| 385 | 'id' => $row['wf_role_id'], |
---|
| 386 | 'name' => $row['wf_name'], |
---|
| 387 | 'description' => $row['wf_description'] |
---|
| 388 | ); |
---|
| 389 | |
---|
| 390 | return $output; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | /** |
---|
| 394 | * Verifica se um usuário pertence a um determinado perfil |
---|
| 395 | * @param int $userID O ID do usuário. |
---|
| 396 | * @param string $roleName O nome do perfil. |
---|
| 397 | * @return bool True se o usuário pertence ao perfil ou false caso contrário. |
---|
| 398 | * @access public |
---|
| 399 | */ |
---|
| 400 | function checkUserInRole($userID, $roleName) |
---|
| 401 | { |
---|
| 402 | $roles = $this->getUserRoles($userID); |
---|
| 403 | foreach ($roles as $role) |
---|
| 404 | if ($role['name'] == $roleName) |
---|
| 405 | return true; |
---|
| 406 | |
---|
| 407 | return false; |
---|
| 408 | } |
---|
| 409 | } |
---|
| 410 | ?> |
---|