| 1 |
<?php |
<?php |
| 2 |
// $Id: field_permissions.module,v 1.1.2.2 2009/10/08 09:22:50 markuspetrux Exp $ |
// $Id: field_permissions.module,v 1.1.2.3 2009/11/06 00:55:45 markuspetrux Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* Main script for the Field Permissions module. |
* This is the main script for the Field Permissions module. It merely contains |
| 7 |
|
* the implementation of hooks invoked by Drupal core and CCK. |
| 8 |
|
* All common functions are externalized into several scripts that are included |
| 9 |
|
* on demand to save memory consumption during normal site operation. |
| 10 |
*/ |
*/ |
| 11 |
|
|
| 12 |
/** |
/** |
|
* Obtain the list of field permissions. |
|
|
*/ |
|
|
function field_permissions_list($field_label = '') { |
|
|
return array( |
|
|
'create' => t('Create @field (edit on node creation).', array('@field' => $field_label)), |
|
|
'edit' => t('Edit any @field, regardless of its node author.', array('@field' => $field_label)), |
|
|
'edit own' => t('Edit own @field on node created by the user.', array('@field' => $field_label)), |
|
|
'view' => t('View any @field, regardless of its node author.', array('@field' => $field_label)), |
|
|
'view own' => t('View own @field on node created by the user.', array('@field' => $field_label)), |
|
|
); |
|
|
} |
|
|
|
|
|
/** |
|
| 13 |
* Implementation of hook_perm(). |
* Implementation of hook_perm(). |
| 14 |
*/ |
*/ |
| 15 |
function field_permissions_perm() { |
function field_permissions_perm() { |
| 16 |
$perms = array(); |
module_load_include('inc', 'field_permissions', 'includes/admin'); |
| 17 |
foreach (content_fields() as $field_name => $field) { |
return _field_permissions_perm(); |
|
if (!empty($field['field_permissions'])) { |
|
|
foreach (array_keys(field_permissions_list()) as $permission_type) { |
|
|
if (!empty($field['field_permissions'][$permission_type])) { |
|
|
$perms[] = $permission_type .' '. $field_name; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return $perms; |
|
| 18 |
} |
} |
| 19 |
|
|
| 20 |
/** |
/** |
| 21 |
* Implementation of hook_field_settings_alter(). |
* Implementation of hook_field_settings_alter(). |
| 22 |
*/ |
*/ |
| 23 |
function field_permissions_field_settings_alter(&$settings, $op, $field) { |
function field_permissions_field_settings_alter(&$settings, $op, $field) { |
| 24 |
switch ($op) { |
if ($op == 'form' || $op == 'save') { |
| 25 |
case 'form': |
module_load_include('inc', 'field_permissions', 'includes/admin'); |
| 26 |
$field_permissions = array(); |
return _field_permissions_field_settings_alter($settings, $op, $field); |
|
foreach (field_permissions_list($field['widget']['label']) as $permission_type => $description) { |
|
|
$field_permissions[$permission_type] = $description; |
|
|
} |
|
|
$settings['field_permissions'] = array( |
|
|
'#title' => t('Field permissions'), |
|
|
'#type' => 'checkboxes', |
|
|
'#checkall' => TRUE, |
|
|
'#options' => $field_permissions, |
|
|
'#default_value' => (isset($field['field_permissions']) && is_array($field['field_permissions']) ? $field['field_permissions'] : array()), |
|
|
'#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 node 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/user/permissions'), |
|
|
)), |
|
|
'#weight' => -1, |
|
|
); |
|
|
break; |
|
|
|
|
|
case 'save': |
|
|
$settings[] = 'field_permissions'; |
|
|
break; |
|
| 27 |
} |
} |
| 28 |
} |
} |
| 29 |
|
|
| 33 |
* @see content_access() |
* @see content_access() |
| 34 |
*/ |
*/ |
| 35 |
function field_permissions_field_access($op, $field, $account, $node) { |
function field_permissions_field_access($op, $field, $account, $node) { |
| 36 |
// Check access only if permissions has been enabled for this field. |
// Ignore the request if permissions have not been enabled for this field. |
| 37 |
if (!empty($field['field_permissions']) && ($op == 'view' || $op == 'edit')) { |
$field_permissions = (isset($field['field_permissions']) ? array_filter($field['field_permissions']) : array()); |
| 38 |
// Check if user has access to edit this field on node creation. |
if (empty($field_permissions)) { |
| 39 |
if ($op == 'edit' && empty($node->nid)) { |
return; |
| 40 |
return user_access('create '. $field['field_name'], $account); |
} |
|
} |
|
| 41 |
|
|
| 42 |
// Check if user has access to view/edit this field in any node. |
if ($op == 'view') { |
| 43 |
if (user_access($op .' '. $field['field_name'], $account)) { |
if (!empty($field_permissions['view']) || !empty($field_permissions['view own'])) { |
| 44 |
return TRUE; |
module_load_include('inc', 'field_permissions', 'includes/field_access'); |
| 45 |
|
return _field_permissions_field_view_access($field['field_name'], $field_permissions, $account, $node); |
| 46 |
} |
} |
| 47 |
|
} |
| 48 |
// Check if user has access to view/edit this field in own node, |
elseif ($op == 'edit') { |
| 49 |
// but only if 'own' permissions have been enabled for this field. |
if (!empty($field_permissions['edit']) || !empty($field_permissions['edit own']) || !empty($field_permissions['create'])) { |
| 50 |
if ((!empty($field['field_permissions']['view own']) || !empty($field['field_permissions']['edit own'])) && user_access($op .' own '. $field['field_name'], $account)) { |
module_load_include('inc', 'field_permissions', 'includes/field_access'); |
| 51 |
|
return _field_permissions_field_edit_access($field['field_name'], $field_permissions, $account, $node); |
|
// When content_access('view') is invoked, it may or may not provide a |
|
|
// node object. It will, almost always, except when this function is |
|
|
// invoked as a field access callback from Views, where it is used to |
|
|
// evaluate if the field can be included in the query itself. In this |
|
|
// case we should grant access. Views will invoke content_access('view') |
|
|
// again, indirectly, when rendering the fields using content_format(), |
|
|
// and this time it will provide a pseudo node object that includes the |
|
|
// uid of the node author, so here is where we have the chance to |
|
|
// evaluate ownership to check for 'view own <field>' permission. |
|
|
if ($op == 'view') { |
|
|
return (!isset($node) || $node->uid == $account->uid); |
|
|
} |
|
|
|
|
|
// When content_access('edit') is invoked, it always provides a node, |
|
|
// so we can always check the ownership of the node. |
|
|
if ($op == 'edit') { |
|
|
return (isset($node) && $node->uid == $account->uid); |
|
|
} |
|
| 52 |
} |
} |
|
return FALSE; |
|
| 53 |
} |
} |
|
return TRUE; |
|
| 54 |
} |
} |