function getUserResources($userprivs, $resourceprivs = array("available"), $onlygroups = 0, $includedeleted = 0, $userid = 0) { global $user, $viewmode; $key = getKey(array($userprivs, $resourceprivs, $onlygroups, $includedeleted, $userid)); if (array_key_exists($key, $_SESSION['userresources'])) { return $_SESSION['userresources'][$key]; } #FIXME this whole function could be much more efficient if (!$userid) { $userid = $user["id"]; } $return = array(); $nodeprivs = array(); $startnodes = array(); # build a list of nodes where user is granted $userprivs $inlist = "'" . implode("','", $userprivs) . "'"; $query = "SELECT u.privnodeid " . "FROM userpriv u, " . "userprivtype t " . "WHERE u.userprivtypeid = t.id AND " . "t.name IN ({$inlist}) AND " . "(u.userid = {$userid} OR " . "u.usergroupid IN (SELECT usergroupid " . "FROM usergroupmembers " . "WHERE userid = {$userid}))"; $qh = doQuery($query, 101); while ($row = mysql_fetch_assoc($qh)) { array_push($startnodes, $row["privnodeid"]); } # travel up tree looking at privileges granted at parent nodes foreach ($startnodes as $nodeid) { getUserResourcesUp($nodeprivs, $nodeid, $userid, $userprivs); } # travel down tree looking at privileges granted at child nodes if cascade privs at this node foreach ($startnodes as $nodeid) { getUserResourcesDown($nodeprivs, $nodeid, $userid, $userprivs); } $nodeprivs = simplifyNodePrivs($nodeprivs, $userprivs); // call this before calling addUserResources addUserResources($nodeprivs, $userid); # build a list of resource groups user has access to $resourcegroups = array(); $types = getTypes("resources"); foreach ($types["resources"] as $type) { $resourcegroups[$type] = array(); } foreach (array_keys($nodeprivs) as $nodeid) { // if user doesn't have privs at this node, no need to look // at any resource groups here $haspriv = 0; foreach ($userprivs as $priv) { if ($nodeprivs[$nodeid][$priv]) { $haspriv = 1; } } if (!$haspriv) { continue; } # check to see if resource groups has any of $resourceprivs at this node foreach (array_keys($nodeprivs[$nodeid]["resources"]) as $resourceid) { foreach ($resourceprivs as $priv) { if (in_array($priv, $nodeprivs[$nodeid]["resources"][$resourceid])) { list($type, $name, $id) = split('/', $resourceid); if (!array_key_exists($type, $resourcegroups)) { $resourcegroups[$type] = array(); } if (!in_array($name, $resourcegroups[$type])) { $resourcegroups[$type][$id] = $name; } } } } # check to see if resource groups has any of $resourceprivs cascaded to this node foreach (array_keys($nodeprivs[$nodeid]["cascaderesources"]) as $resourceid) { foreach ($resourceprivs as $priv) { if (in_array($priv, $nodeprivs[$nodeid]["cascaderesources"][$resourceid]) && !(array_key_exists($resourceid, $nodeprivs[$nodeid]["resources"]) && in_array("block", $nodeprivs[$nodeid]["resources"][$resourceid]))) { list($type, $name, $id) = split('/', $resourceid); if (!array_key_exists($type, $resourcegroups)) { $resourcegroups[$type] = array(); } if (!in_array($name, $resourcegroups[$type])) { $resourcegroups[$type][$id] = $name; } } } } } addOwnedResourceGroups($resourcegroups, $userid); if ($onlygroups) { foreach (array_keys($resourcegroups) as $type) { uasort($resourcegroups[$type], "sortKeepIndex"); } $_SESSION['userresources'][$key] = $resourcegroups; return $resourcegroups; } $resources = array(); foreach (array_keys($resourcegroups) as $type) { $resources[$type] = getResourcesFromGroups($resourcegroups[$type], $type, $includedeleted); } addOwnedResources($resources, $includedeleted, $userid); $_SESSION['userresources'][$key] = $resources; return $resources; }
function getUserResources($userprivs, $resourceprivs = array("available"), $onlygroups = 0, $includedeleted = 0, $userid = 0, $groupid = 0) { global $user; if (in_array('managementnodeAdmin', $userprivs)) { $userprivs[] = 'mgmtnodeAdmin'; } $key = getKey(array($userprivs, $resourceprivs, $onlygroups, $includedeleted, $userid, $groupid)); if (array_key_exists($key, $_SESSION['userresources'])) { return $_SESSION['userresources'][$key]; } #FIXME this whole function could be much more efficient $bygroup = 0; if ($userid == 0 && $groupid != 0) { $bygroup = 1; } if (!$userid) { $userid = $user["id"]; } $return = array(); $nodeprivs = array(); $startnodes = array(); # build a list of nodes where user is granted $userprivs $inlist = "'" . implode("','", $userprivs) . "'"; $query = "SELECT u.privnodeid " . "FROM userpriv u, " . "userprivtype t " . "WHERE u.userprivtypeid = t.id AND " . "t.name IN ({$inlist}) AND "; if (!$bygroup) { $query .= "(u.userid = {$userid} OR " . "u.usergroupid IN (SELECT usergroupid " . "FROM usergroupmembers " . "WHERE userid = {$userid}))"; } else { $query .= "u.usergroupid = {$groupid}"; } $qh = doQuery($query, 101); while ($row = mysql_fetch_assoc($qh)) { array_push($startnodes, $row["privnodeid"]); } # build data array from userprivtype and userpriv tables to reduce queries # in addNodeUserResourcePrivs $privdataset = array('user' => array(), 'usergroup' => array()); $query = "SELECT t.name, " . "u.privnodeid " . "FROM userprivtype t, " . "userpriv u " . "WHERE u.userprivtypeid = t.id AND " . "u.userid IS NOT NULL AND " . "u.userid = {$userid} AND " . "t.name IN ('block','cascade',{$inlist})"; $qh = doQuery($query); while ($row = mysql_fetch_assoc($qh)) { if (!array_key_exists($row['privnodeid'], $privdataset['user'])) { $privdataset['user'][$row['privnodeid']] = array(); } $privdataset['user'][$row['privnodeid']][] = $row['name']; } $query = "SELECT t.name, " . "u.usergroupid, " . "u.privnodeid " . "FROM userprivtype t, " . "userpriv u " . "WHERE u.userprivtypeid = t.id AND " . "u.usergroupid IS NOT NULL AND "; if ($bygroup) { $query .= "u.usergroupid = {$groupid} AND "; } else { $query .= "u.usergroupid IN (SELECT usergroupid " . "FROM usergroupmembers " . "WHERE userid = {$userid}) AND "; } $query .= "t.name IN ('block','cascade',{$inlist}) " . "ORDER BY u.privnodeid, " . "u.usergroupid"; $qh = doQuery($query, 101); while ($row = mysql_fetch_assoc($qh)) { if (!array_key_exists($row['privnodeid'], $privdataset['usergroup'])) { $privdataset['usergroup'][$row['privnodeid']] = array(); } $privdataset['usergroup'][$row['privnodeid']][] = array('name' => $row['name'], 'groupid' => $row['usergroupid']); } # travel up tree looking at privileges granted at parent nodes foreach ($startnodes as $nodeid) { getUserResourcesUp($nodeprivs, $nodeid, $userid, $userprivs, $privdataset); } # travel down tree looking at privileges granted at child nodes if cascade privs at this node foreach ($startnodes as $nodeid) { getUserResourcesDown($nodeprivs, $nodeid, $userid, $userprivs, $privdataset); } $nodeprivs = simplifyNodePrivs($nodeprivs, $userprivs); // call this before calling addUserResources addUserResources($nodeprivs, $userid); # build a list of resource groups user has access to $resourcegroups = array(); $types = getTypes("resources"); foreach ($types["resources"] as $type) { $resourcegroups[$type] = array(); } foreach (array_keys($nodeprivs) as $nodeid) { // if user doesn't have privs at this node, no need to look // at any resource groups here $haspriv = 0; foreach ($userprivs as $priv) { if ($nodeprivs[$nodeid][$priv]) { $haspriv = 1; } } if (!$haspriv) { continue; } # check to see if resource groups has any of $resourceprivs at this node foreach (array_keys($nodeprivs[$nodeid]["resources"]) as $resourceid) { foreach ($resourceprivs as $priv) { if (in_array($priv, $nodeprivs[$nodeid]["resources"][$resourceid])) { list($type, $name, $id) = explode('/', $resourceid); if (!array_key_exists($type, $resourcegroups)) { $resourcegroups[$type] = array(); } if (!in_array($name, $resourcegroups[$type])) { $resourcegroups[$type][$id] = $name; } } } } # check to see if resource groups has any of $resourceprivs cascaded to this node foreach (array_keys($nodeprivs[$nodeid]["cascaderesources"]) as $resourceid) { foreach ($resourceprivs as $priv) { if (in_array($priv, $nodeprivs[$nodeid]["cascaderesources"][$resourceid]) && !(array_key_exists($resourceid, $nodeprivs[$nodeid]["resources"]) && in_array("block", $nodeprivs[$nodeid]["resources"][$resourceid]))) { list($type, $name, $id) = explode('/', $resourceid); if (!array_key_exists($type, $resourcegroups)) { $resourcegroups[$type] = array(); } if (!in_array($name, $resourcegroups[$type])) { $resourcegroups[$type][$id] = $name; } } } } } if (!$bygroup) { addOwnedResourceGroups($resourcegroups, $userid); } if ($onlygroups) { foreach (array_keys($resourcegroups) as $type) { uasort($resourcegroups[$type], "sortKeepIndex"); } $_SESSION['userresources'][$key] = $resourcegroups; return $resourcegroups; } $resources = array(); foreach (array_keys($resourcegroups) as $type) { $resources[$type] = getResourcesFromGroups($resourcegroups[$type], $type, $includedeleted); } if (!$bygroup) { addOwnedResources($resources, $includedeleted, $userid); } $noimageid = getImageId('noimage'); if (array_key_exists($noimageid, $resources['image'])) { unset($resources['image'][$noimageid]); } $_SESSION['userresources'][$key] = $resources; return $resources; }