action.php 2.6 KB

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