View source
<?php
use Drupal\Core\Database\Database;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
function paranoia_system_info_alter(array &$info, Extension $file, $type) {
$hidden_modules = \Drupal::moduleHandler()
->invokeAll('paranoia_hide_modules');
if ($type == 'module' && in_array($file
->getName(), $hidden_modules)) {
$info['hidden'] = TRUE;
}
}
function paranoia_user_role_presave(RoleInterface $role) {
if ($role
->isAdmin() || isset($role->original) && $role->original
->isAdmin()) {
$role
->setIsAdmin(FALSE);
\Drupal::service('logger.channel.paranoia')
->alert('An attempt to elevate the role %role_label to admin was detected and blocked.', [
'%role_label' => $role
->label(),
]);
}
}
function paranoia_form_user_admin_settings_alter(&$form, FormStateInterface $form_state) {
$form['admin_role']['#access'] = FALSE;
}
function paranoia_form_system_modules_uninstall_alter(&$form, FormStateInterface $form_state) {
$hidden_modules = \Drupal::moduleHandler()
->invokeAll('paranoia_hide_modules');
foreach ($hidden_modules as $module) {
if (isset($form['modules'][$module])) {
unset($form['modules'][$module]);
}
}
}
function paranoia_form_system_modules_alter(&$form, FormStateInterface $form_state) {
$form['#validate'][] = 'paranoia_module_validate';
}
function paranoia_module_validate($form, FormStateInterface $form_state) {
paranoia_remove_disabled_modules();
}
function paranoia_remove_disabled_modules() {
$module_handler = \Drupal::moduleHandler();
$disabled_modules = $module_handler
->invokeAll('paranoia_disable_modules');
foreach ($disabled_modules as $module) {
if ($module_handler
->moduleExists($module)) {
\Drupal::messenger()
->addMessage(t('The module %module has been disabled as it is not allowed on this site.', [
'%module' => $module,
]));
\Drupal::service('module_installer')
->uninstall([
$module,
]);
}
}
}
function paranoia_form_user_form_alter(&$form, FormStateInterface $form_state) {
$user_id = $form_state
->getBuildInfo()['callback_object']
->getEntity()
->id();
if ($user_id == 1) {
$user = \Drupal::currentUser();
if ($user
->id() != 1) {
\Drupal::messenger()
->addWarning(t('You must log in as this user (user/1) to modify the name, email address, and password for this account.'));
$form['account']['name']['#access'] = FALSE;
$form['account']['mail']['#access'] = FALSE;
$form['account']['pass']['#access'] = FALSE;
$form['account']['current_pass']['#access'] = FALSE;
}
}
}
function paranoia_form_user_admin_permissions_alter(&$form, FormStateInterface $form_state) {
$banned_permissions = \Drupal::moduleHandler()
->invokeAll('paranoia_hide_permissions');
$permissions = \Drupal::service('user.permissions')
->getPermissions();
$permissions_by_provider = [];
foreach ($permissions as $permission_name => $permission) {
$permissions_by_provider[$permission['provider']][$permission_name] = $permission;
}
$has_hidden = FALSE;
foreach ($permissions_by_provider as $provider => $provider_permissions) {
$hidden_count = 0;
foreach ($provider_permissions as $permission_name => $permission) {
if (in_array($permission_name, $banned_permissions)) {
unset($form['permissions'][$permission_name]);
$hidden_count++;
$has_hidden = TRUE;
}
elseif (!empty($permission['restrict access'])) {
foreach ([
RoleInterface::ANONYMOUS_ID,
RoleInterface::AUTHENTICATED_ID,
] as $rid) {
$form['permissions'][$permission_name][$rid]['#disabled'] = TRUE;
$form['permissions'][$permission_name][$rid]['#default_value'] = FALSE;
}
}
}
if ($hidden_count == count($provider_permissions)) {
unset($form['permissions'][$provider]);
}
}
if ($has_hidden) {
\Drupal::messenger()
->addMessage(t('To make this site more secure, some permissions have been removed from this form.'));
}
$form['#submit'][] = 'paranoia_permissions_submit';
}
function _paranoia_remove_risky_permissions() {
$banned_permissions = \Drupal::moduleHandler()
->invokeAll('paranoia_hide_permissions');
$roles = Role::loadMultiple();
foreach ($roles as $role) {
foreach ($banned_permissions as $permission) {
$role
->revokePermission($permission);
}
$role
->save();
}
}
function paranoia_permissions_submit($form, FormStateInterface $form_state) {
_paranoia_remove_risky_permissions();
}
function paranoia_paranoia_disable_modules() {
return [
'php',
'skinr_ui',
];
}
function paranoia_paranoia_hide_permissions() {
return [
'use PHP for settings',
'use text format php_code',
];
}
function paranoia_paranoia_hide_modules() {
$hidden_modules = [
'paranoia',
];
$disabled_modules = \Drupal::moduleHandler()
->invokeAll('paranoia_disable_modules');
return array_unique(array_merge($hidden_modules, $disabled_modules));
}
function devel_paranoia_hide_permissions() {
return [
'execute php code',
];
}
function googleanalytics_paranoia_hide_permissions() {
return [
'use PHP for tracking visibility',
];
}
function bueditor_paranoia_hide_permissions() {
return [
'administer bueditor',
];
}
function auto_username_paranoia_hide_permissions() {
return [
'use PHP for username patterns',
];
}
function auto_entitylabel_paranoia_hide_permissions() {
return [
'use PHP for auto entity labels',
];
}
function devel_paranoia_hide_routes() {
return [
'devel.execute_php',
];
}
function paranoia_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$forms_to_disable = \Drupal::moduleHandler()
->invokeAll('paranoia_risky_forms');
$forms_to_disable = array_combine($forms_to_disable, $forms_to_disable);
if (array_key_exists($form_id, $forms_to_disable)) {
$form['#access'] = FALSE;
$form['#validate'][] = 'paranoia_form_validate_always_fail';
\Drupal::messenger()
->addError(t('This form is disabled for security reasons. See <a href="https://www.drupal.org/node/2313945">details</a> on why this form is disabled.'));
}
}
function paranoia_form_validate_always_fail() {
$form_state
->setErrorByName('', t('This form is disabled for security reasons. See <a href="https://www.drupal.org/node/2313945">details</a> on why this form is disabled.'));
}
function devel_paranoia_risky_forms() {
return [
'devel_execute_php',
];
}