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