View source
<?php
function pmpapi_permissions_permission() {
return array(
'apply PMP permissions' => array(
'title' => t('Apply PMP API permissions'),
'description' => t('Apply PMP API permissions to pushed entities.'),
),
);
}
function pmpapi_permissions_get_group_options() {
$options = array();
$options[0] = t('All PMP users');
$user_id = variable_get('pmpapi_user_id');
$options[$user_id] = t('Only this user (private)');
foreach (pmpapi_fetch_groups() as $group) {
$options[$group->attributes->guid] = $group->attributes->title;
}
return $options;
}
function pmpapi_permissions_form_node_type_form_alter(&$form, &$form_state) {
$entity_type = 'node';
$bundle_name = $form['#node_type']->type;
$uname = $entity_type . '__' . $bundle_name;
if (variable_get('pmpapi_push_' . $uname . '_profile')) {
$form['pmpapi_permissions'] = array(
'#type' => 'fieldset',
'#title' => t('PMP API permission settings'),
'#collapsible' => TRUE,
'#group' => 'additional_settings',
);
$form['pmpapi_permissions']['pmpapi_permissions_default_group_node_'] = array(
'#title' => t('Which PMP users can access these nodes (via the PMP API)?'),
'#type' => 'select',
'#multiple' => FALSE,
'#default_value' => variable_get('pmpapi_permissions_default_group_' . $uname, 0),
'#required' => TRUE,
'#options' => pmpapi_permissions_get_group_options(),
'#disabled' => !user_access('administer content types'),
);
}
}
function pmpapi_permissions_form_file_entity_file_type_form_alter(&$form, &$form_state, $type) {
$entity_type = 'file';
$bundle_name = $form['#file_type']->type;
$uname = $entity_type . '__' . $bundle_name;
if (variable_get('pmpapi_push_' . $uname . '_profile')) {
$form['pmpapi_permissions'] = array(
'#type' => 'fieldset',
'#title' => t('PMP API permission settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['pmpapi_permissions']['pmpapi_permissions_default_group'] = array(
'#title' => t('Which PMP users can access these files (via the PMP API)?'),
'#type' => 'select',
'#multiple' => FALSE,
'#default_value' => variable_get('pmpapi_permissions_default_group_' . $uname, 0),
'#required' => TRUE,
'#options' => pmpapi_permissions_get_group_options(),
'#disabled' => !user_access('administer content types'),
);
$form['#submit'][] = 'pmpapi_permissions_set_file_entity_defaults';
}
}
function pmpapi_permissions_set_file_entity_defaults($form, &$form_state) {
$entity_type = 'file';
$bundle_name = $form['#file_type']->type;
$uname = $entity_type . '__' . $bundle_name;
if (variable_get('pmpapi_push_' . $uname . '_profile')) {
$group = $form_state['values']['pmpapi_permissions_default_group'];
variable_set('pmpapi_permissions_default_group_' . $uname, $group);
}
}
function pmpapi_permissions_form_node_form_alter(&$form, &$form_state) {
$entity_type = 'node';
$bundle_name = $form['#node']->type;
$uname = $entity_type . '__' . $bundle_name;
if (!empty($form['nid']['#value'])) {
$entity = node_load($form['nid']['#value']);
$add_perms = pmpapi_push_entity_ok_to_push($entity, 'node');
}
else {
$add_perms = variable_get('pmpapi_push_' . $uname . '_profile');
}
if ($add_perms) {
pmpapi_permissions_attach_perm_elements($form, 'node', $bundle_name);
}
}
function pmpapi_permissions_form_file_entity_add_upload_alter(&$form, &$form_state) {
if (isset($form['#step']) && $form['#step'] == 4) {
$bundle_name = $form['#entity']->type;
pmpapi_permissions_attach_perm_elements($form, 'file', $bundle_name);
}
}
function pmpapi_permissions_form_file_entity_edit_alter(&$form, &$form_state) {
$bundle_name = $form['#bundle'];
pmpapi_permissions_attach_perm_elements($form, 'file', $bundle_name);
}
function pmpapi_permissions_attach_perm_elements(&$form, $entity_type, $bundle_name) {
$uname = $entity_type . '__' . $bundle_name;
if (variable_get('pmpapi_push_' . $uname . '_profile')) {
$form['pmpapi_permissions'] = array(
'#type' => 'fieldset',
'#title' => t('PMP API permission settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
if (!empty($form['additional_settings'])) {
$form['pmpapi_permissions']['#group'] = 'additional_settings';
}
$guid = pmpapi_permissions_extract_guid_from_form($form, $entity_type);
$permissions = $guid ? pmpapi_permissions_fetch_permissions($guid) : array();
$permission = !empty($permissions) ? $permissions[0] : NULL;
if ($guid) {
if ($permission) {
$default_group_value = end(explode('/', $permission->href));
}
else {
$default_group_value = 0;
}
}
else {
$default_group_value = variable_get('pmpapi_permissions_default_group_' . $uname, 0);
}
$form['pmpapi_permissions']['pmpapi_permissions_group'] = array(
'#title' => t('Which PMP users can access these files (via the PMP API)?'),
'#type' => 'select',
'#multiple' => FALSE,
'#default_value' => $default_group_value,
'#required' => TRUE,
'#options' => pmpapi_permissions_get_group_options(),
'#disabled' => !user_access('apply PMP permissions'),
);
if (isset($form['actions']['submit']['#submit'])) {
array_unshift($form['actions']['submit']['#submit'], 'pmpapi_permissions_set_' . $entity_type . '_permissions');
}
elseif (isset($form['#submit'])) {
array_unshift($form['#submit'], 'pmpapi_permissions_set_' . $entity_type . '_permissions');
}
}
}
function pmpapi_permissions_set_node_permissions($form, &$form_state) {
$entity_type = 'node';
$group = !empty($form_state['values']['pmpapi_permissions_group']) ? $form_state['values']['pmpapi_permissions_group'] : NULL;
$permission = pmp_permissions_create_permission($group);
if ($permission !== NULL) {
$form_state[$entity_type]->pmpapi_permissions = array(
$permission,
);
}
else {
unset($form_state[$entity_type]->pmpapi_permissions);
}
$form_state[$entity_type]->pmpapi_permissions_added = TRUE;
}
function pmpapi_permissions_set_file_permissions($form, &$form_state) {
$entity_type = 'file';
$group = !empty($form_state['values']['pmpapi_permissions_group']) ? $form_state['values']['pmpapi_permissions_group'] : NULL;
$permission = pmp_permissions_create_permission($group);
$permissions = $permission !== NULL ? array(
$permission,
) : NULL;
if (isset($form_state['build_info']['form_id']) && $form_state['build_info']['form_id'] == 'file_entity_add_upload' && $form_state['step'] == 4) {
$form_state['values']['pmpapi_permissions'] = $permissions;
$form_state['values']['pmpapi_permissions_added'] = TRUE;
}
else {
$form_state[$entity_type]->pmpapi_permissions = $permissions;
$form_state[$entity_type]->pmpapi_permissions_added = TRUE;
}
}
function pmp_permissions_create_permission($group, $blacklist = FALSE, $operation = 'read') {
$permission = NULL;
if ($group !== NULL) {
$permission = new stdClass();
$permission->href = variable_get('pmpapi_base_url') . '/docs/' . $group;
$permission->operation = $operation;
if ($blacklist) {
$permission->blacklist = TRUE;
}
}
return $permission;
}
function pmpapi_permissions_extract_guid_from_form($form, $entity_type) {
$indexes = array(
'node' => '#node',
'file' => '#entity',
);
$index = $indexes[$entity_type];
if (!empty($form[$index]->pmpapi_guid)) {
return $form[$index]->pmpapi_guid;
}
}
function pmpapi_permissions_fetch_permissions($guid) {
$permissions = db_query('SELECT permissions FROM {pmpapi_local_docs} WHERE guid=:guid', array(
':guid' => $guid,
))
->fetchField();
if (!empty($permissions)) {
return unserialize($permissions);
}
}
function pmpapi_permissions_entity_presave($entity, $type) {
$wrapper = entity_metadata_wrapper($type, $entity);
$bundle = $wrapper
->getBundle();
$uname = $type . '__' . $bundle;
$default_group = variable_get('pmpapi_permissions_default_group_' . $uname, 0);
if (empty($entity->pmpapi_permissions_added) && $default_group) {
$permission = pmp_permissions_create_permission($default_group);
$entity->pmpapi_permissions = array(
$permission,
);
}
}