xref: /dokuwiki/lib/plugins/authad/action.php (revision c6977b3a35d43dd2a2c9271648980059f410f648)
1741b8a48SAndreas Gohr<?php
2741b8a48SAndreas Gohr/**
3741b8a48SAndreas Gohr * DokuWiki Plugin addomain (Action Component)
4741b8a48SAndreas Gohr *
5741b8a48SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6741b8a48SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
7741b8a48SAndreas Gohr */
8741b8a48SAndreas Gohr
9741b8a48SAndreas Gohr/**
10741b8a48SAndreas Gohr * Class action_plugin_addomain
11741b8a48SAndreas Gohr */
12a4337320SAndreas Gohrclass action_plugin_authad extends DokuWiki_Action_Plugin
13a4337320SAndreas Gohr{
14741b8a48SAndreas Gohr
15741b8a48SAndreas Gohr    /**
16741b8a48SAndreas Gohr     * Registers a callback function for a given event
17741b8a48SAndreas Gohr     */
18a4337320SAndreas Gohr    public function register(Doku_Event_Handler $controller)
19a4337320SAndreas Gohr    {
20a4337320SAndreas Gohr        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'handleAuthLoginCheck');
21*c6977b3aSSatoshi Sahara        $controller->register_hook('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handleFormLoginOutput');
22741b8a48SAndreas Gohr    }
23741b8a48SAndreas Gohr
24741b8a48SAndreas Gohr    /**
25741b8a48SAndreas Gohr     * Adds the selected domain as user postfix when attempting a login
26741b8a48SAndreas Gohr     *
27741b8a48SAndreas Gohr     * @param Doku_Event $event
28741b8a48SAndreas Gohr     * @param array      $param
29741b8a48SAndreas Gohr     */
30a4337320SAndreas Gohr    public function handleAuthLoginCheck(Doku_Event $event, $param)
31a4337320SAndreas Gohr    {
32741b8a48SAndreas Gohr        global $INPUT;
33741b8a48SAndreas Gohr
34741b8a48SAndreas Gohr        /** @var auth_plugin_authad $auth */
35741b8a48SAndreas Gohr        global $auth;
36741b8a48SAndreas Gohr        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
37741b8a48SAndreas Gohr
38741b8a48SAndreas Gohr        if ($INPUT->str('dom')) {
39741b8a48SAndreas Gohr            $usr = $auth->cleanUser($event->data['user']);
40a4337320SAndreas Gohr            $dom = $auth->getUserDomain($usr);
41741b8a48SAndreas Gohr            if (!$dom) {
42741b8a48SAndreas Gohr                $usr = "$usr@".$INPUT->str('dom');
43741b8a48SAndreas Gohr            }
44741b8a48SAndreas Gohr            $INPUT->post->set('u', $usr);
45741b8a48SAndreas Gohr            $event->data['user'] = $usr;
46741b8a48SAndreas Gohr        }
47741b8a48SAndreas Gohr    }
48741b8a48SAndreas Gohr
49741b8a48SAndreas Gohr    /**
50741b8a48SAndreas Gohr     * Shows a domain selection in the login form when more than one domain is configured
51741b8a48SAndreas Gohr     *
52741b8a48SAndreas Gohr     * @param Doku_Event $event
53741b8a48SAndreas Gohr     * @param array      $param
54741b8a48SAndreas Gohr     */
55*c6977b3aSSatoshi Sahara    public function handleFormLoginOutput(Doku_Event $event, $param)
56a4337320SAndreas Gohr    {
57741b8a48SAndreas Gohr        global $INPUT;
58741b8a48SAndreas Gohr        /** @var auth_plugin_authad $auth */
59741b8a48SAndreas Gohr        global $auth;
60741b8a48SAndreas Gohr        if (!is_a($auth, 'auth_plugin_authad')) return; // AD not even used
61a4337320SAndreas Gohr        $domains = $auth->getConfiguredDomains();
62741b8a48SAndreas Gohr        if (count($domains) <= 1) return; // no choice at all
63741b8a48SAndreas Gohr
64bde2a644SSatoshi Sahara        /** @var dokuwiki\Form\Form $form */
65741b8a48SAndreas Gohr        $form =& $event->data;
66741b8a48SAndreas Gohr
67bde2a644SSatoshi Sahara        // find the username input box
68bde2a644SSatoshi Sahara        $pos = $form->findPositionByAttribute('name', 'u');
69bde2a644SSatoshi Sahara        if ($pos === false) return;
70bde2a644SSatoshi Sahara
71741b8a48SAndreas Gohr        // any default?
72741b8a48SAndreas Gohr        if ($INPUT->has('u')) {
73741b8a48SAndreas Gohr            $usr = $auth->cleanUser($INPUT->str('u'));
74a4337320SAndreas Gohr            $dom = $auth->getUserDomain($usr);
75741b8a48SAndreas Gohr
76741b8a48SAndreas Gohr            // update user field value
77741b8a48SAndreas Gohr            if ($dom) {
78a4337320SAndreas Gohr                $usr = $auth->getUserName($usr);
798549e2b5SSatoshi Sahara                $element = $form->getElementAt($pos);
808549e2b5SSatoshi Sahara                $element->val($usr);
81741b8a48SAndreas Gohr            }
828549e2b5SSatoshi Sahara        }
83741b8a48SAndreas Gohr
84903616edSSatoshi Sahara        // add locate domain selector just after the username input box
85903616edSSatoshi Sahara        $element = $form->addDropdown('dom', $domains, $this->getLang('domain'), $pos +1);
868549e2b5SSatoshi Sahara        $element->addClass('block');
87741b8a48SAndreas Gohr    }
888549e2b5SSatoshi Sahara}
89741b8a48SAndreas Gohr
90741b8a48SAndreas Gohr// vim:ts=4:sw=4:et:
91