xref: /dokuwiki/lib/plugins/authad/action.php (revision 8553d24d33ab5f260c6e19959de764dd8472d438)
1741b8a48SAndreas Gohr<?php
2*8553d24dSAndreas Gohruse dokuwiki\Extension\ActionPlugin;
3*8553d24dSAndreas Gohruse dokuwiki\Extension\EventHandler;
4*8553d24dSAndreas Gohruse dokuwiki\Extension\Event;
5741b8a48SAndreas Gohr/**
6741b8a48SAndreas Gohr * DokuWiki Plugin addomain (Action Component)
7741b8a48SAndreas Gohr *
8741b8a48SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9741b8a48SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
10741b8a48SAndreas Gohr */
11741b8a48SAndreas Gohr
12741b8a48SAndreas Gohr/**
13741b8a48SAndreas Gohr * Class action_plugin_addomain
14741b8a48SAndreas Gohr */
15*8553d24dSAndreas Gohrclass action_plugin_authad extends ActionPlugin
16a4337320SAndreas Gohr{
17741b8a48SAndreas Gohr
18741b8a48SAndreas Gohr    /**
19741b8a48SAndreas Gohr     * Registers a callback function for a given event
20741b8a48SAndreas Gohr     */
21*8553d24dSAndreas Gohr    public function register(EventHandler $controller)
22a4337320SAndreas Gohr    {
23a4337320SAndreas Gohr        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handleAuthLoginCheck');
24c6977b3aSSatoshi Sahara        $controller->register_hook('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handleFormLoginOutput');
25741b8a48SAndreas Gohr    }
26741b8a48SAndreas Gohr
27741b8a48SAndreas Gohr    /**
28741b8a48SAndreas Gohr     * Adds the selected domain as user postfix when attempting a login
29741b8a48SAndreas Gohr     *
30741b8a48SAndreas Gohr     * @param Doku_Event $event
31741b8a48SAndreas Gohr     * @param array      $param
32741b8a48SAndreas Gohr     */
33*8553d24dSAndreas Gohr    public function handleAuthLoginCheck(Event $event, $param)
34a4337320SAndreas Gohr    {
35741b8a48SAndreas Gohr        global $INPUT;
36741b8a48SAndreas Gohr
37741b8a48SAndreas Gohr        /** @var auth_plugin_authad $auth */
38741b8a48SAndreas Gohr        global $auth;
39741b8a48SAndreas Gohr        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
40741b8a48SAndreas Gohr
41741b8a48SAndreas Gohr        if ($INPUT->str('dom')) {
42741b8a48SAndreas Gohr            $usr = $auth->cleanUser($event->data['user']);
43a4337320SAndreas Gohr            $dom = $auth->getUserDomain($usr);
44741b8a48SAndreas Gohr            if (!$dom) {
45741b8a48SAndreas Gohr                $usr = "$usr@".$INPUT->str('dom');
46741b8a48SAndreas Gohr            }
47741b8a48SAndreas Gohr            $INPUT->post->set('u', $usr);
48741b8a48SAndreas Gohr            $event->data['user'] = $usr;
49741b8a48SAndreas Gohr        }
50741b8a48SAndreas Gohr    }
51741b8a48SAndreas Gohr
52741b8a48SAndreas Gohr    /**
53741b8a48SAndreas Gohr     * Shows a domain selection in the login form when more than one domain is configured
54741b8a48SAndreas Gohr     *
55741b8a48SAndreas Gohr     * @param Doku_Event $event
56741b8a48SAndreas Gohr     * @param array      $param
57741b8a48SAndreas Gohr     */
58*8553d24dSAndreas Gohr    public function handleFormLoginOutput(Event $event, $param)
59a4337320SAndreas Gohr    {
60741b8a48SAndreas Gohr        global $INPUT;
61741b8a48SAndreas Gohr        /** @var auth_plugin_authad $auth */
62741b8a48SAndreas Gohr        global $auth;
63741b8a48SAndreas Gohr        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
64a4337320SAndreas Gohr        $domains = $auth->getConfiguredDomains();
65741b8a48SAndreas Gohr        if (count($domains) <= 1) return; // no choice at all
66741b8a48SAndreas Gohr
67bde2a644SSatoshi Sahara        /** @var dokuwiki\Form\Form $form */
68741b8a48SAndreas Gohr        $form =& $event->data;
69741b8a48SAndreas Gohr
70bde2a644SSatoshi Sahara        // find the username input box
71bde2a644SSatoshi Sahara        $pos = $form->findPositionByAttribute('name', 'u');
72bde2a644SSatoshi Sahara        if ($pos === false) return;
73bde2a644SSatoshi Sahara
74741b8a48SAndreas Gohr        // any default?
75741b8a48SAndreas Gohr        if ($INPUT->has('u')) {
76741b8a48SAndreas Gohr            $usr = $auth->cleanUser($INPUT->str('u'));
77a4337320SAndreas Gohr            $dom = $auth->getUserDomain($usr);
78741b8a48SAndreas Gohr
79741b8a48SAndreas Gohr            // update user field value
80741b8a48SAndreas Gohr            if ($dom) {
81a4337320SAndreas Gohr                $usr = $auth->getUserName($usr);
828549e2b5SSatoshi Sahara                $element = $form->getElementAt($pos);
838549e2b5SSatoshi Sahara                $element->val($usr);
84741b8a48SAndreas Gohr            }
858549e2b5SSatoshi Sahara        }
86741b8a48SAndreas Gohr
87903616edSSatoshi Sahara        // add locate domain selector just after the username input box
88903616edSSatoshi Sahara        $element = $form->addDropdown('dom', $domains, $this->getLang('domain'), $pos +1);
898549e2b5SSatoshi Sahara        $element->addClass('block');
90741b8a48SAndreas Gohr    }
918549e2b5SSatoshi Sahara}
92741b8a48SAndreas Gohr
93741b8a48SAndreas Gohr// vim:ts=4:sw=4:et:
94