xref: /dokuwiki/lib/plugins/authad/action.php (revision 3dc2d50c5fda9c4bf708ff4c26e266ba239af62c)
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     * Registers a callback function for a given event
16     */
17    public function register(Doku_Event_Handler $controller) {
18
19        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handle_auth_login_check');
20        $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_html_loginform_output');
21
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 handle_auth_login_check(Doku_Event &$event, $param) {
31        global $INPUT;
32
33        /** @var auth_plugin_authad $auth */
34        global $auth;
35        if(!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
36
37        if($INPUT->str('dom')) {
38            $usr = $auth->cleanUser($event->data['user']);
39            $dom = $auth->_userDomain($usr);
40            if(!$dom) {
41                $usr = "$usr@".$INPUT->str('dom');
42            }
43            $INPUT->post->set('u', $usr);
44            $event->data['user'] = $usr;
45        }
46    }
47
48    /**
49     * Shows a domain selection in the login form when more than one domain is configured
50     *
51     * @param Doku_Event $event
52     * @param array      $param
53     */
54    public function handle_html_loginform_output(Doku_Event &$event, $param) {
55        global $INPUT;
56        /** @var auth_plugin_authad $auth */
57        global $auth;
58        if(!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
59        $domains = $auth->_getConfiguredDomains();
60        if(count($domains) <= 1) return; // no choice at all
61
62        /** @var Doku_Form $form */
63        $form =& $event->data;
64
65        // any default?
66        $dom = '';
67        if($INPUT->has('u')) {
68            $usr = $auth->cleanUser($INPUT->str('u'));
69            $dom = $auth->_userDomain($usr);
70
71            // update user field value
72            if($dom) {
73                $usr          = $auth->_userName($usr);
74                $pos          = $form->findElementByAttribute('name', 'u');
75                $ele          =& $form->getElementAt($pos);
76                $ele['value'] = $usr;
77            }
78        }
79
80        // add select box
81        $element = form_makeListboxField('dom', $domains, $dom, $this->getLang('domain'), '', 'block');
82        $pos     = $form->findElementByAttribute('name', 'p');
83        $form->insertElement($pos + 1, $element);
84    }
85
86}
87
88// vim:ts=4:sw=4:et:
89