summaryrefslogtreecommitdiff
path: root/lib/plugins/authad/action.php
blob: bc0f90c7ed3121bc974380d8aef2b360745bad0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
 * DokuWiki Plugin addomain (Action Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Andreas Gohr <gohr@cosmocode.de>
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

/**
 * Class action_plugin_addomain
 */
class action_plugin_authad extends DokuWiki_Action_Plugin {

    /**
     * Registers a callback function for a given event
     */
    public function register(Doku_Event_Handler $controller) {

        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handle_auth_login_check');
        $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_html_loginform_output');

    }

    /**
     * Adds the selected domain as user postfix when attempting a login
     *
     * @param Doku_Event $event
     * @param array      $param
     */
    public function handle_auth_login_check(Doku_Event &$event, $param) {
        global $INPUT;

        /** @var auth_plugin_authad $auth */
        global $auth;
        if(!is_a($auth, 'auth_plugin_authad')) return; // AD not even used

        if($INPUT->str('dom')) {
            $usr = $auth->cleanUser($event->data['user']);
            $dom = $auth->_userDomain($usr);
            if(!$dom) {
                $usr = "$usr@".$INPUT->str('dom');
            }
            $INPUT->post->set('u', $usr);
            $event->data['user'] = $usr;
        }
    }

    /**
     * Shows a domain selection in the login form when more than one domain is configured
     *
     * @param Doku_Event $event
     * @param array      $param
     */
    public function handle_html_loginform_output(Doku_Event &$event, $param) {
        global $INPUT;
        /** @var auth_plugin_authad $auth */
        global $auth;
        if(!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
        $domains = $auth->_getConfiguredDomains();
        if(count($domains) <= 1) return; // no choice at all

        /** @var Doku_Form $form */
        $form =& $event->data;

        // any default?
        $dom = '';
        if($INPUT->has('u')) {
            $usr = $auth->cleanUser($INPUT->str('u'));
            $dom = $auth->_userDomain($usr);

            // update user field value
            if($dom) {
                $usr          = $auth->_userName($usr);
                $pos          = $form->findElementByAttribute('name', 'u');
                $ele          =& $form->getElementAt($pos);
                $ele['value'] = $usr;
            }
        }

        // add select box
        $element = form_makeListboxField('dom', $domains, $dom, $this->getLang('domain'), '', 'block');
        $pos     = $form->findElementByAttribute('name', 'p');
        $form->insertElement($pos + 1, $element);
    }

}

// vim:ts=4:sw=4:et: