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('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handleFormLoginOutput');
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 handleFormLoginOutput(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        if ($INPUT->has('u')) {
73            $usr = $auth->cleanUser($INPUT->str('u'));
74            $dom = $auth->getUserDomain($usr);
75
76            // update user field value
77            if ($dom) {
78                $usr = $auth->getUserName($usr);
79                $element = $form->getElementAt($pos);
80                $element->val($usr);
81            }
82        }
83
84        // add locate domain selector just after the username input box
85        $element = $form->addDropdown('dom', $domains, $this->getLang('domain'), $pos +1);
86        $element->addClass('block');
87    }
88}
89
90// vim:ts=4:sw=4:et:
91