/** * Pobranie wszystkich ról z dziedziczeniem oraz z przypisanymi groupmi * uprawnień, przyczym jeśli profil jest obecny, zwraca tylko role dla * profilu. Wykożystywane przy inicjowaniu rów w resource ACL (co poprawia * działanie $acl->hasRole('role'). * * @param $profiel mixed null | Row_Profile * @return array * */ public function getRoles($profile = null) { $roleModel = new Role(); $roleParentsModel = new RoleRole(); $roleGroupsModel = new RoleGroup(); if ($profile && $profile->id) { $db = Zend_Db_Table::getDefaultAdapter(); $sql = $db->quoteInto('select r.* from profile_role pr left join "role" r on r.id = pr.id_role where pr.id_profile = ?', $profile->id); $roles = $db->fetchAll($sql); } else { $roles = $roleModel->fetchAll("ghost = false ", 'priority asc')->toArray(); } $roleGroups = $roleGroupsModel->fetchAll()->toArray(); $roleParents = $roleParentsModel->fetchAll()->toArray(); $rolesArray = array(); foreach ($roles as $role) { $role['parents']['group'] = array(); $role['parents']['role'] = array(); $rolesArray[$role['id']] = $role; } foreach ($roleParents as $parent) { if (isset($rolesArray[$parent['id_role']])) { $rolesArray[$parent['id_role']]['parents']['role'][] = $parent['id_parent']; } } foreach ($roleGroups as $group) { if (isset($rolesArray[$group['id_role']])) { $rolesArray[$group['id_role']]['parents']['group'][] = $group['id_group']; } } return $rolesArray; }
/** * Pobranie wszystkich ról z dziedziczeniem oraz z przypisanymi groupmi * uprawnień, przyczym jeśli profil jest obecny, zwraca tylko role dla * profilu. Wykorzystywane przy inicjowaniu rów w resource ACL (co poprawia * działanie $acl->hasRole('role'). * * @param $profiel mixed null | Row_Profile * @return array * */ public function getRoles($profile = null) { $roleModel = new Role(); $groupModel = new Group(); $roleParentsModel = new RoleRole(); $roleGroupsModel = new RoleGroup(); $db = Zend_Db_Table::getDefaultAdapter(); $cm = $this->getBootstrap()->getResource('cachemanager'); $cache = $cm->getCache('rolecache'); if ($profile && $profile->id) { $cache_id = 'getRoleProfileWithID' . $profile->id; if (!($found = $cache->load($cache_id))) { $sql = $db->quoteInto('WITH RECURSIVE Roles (id, priority, child_id) AS ( SELECT id, priority, null::bigint as child_id FROM "role" WHERE id IN (select r.id from profile_role pr left join "role" r on r.id = pr.id_role where pr.id_profile = ?) UNION ALL SELECT r.id, r.priority, p.id as child_id FROM "role" r INNER JOIN role_role as rr ON r.id = rr.id_parent INNER JOIN Roles p ON rr.id_role = p.id ) SELECT * FROM Roles ORDER BY priority;', $profile->id); $found = $db->fetchAll($sql); $cache->save($found, $cache_id); } } else { $cache_id = 'getRoleProfileNoID'; if (!($found = $cache->load($cache_id))) { $sql = 'WITH RECURSIVE Roles (id, priority, child_id) AS ( SELECT id, priority, null::bigint as child_id FROM "role" UNION ALL SELECT r.id, r.priority, p.id as child_id FROM "role" r INNER JOIN role_role as rr ON r.id = rr.id_role INNER JOIN Roles p ON rr.id_parent = p.id ) SELECT * FROM Roles ORDER BY priority;'; $found = $db->fetchAll($sql); $cache->save($found, $cache_id); } // $roles = $roleModel->fetchAll("ghost = false ", 'priority asc')->toArray(); } $roles = $found; $cache_id = 'roleGroupsCache'; if (!($roleGroups = $cache->load($cache_id))) { $roleGroups = $roleGroupsModel->fetchAll()->toArray(); $cache->save($roleGroups, $cache_id); } $cache_id = 'roleParentsCache'; if (!($roleParents = $cache->load($cache_id))) { $roleParents = $roleParentsModel->fetchAll()->toArray(); $cache->save($roleParents, $cache_id); } $cache_id = 'groupsArrayCache'; if (!($groupsArray = $cache->load($cache_id))) { $groupsArray = $groupModel->fetchAll()->toArray(); $cache->save($groupsArray, $cache_id); } $groups = array(); foreach ($groupsArray as $groupFromArray) { $groupFromArray['priority'] = (int) $groupFromArray['priority']; $groups[$groupFromArray['id']] = $groupFromArray; } $rolesArray = array(); foreach ($roles as $role) { $role['parents']['group'] = array(); $role['parents']['role'] = array(); $rolesArray[$role['id']] = $role; } foreach ($roleParents as $parent) { if (isset($rolesArray[$parent['id_role']])) { $rolesArray[$parent['id_role']]['parents']['role'][$parent['id_parent']] = array('id' => $parent['id_parent'], 'priority' => $rolesArray[$parent['id_parent']]['priority']); } } foreach ($roleGroups as $group) { if (isset($rolesArray[$group['id_role']])) { $rolesArray[$group['id_role']]['parents']['group'][$group['id_group']] = array('id' => $group['id_group'], 'priority' => $groups[$group['id_group']]['priority']); } } return $rolesArray; }
/** * Pobieranie uprawnień dla profilu * @param Integer $id * @return Array $resources */ public function getResourcesForProfile($id) { $profileModel = new Profile(); $profile = $profileModel->findOne($id); $cm = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('cachemanager'); $cache = $cm->getCache('filecache'); if (!($data = $cache->load('profile_' . $id))) { $roles = $profile->findDependentRowset('ProfileRole')->toArray(); $roleGroupModel = new RoleGroup(); $profileResourceDenyModel = new ProfileResourceDeny(); $denyArr = $profileResourceDenyModel->fetchAll("id_profile=" . $id)->toArray(); foreach ($denyArr as $key => $deny) { if ($key > 0) { $comma = ", "; } $deniedGroups = $deniedGroups . $comma . $deny['id_group']; } $res['group'] = array(); foreach ($roles as $role) { if ($deniedGroups) { $groups = $roleGroupModel->fetchAll("id_role=" . $role['id_role'] . " AND id_group NOT IN (" . $deniedGroups . ")")->toArray(); } else { $groups = $roleGroupModel->fetchAll("id_role=" . $role['id_role'])->toArray(); } $res['group'] = array_merge($res['group'], $groups); } $cache->save($data, 'profile_' . $id, array('profiles', 'user_' . $this->id, 'groups')); } return $res; }