xref: /plugin/oauth/action/user.php (revision 74b4d4a4cf1d79813740d8ba18696e5fb2b4089b)
1<?php
2
3/**
4 * DokuWiki Plugin oauth (Action Component)
5 *
6 * This manages profile changes and allows the user to change their oauth groups.
7 * We use group memberships to define if logins are okay with the given services.
8 *
9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10 * @author  Andreas Gohr <andi@splitbrain.org>
11 */
12class action_plugin_oauth_user extends DokuWiki_Action_Plugin
13{
14    /** @var helper_plugin_oauth */
15    protected $hlp;
16
17    /**
18     * Constructor
19     *
20     * Initializes the helper
21     */
22    public function __construct()
23    {
24        $this->hlp = plugin_load('helper', 'oauth');
25    }
26
27    /**
28     * Registers a callback function for a given event
29     *
30     * @param Doku_Event_Handler $controller DokuWiki's event controller object
31     * @return void
32     */
33    public function register(Doku_Event_Handler $controller)
34    {
35        global $conf;
36        if ($conf['authtype'] != 'oauth') return;
37
38        $conf['profileconfirm'] = false; // password confirmation doesn't work with oauth only users
39
40        $controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'BEFORE', $this, 'handleProfileform');
41        $controller->register_hook('AUTH_USER_CHANGE', 'BEFORE', $this, 'handleUsermod');
42    }
43    /**
44     * Save groups for all the services a user has enabled
45     *
46     * @param Doku_Event $event event object by reference
47     * @return void
48     */
49    public function handleUsermod(Doku_Event $event)
50    {
51        global $ACT;
52        global $USERINFO;
53        global $auth;
54        global $INPUT;
55
56        if ($event->data['type'] != 'modify') return;
57        if ($ACT != 'profile') return;
58
59        // we want to modify the user's groups
60        $groups = $USERINFO['grps']; //current groups
61        if (isset($event->data['params'][1]['grps'])) {
62            // something already defined new groups
63            $groups = $event->data['params'][1]['grps'];
64        }
65
66        // get enabled and configured services
67        $enabled = $INPUT->arr('oauth_group');
68        $services = array_keys($this->hlp->listServices());
69        $services = array_map([$auth, 'cleanGroup'], $services);
70
71        // add all enabled services as group, remove all disabled services
72        foreach ($services as $service) {
73            if (isset($enabled[$service])) {
74                $groups[] = $service;
75            } else {
76                $idx = array_search($service, $groups);
77                if ($idx !== false) unset($groups[$idx]);
78            }
79        }
80        $groups = array_unique($groups);
81
82        // add new group array to event data
83        $event->data['params'][1]['grps'] = $groups;
84    }
85
86    /**
87     * Add service selection to user profile
88     *
89     * @param Doku_Event $event event object by reference
90     * @return void
91     */
92    public function handleProfileform(Doku_Event $event)
93    {
94        global $USERINFO;
95        /** @var auth_plugin_authplain $auth */
96        global $auth;
97
98        /** @var Doku_Form $form */
99        $form = $event->data;
100        $pos = $form->findElementByAttribute('type', 'submit');
101
102        $services = $this->hlp->listServices();
103        if (!$services) return;
104
105        $form->insertElement($pos, form_closefieldset());
106        $form->insertElement(
107            ++$pos,
108            form_openfieldset(['_legend' => $this->getLang('loginwith'), 'class' => 'plugin_oauth'])
109        );
110        foreach ($services as $service) {
111            $group = $auth->cleanGroup($service->getServiceID());
112            $elem = form_makeCheckboxField(
113                'oauth_group[' . $group . ']',
114                1, $service->getServiceLabel(), '', 'simple',
115                [
116                    'checked' => (in_array($group, $USERINFO['grps'])) ? 'checked' : '',
117                ]
118            );
119
120            $form->insertElement(++$pos, $elem);
121        }
122        $form->insertElement(++$pos, form_closefieldset());
123        $form->insertElement(++$pos, form_openfieldset([]));
124    }
125}
126