public static function insert_role_assignments($role_name, $agent_type, $agents, $args = array()) { //$defaults = array( ); // auto_insertion arg set for role propagation from parent objects //$args = array_merge( $defaults, (array) $args ); //extract($args, EXTR_SKIP); if (!$agents) { return; } global $wpdb, $current_user; $assigner_id = $current_user->ID; $insert_data = compact('role_name', 'agent_type', 'assigner_id'); // Before inserting a role, delete any overlooked old assignment. foreach (array_keys($agents) as $agent_id) { if (!$agent_id) { continue; } if ($ass_ids = $wpdb->get_col($wpdb->prepare("SELECT assignment_id FROM {$wpdb->ppc_roles} WHERE role_name = %s AND agent_id = %d", $role_name, $agent_id))) { PP_RoleAssigner::remove_roles_by_id($ass_ids); } // insert role for specified object and group(s) $insert_data['agent_id'] = $agent_id; $wpdb->insert($wpdb->ppc_roles, $insert_data); $assignment_id = $wpdb->insert_id; do_action('pp_assigned_sitewide_role', $assignment_id, compact('role_name', 'agent_type', 'agent_id', 'assigner_id')); } }
function _pp_item_deletion_aftermath($item_source, $item_id) { global $wpdb; require_once dirname(__FILE__) . '/role_assigner_pp.php'; // delete role assignments for deleted term if ($eitem_ids = $wpdb->get_col("SELECT eitem_id FROM {$wpdb->ppc_exception_items} AS i INNER JOIN {$wpdb->ppc_exceptions} AS e ON e.exception_id = i.exception_id WHERE e.via_item_source = '{$item_source}' AND i.item_id = '{$item_id}'")) { PP_RoleAssigner::remove_exception_items_by_id($eitem_ids); // Propagated roles will be converted to direct-assigned roles if the original progenetor goes away. Removal of a "link" in the parent/child propagation chain has no effect. $id_in = "'" . implode("', '", $eitem_ids) . "'"; $wpdb->query("UPDATE {$wpdb->ppc_exception_items} SET inherited_from = '0' WHERE inherited_from IN ({$id_in})"); } }
/** * Assign exceptions for a user or group * @param array agents : agents['item'|'children'][agent_id] = true|false * @param string agent_type * @param array args : * - operation ('read'|'edit'|'associate'|'assign'...) * - mod_type ('additional'|'exclude'|'include') * - for_item_source ('post' or 'term' - data source to which the role applies) * - for_item_type (post_type or taxonomy to which the role applies) * - for_item_status (status which the role applies to; default '' means all stati) * - via_item_source ('post' or 'term' - data source which the role is tied to) * - item_id (post ID or term_taxonomy_id) * - via_item_type (post_type or taxonomy of item which the role is tied to; default '' means unspecified when via_item_source is 'post') * - remove_assignments (default false) */ function ppc_assign_exceptions($agents, $agent_type = 'pp_group', $args = array()) { require_once dirname(__FILE__) . '/admin/role_assigner_pp.php'; return PP_RoleAssigner::assign_exceptions($agents, $agent_type, $args); }
$deleted_eitem_ids = array(); $input_vals = explode('|', pp_sanitize_csv($_GET['pp_eitem_ids'])); foreach ($input_vals as $id_csv) { $eitem_ids = _pp_editable_eitem_ids(explode(',', $id_csv)); $deleted_eitem_ids = array_merge($deleted_eitem_ids, $eitem_ids); // possible TODO: remove elem from $input_vals if content-specific assign_roles authentication fails } if ($deleted_eitem_ids) { require_once dirname(__FILE__) . '/role_assigner_pp.php'; $exc_clause = $agent_clause ? "exception_id IN ( SELECT exception_id FROM {$wpdb->ppc_exceptions} WHERE {$agent_clause} 1=1 ) AND" : ''; $results = $wpdb->get_results("SELECT exception_id, item_id FROM {$wpdb->ppc_exception_items} WHERE {$exc_clause} eitem_id IN ('" . implode("','", $deleted_eitem_ids) . "')"); // safeguard against accidental deletion of a different agent's exceptions foreach ($results as $row) { // also delete any redundant item exceptions for this agent if ($_eitem_ids = $wpdb->get_col("SELECT eitem_id FROM {$wpdb->ppc_exception_items} WHERE exception_id='{$row->exception_id}' AND item_id='{$row->item_id}'")) { PP_RoleAssigner::remove_exception_items_by_id($_eitem_ids); } } } echo '<!--ppResponse-->' . implode('|', $input_vals) . '<--ppResponse-->'; break; case 'exceptions_propagate': case 'exceptions_unpropagate': case 'exceptions_children_only': if (empty($_GET['pp_eitem_ids'])) { exit; } if (!current_user_can('pp_assign_roles')) { exit; } $edited_input_ids = array();
function _pp_inherit_parent_exceptions($via_item_source, $item_id, $parent_id, $args = array()) { require_once dirname(__FILE__) . '/role_assigner_pp.php'; $defaults = array('parent_exceptions' => array(), 'retain_exceptions' => array(), 'force_for_item_type' => ''); $args = array_merge($defaults, $args); extract($args, EXTR_SKIP); if (!$parent_exceptions) { $parent_exceptions = _pp_get_parent_exceptions($via_item_source, $item_id, $parent_id); } foreach ($retain_exceptions as $r) { // can't just compare exception_id because want to avoid inheriting an include exception if an exclude is manually stored, etc. foreach ($parent_exceptions as $ekey => $e) { if ($r->item_id == $item_id && $r->via_item_source == $e->via_item_source && $r->agent_type == $e->agent_type && $r->agent_id == $e->agent_id && $r->for_item_source == $e->for_item_source && $r->for_item_type == $e->for_item_type && $r->operation == $e->operation && $r->for_item_status == $e->for_item_status) { unset($parent_exceptions[$ekey]); } } } if ($parent_exceptions) { if ('post' == $via_item_source) { $item_type = get_post_field('post_type', $item_id); } foreach ($parent_exceptions as $exc) { $insert_agents = array($exc->agent_id => true); $args = array('for_item_status' => $exc->for_item_status, 'inherited_from' => array($exc->agent_id => $exc->eitem_id)); //$for_item_type = ( $force_for_item_type ) ? $force_for_item_type : $exc->for_item_type; if ($force_for_item_type) { $for_item_type = $force_for_item_type; } elseif ('post' != $via_item_source) { $for_item_type = $exc->for_item_type; } else { $for_item_type = 'revision' == $item_type ? $exc->for_item_type : $item_type; } foreach (array('item', 'children') as $assign_for) { $args['assign_for'] = $assign_for; if (PP_RoleAssigner::insert_exceptions($exc->mod_type, $exc->operation, $exc->via_item_source, $exc->via_item_type, $exc->for_item_source, $for_item_type, $item_id, $exc->agent_type, $insert_agents, $args)) { $any_inserts = true; } } } } return !empty($any_inserts); }