Skip to content

Commit

Permalink
Updated Field permissions module (requires database update).
Browse files Browse the repository at this point in the history
  • Loading branch information
mlncn committed Apr 5, 2011
1 parent dcf43f8 commit 8784ad6
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 108 deletions.
9 changes: 8 additions & 1 deletion drupal/sites/all/modules/field_permissions/CHANGELOG.txt
@@ -1,7 +1,14 @@
7.x-1.0-alpha1
==============
- #1063162 by jide, Rob Loach: Field Permissions not accessible for some fields.
- #965110 by Danic: Move Field Permissions UI to Reports.
- #965094 by Danic, Rob Loach: Group Title and Description in modules page.
- #1043522 by erikwebb: Permissions administration link wrong on Edit Field page.
- Small documentation changes.


7.x-1.x-dev
===========

- Added support for create field permission.
- Every permission type can be enabled independently.
- Sync code base from 6.x-1.0 release.
Expand Down
21 changes: 14 additions & 7 deletions drupal/sites/all/modules/field_permissions/README.txt
Expand Up @@ -56,13 +56,20 @@ REQUIREMENTS
INSTALLATION
============

- Be sure to install all dependent modules.
1) Be sure to install all dependent modules.

- Copy all contents of this package to your modules directory preserving
subdirectory structure.
2) Copy all contents of this package to your modules directory preserving
subdirectory structure.

- Go to Administer -> Site building -> Modules to install module.
3) Go to Administer -> Modules to install module.

4) Review the settings of your fields. You will find a new option labelled
"Field permissions" that allows you to enable permissions per field. It
is disabled by default.

5) Visit the Administer -> People -> Permissions page to enable the permission
for selected roles.

6) Get an overview of the Field Permissions at:
Administer -> Reports -> Field list -> Permissions

- Review the settings of your fields. You will find a new option labelled
"Field permissions" that allows you to enable permissions per field. It
is disabled by default.
@@ -1,15 +1,16 @@
name = Field Permissions
description = Set field-level permissions to edit or view CCK fields in any node, edit field during node creation, and edit or view permissions for nodes owned by the current user.
description = Set field-level permissions to create, update or view fields.
dependencies[] = field_ui
package = CCK
package = Fields
core = 7.x
files[] = field_permissions.module
files[] = includes/admin.inc
files[] = includes/field_access.inc
configure = admin/reports/fields/permissions

; Information added by drupal.org packaging script on 2011-02-25
; Information added by drupal.org packaging script on 2011-03-31
version = "7.x-1.x-dev"
core = "7.x"
project = "field_permissions"
datestamp = "1298619255"
datestamp = "1301530067"

@@ -0,0 +1,28 @@
<?php

/**
* @file
* Install, update and uninstall functions for the Field Permissions module.
*/

/**
* Implements hook_install().
*/
function field_permissions_install() {
// Set a larger weight for the module.
db_update('system')
->fields(array('weight' => 50))
->condition('name', 'field_permissions')
->execute();
}

/**
* Sets a larger weight for the module so that the Field Permissions become available.
*/
function field_permissions_update_7000(&$sandbox) {
db_update('system')
->fields(array('weight' => 50))
->condition('name', 'field_permissions')
->execute();
}

Expand Up @@ -9,11 +9,59 @@
*/

/**
* Implementation of hook_menu().
* Implements hook_help().
*/
function field_permissions_help($path, $arg) {
switch ($path) {
// Main module help for the Field Permissions module.
case 'admin/help#field_permissions':
return '<p>' . t('Set field-level permissions to edit or view CCK fields in any node, edit field during node creation, and edit or view permissions for nodes owned by the current user.') . '</p>';

// Help for the Field Permissions overview page.
case 'admin/reports/fields/permissions':
return '<p>' . t('Report and troubleshoot field permissions.') . '</p>';
}
}

