xref: /dokuwiki/lib/plugins/authad/action.php (revision 741b8a48682890ee68e3f650510fec9a4d47d5b0)
1*741b8a48SAndreas Gohr<?php
2*741b8a48SAndreas Gohr/**
3*741b8a48SAndreas Gohr * DokuWiki Plugin addomain (Action Component)
4*741b8a48SAndreas Gohr *
5*741b8a48SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*741b8a48SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
7*741b8a48SAndreas Gohr */
8*741b8a48SAndreas Gohr
9*741b8a48SAndreas Gohr// must be run within Dokuwiki
10*741b8a48SAndreas Gohrif(!defined('DOKU_INC')) die();
11*741b8a48SAndreas Gohr
12*741b8a48SAndreas Gohr/**
13*741b8a48SAndreas Gohr * Class action_plugin_addomain
14*741b8a48SAndreas Gohr */
15*741b8a48SAndreas Gohrclass action_plugin_authad extends DokuWiki_Action_Plugin {
16*741b8a48SAndreas Gohr
17*741b8a48SAndreas Gohr    /**
18*741b8a48SAndreas Gohr     * Registers a callback function for a given event
19*741b8a48SAndreas Gohr     */
20*741b8a48SAndreas Gohr    public function register(Doku_Event_Handler &$controller) {
21*741b8a48SAndreas Gohr
22*741b8a48SAndreas Gohr        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handle_auth_login_check');
23*741b8a48SAndreas Gohr        $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_html_loginform_output');
24*741b8a48SAndreas Gohr
25*741b8a48SAndreas Gohr    }
26*741b8a48SAndreas Gohr
27*741b8a48SAndreas Gohr    /**
28*741b8a48SAndreas Gohr     * Adds the selected domain as user postfix when attempting a login
29*741b8a48SAndreas Gohr     *
30*741b8a48SAndreas Gohr     * @param Doku_Event $event
31*741b8a48SAndreas Gohr     * @param array      $param
32*741b8a48SAndreas Gohr     */
33*741b8a48SAndreas Gohr    public function handle_auth_login_check(Doku_Event &$event, $param) {
34*741b8a48SAndreas Gohr        global $INPUT;
35*741b8a48SAndreas Gohr
36*741b8a48SAndreas Gohr        /** @var auth_plugin_authad $auth */
37*741b8a48SAndreas Gohr        global $auth;
38*741b8a48SAndreas Gohr        if(!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
39*741b8a48SAndreas Gohr
40*741b8a48SAndreas Gohr        if($INPUT->str('dom')) {
41*741b8a48SAndreas Gohr            $usr = $auth->cleanUser($event->data['user']);
42*741b8a48SAndreas Gohr            $dom = $auth->_userDomain($usr);
43*741b8a48SAndreas Gohr            if(!$dom) {
44*741b8a48SAndreas Gohr                $usr = "$usr@".$INPUT->str('dom');
45*741b8a48SAndreas Gohr            }
46*741b8a48SAndreas Gohr            $INPUT->post->set('u', $usr);
47*741b8a48SAndreas Gohr            $event->data['user'] = $usr;
48*741b8a48SAndreas Gohr        }
49*741b8a48SAndreas Gohr    }
50*741b8a48SAndreas Gohr
51*741b8a48SAndreas Gohr    /**
52*741b8a48SAndreas Gohr     * Shows a domain selection in the login form when more than one domain is configured
53*741b8a48SAndreas Gohr     *
54*741b8a48SAndreas Gohr     * @param Doku_Event $event
55*741b8a48SAndreas Gohr     * @param array      $param
56*741b8a48SAndreas Gohr     */
57*741b8a48SAndreas Gohr    public function handle_html_loginform_output(Doku_Event &$event, $param) {
58*741b8a48SAndreas Gohr        global $INPUT;
59*741b8a48SAndreas Gohr        /** @var auth_plugin_authad $auth */
60*741b8a48SAndreas Gohr        global $auth;
61*741b8a48SAndreas Gohr        if(!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
62*741b8a48SAndreas Gohr        $domains = $auth->_getConfiguredDomains();
63*741b8a48SAndreas Gohr        if(count($domains) <= 1) return; // no choice at all
64*741b8a48SAndreas Gohr
65*741b8a48SAndreas Gohr        /** @var Doku_Form $form */
66*741b8a48SAndreas Gohr        $form =& $event->data;
67*741b8a48SAndreas Gohr
68*741b8a48SAndreas Gohr        // any default?
69*741b8a48SAndreas Gohr        $dom = '';
70*741b8a48SAndreas Gohr        if($INPUT->has('u')) {
71*741b8a48SAndreas Gohr            $usr = $auth->cleanUser($INPUT->str('u'));
72*741b8a48SAndreas Gohr            $dom = $auth->_userDomain($usr);
73*741b8a48SAndreas Gohr
74*741b8a48SAndreas Gohr            // update user field value
75*741b8a48SAndreas Gohr            if($dom) {
76*741b8a48SAndreas Gohr                $usr          = $auth->_userName($usr);
77*741b8a48SAndreas Gohr                $pos          = $form->findElementByAttribute('name', 'u');
78*741b8a48SAndreas Gohr                $ele          =& $form->getElementAt($pos);
79*741b8a48SAndreas Gohr                $ele['value'] = $usr;
80*741b8a48SAndreas Gohr            }
81*741b8a48SAndreas Gohr        }
82*741b8a48SAndreas Gohr
83*741b8a48SAndreas Gohr        // add select box
84*741b8a48SAndreas Gohr        $element = form_makeListboxField('dom', $domains, $dom, $this->getLang('domain'), '', 'block');
85*741b8a48SAndreas Gohr        $pos     = $form->findElementByAttribute('name', 'p');
86*741b8a48SAndreas Gohr        $form->insertElement($pos + 1, $element);
87*741b8a48SAndreas Gohr    }
88*741b8a48SAndreas Gohr
89*741b8a48SAndreas Gohr}
90*741b8a48SAndreas Gohr
91*741b8a48SAndreas Gohr// vim:ts=4:sw=4:et: