You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

95 lines
2.7 KiB

  1. <?php
  2. use dokuwiki\Extension\ActionPlugin;
  3. use dokuwiki\Extension\EventHandler;
  4. use dokuwiki\Extension\Event;
  5. /**
  6. * DokuWiki Plugin addomain (Action Component)
  7. *
  8. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  9. * @author Andreas Gohr <gohr@cosmocode.de>
  10. */
  11. /**
  12. * Class action_plugin_addomain
  13. */
  14. class action_plugin_authad extends ActionPlugin
  15. {
  16. /**
  17. * Registers a callback function for a given event
  18. */
  19. public function register(EventHandler $controller)
  20. {
  21. $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handleAuthLoginCheck');
  22. $controller->register_hook('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handleFormLoginOutput');
  23. }
  24. /**
  25. * Adds the selected domain as user postfix when attempting a login
  26. *
  27. * @param Event $event
  28. * @param array $param
  29. */
  30. public function handleAuthLoginCheck(Event $event, $param)
  31. {
  32. global $INPUT;
  33. /** @var auth_plugin_authad $auth */
  34. global $auth;
  35. if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
  36. if ($INPUT->str('dom')) {
  37. $usr = $auth->cleanUser($event->data['user']);
  38. $dom = $auth->getUserDomain($usr);
  39. if (!$dom) {
  40. $usr = "$usr@" . $INPUT->str('dom');
  41. }
  42. $INPUT->post->set('u', $usr);
  43. $event->data['user'] = $usr;
  44. }
  45. }
  46. /**
  47. * Shows a domain selection in the login form when more than one domain is configured
  48. *
  49. * @param Event $event
  50. * @param array $param
  51. */
  52. public function handleFormLoginOutput(Event $event, $param)
  53. {
  54. global $INPUT;
  55. /** @var auth_plugin_authad $auth */
  56. global $auth;
  57. if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
  58. $domains = $auth->getConfiguredDomains();
  59. if (count($domains) <= 1) return; // no choice at all
  60. /** @var dokuwiki\Form\Form $form */
  61. $form =& $event->data;
  62. // find the username input box
  63. $pos = $form->findPositionByAttribute('name', 'u');
  64. if ($pos === false) return;
  65. // any default?
  66. if ($INPUT->has('u')) {
  67. $usr = $auth->cleanUser($INPUT->str('u'));
  68. $dom = $auth->getUserDomain($usr);
  69. // update user field value
  70. if ($dom) {
  71. $usr = $auth->getUserName($usr);
  72. $element = $form->getElementAt($pos);
  73. $element->val($usr);
  74. }
  75. }
  76. // add locate domain selector just after the username input box
  77. $element = $form->addDropdown('dom', $domains, $this->getLang('domain'), $pos + 1);
  78. $element->addClass('block');
  79. }
  80. }
  81. // vim:ts=4:sw=4:et: