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