diff -urp taxonomy_access/taxonomy_access.module taxonomy_access_new/taxonomy_access.module --- taxonomy_access/taxonomy_access.module 2009-05-14 20:23:33.000000000 -0500 +++ taxonomy_access_new/taxonomy_access.module 2010-02-20 21:24:09.000000000 -0600 @@ -398,3 +398,240 @@ function taxonomy_access_restore_terms($ } } } + + +/** + * Updates permissions for a role for a term + * @param $tid + * The term to add the permission for. + * @param $rid + * The role id to add the permission for. + * @param $grants + * A hash of the grants in the form of $grants['perm'] = boolean + * A value of 1 will grant the permission for this user and term. +**/ +function taxonomy_access_grant_update($tid, $rid = null, $grants = null) { + if (!isset($tid) OR !is_numeric($rid)) { + return FALSE; + } + + $ta_sql = "INSERT INTO {term_access} (tid"; + $ta_sql_values = " VALUES ($tid"; + if (isset($rid)) { + $ta_sql .= ",rid"; + $ta_sql_values .= ",$rid"; + } + $sql = ""; + if (isset($grants)) { + foreach ($grants as $perm => $value) { + $sql .= ",grant_$perm"; + $ta_sql_values .= is_array($value) ? ",". $value[0] : ",$value"; + } + $sql .= ")"; + $ta_sql_values .= ")"; + } + else { + $sql .= ")"; + $ta_sql_values .= ")"; + } + $ta_sql .= $sql . $ta_sql_values; + + // issue #167977 - klance + $affected_nodes = _taxonomy_access_get_nodes_for_term($tid); + db_query("DELETE FROM {term_access} WHERE tid=%d AND rid=%d", $tid, ($rid)); + db_query($ta_sql); // insert into term_access + // issue #167977 - klance + _taxonomy_access_node_access_update($affected_nodes); +} + +/** + * Updates default permissions for a role for a vocabulary + * @param $vid + * The vocab to add the permission for. + * @param $rid + * The role id to add the permission to. + * @param $grants + * A hash of the grants in the form of $grants['perm'] = boolean + * A value of 1 will grant the permission for this user and term. +**/ +function taxonomy_access_defaults_update($vid, $rid = null, $grants = null) { + if (!isset($vid) OR !is_numeric($rid)) { + return FALSE; + } + + $ta_sql = "INSERT INTO {term_access_defaults} (vid"; + $ta_sql_values = " VALUES ($vid"; + if (isset($rid)) { + $ta_sql .= ",rid"; + $ta_sql_values .= ",$rid"; + } + $sql = ""; + if (isset($grants)) { + foreach ($grants as $perm => $value) { + $sql .= ",grant_$perm"; + $ta_sql_values .= ",$value"; + } + $sql .= ")"; + $ta_sql_values .= ")"; + } + else { + $sql .= ")"; + $ta_sql_values .= ")"; + } + $ta_sql .= $sql . $ta_sql_values; + + // issue #167977 - klance + $affected_nodes = _taxonomy_access_get_nodes_for_vocabulary($vid, $rid); + db_query("DELETE FROM {term_access_defaults} WHERE vid=%d AND rid=%d", $vid, $rid); + db_query($ta_sql); // insert into term_access_defaults + // issue #167977 - klance + _taxonomy_access_node_access_update($affected_nodes); +} + +/** + * Gets permissions for a given role + * @param $rid + * The role id to retrieve the permissions for. + * @return + * A two dimensional hash of the form $grants[tid][grant] where + * tid is the term id and + * grant is the permission (i.e. 'view','delete',ect.) + * this entry in the hash is true if permission is granted, false otherwise +**/ +function taxonomy_access_get_grants($rid) { + if (!isset($rid)) { + return false; + } + if (isset($rid) && !is_numeric($rid)) { + $rid = db_result(db_query("SELECT rid FROM {role} WHERE name='%s'", $rid)); + } + $result = db_query("SELECT * FROM {term_access} WHERE rid=%d", $rid); + $grants = array(); + while ($grant = db_fetch_array($result)) { + $tid = $grant['tid']; + foreach ($grant as $key => $grant_val) { + if (strpos($key, 'grant_') !== FALSE) { + $grant_name = ''; + $grant_name = str_replace('grant_', '', $key); + if (!isset($grants[$tid][$grant_name]) || !($grants[$tid][$grant_name])) { + // If there's conflicting DB rules, take the most lenient + $grants[$tid][$grant_name] = $grant_val; + } + } + } + } + return $grants; +} +/** + * Gets default permissions for a given role + * @param $rid + * The role id to retrieve the permissions for. + * @return + * A two dimensional hash of the form $grants[vid][grant] where + * vid is the vocab id and + * grant is the permission (i.e. 'view','delete',ect.) + * this entry in the hash is true if permission is granted, false otherwise +**/ +function taxonomy_access_get_default_grants($rid) { + if (!is_numeric($rid)) { + return false; + } + $result = db_query("SELECT * FROM {term_access_defaults} WHERE rid=%d", $rid); + $grants = array(); + while ($grant = db_fetch_array($result)) { + $vid = $grant['vid']; + foreach ($grant as $key => $grant_val) { + if (strpos($key, 'grant_') !== FALSE) { + $grant_name = ''; + $grant_name = str_replace('grant_', '', $key); + if (!isset($grants[$vid][$grant_name]) || !($grants[$vid][$grant_name])) { + // If there's conflicting DB rules, take the most lenient + $grants[$vid][$grant_name] = $grant_val; + } + } + } + } + return $grants; +} + +/* + * Issue #167977 - klance + * Gets node ids associated with a given term + * @param $tid + * The term id for which to retrieve associated nodes + * @return $nid + * An array of node ids associated with the given term + */ +function _taxonomy_access_get_nodes_for_term($tid) { + $nid = array(); + $result = db_query("SELECT nid FROM {term_node} WHERE tid = '$tid'"); + + while($node = db_fetch_object($result)) { + $nid[] = $node->nid; + } + return $nid; +} + +/* + * Issue #167977 - klance + * Gets node ids associated with a given vocabulary + * @param $vid + * The vocabulary id for which to retrieve associated term ids + * @params $rid + * The role id for which to retrieve associated term ids + * @return $nid + * An array of node ids associated with the given term + */ +function _taxonomy_access_get_nodes_for_vocabulary($vid, $rid = NULL) { + $nid = array(); + $query = "SELECT n.nid FROM {term_node} n + LEFT JOIN {term_data} d ON n.tid = d.tid + LEFT JOIN {term_access} a ON n.tid = a.tid + WHERE d.vid = '$vid'"; + if(!is_null($rid)) { + $query .= " AND a.rid = '$rid'"; + } + $result = db_query($query); + + while($node = db_fetch_object($result)) { + $nid[] = $node->nid; + } + return $nid; +} + +/* + * Issue #167977 - klance + * Gets node ids associated with the given role + * @param $rid + * The role id for which to retrieve term ids that are + * access-controlled for this role + * @return $nid + * An array of node ids associated with the given term + */ +function _taxonomy_access_get_nodes_for_role($rid) { + $nid = array(); + $result = db_query(" + SELECT n.nid FROM {term_node} n LEFT JOIN {term_access} a ON n.tid = a.tid WHERE a.rid = '$rid' + "); + + while($node = db_fetch_object($result)) { + $nid[] = $node->nid; + } + return $nid; +} + +/* + * Issue #167977 + * Gets node ids associated with the given term + * @return $nid + * An array of node ids for which to acquire access permissions + */ +function _taxonomy_access_node_access_update($nid) { + foreach($nid as $node) { + $loaded_node = node_load($node, NULL, TRUE); + if (!empty($loaded_node)) { + node_access_acquire_grants($loaded_node); + } + } + return TRUE; +} diff -urp taxonomy_access/taxonomy_access_admin.inc taxonomy_access_new/taxonomy_access_admin.inc --- taxonomy_access/taxonomy_access_admin.inc 2008-07-29 12:47:58.000000000 -0500 +++ taxonomy_access_new/taxonomy_access_admin.inc 2010-02-20 21:24:16.000000000 -0600 @@ -337,239 +337,3 @@ function taxonomy_access_admin_form_subm drupal_goto('admin/user/taxonomy_access'); } } - -/** - * Updates permissions for a role for a term - * @param $tid - * The term to add the permission for. - * @param $rid - * The role id to add the permission for. - * @param $grants - * A hash of the grants in the form of $grants['perm'] = boolean - * A value of 1 will grant the permission for this user and term. -**/ -function taxonomy_access_grant_update($tid, $rid = null, $grants = null) { - if (!isset($tid) OR !is_numeric($rid)) { - return FALSE; - } - - $ta_sql = "INSERT INTO {term_access} (tid"; - $ta_sql_values = " VALUES ($tid"; - if (isset($rid)) { - $ta_sql .= ",rid"; - $ta_sql_values .= ",$rid"; - } - $sql = ""; - if (isset($grants)) { - foreach ($grants as $perm => $value) { - $sql .= ",grant_$perm"; - $ta_sql_values .= is_array($value) ? ",". $value[0] : ",$value"; - } - $sql .= ")"; - $ta_sql_values .= ")"; - } - else { - $sql .= ")"; - $ta_sql_values .= ")"; - } - $ta_sql .= $sql . $ta_sql_values; - - // issue #167977 - klance - $affected_nodes = _taxonomy_access_get_nodes_for_term($tid); - db_query("DELETE FROM {term_access} WHERE tid=%d AND rid=%d", $tid, ($rid)); - db_query($ta_sql); // insert into term_access - // issue #167977 - klance - _taxonomy_access_node_access_update($affected_nodes); -} - -/** - * Updates default permissions for a role for a vocabulary - * @param $vid - * The vocab to add the permission for. - * @param $rid - * The role id to add the permission to. - * @param $grants - * A hash of the grants in the form of $grants['perm'] = boolean - * A value of 1 will grant the permission for this user and term. -**/ -function taxonomy_access_defaults_update($vid, $rid = null, $grants = null) { - if (!isset($vid) OR !is_numeric($rid)) { - return FALSE; - } - - $ta_sql = "INSERT INTO {term_access_defaults} (vid"; - $ta_sql_values = " VALUES ($vid"; - if (isset($rid)) { - $ta_sql .= ",rid"; - $ta_sql_values .= ",$rid"; - } - $sql = ""; - if (isset($grants)) { - foreach ($grants as $perm => $value) { - $sql .= ",grant_$perm"; - $ta_sql_values .= ",$value"; - } - $sql .= ")"; - $ta_sql_values .= ")"; - } - else { - $sql .= ")"; - $ta_sql_values .= ")"; - } - $ta_sql .= $sql . $ta_sql_values; - - // issue #167977 - klance - $affected_nodes = _taxonomy_access_get_nodes_for_vocabulary($vid, $rid); - db_query("DELETE FROM {term_access_defaults} WHERE vid=%d AND rid=%d", $vid, $rid); - db_query($ta_sql); // insert into term_access_defaults - // issue #167977 - klance - _taxonomy_access_node_access_update($affected_nodes); -} - -/** - * Gets permissions for a given role - * @param $rid - * The role id to retrieve the permissions for. - * @return - * A two dimensional hash of the form $grants[tid][grant] where - * tid is the term id and - * grant is the permission (i.e. 'view','delete',ect.) - * this entry in the hash is true if permission is granted, false otherwise -**/ -function taxonomy_access_get_grants($rid) { - if (!isset($rid)) { - return false; - } - if (isset($rid) && !is_numeric($rid)) { - $rid = db_result(db_query("SELECT rid FROM {role} WHERE name='%s'", $rid)); - } - $result = db_query("SELECT * FROM {term_access} WHERE rid=%d", $rid); - $grants = array(); - while ($grant = db_fetch_array($result)) { - $tid = $grant['tid']; - foreach ($grant as $key => $grant_val) { - if (strpos($key, 'grant_') !== FALSE) { - $grant_name = ''; - $grant_name = str_replace('grant_', '', $key); - if (!isset($grants[$tid][$grant_name]) || !($grants[$tid][$grant_name])) { - // If there's conflicting DB rules, take the most lenient - $grants[$tid][$grant_name] = $grant_val; - } - } - } - } - return $grants; -} -/** - * Gets default permissions for a given role - * @param $rid - * The role id to retrieve the permissions for. - * @return - * A two dimensional hash of the form $grants[vid][grant] where - * vid is the vocab id and - * grant is the permission (i.e. 'view','delete',ect.) - * this entry in the hash is true if permission is granted, false otherwise -**/ -function taxonomy_access_get_default_grants($rid) { - if (!is_numeric($rid)) { - return false; - } - $result = db_query("SELECT * FROM {term_access_defaults} WHERE rid=%d", $rid); - $grants = array(); - while ($grant = db_fetch_array($result)) { - $vid = $grant['vid']; - foreach ($grant as $key => $grant_val) { - if (strpos($key, 'grant_') !== FALSE) { - $grant_name = ''; - $grant_name = str_replace('grant_', '', $key); - if (!isset($grants[$vid][$grant_name]) || !($grants[$vid][$grant_name])) { - // If there's conflicting DB rules, take the most lenient - $grants[$vid][$grant_name] = $grant_val; - } - } - } - } - return $grants; -} - -/* - * Issue #167977 - klance - * Gets node ids associated with a given term - * @param $tid - * The term id for which to retrieve associated nodes - * @return $nid - * An array of node ids associated with the given term - */ -function _taxonomy_access_get_nodes_for_term($tid) { - $nid = array(); - $result = db_query("SELECT nid FROM {term_node} WHERE tid = '$tid'"); - - while($node = db_fetch_object($result)) { - $nid[] = $node->nid; - } - return $nid; -} - -/* - * Issue #167977 - klance - * Gets node ids associated with a given vocabulary - * @param $vid - * The vocabulary id for which to retrieve associated term ids - * @params $rid - * The role id for which to retrieve associated term ids - * @return $nid - * An array of node ids associated with the given term - */ -function _taxonomy_access_get_nodes_for_vocabulary($vid, $rid = NULL) { - $nid = array(); - $query = "SELECT n.nid FROM {term_node} n - LEFT JOIN {term_data} d ON n.tid = d.tid - LEFT JOIN {term_access} a ON n.tid = a.tid - WHERE d.vid = '$vid'"; - if(!is_null($rid)) { - $query .= " AND a.rid = '$rid'"; - } - $result = db_query($query); - - while($node = db_fetch_object($result)) { - $nid[] = $node->nid; - } - return $nid; -} - -/* - * Issue #167977 - klance - * Gets node ids associated with the given role - * @param $rid - * The role id for which to retrieve term ids that are - * access-controlled for this role - * @return $nid - * An array of node ids associated with the given term - */ -function _taxonomy_access_get_nodes_for_role($rid) { - $nid = array(); - $result = db_query(" - SELECT n.nid FROM {term_node} n LEFT JOIN {term_access} a ON n.tid = a.tid WHERE a.rid = '$rid' - "); - - while($node = db_fetch_object($result)) { - $nid[] = $node->nid; - } - return $nid; -} - -/* - * Issue #167977 - * Gets node ids associated with the given term - * @return $nid - * An array of node ids for which to acquire access permissions - */ -function _taxonomy_access_node_access_update($nid) { - foreach($nid as $node) { - $loaded_node = node_load($node, NULL, TRUE); - if (!empty($loaded_node)) { - node_access_acquire_grants($loaded_node); - } - } - return TRUE; -}