xref: /plugin/oauth/auth.php (revision 3e7ac5b1c2847ba6fd6e53113e84776ead1a1378)
180852c15SAndreas Gohr<?php
2*3e7ac5b1SAndreas Gohr
380852c15SAndreas Gohr/**
480852c15SAndreas Gohr * DokuWiki Plugin oauth (Auth Component)
580852c15SAndreas Gohr *
680852c15SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
780852c15SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
880852c15SAndreas Gohr */
9*3e7ac5b1SAndreas Gohrclass auth_plugin_oauth extends auth_plugin_authplain
10*3e7ac5b1SAndreas Gohr{
1180852c15SAndreas Gohr
12*3e7ac5b1SAndreas Gohr    /** @inheritDoc */
13*3e7ac5b1SAndreas Gohr    public function __construct()
14*3e7ac5b1SAndreas Gohr    {
15f10e09e2SAndreas Gohr        parent::__construct();
1680852c15SAndreas Gohr
17f10e09e2SAndreas Gohr        $this->cando['external'] = true;
1880852c15SAndreas Gohr    }
1980852c15SAndreas Gohr
20*3e7ac5b1SAndreas Gohr    private function handleState($state)
21*3e7ac5b1SAndreas Gohr    {
22438dcc52SMichael Grosse        /** @var \helper_plugin_farmer $farmer */
23438dcc52SMichael Grosse        $farmer = plugin_load('helper', 'farmer', false, true);
24438dcc52SMichael Grosse        $data = json_decode(base64_decode(urldecode($state)));
25438dcc52SMichael Grosse        if (empty($data->animal) || $farmer->getAnimal() == $data->animal) {
26438dcc52SMichael Grosse            return;
27438dcc52SMichael Grosse        }
28438dcc52SMichael Grosse        $animal = $data->animal;
29438dcc52SMichael Grosse        $allAnimals = $farmer->getAllAnimals();
30438dcc52SMichael Grosse        if (!in_array($animal, $allAnimals)) {
31438dcc52SMichael Grosse            msg('Animal ' . $animal . ' does not exist!');
32438dcc52SMichael Grosse            return;
33438dcc52SMichael Grosse        }
34438dcc52SMichael Grosse        global $INPUT;
35518d0e6eSMichael Große        $url = $farmer->getAnimalURL($animal) . '/doku.php?' . $INPUT->server->str('QUERY_STRING');
36438dcc52SMichael Grosse        send_redirect($url);
37438dcc52SMichael Grosse    }
38438dcc52SMichael Grosse
39*3e7ac5b1SAndreas Gohr    /** @inheritDoc */
40*3e7ac5b1SAndreas Gohr    function trustExternal($user, $pass, $sticky = false)
41*3e7ac5b1SAndreas Gohr    {
42438dcc52SMichael Grosse        global $USERINFO, $INPUT;
43438dcc52SMichael Grosse
44438dcc52SMichael Grosse        if ($INPUT->has('state') && plugin_load('helper', 'farmer', false, true)) {
45438dcc52SMichael Grosse            $this->handleState($INPUT->str('state'));
46438dcc52SMichael Grosse        }
4780852c15SAndreas Gohr
48a7a8f46aSAndreas Gohr        // check session for existing oAuth login data
49a7a8f46aSAndreas Gohr        $session = $_SESSION[DOKU_COOKIE]['auth'];
50523e6571SMichael Große        if (isset($session['oauth'])) {
51a7a8f46aSAndreas Gohr            $servicename = $session['oauth'];
52a7a8f46aSAndreas Gohr            // check if session data is still considered valid
53f2e164b0SMichael Große            if ($this->isSessionValid($session)) {
54a7a8f46aSAndreas Gohr                $_SERVER['REMOTE_USER'] = $session['user'];
55a7a8f46aSAndreas Gohr                $USERINFO = $session['info'];
5680852c15SAndreas Gohr                return true;
57f10e09e2SAndreas Gohr            }
5880852c15SAndreas Gohr        }
5980852c15SAndreas Gohr
60523e6571SMichael Große        $existingLoginProcess = false;
61523e6571SMichael Große        // are we in login progress?
62523e6571SMichael Große        if (isset($_SESSION[DOKU_COOKIE]['oauth-inprogress'])) {
63523e6571SMichael Große            $servicename = $_SESSION[DOKU_COOKIE]['oauth-inprogress']['service'];
64523e6571SMichael Große            $page = $_SESSION[DOKU_COOKIE]['oauth-inprogress']['id'];
65188ba446SMichael Große            $params = $_SESSION[DOKU_COOKIE]['oauth-inprogress']['params'];
66523e6571SMichael Große
67523e6571SMichael Große            unset($_SESSION[DOKU_COOKIE]['oauth-inprogress']);
68523e6571SMichael Große            $existingLoginProcess = true;
69523e6571SMichael Große        }
70523e6571SMichael Große
71a7a8f46aSAndreas Gohr        // either we're in oauth login or a previous log needs to be rechecked
722e94f0b8SAndreas Gohr        if (isset($servicename)) {
73a7a8f46aSAndreas Gohr            /** @var helper_plugin_oauth $hlp */
74a7a8f46aSAndreas Gohr            $hlp = plugin_load('helper', 'oauth');
75827232fcSMichael Große
76827232fcSMichael Große            /** @var OAuth\Plugin\AbstractAdapter $service */
77a7a8f46aSAndreas Gohr            $service = $hlp->loadService($servicename);
78523e6571SMichael Große            if (is_null($service)) {
79523e6571SMichael Große                $this->cleanLogout();
80523e6571SMichael Große                return false;
81a7a8f46aSAndreas Gohr            }
82a7a8f46aSAndreas Gohr
83523e6571SMichael Große            if ($service->checkToken()) {
84188ba446SMichael Große                $ok = $this->processLogin($sticky, $service, $servicename, $page, $params);
85523e6571SMichael Große                if (!$ok) {
86523e6571SMichael Große                    $this->cleanLogout();
87523e6571SMichael Große                    return false;
88523e6571SMichael Große                }
89523e6571SMichael Große                return true;
90523e6571SMichael Große            } else {
91523e6571SMichael Große                if ($existingLoginProcess) {
92523e6571SMichael Große                    msg($this->getLang('oauth login failed'), 0);
93523e6571SMichael Große                    $this->cleanLogout();
94523e6571SMichael Große                    return false;
95523e6571SMichael Große                } else {
96523e6571SMichael Große                    // first time here
97523e6571SMichael Große                    $this->relogin($servicename);
98523e6571SMichael Große                }
99523e6571SMichael Große            }
100523e6571SMichael Große
101523e6571SMichael Große            $this->cleanLogout();
102a7a8f46aSAndreas Gohr            return false; // something went wrong during oAuth login
103213f4618SMichael Große        } elseif (isset($_COOKIE[DOKU_COOKIE])) {
104213f4618SMichael Große            global $INPUT;
105213f4618SMichael Große            //try cookie
106213f4618SMichael Große            list($cookieuser, $cookiesticky, $auth, $servicename) = explode('|', $_COOKIE[DOKU_COOKIE]);
107213f4618SMichael Große            $cookieuser = base64_decode($cookieuser, true);
108213f4618SMichael Große            $auth = base64_decode($auth, true);
109213f4618SMichael Große            $servicename = base64_decode($servicename, true);
110213f4618SMichael Große            if ($auth === 'oauth') {
111213f4618SMichael Große                $this->relogin($servicename);
112213f4618SMichael Große            }
11380852c15SAndreas Gohr        }
11480852c15SAndreas Gohr
115a7a8f46aSAndreas Gohr        // do the "normal" plain auth login via form
116a7a8f46aSAndreas Gohr        return auth_login($user, $pass, $sticky);
117a7a8f46aSAndreas Gohr    }
11880852c15SAndreas Gohr
119f2e164b0SMichael Große    /**
120f2e164b0SMichael Große     * @param array $session cookie auth session
121f2e164b0SMichael Große     *
122f2e164b0SMichael Große     * @return bool
123f2e164b0SMichael Große     */
124*3e7ac5b1SAndreas Gohr    protected function isSessionValid($session)
125*3e7ac5b1SAndreas Gohr    {
126f2e164b0SMichael Große        /** @var helper_plugin_oauth $hlp */
127f2e164b0SMichael Große        $hlp = plugin_load('helper', 'oauth');
128f2e164b0SMichael Große        if ($hlp->validBrowserID($session)) {
129f2e164b0SMichael Große            if (!$hlp->isSessionTimedOut($session)) {
130f2e164b0SMichael Große                return true;
131f2e164b0SMichael Große            } elseif (!($hlp->isGETRequest() && $hlp->isDokuPHP())) {
132f2e164b0SMichael Große                // only force a recheck on a timed-out session during a GET request on the main script doku.php
133f2e164b0SMichael Große                return true;
134f2e164b0SMichael Große            }
135f2e164b0SMichael Große        }
136f2e164b0SMichael Große        return false;
137f2e164b0SMichael Große    }
138f2e164b0SMichael Große
139*3e7ac5b1SAndreas Gohr    protected function relogin($servicename)
140*3e7ac5b1SAndreas Gohr    {
141213f4618SMichael Große        global $INPUT;
142213f4618SMichael Große
143213f4618SMichael Große        /** @var helper_plugin_oauth $hlp */
144213f4618SMichael Große        $hlp = plugin_load('helper', 'oauth');
145213f4618SMichael Große        $service = $hlp->loadService($servicename);
146213f4618SMichael Große        if (is_null($service)) return false;
147213f4618SMichael Große
148213f4618SMichael Große        // remember service in session
149213f4618SMichael Große        session_start();
150213f4618SMichael Große        $_SESSION[DOKU_COOKIE]['oauth-inprogress']['service'] = $servicename;
151213f4618SMichael Große        $_SESSION[DOKU_COOKIE]['oauth-inprogress']['id'] = $INPUT->str('id');
152188ba446SMichael Große        $_SESSION[DOKU_COOKIE]['oauth-inprogress']['params'] = $_GET;
153213f4618SMichael Große
15409623faaSMichael Große        $_SESSION[DOKU_COOKIE]['oauth-done']['$_REQUEST'] = $_REQUEST;
155213f4618SMichael Große
156213f4618SMichael Große        if (is_array($INPUT->post->param('do'))) {
157213f4618SMichael Große            $doPost = key($INPUT->post->arr('do'));
158213f4618SMichael Große        } else {
159213f4618SMichael Große            $doPost = $INPUT->post->str('do');
160213f4618SMichael Große        }
161213f4618SMichael Große        $doGet = $INPUT->get->str('do');
162213f4618SMichael Große        if (!empty($doPost)) {
163213f4618SMichael Große            $_SESSION[DOKU_COOKIE]['oauth-done']['do'] = $doPost;
164213f4618SMichael Große        } elseif (!empty($doGet)) {
165213f4618SMichael Große            $_SESSION[DOKU_COOKIE]['oauth-done']['do'] = $doGet;
166213f4618SMichael Große        }
167213f4618SMichael Große
168213f4618SMichael Große        session_write_close();
169213f4618SMichael Große
170213f4618SMichael Große        $service->login();
171213f4618SMichael Große    }
172213f4618SMichael Große
173a7a8f46aSAndreas Gohr    /**
174b2b9fbc7SMichael Große     * @param                              $sticky
175b2b9fbc7SMichael Große     * @param OAuth\Plugin\AbstractAdapter $service
1769928f5efSMichael Große     * @param string $servicename
177b2b9fbc7SMichael Große     * @param string $page
178188ba446SMichael Große     * @param array $params
179f07c7607SMichael Große     *
180f07c7607SMichael Große     * @return bool
181f07c7607SMichael Große     */
182*3e7ac5b1SAndreas Gohr    protected function processLogin($sticky, $service, $servicename, $page, $params = array())
183*3e7ac5b1SAndreas Gohr    {
184b2b9fbc7SMichael Große        $uinfo = $service->getUser();
185b2b9fbc7SMichael Große        $ok = $this->processUser($uinfo, $servicename);
186f07c7607SMichael Große        if (!$ok) {
187f07c7607SMichael Große            return false;
188f07c7607SMichael Große        }
189b2b9fbc7SMichael Große        $this->setUserSession($uinfo, $servicename);
190b2b9fbc7SMichael Große        $this->setUserCookie($uinfo['user'], $sticky, $servicename);
191b2b9fbc7SMichael Große        if (isset($page)) {
192188ba446SMichael Große            if (!empty($params['id'])) unset($params['id']);
193188ba446SMichael Große            send_redirect(wl($page, $params, false, '&'));
194b2b9fbc7SMichael Große        }
195f07c7607SMichael Große        return true;
196f07c7607SMichael Große    }
197f07c7607SMichael Große
1989928f5efSMichael Große    /**
1999928f5efSMichael Große     * process the user and update the $uinfo array
2009928f5efSMichael Große     *
2019928f5efSMichael Große     * @param $uinfo
2029928f5efSMichael Große     * @param $servicename
2039928f5efSMichael Große     *
2049928f5efSMichael Große     * @return bool
2059928f5efSMichael Große     */
206*3e7ac5b1SAndreas Gohr    protected function processUser(&$uinfo, $servicename)
207*3e7ac5b1SAndreas Gohr    {
2089928f5efSMichael Große        $uinfo['user'] = $this->cleanUser((string)$uinfo['user']);
2099928f5efSMichael Große        if (!$uinfo['name']) $uinfo['name'] = $uinfo['user'];
2109928f5efSMichael Große
2119928f5efSMichael Große        if (!$uinfo['user'] || !$uinfo['mail']) {
2129928f5efSMichael Große            msg("$servicename did not provide the needed user info. Can't log you in", -1);
2139928f5efSMichael Große            return false;
2149928f5efSMichael Große        }
2159928f5efSMichael Große
2169928f5efSMichael Große        // see if the user is known already
2179928f5efSMichael Große        $user = $this->getUserByEmail($uinfo['mail']);
2189928f5efSMichael Große        if ($user) {
2199928f5efSMichael Große            $sinfo = $this->getUserData($user);
2209928f5efSMichael Große            // check if the user allowed access via this service
2219928f5efSMichael Große            if (!in_array($this->cleanGroup($servicename), $sinfo['grps'])) {
2229928f5efSMichael Große                msg(sprintf($this->getLang('authnotenabled'), $servicename), -1);
2239928f5efSMichael Große                return false;
2249928f5efSMichael Große            }
2259928f5efSMichael Große            $uinfo['user'] = $user;
2269928f5efSMichael Große            $uinfo['name'] = $sinfo['name'];
2279928f5efSMichael Große            $uinfo['grps'] = array_merge((array)$uinfo['grps'], $sinfo['grps']);
228d313403cSAnna Dabrowska        } elseif (actionOK('register') || $this->getConf('register-on-auth')) {
2299928f5efSMichael Große            $ok = $this->addUser($uinfo, $servicename);
2309928f5efSMichael Große            if (!$ok) {
2319928f5efSMichael Große                msg('something went wrong creating your user account. please try again later.', -1);
2329928f5efSMichael Große                return false;
2339928f5efSMichael Große            }
2349928f5efSMichael Große        } else {
2359928f5efSMichael Große            msg($this->getLang('addUser not possible'), -1);
2369928f5efSMichael Große            return false;
2379928f5efSMichael Große        }
2389928f5efSMichael Große        return true;
2399928f5efSMichael Große    }
2409928f5efSMichael Große
2419928f5efSMichael Große    /**
242b2b9fbc7SMichael Große     * new user, create him - making sure the login is unique by adding a number if needed
243b2b9fbc7SMichael Große     *
244b2b9fbc7SMichael Große     * @param array $uinfo user info received from the oAuth service
245b2b9fbc7SMichael Große     * @param string $servicename
246b2b9fbc7SMichael Große     *
247b2b9fbc7SMichael Große     * @return bool
248b2b9fbc7SMichael Große     */
249*3e7ac5b1SAndreas Gohr    protected function addUser(&$uinfo, $servicename)
250*3e7ac5b1SAndreas Gohr    {
251b2b9fbc7SMichael Große        global $conf;
252b2b9fbc7SMichael Große        $user = $uinfo['user'];
253b2b9fbc7SMichael Große        $count = '';
254b2b9fbc7SMichael Große        while ($this->getUserData($user . $count)) {
255b2b9fbc7SMichael Große            if ($count) {
256b2b9fbc7SMichael Große                $count++;
257b2b9fbc7SMichael Große            } else {
258b2b9fbc7SMichael Große                $count = 1;
259b2b9fbc7SMichael Große            }
260b2b9fbc7SMichael Große        }
261b2b9fbc7SMichael Große        $user = $user . $count;
262b2b9fbc7SMichael Große        $uinfo['user'] = $user;
263b2b9fbc7SMichael Große        $groups_on_creation = array();
264b2b9fbc7SMichael Große        $groups_on_creation[] = $conf['defaultgroup'];
265b2b9fbc7SMichael Große        $groups_on_creation[] = $this->cleanGroup($servicename); // add service as group
266b2b9fbc7SMichael Große        $uinfo['grps'] = array_merge((array)$uinfo['grps'], $groups_on_creation);
267b2b9fbc7SMichael Große
268b2b9fbc7SMichael Große        $ok = $this->triggerUserMod(
269b2b9fbc7SMichael Große            'create',
270b2b9fbc7SMichael Große            array($user, auth_pwgen($user), $uinfo['name'], $uinfo['mail'], $groups_on_creation,)
271b2b9fbc7SMichael Große        );
272b2b9fbc7SMichael Große        if (!$ok) {
273b2b9fbc7SMichael Große            return false;
274b2b9fbc7SMichael Große        }
275b2b9fbc7SMichael Große
276b2b9fbc7SMichael Große        // send notification about the new user
277b2b9fbc7SMichael Große        $subscription = new Subscription();
278b2b9fbc7SMichael Große        $subscription->send_register($user, $uinfo['name'], $uinfo['mail']);
279b2b9fbc7SMichael Große        return true;
280b2b9fbc7SMichael Große    }
281b2b9fbc7SMichael Große
282b2b9fbc7SMichael Große    /**
283b2b9fbc7SMichael Große     * Find a user by his email address
284b2b9fbc7SMichael Große     *
285b2b9fbc7SMichael Große     * @param $mail
286b2b9fbc7SMichael Große     * @return bool|string
287b2b9fbc7SMichael Große     */
288*3e7ac5b1SAndreas Gohr    protected function getUserByEmail($mail)
289*3e7ac5b1SAndreas Gohr    {
2908b214edcSAndreas Gohr        if ($this->users === null) {
2918b214edcSAndreas Gohr            if (is_callable([$this, '_loadUserData'])) {
2928b214edcSAndreas Gohr                $this->_loadUserData();
2938b214edcSAndreas Gohr            } else {
2948b214edcSAndreas Gohr                $this->loadUserData();
2958b214edcSAndreas Gohr            }
2968b214edcSAndreas Gohr        }
297b2b9fbc7SMichael Große        $mail = strtolower($mail);
298b2b9fbc7SMichael Große
299b2b9fbc7SMichael Große        foreach ($this->users as $user => $uinfo) {
300b2b9fbc7SMichael Große            if (strtolower($uinfo['mail']) == $mail) return $user;
301b2b9fbc7SMichael Große        }
302b2b9fbc7SMichael Große
303b2b9fbc7SMichael Große        return false;
304b2b9fbc7SMichael Große    }
305b2b9fbc7SMichael Große
306b2b9fbc7SMichael Große    /**
307b2b9fbc7SMichael Große     * @param array $data
308b2b9fbc7SMichael Große     * @param string $service
309b2b9fbc7SMichael Große     */
310*3e7ac5b1SAndreas Gohr    protected function setUserSession($data, $service)
311*3e7ac5b1SAndreas Gohr    {
312b2b9fbc7SMichael Große        global $USERINFO;
313b2b9fbc7SMichael Große        global $conf;
314b2b9fbc7SMichael Große
315b2b9fbc7SMichael Große        // set up groups
316b2b9fbc7SMichael Große        if (!is_array($data['grps'])) {
317b2b9fbc7SMichael Große            $data['grps'] = array();
318b2b9fbc7SMichael Große        }
319b2b9fbc7SMichael Große        $data['grps'][] = $this->cleanGroup($service);
320b2b9fbc7SMichael Große        $data['grps'] = array_unique($data['grps']);
321b2b9fbc7SMichael Große
322b2b9fbc7SMichael Große        $USERINFO = $data;
323b2b9fbc7SMichael Große        $_SERVER['REMOTE_USER'] = $data['user'];
324b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['user'] = $data['user'];
325b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['pass'] = $data['pass'];
326b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
327b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid();
328b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['time'] = time();
329b2b9fbc7SMichael Große        $_SESSION[DOKU_COOKIE]['auth']['oauth'] = $service;
330b2b9fbc7SMichael Große    }
331b2b9fbc7SMichael Große
332b2b9fbc7SMichael Große    /**
3339928f5efSMichael Große     * @param string $user
334523e6571SMichael Große     * @param bool $sticky
3359928f5efSMichael Große     * @param string $servicename
336523e6571SMichael Große     * @param int $validityPeriodInSeconds optional, per default 1 Year
3379928f5efSMichael Große     */
338*3e7ac5b1SAndreas Gohr    private function setUserCookie($user, $sticky, $servicename, $validityPeriodInSeconds = 31536000)
339*3e7ac5b1SAndreas Gohr    {
3409928f5efSMichael Große        $cookie = base64_encode($user) . '|' . ((int)$sticky) . '|' . base64_encode('oauth') . '|' . base64_encode($servicename);
3419928f5efSMichael Große        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
342523e6571SMichael Große        $time = $sticky ? (time() + $validityPeriodInSeconds) : 0;
3439928f5efSMichael Große        setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true);
3449928f5efSMichael Große    }
3459928f5efSMichael Große
346827232fcSMichael Große    /**
347b2b9fbc7SMichael Große     * Unset additional stuff in session on logout
348827232fcSMichael Große     */
349*3e7ac5b1SAndreas Gohr    public function logOff()
350*3e7ac5b1SAndreas Gohr    {
351b2b9fbc7SMichael Große        parent::logOff();
352b2b9fbc7SMichael Große
353af2a4e8fSMichael Große        $this->cleanLogout();
354b2b9fbc7SMichael Große    }
355b2b9fbc7SMichael Große
356b2b9fbc7SMichael Große    /**
357b2b9fbc7SMichael Große     * unset auth cookies and session information
358b2b9fbc7SMichael Große     */
359*3e7ac5b1SAndreas Gohr    private function cleanLogout()
360*3e7ac5b1SAndreas Gohr    {
361af2a4e8fSMichael Große        if (isset($_SESSION[DOKU_COOKIE]['oauth-done'])) {
362b2b9fbc7SMichael Große            unset($_SESSION[DOKU_COOKIE]['oauth-done']);
363af2a4e8fSMichael Große        }
364af2a4e8fSMichael Große        if (isset($_SESSION[DOKU_COOKIE]['auth'])) {
365b2b9fbc7SMichael Große            unset($_SESSION[DOKU_COOKIE]['auth']);
366af2a4e8fSMichael Große        }
367b2b9fbc7SMichael Große        $this->setUserCookie('', true, '', -60);
368b2b9fbc7SMichael Große    }
369b2b9fbc7SMichael Große
370b2b9fbc7SMichael Große    /**
371b2b9fbc7SMichael Große     * Enhance function to check against duplicate emails
372b2b9fbc7SMichael Große     *
373b2b9fbc7SMichael Große     * @param string $user
374b2b9fbc7SMichael Große     * @param string $pwd
375b2b9fbc7SMichael Große     * @param string $name
376b2b9fbc7SMichael Große     * @param string $mail
377b2b9fbc7SMichael Große     * @param null $grps
378b2b9fbc7SMichael Große     * @return bool|null|string
379b2b9fbc7SMichael Große     */
380*3e7ac5b1SAndreas Gohr    public function createUser($user, $pwd, $name, $mail, $grps = null)
381*3e7ac5b1SAndreas Gohr    {
382b2b9fbc7SMichael Große        if ($this->getUserByEmail($mail)) {
383b2b9fbc7SMichael Große            msg($this->getLang('emailduplicate'), -1);
384827232fcSMichael Große            return false;
385827232fcSMichael Große        }
386b2b9fbc7SMichael Große
387b2b9fbc7SMichael Große        return parent::createUser($user, $pwd, $name, $mail, $grps);
388827232fcSMichael Große    }
389b2b9fbc7SMichael Große
390b2b9fbc7SMichael Große    /**
391b2b9fbc7SMichael Große     * Enhance function to check aainst duplicate emails
392b2b9fbc7SMichael Große     *
393b2b9fbc7SMichael Große     * @param string $user
394b2b9fbc7SMichael Große     * @param array $changes
395b2b9fbc7SMichael Große     * @return bool
396b2b9fbc7SMichael Große     */
397*3e7ac5b1SAndreas Gohr    public function modifyUser($user, $changes)
398*3e7ac5b1SAndreas Gohr    {
399b2b9fbc7SMichael Große        global $conf;
400b2b9fbc7SMichael Große
401b2b9fbc7SMichael Große        if (isset($changes['mail'])) {
402b2b9fbc7SMichael Große            $found = $this->getUserByEmail($changes['mail']);
40311997ac2SLindy Blackburn            if ($found && $found != $user) {
404b2b9fbc7SMichael Große                msg($this->getLang('emailduplicate'), -1);
405b2b9fbc7SMichael Große                return false;
406b2b9fbc7SMichael Große            }
407b2b9fbc7SMichael Große        }
408b2b9fbc7SMichael Große
409b2b9fbc7SMichael Große        $ok = parent::modifyUser($user, $changes);
410b2b9fbc7SMichael Große
411b2b9fbc7SMichael Große        // refresh session cache
412b2b9fbc7SMichael Große        touch($conf['cachedir'] . '/sessionpurge');
413b2b9fbc7SMichael Große
414b2b9fbc7SMichael Große        return $ok;
415827232fcSMichael Große    }
416827232fcSMichael Große
41780852c15SAndreas Gohr}
41880852c15SAndreas Gohr
41980852c15SAndreas Gohr// vim:ts=4:sw=4:et:
420