View source
<?php
function toolbar_patterns($data) {
$files = array(
'modules/user/user.admin.inc',
);
$actions['toolbarrole'] = array(
PATTERNS_INFO => t('Decide who can see the toolbar'),
PATTERNS_MODIFY => array(
'user_admin_permissions',
),
PATTERNS_FILES => $files,
);
return $actions;
}
function toolbar_patterns_prepare($action, $tag, &$data) {
module_load_include('module', 'user', 'user');
$data['role_names'] = user_roles();
$data['role_permissions'] = user_role_permissions($data['role_names']);
$module_info = system_get_info('module');
$modules = array();
foreach (module_implements('permission') as $module) {
$modules[$module] = $module_info[$module]['name'];
}
asort($modules);
foreach ($modules as $module => $display_name) {
if ($permissions = module_invoke($module, 'permission')) {
foreach ($permissions as $perm => $perm_item) {
foreach ($data['role_names'] as $rid => $name) {
if (isset($data['role_permissions'][$rid][$perm])) {
$data['status'][$rid][] = $perm;
}
}
}
}
}
return patterns_results();
}
function toolbar_patterns_validate($action, $tag, &$data) {
$status = PATTERNS_SUCCESS;
$msg = '';
if (!isset($data['addroles']) && !isset($data['delroles'])) {
$status = PATTERNS_ERR;
$msg = t('there is nothing to do.');
return patterns_results($status, $msg);
}
if (is_array($data['addroles']) && is_array($data['delroles'])) {
$a = array_intersect($data['addroles'], $data['delroles']);
if (count($a) != 0) {
$status = PATTERNS_ERR;
$msg = t('same name in both addroles and delroles');
return patterns_results($status, $msg);
}
}
if (is_array($data['addroles'])) {
foreach ($data['addroles'] as $key => $value) {
$rid = db_select('role', 'r')
->fields('r', array(
'rid',
))
->condition('r.name', $value)
->execute()
->fetchField();
if ($rid == 3) {
$status = PATTERNS_ERR;
$msg = t('administrator should keep static.');
return patterns_results($status, $msg);
}
if ($rid == FALSE) {
$status = PATTERNS_ERR;
$msg = t('there is a wrong role name');
return patterns_results($status, $msg);
}
if (isset($data['status'][$rid])) {
if (in_array("access toolbar", $data['status'][$rid])) {
$status = PATTERNS_ERR;
$msg = t('the role have the toolbar permission already.');
return patterns_results($status, $msg);
}
}
$data['status'][$rid][] = "access toolbar";
}
}
if (is_array($data['delroles'])) {
foreach ($data['delroles'] as $key => $value) {
$rid = db_select('role', 'r')
->fields('r', array(
'rid',
))
->condition('r.name', $value)
->execute()
->fetchField();
if ($rid == 3) {
$status = PATTERNS_ERR;
$msg = t('administrator should keep static.');
return patterns_results($status, $msg);
}
if ($rid == FALSE) {
$status = PATTERNS_ERR;
$msg = t('there is a wrong role name');
return patterns_results($status, $msg);
}
if (isset($data['status'][$rid])) {
if (!in_array("access toolbar", $data['status'][$rid])) {
$status = PATTERNS_ERR;
$msg = t('the role do not have the toolbar permission yet.');
return patterns_results($status, $msg);
}
$data['status'][$rid] = array_diff($data['status'][$rid], array(
"access toolbar",
));
}
else {
$status = PATTERNS_ERR;
$msg = t('the role does not have any permissions.');
return patterns_results($status, $msg);
}
}
}
foreach ($data['status'] as $rid => $value) {
$data[$rid] = $value;
}
return patterns_results($status, $msg);
}
function toolbar_patterns_callbacks($action, $tag, &$data) {
$result = array(
'user_admin_permissions',
);
return patterns_results(PATTERNS_SUCCESS, t('Execution successful'), $result);
}
function toolbar_patterns_build($action, $form_id, &$data = NULL, &$a) {
module_load_include('inc', 'user', 'user.admin');
return patterns_results(PATTERNS_SUCCESS, t('Execution successful(in build function)'), $data);
}
function toolbar_patterns_params($action, $form_id, &$data = NULL, &$a) {
return patterns_results();
}
function toolbar_patterns_cleanup($action, $tag, &$data) {
return patterns_results();
}