xref: /plugin/authchained/auth.php (revision 8e31970440b94899e329be9e47b1f310017188cc)
1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5/**
6* Chained authentication backend
7*
8* @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9* @author     Philipp Neuser <pneuser@physik.fu-berlin.de>
10* @author     Christian Marg <marg@rz.tu-clausthal.de>
11*
12* Based on "Chained authentication backend"
13* by Grant Gardner <grant@lastweekend.com.au>
14* see https://www.dokuwiki.org/auth:ggauth
15*
16*/
17class auth_plugin_authchained extends DokuWiki_Auth_Plugin {
18    public $success = true;
19    //array with authentication plugins
20    protected $chained_plugins = array();
21    protected $chained_auth = NULL;
22    protected $usermanager_auth = NULL;
23
24    /**
25    * Constructor.
26    *
27    * Loads all configured plugins or the authentication plugin of the
28    * logged in user.
29    *
30    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
31    * @author  Christian Marg <marg@rz.tu-clausthal.de>
32    */
33    public function __construct() {
34        global $conf;
35        // call parent
36        #      parent::__constructor();
37
38        //check if there is already an authentication plugin selected
39        if(     isset($_SESSION[DOKU_COOKIE]['plugin']['authchained']['module']) &&
40                !empty($_SESSION[DOKU_COOKIE]['plugin']['authchained']['module']) ) {
41
42            //get previously selected authentication plugin
43            $this->chained_auth =& plugin_load('auth',$_SESSION[DOKU_COOKIE]['plugin']['authchained']['module']);
44            if ( is_null($this->chained_auth) || !$this->chained_auth->success ) {
45                $this->success = false;
46            }
47        }
48
49        //get authentication plugins
50        if($this->getConf('authtypes')){
51            foreach(explode(":",$this->getConf('authtypes')) as $tmp_plugin){
52                $tmp_class =& plugin_load('auth',$tmp_plugin);
53
54                if ( !is_null($tmp_class) || $tmp_class->success ) {
55                    $tmp_module = array($tmp_plugin,$tmp_class);
56                    array_push($this->chained_plugins, $tmp_module);
57                } else {
58                    msg("Problem constructing $tmp_plugin",-1);
59                    $this->success = false;
60                }
61            }
62        } else {
63            $success = false;
64        }
65
66        // If defined, instantiate usermanager authtype.
67        // No need to check for duplicates, "plugin_load" does that for us.
68        if($this->getConf('usermanager_authtype')){
69            $this->usermanager_auth =& plugin_load('auth',$this->getConf('usermanager_authtype'));
70            if(is_null($this->usermanager_auth) || !$this->usermanager_auth->success ) {
71                    msg("Problem constructing usermanager authtype: ".$this->getConf('usermanager_authtype'),-1);
72                    $this->success = false;
73            }
74        } else {
75            $this->usermanager_auth =& $this->chained_auth;
76        }
77
78        //debug
79        // print_r($chained_plugins);
80    }
81
82    /**
83    * Forwards the authentication to configured authplugins.
84    * Returns true, if the usermanager authtype has the capability and no user
85    * is logged in.
86    *
87    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
88    * @author  Christian Marg <marg@rz.tu-clausthal.de>
89    * @param   string $cap the capability to check
90    * @return  bool
91    */
92    public function canDo($cap) {
93        global $ACT;
94        #      print_r($cap);
95        if(is_null($this->chained_auth)) {
96            if (!is_null($this->usermanager_auth)) {
97                return $this->usermanager_auth->canDo($cap);
98            } else {
99                return parent::canDo($cap);
100            }
101        } else {
102            switch($cap) {
103                case 'Profile':
104                case 'logoff':
105                    //Depends on current user.
106                    return $this->chained_auth->canDo($cap);
107                case 'UserMod':
108                case 'addUser':
109                case 'delUser':
110                case 'getUsers':
111                case 'getUserCount':
112                case 'getGroups':
113                    //Depends on the auth for use with user manager
114                    return $this->usermanager_auth->canDo($cap);
115                case 'modPass':
116                case 'modName':
117                case 'modLogin':
118                case 'modGroups':
119                case 'modMail':
120                    /**
121                    * Use request attributes to guess whether we are in the Profile or UserManager
122                    * and return the appropriate auth capabilities
123                    */
124                    if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
125                        return $this->usermanager_auth->canDo($cap);
126                    } else {
127                        // assume we want profile info.
128                        return $this->chained_auth->canDo($cap);
129                    }
130// I don't know how to handle "external" in this context yet.
131// Is it in any way sensible to mix regular auth with external auth?
132//                case 'external':
133//                    //We are external if one of the chains is valid for external use
134//                    return $this->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']);
135                default:
136                    //Everything else (false)
137                    return parent::canDo($cap);
138            }
139            #echo "canDo $cap ".$this->chained_auth->canDo($cap)."\n";
140        }
141    }
142
143    /**
144    * Forwards the result of the auth plugin of the logged in user and
145    * unsets our session variable.
146    * @see     auth_logoff()
147    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de
148    * @author  Christian Marg <marg@rz.tu-clausthal.de>
149    */
150    public function logOff() {
151        if(!is_null($this->chained_auth))
152            $this->chained_auth->logOff();
153        unset($_SESSION[DOKU_COOKIE]['plugin']['authchained']['module']);
154    }
155
156    /**
157    * Do all authentication [ OPTIONAL ]
158    * If the current plugin is external, be external.
159    *
160    * @see     auth_login()
161    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
162    * @author  Christian Marg <marg@rz.tu-clausthal.de>
163    *
164    * @param   string  $user    Username
165    * @param   string  $pass    Cleartext Password
166    * @param   bool    $sticky  Cookie should not expire
167    * @return  bool             true on successful auth
168    */
169    public function trustExternal($user, $pass, $sticky = false) {
170        if(!is_null($this->chained_auth) && $this->chained_auth->canDo('external'))
171            $this->chained_auth->trustExternal($user, $pass, $sticky);
172    }
173
174    /**
175    * Check user+password [ MUST BE OVERRIDDEN ]
176    *
177    * Checks if the given user exists in one of the plugins and checks
178    * against the given password. The first plugin returning true becomes
179    * auth plugin of the user session.
180    *
181    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de
182    * @author  Christian Marg <marg@rz.tu-clausthal.de>
183    * @param   string $user the user name
184    * @param   string $pass the clear text password
185    * @return  bool
186    */
187    public function checkPass($user, $pass) {
188        //debug
189        // print_r($this->chained_plugins);
190        if(!is_null($this->chained_auth))
191            return $this->chained_auth->checkPass($user, $pass);
192        foreach($this->chained_plugins as $module) {
193            if($module[1]->canDo('external') && $module[1]->trustExternal($user, $pass)) {
194                $_SESSION[DOKU_COOKIE]['plugin']['authchained']['module'] = $module[0];
195                $this->chained_auth = $module[1];
196                return true;
197            }
198            if($module[1]->checkPass($user, $pass)) {
199                $_SESSION[DOKU_COOKIE]['plugin']['authchained']['module'] = $module[0];
200                $this->chained_auth = $module[1];
201                return true;
202            }
203        }
204        return false;
205    }
206
207    /**
208    * Forwards the result of the auth plugin of the logged in user or
209    * checks all plugins if the users exists. The first plugin returning
210    * data is used.
211    *
212    * name string  full name of the user
213    * mail string  email addres of the user
214    * grps array   list of groups the user is in
215    *
216    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
217    * @author  Christian Marg <marg@rz.tu-clausthal.de>
218    * @param   string $user the user name
219    * @return  array containing user data or false
220    */
221    public function getUserData($user, $requireGroups=true) {
222        global $ACT, $INPUT;
223
224        //if(!$this->cando['external']) msg("no valid authorisation system in use", -1);
225        //       echo "TESTSETEST";
226
227        //print_r($this->chained_auth);
228        if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
229            if(!is_null($this->usermanager_auth))
230                return $this->usermanager_auth->getUserData($user);
231	}
232
233        if(is_null($this->chained_auth)||(!is_null($INPUT->server) && $user != $INPUT->server->str('REMOTE_USER'))) {
234            foreach($this->chained_plugins as $module) {
235                $tmp_array = $module[1]->getUserData($user);
236                if(!is_bool($tmp_array))
237                    $tmp_chk_arr =array_filter($tmp_array);
238                if(!empty($tmp_chk_arr) && $tmp_array)
239                    return $tmp_array;
240            }
241            return false;
242        } else {
243            return $this->chained_auth->getUserData($user);
244        }
245    }
246
247    /**
248    * Forwards the result of the auth plugin of the logged in user or
249    * returns null.
250    *
251    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
252    * @author  Christian Marg <marg@rz.tu-clausthal.de>
253    * @param   string     $user
254    * @param   string     $pass
255    * @param   string     $name
256    * @param   string     $mail
257    * @param   null|array $grps
258    * @return  bool|null
259    */
260    public function createUser($user, $pass, $name, $mail, $grps = null) {
261        if(!is_null($this->usermanager_auth) && $this->canDo('addUser')) {
262            return $this->usermanager_auth->createUser($user, $pass, $name, $mail, $grps);
263        } else {
264            msg("authorisation method does not allow creation of new users", -1);
265            return null;
266        }
267    }
268
269    /**
270    * Forwards the result of the auth plugin of the logged in user or
271    * returns false
272    *
273    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
274    * @author  Christian Marg <marg@rz.tu-clausthal.de>
275    * @param   string $user    nick of the user to be changed
276    * @param   array  $changes array of field/value pairs to be changed (password will be clear text)
277    * @return  bool
278    */
279    public function modifyUser($user, $changes) {
280        if(!is_null($this->usermanager_auth) && $this->canDo('UserMod') ) {
281            return $this->usermanager_auth->modifyUser($user, $changes);
282        } else {
283            msg("authorisation method does not allow modifying of user data", -1);
284            return null;
285        }
286    }
287
288    /**
289    * Forwards the result of the auth plugin of the logged in user or
290    * returns false
291    *
292    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
293    * @author  Christian Marg <marg@rz.tu-clausthal.de>
294    * @param   array  $users
295    * @return  int    number of users deleted
296    */
297    public function deleteUsers($users) {
298        if(!is_null($this->usermanager_auth) && $this->canDo('delUser') ) {
299            return $this->usermanager_auth->deleteUsers($users);
300        }else{
301            msg("authorisation method does not allow deleting of users", -1);
302            return false;
303        }
304    }
305
306    /**
307    * Forwards the result of the auth plugin of the logged in user or
308    * returns 0
309    *
310    * @author Philipp Neuser <pneuser@physik.fu-berlin.de>
311    * @author Christian Marg <marg@rz.tu-clausthal.de>
312    * @param  array $filter array of field/pattern pairs, empty array for no filter
313    * @return int
314    */
315    public function getUserCount($filter = array()) {
316        if(!is_null($this->usermanager_auth) && $this->canDo('getUserCount') ){
317            return $this->usermanager_auth->getUserCount($filter);
318        } else {
319            msg("authorisation method does not provide user counts", -1);
320            return 0;
321        }
322    }
323
324    /**
325    * Forwards the result of the auth plugin of the logged in user or
326    * returns empty array
327    *
328    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
329    * @author  Christian Marg <marg@rz.tu-clausthal.de>
330    * @param   int   $start     index of first user to be returned
331    * @param   int   $limit     max number of users to be returned
332    * @param   array $filter    array of field/pattern pairs, null for no filter
333    * @return  array list of userinfo (refer getUserData for internal userinfo details)
334    */
335    public function retrieveUsers($start = 0, $limit = -1, $filter = null) {
336        if(!is_null($this->usermanager_auth) && $this->canDo('getUsers') ) {
337            //msg("RetrieveUsers is using ".get_class($this->usermanager_auth));
338            return $this->usermanager_auth->retrieveUsers($start, $limit, $filter);
339        } else {
340            msg("authorisation method does not support mass retrievals", -1);
341            return array();
342        }
343    }
344
345    /**
346    * Forwards the result of the auth plugin of the logged in user or
347    * returns false
348    *
349    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
350    * @author  Christian Marg <marg@rz.tu-clausthal.de>
351    * @param   string $group
352    * @return  bool
353    */
354    public function addGroup($group) {
355        if(!is_null($this->usermanager_auth) && $this->canDo('addGroup') ) {
356            return $this->usermanager_auth->addGroup($group);
357        } else {
358            msg("authorisation method does not support independent group creation", -1);
359            return false;
360        }
361    }
362
363    /**
364    * Forwards the result of the auth plugin of the logged in user or
365    * returns empty array
366    *
367    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
368    * @author  Christian Marg <marg@rz.tu-clausthal.de>
369    * @param   int $start
370    * @param   int $limit
371    * @return  array
372    */
373    public function retrieveGroups($start = 0, $limit = 0) {
374        if(!is_null($this->usermanager_auth) && $this->canDo('getGroups') ) {
375                return $this->usermanager_auth->retrieveGroups($start,$limit);
376        } else {
377            msg("authorisation method does not support group list retrieval", -1);
378            return array();
379        }
380    }
381
382    /**
383    * Forwards the result of the auth plugin of the logged in user or
384    * returns true
385    *
386    * @return bool
387    */
388    public function isCaseSensitive() {
389        if(is_null($this->chained_auth))
390            return parent::isCaseSensitive();
391        else
392            return $this->chained_auth->isCaseSensitive();
393    }
394
395    /**
396    * Sanitize a given username [OPTIONAL]
397    * Forwards the result of the auth plugin of the logged in user or
398    * returns false
399    *
400    *
401    * @author Philipp Neuser <pneuser@physik.fu-berlin.de>
402    * @author Christian Marg <marg@rz.tu-clausthal.de>
403    * @param  string $user username
404    * @return string the cleaned username
405    */
406    public function cleanUser($user) {
407        global $ACT;
408        //print_r($this->chained_auth);
409        if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
410            if(!is_null($this->usermanager_auth))
411                return $this->usermanager_auth->cleanUser($user);
412        } else {
413            if(!is_null($this->chained_auth))
414                return $this->chained_auth->cleanUser($user);
415        }
416        return parent::cleanUser($user);
417    }
418
419    /**
420    * Sanitize a given groupname [OPTIONAL]
421    * Forwards the result of the auth plugin of the logged in user or
422    * returns false
423    *
424    * @author Philipp Neuser <pneuser@physik.fu-berlin.de>
425    * @author Christian Marg <marg@rz.tu-clausthal.de>
426    * @param  string $group groupname
427    * @return string the cleaned groupname
428    */
429    public function cleanGroup($group) {
430        global $ACT;
431        if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
432            if(!is_null($this->usermanager_auth))
433                return $this->usermanager_auth->cleanGroup($group);
434        } else {
435            if(!is_null($this->chained_auth))
436                return $this->chained_auth->cleanGroup($group);
437        }
438        return parent::cleanGroup($group);
439    }
440
441
442    public function useSessionCache($user) {
443        global $conf;
444        if(is_null($this->chained_auth))
445            return parent::useSessionCache($user);
446        else
447            return $this->chained_auth->useSessionCache($user);
448    }
449
450}
451