Index: modules/simple_access/simple_access.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/simple_access/simple_access.module,v retrieving revision 1.23 diff -u -r1.23 simple_access.module --- modules/simple_access/simple_access.module 5 Feb 2007 13:05:27 -0000 1.23 +++ modules/simple_access/simple_access.module 3 Mar 2007 01:13:12 -0000 @@ -4,7 +4,7 @@ /** * @file - * This module allows node administrators to make nodes viewable by specific + * This module allows administrators to make nodes viewable by specific * 'access groups'. Each access group can contain any number of roles. * If a node is not assigned to any access groups, it will remain viewable by * all users. @@ -48,7 +48,7 @@ ); $items[] = array( 'path' => 'admin/user/simple_access/add', - 'title' => t('Add Group'), + 'title' => t('Add Group'), 'callback' => 'drupal_get_form', 'callback arguments' => array('simple_access_group_form'), 'access' => $access, @@ -65,7 +65,7 @@ 'weight' => -6 ); $items[] = array( - 'path' => 'admin/settings/settings', + 'path' => 'admin/settings/simple_access', 'title' => t('Simple Access'), 'callback' => 'drupal_get_form', 'callback arguments' => array('simple_access_settings_page'), @@ -78,7 +78,7 @@ 'path' => 'admin/content/simple_access', 'title' => t('Simple Access'), 'access' => user_access('administer nodes'), - 'callback' => 'simple_access_nodes', + 'callback' => 'simple_access_nodes', 'type' => MENU_NORMAL_ITEM, 'description' => t('View node access which has been set up via Simple Access'), ); @@ -86,40 +86,29 @@ 'path' => 'admin/content/simple_access/view', 'title' => t('View'), 'callback' => 'simple_access_nodes', - 'access' => user_access('administer nodes'), + 'access' => user_access('administer nodes'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight'=>-5 ); $items[] = array( - 'path' => 'admin/content/simple_access/edit', - 'title' => t('Edit Access'), - 'callback' => 'simple_access_nodes', - 'access' => user_access('administer nodes'), - 'type' => MENU_CALLBACK, + 'path' => 'admin/content/simple_access/edit', + 'title' => t('Edit Access'), + 'callback' => 'simple_access_nodes', + 'access' => user_access('administer nodes'), + 'type' => MENU_CALLBACK, 'weight' => -4 ); $items[] = array( - 'path' => 'admin/content/simple_access/delete', - 'title' => t('Delete Access'), - 'callback' => 'simple_access_nodes', - 'access' => user_access('administer nodes'), - 'type' => MENU_CALLBACK, + 'path' => 'admin/content/simple_access/delete', + 'title' => t('Delete Access'), + 'callback' => 'simple_access_nodes', + 'access' => user_access('administer nodes'), + 'type' => MENU_CALLBACK, 'weight' => -3 ); } - /* else { - $items[] = array( - 'path' => 'admin/user/simple_access/setup', - 'title' => variable_get('simple_access_active', FALSE) ? t('deactivate') : t('setup'), - 'callback' => 'simple_access_setup_page', - 'access' => $access, - 'type' => MENU_LOCAL_TASK, - 'weight' => 8 - ); - } */ return $items; } - /** * Implementation of hook_perm(). * @@ -129,52 +118,23 @@ } /** - * Implementation of hook_node_grants(). - * - * + * Implementation of hook_node_access_records */ -function simple_access_node_grants($account, $op) { - $grants = array(); - switch ($op) { - case 'view': - if (user_access('access content', $account)) { - $grants[] = 0; - } - case 'update' : - case 'delete' : - if (isset($account->roles)) { - $grants = array_merge( $grants, simple_access_groups_from_roles( array_keys($account->roles) )); - } - return array('simple_access' => $grants); - } -} +function simple_access_node_access_records($node) +{ + $records = array(); -/** - * Implementation of hook_nodeapi(). - * - */ -function simple_access_nodeapi(&$node, $op, $arg = 0) { - switch ($op) { - case 'delete': - // When a node is deleted, delete any relevant grants. - db_query('DELETE FROM {node_access} WHERE nid = %d AND realm = \'simple_access\'', $node->nid); - break; - case 'insert': - case 'update': - // if user doesn't have administer nodes permission, then the simple_access - // boxes weren't on the page... so don't parse what ain't there! (see 'else') - if (user_access('assign access to nodes')) { - // Clear out any existing grants for the node. Don't worry, we'll set them again. - db_query('DELETE FROM {node_access} WHERE nid = %d AND realm = \'simple_access\'', $node->nid); - $views = FALSE; + if ($node->simple_access) + { // loop through simple_access arrays from page submission // $type is either 'view', 'update', or 'delete' + foreach((array)$node->simple_access as $type => $array) { // loop through each checkbox of the group foreach((array)$array['checks'] as $gid => $checked) { // make an array of all grants for node if($checked){ - $grants[$gid][$type] = 1; + $sa_grants[$gid][$type] = true; if ($type == 'view') { // if we've gotten here, then one of // the view boxes is checked so... @@ -183,32 +143,61 @@ } } } - if (!$views) { - // if no 'view' access groups are selected, - // set public view grant for node - $grants[0]['view'] = 1; - // there may be modules that let the public edit and delete nodes, but this isn't one of 'em. So.... - $grants[0]['update'] = 0; - $grants[0]['delete'] = 0; + // Because the UI is organized by access rather than GID, we need to + // reorganize the array by realm/GID + if ($sa_grants) + { + foreach($sa_grants as $gid => $access) + { + $records[] = array( + 'realm' => 'simple_access', + 'gid' => $gid, + 'grant_view' => $access['view'], + 'grant_update' => $access['update'], + 'grant_delete' => $access['delete'], + 'priority' => 1, + ); + } } - // this next line is wrong! Where is $gid coming from? - foreach($grants as $gid => $gidgrants) { - db_query("INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, 'simple_access', %d, %d, %d)", $node->nid, $gid, $gidgrants['view'], $gidgrants['update'], $gidgrants['delete']); - } - } - else { - // the user is not an administrator - // so does the node already have some access permissions? - if (!db_result(db_query("SELECT nid FROM {node_access} WHERE nid = %d AND realm = 'simple_access'", $node->nid))) { - // if not set it up for everyone to view... - db_query("INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, 'simple_access', %d, %d, %d)", $node->nid, 0, 1, 0, 0); - } - } - break; - } + } + // if there are new view recrods set + if (!$views) + { + $records[] = array( + 'realm' => 'simple_access', + 'gid' => 0, + 'grant_view' => 1, + 'grant_update' => 0, + 'grant_update' => 0, + 'priority' => 1, + ); + } + $records[] = array( + 'realm' => 'simple_access_author', + 'gid' => $node->uid, + 'grant_view' => user_access('access content'), + 'grant_update' => user_access('access content'), + 'grant_delete' => user_access('access content'), + 'priority' => 0, + ); + return $records; +} + +/** + * Implementation of hook_node_grants(). + * + * @TODO implement to correcly return groups in all cases. + */ +function simple_access_node_grants($account, $op) { + + $gids = simple_access_groups_from_roles( array_keys($account->roles)); + $grants['simple_access'] = $gids; + $grants['simple_access_author'] = array($account->uid); + return $grants; } + function simple_access_form_alter($form_id, &$form){ // if this is a node form... if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) { @@ -222,7 +211,6 @@ if (user_access('assign access to nodes') || user_access('administer nodes')){ //include the css drupal_add_css(drupal_get_path('module', 'simple_access') .'/simple_access.css'); - // set up the outer fieldset $form['simple_access'] = array( '#title' => t('Access'), @@ -238,6 +226,7 @@ // Load the grants from the database. $result = db_query('SELECT na.gid, na.grant_view, na.grant_update, na.grant_delete FROM {node_access} na WHERE na.nid = %d AND na.realm = \'simple_access\'', $node->nid); while($grant = db_fetch_object($result)) { + if ($grant->gid > 0) { if ($grant->grant_view) { $views[$grant->gid] = $grant->gid; @@ -260,7 +249,7 @@ $count = count($variable) ? count($variable) : 1; $percent = 90/$count; - $options = simple_access_group_select(user_access('administer nodes')); + $options = simple_access_group_select(user_access('administer nodes')|| variable_get('sa_showgroups',0)); // this is the only stuff that changes for each // so we put it all in one place for easy editing @@ -281,9 +270,9 @@ 'default' => $deletes ) ); - foreach($loop as $key => $vals) { - if (in_array($key, $variable)) { + //changed this from in_array cause array was containing view->view and update=>0 even when key wasn't checked.- metzlerd + if ($variable[$key]) { $form['simple_access'][$key] = array( '#type' => 'fieldset', '#title' => $vals['title'], @@ -313,72 +302,6 @@ '#value' => '
' ); - /* - // EDIT (a.k.a update) stuff ************************ - if (in_array('update', $variable)) { - if ($options) { - //$form = form_checkboxes('', 'simple_access][update', $updates, simple_access_group_select()); - $form['simple_access']['update'] = array( - '#type' => 'checkboxes', - '#title' => '', - '#default_value' => $updates, - '#options' => simple_access_group_select(), - ); - } - else { - $form['simple_access']['update'] = array( - '#type' => 'markup', - '#value' => t('No access groups have been defined', array('!url', url('admin/user/simple_access'))) - ); - } - //$output .= form_group(t('Additionally editable by'), $form, ''.t('All unchecked = normal behavior'.t('Simple Access needs to configure the database before use. Please go to !link to set it up.', array('!link'=>l('admin/access/simple_access/setup', 'admin/access/simple_access/setup'))).'
' - ); - } */ return $form; } @@ -442,10 +365,6 @@ function simple_access_settings_page() { drupal_set_title(t('Simple Access Settings')); - if ($_POST['op'] == t('Save Options')) { - variable_set('sa_display', array_keys($_POST['edit']['sa_display'])); - drupal_set_message(t('Options saved')); - } $options = array( 'view' => t('View: Displays viewability selections at top of node form. Selected access groups will be the only users who can view the node. All unselected = normal node behavior (viewable by all).In order to activate simple_access, the database needs to be configured.
Click this button to get things going.
") + $form['sa_showgroups'] = array( + '#type' => 'checkbox', + '#title' => 'Show groups even when user is not a member.', + '#default_value' => variable_get('sa_showgroups',0), + '#description' => 'This is useful when you want to have a user be able to make content viewable by themselves and a higher privileged group (e.g. students sharing work with faculty)', ); - $output .= drupal_get_form('simple_access_activate', $form); - } - else { - drupal_set_title(t('Deactivate Simple Access')); - $form[] = array( - '#type' => 'submit', - '#value' => t('Deactivate'), - '#prefix' => t("Before deactivating simple_access.module, you'll need to click this button in order to remove the simple_access grants from the database. If you do not do this, all of your nodes will become invisible to anyone but administators when you deactivate the module.
Do this only when you want to deactivate the module.
") - ); - $output .= drupal_get_form('simple_access_deactivate', $form); - } - return $output; + return system_settings_form($form); } function simple_access_deactivate_confirm_submit($form_id, $form_values) { @@ -595,7 +490,8 @@ function simple_access_groups_from_roles($roles) { // there probably should be some 'static' stuff going on here - $gids = array(); + // always return gid 0 just to be safe. + $gids = array(0); $result = db_query("SELECT gid FROM {simple_access_roles} WHERE rid IN (%s)", implode(",", $roles)); while ($g = db_fetch_object($result)) { $gids[] = $g->gid; @@ -710,3 +606,16 @@ //print theme('page', $output); return $output; } + +/** + * Implementation of hook_enable + */ +function simple_access_enable() +{ + drupal_set_message('To fully activate simple_access you also need to'.l('rebuild permissions ','admin/content/node-settings')); +} + +function simple_access_disable() +{ + drupal_set_message('To fully disable simple_access you also need to'.l('rebuild permissions ','admin/content/node-settings')); +} \ No newline at end of file