xref: /dokuwiki/lib/plugins/authad/action.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1741b8a48SAndreas Gohr<?php
2*d4f83172SAndreas Gohr
38553d24dSAndreas Gohruse dokuwiki\Extension\ActionPlugin;
48553d24dSAndreas Gohruse dokuwiki\Extension\EventHandler;
58553d24dSAndreas Gohruse dokuwiki\Extension\Event;
6*d4f83172SAndreas Gohr
7741b8a48SAndreas Gohr/**
8741b8a48SAndreas Gohr * DokuWiki Plugin addomain (Action Component)
9741b8a48SAndreas Gohr *
10741b8a48SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11741b8a48SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
12741b8a48SAndreas Gohr */
13741b8a48SAndreas Gohr
14741b8a48SAndreas Gohr/**
15741b8a48SAndreas Gohr * Class action_plugin_addomain
16741b8a48SAndreas Gohr */
178553d24dSAndreas Gohrclass action_plugin_authad extends ActionPlugin
18a4337320SAndreas Gohr{
19741b8a48SAndreas Gohr    /**
20741b8a48SAndreas Gohr     * Registers a callback function for a given event
21741b8a48SAndreas Gohr     */
228553d24dSAndreas Gohr    public function register(EventHandler $controller)
23a4337320SAndreas Gohr    {
24a4337320SAndreas Gohr        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handleAuthLoginCheck');
25c6977b3aSSatoshi Sahara        $controller->register_hook('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handleFormLoginOutput');
26741b8a48SAndreas Gohr    }
27741b8a48SAndreas Gohr
28741b8a48SAndreas Gohr    /**
29741b8a48SAndreas Gohr     * Adds the selected domain as user postfix when attempting a login
30741b8a48SAndreas Gohr     *
31741b8a48SAndreas Gohr     * @param Doku_Event $event
32741b8a48SAndreas Gohr     * @param array      $param
33741b8a48SAndreas Gohr     */
348553d24dSAndreas Gohr    public function handleAuthLoginCheck(Event $event, $param)
35a4337320SAndreas Gohr    {
36741b8a48SAndreas Gohr        global $INPUT;
37741b8a48SAndreas Gohr
38741b8a48SAndreas Gohr        /** @var auth_plugin_authad $auth */
39741b8a48SAndreas Gohr        global $auth;
40741b8a48SAndreas Gohr        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
41741b8a48SAndreas Gohr
42741b8a48SAndreas Gohr        if ($INPUT->str('dom')) {
43741b8a48SAndreas Gohr            $usr = $auth->cleanUser($event->data['user']);
44a4337320SAndreas Gohr            $dom = $auth->getUserDomain($usr);
45741b8a48SAndreas Gohr            if (!$dom) {
46741b8a48SAndreas Gohr                $usr = "$usr@" . $INPUT->str('dom');
47741b8a48SAndreas Gohr            }
48741b8a48SAndreas Gohr            $INPUT->post->set('u', $usr);
49741b8a48SAndreas Gohr            $event->data['user'] = $usr;
50741b8a48SAndreas Gohr        }
51741b8a48SAndreas Gohr    }
52741b8a48SAndreas Gohr
53741b8a48SAndreas Gohr    /**
54741b8a48SAndreas Gohr     * Shows a domain selection in the login form when more than one domain is configured
55741b8a48SAndreas Gohr     *
56741b8a48SAndreas Gohr     * @param Doku_Event $event
57741b8a48SAndreas Gohr     * @param array      $param
58741b8a48SAndreas Gohr     */
598553d24dSAndreas Gohr    public function handleFormLoginOutput(Event $event, $param)
60a4337320SAndreas Gohr    {
61741b8a48SAndreas Gohr        global $INPUT;
62741b8a48SAndreas Gohr        /** @var auth_plugin_authad $auth */
63741b8a48SAndreas Gohr        global $auth;
64741b8a48SAndreas Gohr        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
65a4337320SAndreas Gohr        $domains = $auth->getConfiguredDomains();
66741b8a48SAndreas Gohr        if (count($domains) <= 1) return; // no choice at all
67741b8a48SAndreas Gohr
68bde2a644SSatoshi Sahara        /** @var dokuwiki\Form\Form $form */
69741b8a48SAndreas Gohr        $form =& $event->data;
70741b8a48SAndreas Gohr
71bde2a644SSatoshi Sahara        // find the username input box
72bde2a644SSatoshi Sahara        $pos = $form->findPositionByAttribute('name', 'u');
73bde2a644SSatoshi Sahara        if ($pos === false) return;
74bde2a644SSatoshi Sahara
75741b8a48SAndreas Gohr        // any default?
76741b8a48SAndreas Gohr        if ($INPUT->has('u')) {
77741b8a48SAndreas Gohr            $usr = $auth->cleanUser($INPUT->str('u'));
78a4337320SAndreas Gohr            $dom = $auth->getUserDomain($usr);
79741b8a48SAndreas Gohr
80741b8a48SAndreas Gohr            // update user field value
81741b8a48SAndreas Gohr            if ($dom) {
82a4337320SAndreas Gohr                $usr = $auth->getUserName($usr);
838549e2b5SSatoshi Sahara                $element = $form->getElementAt($pos);
848549e2b5SSatoshi Sahara                $element->val($usr);
85741b8a48SAndreas Gohr            }
868549e2b5SSatoshi Sahara        }
87741b8a48SAndreas Gohr
88903616edSSatoshi Sahara        // add locate domain selector just after the username input box
89903616edSSatoshi Sahara        $element = $form->addDropdown('dom', $domains, $this->getLang('domain'), $pos + 1);
908549e2b5SSatoshi Sahara        $element->addClass('block');
91741b8a48SAndreas Gohr    }
928549e2b5SSatoshi Sahara}
93741b8a48SAndreas Gohr
94741b8a48SAndreas Gohr// vim:ts=4:sw=4:et:
95