xref: /dokuwiki/lib/plugins/authad/action.php (revision 8549e2b519f181de69658086844cce6cb4f0bc5c)
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
21        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handleAuthLoginCheck');
22        $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handleHtmlLoginformOutput');
23    }
24
25    /**
26     * Adds the selected domain as user postfix when attempting a login
27     *
28     * @param Doku_Event $event
29     * @param array      $param
30     */
31    public function handleAuthLoginCheck(Doku_Event $event, $param)
32    {
33        global $INPUT;
34
35        /** @var auth_plugin_authad $auth */
36        global $auth;
37        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
38
39        if ($INPUT->str('dom')) {
40            $usr = $auth->cleanUser($event->data['user']);
41            $dom = $auth->getUserDomain($usr);
42            if (!$dom) {
43                $usr = "$usr@".$INPUT->str('dom');
44            }
45            $INPUT->post->set('u', $usr);
46            $event->data['user'] = $usr;
47        }
48    }
49
50    /**
51     * Shows a domain selection in the login form when more than one domain is configured
52     *
53     * @param Doku_Event $event
54     * @param array      $param
55     */
56    public function handleHtmlLoginformOutput(Doku_Event $event, $param)
57    {
58        global $INPUT;
59        /** @var auth_plugin_authad $auth */
60        global $auth;
61        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
62        $domains = $auth->getConfiguredDomains();
63        if (count($domains) <= 1) return; // no choice at all
64
65        /** @var Doku_Form $form */
66        $form =& $event->data;
67
68        // any default?
69        $dom = '';
70        if ($INPUT->has('u')) {
71            $usr = $auth->cleanUser($INPUT->str('u'));
72            $dom = $auth->getUserDomain($usr);
73
74            // update user field value
75            if ($dom) {
76                $usr = $auth->getUserName($usr);
77                if (is_a($form, 'dokuwiki\Form\Form')) {
78                    if (($pos = $form->findPositionByAttribute('name', 'u')) !== false) {
79                        $element = $form->getElementAt($pos);
80                        $element->val($usr);
81                    } else {
82                        return;
83                    }
84                } else {
85                    $pos = $form->findElementByAttribute('name', 'u');
86                    $ele =& $form->getElementAt($pos);
87                    $ele['value'] = $usr;
88                }
89            }
90        }
91
92        // add select box
93        if (is_a($form, 'dokuwiki\Form\Form')) {
94            $element = new \dokuwiki\Form\DropdownElement('dom', $domains, $this->getLang('domain'));
95            $element->addClass('block');
96            // locate domain selector just after the username input box
97            $form->addElement($element, $pos +1);
98        } else {
99            $element = form_makeListboxField('dom', $domains, $dom, $this->getLang('domain'), '', 'block');
100            $pos = $form->findElementByAttribute('name', 'p');
101            $form->insertElement($pos + 1, $element);
102        }
103    }
104}
105
106// vim:ts=4:sw=4:et:
107