perm.inc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based on user permission strings.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t("User: permission"),
  12. 'description' => t('Control access by permission string.'),
  13. 'callback' => 'ctools_perm_ctools_access_check',
  14. 'default' => array('perm' => 'access content'),
  15. 'settings form' => 'ctools_perm_ctools_access_settings',
  16. 'summary' => 'ctools_perm_ctools_access_summary',
  17. 'required context' => new ctools_context_required(t('User'), 'user'),
  18. );
  19. /**
  20. * Settings form for the 'by perm' access plugin
  21. */
  22. function ctools_perm_ctools_access_settings($form, &$form_state, $conf) {
  23. $perms = array();
  24. // Get list of permissions
  25. foreach (module_list(FALSE, FALSE, TRUE) as $module) {
  26. // By keeping them keyed by module we can use optgroups with the
  27. // 'select' type.
  28. if ($permissions = module_invoke($module, 'permission')) {
  29. foreach ($permissions as $id => $permission) {
  30. $perms[$module][$id] = $permission['title'];
  31. }
  32. }
  33. }
  34. $form['settings']['perm'] = array(
  35. '#type' => 'select',
  36. '#options' => $perms,
  37. '#title' => t('Permission'),
  38. '#default_value' => $conf['perm'],
  39. '#description' => t('Only users with the selected permission flag will be able to access this.'),
  40. );
  41. return $form;
  42. }
  43. /**
  44. * Check for access.
  45. */
  46. function ctools_perm_ctools_access_check($conf, $context) {
  47. // As far as I know there should always be a context at this point, but this
  48. // is safe.
  49. if (empty($context) || empty($context->data)) {
  50. return FALSE;
  51. }
  52. return user_access($conf['perm'], $context->data);
  53. }
  54. /**
  55. * Provide a summary description based upon the checked roles.
  56. */
  57. function ctools_perm_ctools_access_summary($conf, $context) {
  58. if (!isset($conf['perm'])) {
  59. return t('Error, unset permission');
  60. }
  61. $permissions = module_invoke_all('permission');
  62. return t('@identifier has "@perm"', array('@identifier' => $context->identifier, '@perm' => $permissions[$conf['perm']]['title']));
  63. }