xref: /dokuwiki/lib/plugins/authad/action.php (revision 94214f97e4a1d5491e048d798c1d5cf63e52fd24)
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// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12/**
13 * Class action_plugin_addomain
14 */
15class action_plugin_authad extends DokuWiki_Action_Plugin {
16
17    /**
18     * Registers a callback function for a given event
19     */
20    public function register(Doku_Event_Handler $controller) {
21
22        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handle_auth_login_check');
23        $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_html_loginform_output');
24
25    }
26
27    /**
28     * Adds the selected domain as user postfix when attempting a login
29     *
30     * @param Doku_Event $event
31     * @param array      $param
32     */
33    public function handle_auth_login_check(Doku_Event &$event, $param) {
34        global $INPUT;
35
36        /** @var auth_plugin_authad $auth */
37        global $auth;
38        if(!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
39
40        if($INPUT->str('dom')) {
41            $usr = $auth->cleanUser($event->data['user']);
42            $dom = $auth->_userDomain($usr);
43            if(!$dom) {
44                $usr = "$usr@".$INPUT->str('dom');
45            }
46            $INPUT->post->set('u', $usr);
47            $event->data['user'] = $usr;
48        }
49    }
50
51    /**
52     * Shows a domain selection in the login form when more than one domain is configured
53     *
54     * @param Doku_Event $event
55     * @param array      $param
56     */
57    public function handle_html_loginform_output(Doku_Event &$event, $param) {
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->_userDomain($usr);
73
74            // update user field value
75            if($dom) {
76                $usr          = $auth->_userName($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
91// vim:ts=4:sw=4:et: