xref: /dokuwiki/lib/plugins/authad/action.php (revision bde2a644b7f10af4f4d8a7ca52281cd954538ea9)
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/**
10 * Class action_plugin_addomain
11 */
12class action_plugin_authad extends DokuWiki_Action_Plugin
13{
14
15    /**
16     * Registers a callback function for a given event
17     */
18    public function register(Doku_Event_Handler $controller)
19    {
20        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handleAuthLoginCheck');
21        $controller->register_hook('HTMLFORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handleHtmlFormLoginOutput');
22    }
23
24    /**
25     * Adds the selected domain as user postfix when attempting a login
26     *
27     * @param Doku_Event $event
28     * @param array      $param
29     */
30    public function handleAuthLoginCheck(Doku_Event $event, $param)
31    {
32        global $INPUT;
33
34        /** @var auth_plugin_authad $auth */
35        global $auth;
36        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
37
38        if ($INPUT->str('dom')) {
39            $usr = $auth->cleanUser($event->data['user']);
40            $dom = $auth->getUserDomain($usr);
41            if (!$dom) {
42                $usr = "$usr@".$INPUT->str('dom');
43            }
44            $INPUT->post->set('u', $usr);
45            $event->data['user'] = $usr;
46        }
47    }
48
49    /**
50     * Shows a domain selection in the login form when more than one domain is configured
51     *
52     * @param Doku_Event $event
53     * @param array      $param
54     */
55    public function handleHtmlFormLoginOutput(Doku_Event $event, $param)
56    {
57        global $INPUT;
58        /** @var auth_plugin_authad $auth */
59        global $auth;
60        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
61        $domains = $auth->getConfiguredDomains();
62        if (count($domains) <= 1) return; // no choice at all
63
64        /** @var dokuwiki\Form\Form $form */
65        $form =& $event->data;
66
67        // find the username input box
68        $pos = $form->findPositionByAttribute('name', 'u');
69        if ($pos === false) return;
70
71        // any default?
72        $dom = '';
73        if ($INPUT->has('u')) {
74            $usr = $auth->cleanUser($INPUT->str('u'));
75            $dom = $auth->getUserDomain($usr);
76
77            // update user field value
78            if ($dom) {
79                $usr = $auth->getUserName($usr);
80                $element = $form->getElementAt($pos);
81                $element->val($usr);
82            }
83        }
84
85        // add locate domain selector just after the username input box
86        $element = $form->addDropdown('dom', $domains, $this->getLang('domain'), $pos +1);
87        $element->addClass('block');
88    }
89}
90
91// vim:ts=4:sw=4:et:
92