xref: /dokuwiki/lib/plugins/authad/action.php (revision ced96239e5e8a370ff7bb54016e287ae50fefcaa)
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                $pos          = $form->findElementByAttribute('name', 'u');
78                $ele          =& $form->getElementAt($pos);
79                $ele['value'] = $usr;
80            }
81        }
82
83        // add select box
84        $element = form_makeListboxField('dom', $domains, $dom, $this->getLang('domain'), '', 'block');
85        $pos     = $form->findElementByAttribute('name', 'p');
86        $form->insertElement($pos + 1, $element);
87    }
88}
89
90// vim:ts=4:sw=4:et:
91