/**
* Implements hook_menu().
*/
function field_permissions_menu() {
module_load_include('inc', 'field_permissions', 'includes/admin');
return _field_permissions_menu();
$items['admin/reports/fields/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/reports/fields/permissions'] = array(
'title' => 'Permissions',
'description' => 'Report and troubleshoot field permissions.',
'page callback' => 'field_permissions_overview',
'access arguments' => array('administer field permissions'),
'file' => 'includes/admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 0,
);

// @todo: what to do with non-node objects?
/*
$items['admin/structure/field_permissions/troubleshooting'] = array(
'title' => 'Troubleshooting',
'page callback' => 'drupal_get_form',
'page arguments' => array('field_permissions_troubleshooting_form'),
'access arguments' => array('administer field permissions'),
'file' => 'includes/admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
$items['field_permissions/autocomplete'] = array(
'title' => 'Field permissions autocomplete',
'page callback' => 'field_permissions_autocomplete',
'access arguments' => array('administer field permissions'),
'file' => 'includes/admin.inc',
'type' => MENU_CALLBACK,
);
//*/
return $items;
}

/**
Expand All @@ -34,13 +82,12 @@ function field_permissions_permission() {
}

/**
* Implementation of hook_form_alter().
* Implements of hook_form_FORM_ID_alter().
*/
function field_permissions_form_alter(&$form, $form_state, $form_id) {
if (in_array($form_id, array('field_ui_field_settings_form', 'field_ui_field_edit_form')) && isset($form['field']['settings'])) {
module_load_include('inc', 'field_permissions', 'includes/admin');
return _field_permissions_field_settings_form_alter($form, $form_state, $form_id);
}
function field_permissions_form_field_ui_field_edit_form_alter(&$form, $form_state, $form_id) {
// Injects the Field Permissions settings on the Edit field tab.
module_load_include('inc', 'field_permissions', 'includes/admin');
return _field_permissions_field_settings_form_alter($form, $form_state, $form_id);
}

/**
Expand Down
82 changes: 26 additions & 56 deletions drupal/sites/all/modules/field_permissions/includes/admin.inc
Expand Up @@ -5,46 +5,6 @@
* Administrative interface for the Field Permissions module.
*/

/**
* Implementation of hook_menu().
*/
function _field_permissions_menu() {
$items = array();
$items['admin/structure/field_permissions'] = array(
'title' => 'Field permissions',
'description' => 'Report and troubleshoot field permissions.',
'page callback' => 'field_permissions_overview',
'access arguments' => array('administer field permissions'),
'file' => 'includes/admin.inc',
);
$items['admin/structure/field_permissions/overview'] = array(
'title' => 'Overview',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);

// @todo: what to do with non-node objects?
/*
$items['admin/structure/field_permissions/troubleshooting'] = array(
'title' => 'Troubleshooting',
'page callback' => 'drupal_get_form',
'page arguments' => array('field_permissions_troubleshooting_form'),
'access arguments' => array('administer field permissions'),
'file' => 'includes/admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
$items['field_permissions/autocomplete'] = array(
'title' => 'Field permissions autocomplete',
'page callback' => 'field_permissions_autocomplete',
'access arguments' => array('administer field permissions'),
'file' => 'includes/admin.inc',
'type' => MENU_CALLBACK,
);
//*/
return $items;
}

/**
* Obtain the list of field permissions.
*/
Expand Down Expand Up @@ -107,16 +67,8 @@ function _field_permissions_permission() {
* Alter the field settings form.
*/
function _field_permissions_field_settings_form_alter(&$form, $form_state, $form_id) {
// Try to obtain the field name from the form itself.
if ($form_id == 'field_ui_field_settings_form') {
$field_name = $form['field']['field_name']['#value'];
}
elseif ($form_id == 'field_ui_field_edit_form') {
$field_name = $form['instance']['field_name']['#value'];
}
else {
return;
}
// Obtain the field name from the form itself.
$field_name = isset($form['instance']['field_name']['#value']) ? $form['instance']['field_name']['#value'] : '';

// Try to obtain information about this field.
$field = field_info_field($field_name);
Expand All @@ -138,7 +90,7 @@ function _field_permissions_field_settings_form_alter(&$form, $form_state, $form
'#description' => t('Use these options to enable role based permissions for this field.
When permissions are enabled, access to this field is denied by default and explicit permissions should be granted to the proper user roles from the <a href="@admin-permissions">permissions administration</a> page.
On the other hand, when these options are disabled, field permissions are inherited from the content view and/or edit permissions. In example, users allowed to view a particular node will also be able to view this field, and so on.', array(
'@admin-permissions' => url('admin/config/people/permissions'),
'@admin-permissions' => url('admin/people/permissions', array('fragment' => 'module-field_permissions')),
)),
'#weight' => -1,
);
Expand Down Expand Up @@ -172,26 +124,44 @@ function field_permissions_overview() {
foreach ($instances as $obj_type => $type_bundles) {
foreach ($type_bundles as $bundle => $bundle_instances) {
foreach ($bundle_instances as $field_name => $instance) {
// Each field will have a row in the table.
$field = field_info_field($field_name);
$admin_path = _field_ui_bundle_admin_path($obj_type, $bundle);
$rows[$field_name]['data'][0] = $field['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name;
$rows[$field_name]['data'][1] = t($field_types[$field['type']]['label']);
$rows[$field_name]['data'][2] = $obj_type;
$rows[$field_name]['data'][3][] = l($bundles[$obj_type][$bundle]['label'], $admin_path . '/fields/'. $field_name .'/field-settings', array('query' => $destination));
$rows[$field_name]['data'][3][] = l($bundles[$obj_type][$bundle]['label'], $admin_path . '/fields/'. $field_name, array(
'query' => $destination,
'fragment' => 'edit-field-settings-field-permissions',
));
$rows[$field_name]['class'] = $field['locked'] ? array('menu-disabled') : array('');

// Append field permissions information to the report.
$field_permissions = (isset($field['settings']['field_permissions']) && is_array($field['settings']['field_permissions']) ? array_filter($field['settings']['field_permissions']) : array());
foreach (array_keys(field_permissions_list()) as $index => $permission_type) {
// Put together the data value for the cell.
$data = '';
if (!empty($field_permissions[$permission_type])) {
$status = 'on';
$title = t('Enabled');
// Link the Enabled permission to the permissions page.
$data = l('', 'admin/people/permissions', array(
'attributes' => array(
'class' => array('field-permissions-status', 'field-permissions-status-on'),
'title' => t('Enabled'),
),
'fragment' => drupal_html_class("edit $permission_type $field_name"),
));
}
else {
$status = 'off';
// Simply display the status off text.
$title = t('Disabled');
$data = '<span class="field-permissions-status field-permissions-status-off" title="' . $title . '"></span>';
}
$rows[$field_name]['data'][4 + $index] = array('data' => '<span class="field-permissions-status field-permissions-status-'. $status .'" title="'. check_plain($title) .'"></span>', 'class' => 'field-permissions-cell');

// Construct the cell.
$rows[$field_name]['data'][4 + $index] = array(
'data' => $data,
'class' => array('field-permissions-cell'),
);
}
}
}
Expand Down
Expand Up @@ -3,46 +3,16 @@
/**
* Sort in menu hierarchy order.
*
* Given a field name of 'p' this produces an ORDER BY on p1, p2, ..., p9;
* optionally also injecting multiple joins to {menu_links} to sort by weight
* and title as well.
*
* Given a field name of 'p' this produces an ORDER BY on p1, p2, ..., p9.
* This is only really useful for the {menu_links} table.
*
* @ingroup views_sort_handlers
*/
class views_handler_sort_menu_hierarchy extends views_handler_sort {
function option_definition() {
$options = parent::option_definition();
$options['sort_within_level'] = array('default' => FALSE);
return $options;
}

function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['sort_within_level'] = array(
'#type' => 'checkbox',
'#title' => t('Sort within each hierarchy level'),
'#description' => t('Enable this to sort the items within each level of the hierarchy by weight and title. Warning: this may produce a slow query.'),
'#default_value' => $this->options['sort_within_level'],
);
}

function query() {
$this->ensure_my_table();
$max_depth = isset($this->definition['max depth']) ? $this->definition['max depth'] : MENU_MAX_DEPTH;
for ($i = 1; $i <= $max_depth; ++$i) {
if ($this->options['sort_within_level']) {
$join = new views_join();
$join->construct('menu_links', $this->table_alias, $this->field . $i, 'mlid');
$menu_links = $this->query->queue_table('menu_links', NULL, $join);
$this->query->add_orderby($menu_links, 'weight', $this->options['order']);
$this->query->add_orderby($menu_links, 'link_title', $this->options['order']);
}

// We need this even when also sorting by weight and title, to make sure
// that children of two parents with the same weight and title are
// correctly separated.
$this->query->add_orderby($this->table_alias, $this->field . $i, $this->options['order']);
}
}
Expand Down

0 comments on commit 8784ad6

Please sign in to comment.