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