You are here

function secure_permissions_export in Secure Permissions 7

Same name and namespace in other branches
  1. 6 secure_permissions.module \secure_permissions_export()
  2. 7.2 secure_permissions.module \secure_permissions_export()

Page callback to generate roles and permissions in code.

1 string reference to 'secure_permissions_export'
secure_permissions_menu in ./secure_permissions.module
Implements hook_menu().

File

./secure_permissions.module, line 351
Secure Permissions module file.

Code

function secure_permissions_export($form, $form_state) {
  $form = array();
  $form['help'] = array(
    '#markup' => t('The Secure permissions module stores the permissions in a module (file) that is inaccessible through
      the user interface.<br />You now need to create and enable that module in 4 easy steps.<ol><li>Create directory.
      cd to /sites/all/modules and issue the command: mkdir secure_permissions_data<li>Create 2 empty files. cd to
      /sites/all/modules/secure_permissions_data and issue the command: touch secure_permissions_data.info
      secure_permissions_data.module<li>Copy data. Copy the text from the fields below into the respective files you just
      created using the tools of your choice.<li>Enable the module. Navigate to admin/build/modules/list and enable your
      new module.</ol>To change permissions with the module enabled, you must now edit your
      /sites/all/modules/secure_permissions_data/secure_permissions_data.module file. After editing the file navigate to
      /admin/user/secure_permissions/view select \'Load permissions from code\' and click \'Save configuration\' to update
      the permissions. You may rename the module; remember to rename all the functions.'),
  );
  $output = '';
  $output .= "name = Secure Permissions Data\n";
  $output .= "description = Role and permission settings for the site.\n";
  $output .= "core = 7.x\n";
  $output .= "dependencies[] = secure_permissions\n";
  $output .= "files[] = secure_permissions_data.module\n";
  $lines = explode("\n", $output);
  $form['info'] = array(
    '#title' => t('Permissions output -- secure_permissions_data.info'),
    '#type' => 'textarea',
    '#cols' => 40,
    '#rows' => count($lines),
    '#default_value' => $output,
    '#description' => t('Module .info file for storing secure permissions.'),
  );
  $output = '';
  $output .= <<<EOT
<?php

/**
 * @file Secure Permissions Data
 * Module file for secure permissions in code.
 */

/**
 * Define site roles in code.
 *
 * Create a secure_permissions_data module directory and place this function
 * in secure_permissions_data.module.
 *
 * @return
 *   An array defining all the roles for the site.
 */

EOT;
  $output .= 'function secure_permissions_data_secure_permissions_roles() {';

  // Get roles.
  $roles = secure_permissions_get_existing_roles();
  $admin_rid = variable_get('user_admin_role', 0);

  // Do not export the administrative role.
  if (!empty($admin_rid) && isset($roles[$admin_rid])) {
    unset($roles[$admin_rid]);
  }
  $output .= "\n  return array(\n";
  foreach ($roles as $role) {
    $output .= "    '" . $role . "',\n";
  }
  $output .= "  );";
  $output .= "\n}\n\n";

  // Now get permissions.
  $output .= <<<EOT
/**
 * Define site permissions in code.
 *
 * Create a secure_permissions_data module directory and place this function
 * in secure_permissions_data.module.
 *
 * @param \$role
 *   The role for which the permissions are being requested.
 *
 * @return
 *   An array defining all the permissions for the site.
 */

EOT;
  $output .= 'function secure_permissions_data_secure_permissions($role) {';
  $output .= "\n  \$permissions = array(\n";
  foreach ($roles as $rid => $role) {
    $output .= "    '{$role}' => array(\n";
    $permissions = user_role_permissions(array(
      $rid => $role,
    ));
    foreach (current($permissions) as $permission => $value) {
      if (!$value) {
        continue;
      }
      $output .= "      '{$permission}',\n";
    }
    $output .= "    ),\n";
  }
  $output .= "  );\n";
  $output .= "  if (isset(\$permissions[\$role])) {";
  $output .= "\n    return \$permissions[\$role];\n";
  $output .= "  }\n";
  $output .= "}";
  $lines = explode("\n", $output);
  $form['export'] = array(
    '#title' => t('Permissions output -- secure_permissions_data.module'),
    '#type' => 'textarea',
    '#cols' => 40,
    '#rows' => count($lines),
    '#default_value' => $output,
    '#description' => t('Module .module file for storing secure permissions.'),
  );
  return $form;
}