View source
<?php
define('BLOCK_ACCESS_ADMIN', 'Administer block access settings');
define('BLOCK_ACCESS_CREATE', 'Create blocks');
define('BLOCK_ACCESS_ALL_VIEW', 'View all blocks');
define('BLOCK_ACCESS_ALL_MOVE', 'Move all blocks');
define('BLOCK_ACCESS_ALL_ENABLE', 'Enable all blocks');
define('BLOCK_ACCESS_ALL_DISABLE', 'Disable all blocks');
define('BLOCK_ACCESS_ALL_DELETE', 'Delete all blocks');
define('BLOCK_ACCESS_ALL_CONFIG', 'Configure all blocks.');
define('BLOCK_ACCESS_ALL_CONFIG_LANG', 'Configure all language settings');
define('BLOCK_ACCESS_ALL_CONFIG_SPEC', 'Configure all specific settings');
define('BLOCK_ACCESS_ALL_CONFIG_USER', 'Configure all user settings');
define('BLOCK_ACCESS_ALL_CONFIG_ROLE', 'Configure all role settings');
define('BLOCK_ACCESS_ALL_CONFIG_PAGE', 'Configure all page settings');
define('BLOCK_ACCESS_SETTINGS', 'admin/build/block/block-access');
define('BLOCK_ACCESS_MENU_ADD', 'admin/build/block/add');
define('BLOCK_ACCESS_MENU_DELETE', 'admin/build/block/delete');
define('BLOCK_ACCESS_MENU_CONFIG', 'admin/build/block/configure');
define('BLOCK_ACCESS_FORM_ADMIN', 'block_admin_display_form');
define('BLOCK_ACCESS_FORM_CONFIG', 'block_admin_configure');
function block_access_init() {
require_once drupal_get_path('module', 'block_access') . '/block_access.db.inc';
}
function block_access_theme() {
return array(
'block_access_admin_list' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'block_access.theme.inc',
),
);
}
function block_access_menu() {
$items[BLOCK_ACCESS_SETTINGS] = array(
'title' => 'Block Access Settings',
'description' => 'Configure block access defaults',
'access arguments' => array(
BLOCK_ACCESS_ADMIN,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'block_access_admin_list',
),
'type' => MENU_LOCAL_TASK,
'file' => 'block_access.admin.inc',
);
$items[BLOCK_ACCESS_SETTINGS . '/edit'] = array(
'title' => 'Block Access Settings',
'description' => 'Configure block access defaults',
'access arguments' => array(
BLOCK_ACCESS_ADMIN,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'block_access_module_form',
5,
),
'type' => MENU_CALLBACK,
'file' => 'block_access.admin.inc',
);
return $items;
}
function block_access_menu_alter(&$items) {
$items[BLOCK_ACCESS_MENU_ADD]['access arguments'] = array(
BLOCK_ACCESS_CREATE,
);
$items[BLOCK_ACCESS_MENU_DELETE]['access callback'] = 'block_access_check_delete';
$items[BLOCK_ACCESS_MENU_DELETE]['access arguments'] = array(
4,
);
$items[BLOCK_ACCESS_MENU_CONFIG]['access callback'] = 'block_access_check_config';
$items[BLOCK_ACCESS_MENU_CONFIG]['access arguments'] = array(
4,
5,
);
}
function block_access_perm() {
$access = array(
t(BLOCK_ACCESS_ADMIN),
t(BLOCK_ACCESS_CREATE),
t(BLOCK_ACCESS_ALL_VIEW),
t(BLOCK_ACCESS_ALL_MOVE),
t(BLOCK_ACCESS_ALL_ENABLE),
t(BLOCK_ACCESS_ALL_DISABLE),
t(BLOCK_ACCESS_ALL_CONFIG),
t(BLOCK_ACCESS_ALL_DELETE),
t(BLOCK_ACCESS_ALL_CONFIG_LANG),
t(BLOCK_ACCESS_ALL_CONFIG_SPEC),
t(BLOCK_ACCESS_ALL_CONFIG_USER),
t(BLOCK_ACCESS_ALL_CONFIG_ROLE),
t(BLOCK_ACCESS_ALL_CONFIG_PAGE),
);
return $access;
}
function block_access_user_access($module, $delta, $perm) {
global $user;
static $perms = array();
if (!isset($perms[$module])) {
$perms[$module] = array(
$delta => array(),
);
}
if (!isset($perms[$module][$delta])) {
$perms[$module][$delta] = array();
}
if (!isset($perms[$module][$delta][$perm])) {
$perms[$module][$delta][$perm] = false;
foreach (block_access_get_roles($module, $perm, $delta) as $rid) {
if (in_array($rid, array_keys($user->roles))) {
$perms[$module][$delta][$perm] = true;
break;
}
}
}
return $perms[$module][$delta][$perm];
}
function block_access_can_view($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_VIEW)) || block_access_user_access($module, $delta, 'view');
}
function block_access_can_delete($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_DELETE)) || block_access_user_access($module, $delta, 'delete');
}
function block_access_can_enable($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_ENABLE)) || block_access_user_access($module, $delta, 'enable');
}
function block_access_can_disable($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_DISABLE)) || block_access_user_access($module, $delta, 'disable');
}
function block_access_can_move($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_MOVE)) || block_access_user_access($module, $delta, 'move');
}
function block_access_can_config($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_CONFIG)) || user_access(t(BLOCK_ACCESS_ALL_CONFIG_LANG)) || user_access(t(BLOCK_ACCESS_ALL_CONFIG_SPEC)) || user_access(t(BLOCK_ACCESS_ALL_CONFIG_USER)) || user_access(t(BLOCK_ACCESS_ALL_CONFIG_ROLE)) || user_access(t(BLOCK_ACCESS_ALL_CONFIG_PAGE)) || block_access_user_access($module, $delta, 'config') || block_access_user_access($module, $delta, 'config_lang') || block_access_user_access($module, $delta, 'config_spec') || block_access_user_access($module, $delta, 'config_user') || block_access_user_access($module, $delta, 'config_role') || block_access_user_access($module, $delta, 'config_page');
}
function block_access_can_config_lang($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_CONFIG_LANG)) || block_access_user_access($module, $delta, 'config_lang');
}
function block_access_can_config_spec($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_CONFIG_SPEC)) || block_access_user_access($module, $delta, 'config_spec');
}
function block_access_can_config_user($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_CONFIG_USER)) || block_access_user_access($module, $delta, 'config_user');
}
function block_access_can_config_role($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_CONFIG_ROLE)) || block_access_user_access($module, $delta, 'config_role');
}
function block_access_can_config_page($module, $delta) {
return user_access(t(BLOCK_ACCESS_ALL_CONFIG_PAGE)) || block_access_user_access($module, $delta, 'config_page');
}
function block_access_check_perms(&$form, $key, $block) {
$module = $block['module']['#value'];
$delta = $block['delta']['#value'];
if (!block_access_can_view($module, $delta)) {
unset($form[$key]);
}
else {
if (!block_access_can_delete($module, $delta)) {
unset($form[$key]['delete']);
}
if (!block_access_can_config($module, $delta)) {
unset($form[$key]['configure']);
}
if ($block['region']['#default_value'] == BLOCK_REGION_NONE) {
if (!block_access_can_enable($module, $delta)) {
$form[$key]['region']['#disabled'] = TRUE;
$form[$key]['weight']['#disabled'] = TRUE;
}
}
else {
if (!block_access_can_move($module, $delta)) {
$form[$key]['region']['#disabled'] = TRUE;
$form[$key]['weight']['#disabled'] = TRUE;
}
if (!block_access_can_disable($module, $delta)) {
unset($form[$key]['region']['#options'][BLOCK_REGION_NONE]);
}
}
}
}
function block_access_check_config_form(&$form) {
$module = $form['module']['#value'];
$delta = $form['delta']['#value'];
if (!block_access_can_config_lang($module, $delta)) {
unset($form['i18n']);
}
if (!block_access_can_config_spec($module, $delta)) {
unset($form['block_settings']);
}
if (!block_access_can_config_user($module, $delta)) {
unset($form['user_vis_settings']);
}
if (!block_access_can_config_role($module, $delta)) {
unset($form['role_vis_settings']);
unset($form['role_view_settings']);
unset($form['role_move_settings']);
unset($form['role_enable_settings']);
unset($form['role_disable_settings']);
unset($form['role_delete_settings']);
unset($form['role_config_settings']);
unset($form['role_config_lang_settings']);
unset($form['role_config_spec_settings']);
unset($form['role_config_user_settings']);
unset($form['role_config_role_settings']);
unset($form['role_config_page_settings']);
}
if (!block_access_can_config_page($module, $delta)) {
unset($form['page_vis_settings']);
}
}
function block_access_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == BLOCK_ACCESS_FORM_ADMIN) {
foreach ($form as $key => $block) {
if (is_array($block) && isset($block['info'])) {
block_access_check_perms($form, $key, $block);
}
}
$form['#submit'][] = 'block_access_admin_display_submit';
}
elseif ($form_id == BLOCK_ACCESS_FORM_CONFIG) {
$role_options = user_roles();
$module = $form['module']['#value'];
$delta = $form['delta']['#value'];
block_access_fieldset($form, $role_options, 'view', $module, $delta);
block_access_fieldset($form, $role_options, 'move', $module, $delta);
block_access_fieldset($form, $role_options, 'enable', $module, $delta);
block_access_fieldset($form, $role_options, 'disable', $module, $delta);
block_access_fieldset($form, $role_options, 'config', $module, $delta);
block_access_fieldset($form, $role_options, 'delete', $module, $delta);
block_access_fieldset($form, $role_options, 'config_lang', $module, $delta);
block_access_fieldset($form, $role_options, 'config_spec', $module, $delta);
block_access_fieldset($form, $role_options, 'config_user', $module, $delta);
block_access_fieldset($form, $role_options, 'config_role', $module, $delta);
block_access_fieldset($form, $role_options, 'config_page', $module, $delta);
array_unshift($form['#submit'], 'block_access_config_presubmit');
$form['#submit'][] = 'block_access_config_submit';
$form['submit']['#weight'] = 99;
block_access_check_config_form($form);
}
}
function block_access_fieldset(&$form, $role_options, $perm, $module, $delta = null) {
$default_role_options = block_access_get_roles($module, $perm, $delta);
$collapsed = empty($default_role_options) ? TRUE : FALSE;
$form['role_' . $perm . '_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Role specific ' . $perm . ' settings'),
'#collapsible' => TRUE,
'#collapsed' => $collapsed,
);
$form['role_' . $perm . '_settings'][$perm . '_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Set permission for specific roles'),
'#default_value' => $default_role_options,
'#options' => $role_options,
'#description' => t('Set this permission for the selected role(s). If no roles are set the option will be available to all.'),
);
}
function block_access_check_submit($form, $key, $block) {
$valid = TRUE;
$name = array(
check_plain($block['info']),
);
if ($block['region'] == BLOCK_REGION_NONE && $form[$key]['region']['#value'] != BLOCK_REGION_NONE && !block_access_can_delete($block['module']['#value'], $block['delta']['#value'])) {
drupal_set_message(t('You cannot disable @block.', $name));
$valid = FALSE;
}
elseif ($block['region'] != BLOCK_REGION_NONE && $form[$key]['region']['#value'] == BLOCK_REGION_NONE && !block_access_can_enable($block['module']['#value'], $block['delta']['#value'])) {
drupal_set_message(t('You cannot enable @block.', $name));
$valid = FALSE;
}
elseif ($block['region'] != BLOCK_REGION_NONE && $form[$key]['region']['#value'] != BLOCK_REGION_NONE && $block['region'] != $form[$key]['region']['#value'] && !block_access_can_move($block['module']['#value'], $block['delta']['#value'])) {
drupal_set_message(t('You cannot move @block.', $name));
$valid = FALSE;
}
return $valid;
}
function block_access_config_presubmit($form, &$form_state) {
$module = $form_state['values']['module'];
$delta = $form_state['values']['delta'];
$edit = db_fetch_array(db_query("\n SELECT pages, visibility, custom, title\n FROM {blocks}\n WHERE module = '%s'\n AND delta = '%s'", $module, $delta));
if (!isset($form_state['values']['pages'])) {
$form_state['values']['pages'] = $edit['pages'];
}
if (!isset($form_state['values']['visibility'])) {
$form_state['values']['visibility'] = $edit['visibility'];
}
if (!isset($form_state['values']['custom'])) {
$form_state['values']['custom'] = $edit['custom'];
}
if (!isset($form_state['values']['title'])) {
$form_state['values']['title'] = $edit['title'];
}
if (!isset($form_state['values']['roles'])) {
$form_state['values']['roles'] = array();
$result = db_query("\n SELECT rid\n FROM {blocks_roles}\n WHERE module = '%s'\n AND delta = '%s'", $module, $delta);
while ($role = db_fetch_object($result)) {
$form_state['values']['roles'][] = $role->rid;
}
}
if (function_exists('i18nblocks_load')) {
$i18n = i18nblocks_load($module, $delta);
$form_state['values']['language'] = $i18n->language;
$form_state['values']['ibid'] = $i18n->ibid;
}
}
function block_access_config_submit($form, &$form_state) {
$module = $form_state['values']['module'];
$delta = $form_state['values']['delta'];
if (isset($form_state['values']['view_roles'])) {
block_access_config_set_roles('view', $form_state['values']['view_roles'], $module, $delta);
block_access_config_set_roles('move', $form_state['values']['move_roles'], $module, $delta);
block_access_config_set_roles('enable', $form_state['values']['enable_roles'], $module, $delta);
block_access_config_set_roles('disable', $form_state['values']['disable_roles'], $module, $delta);
block_access_config_set_roles('config', $form_state['values']['config_roles'], $module, $delta);
block_access_config_set_roles('delete', $form_state['values']['delete_roles'], $module, $delta);
block_access_config_set_roles('config_lang', $form_state['values']['config_lang_roles'], $module, $delta);
block_access_config_set_roles('config_spec', $form_state['values']['config_spec_roles'], $module, $delta);
block_access_config_set_roles('config_user', $form_state['values']['config_user_roles'], $module, $delta);
block_access_config_set_roles('config_role', $form_state['values']['config_role_roles'], $module, $delta);
block_access_config_set_roles('config_page', $form_state['values']['config_page_roles'], $module, $delta);
}
}
function block_access_config_set_roles($perm, $roleset, $module, $delta = null) {
$roles = array_filter($roleset);
block_access_delete_roles($module, $perm, $delta);
foreach ($roles as $rid) {
block_access_add_role($rid, $module, $perm, $delta);
}
}
function block_access_admin_display_submit($form, &$form_state) {
$valid = TRUE;
foreach ($form_state['values'] as $key => $block) {
if (is_array($block) && isset($form[$key]['info'])) {
if (!block_access_check_submit($form, $key, $block)) {
$valid = FALSE;
}
}
}
if ($valid) {
block_admin_display_form_submit($form, $form_state);
}
}
function block_access_check_delete($bid) {
$box = block_box_get($bid);
return block_access_can_delete($box['module'], $box['delta']);
}
function block_access_check_config($module, $delta) {
return block_access_can_config($module, $delta);
}