5 * foundation authorisation class
6 * all auth classes should inherit from this class
8 * @author Chris Smith <chris@jalakai.co.uk>
15 * Posible things an auth backend module may be able to
16 * do. The things a backend can do need to be set to true
20 'addUser' => false, // can Users be created?
21 'delUser' => false, // can Users be deleted?
22 'modLogin' => false, // can login names be changed?
23 'modPass' => false, // can passwords be changed?
24 'modName' => false, // can real names be changed?
25 'modMail' => false, // can emails be changed?
26 'modGroups' => false, // can groups be changed?
27 'getUsers' => false, // can a (filtered) list of users be retrieved?
28 'getUserCount'=> false, // can the number of users be retrieved?
29 'getGroups' => false, // can a list of available groups be retrieved?
30 'external' => false, // does the module do external auth checking?
31 'logout' => true, // can the user logout again? (eg. not possible with HTTP auth)
37 * Carry out sanity checks to ensure the object is
38 * able to operate. Set capabilities in $this->cando
41 * Set $this->success to false if checks fail
43 * @author Christopher Smith <chris@jalakai.co.uk>
45 function __construct() {
46 // the base class constructor does nothing, derived class
47 // constructors do the real work
51 * Capability check. [ DO NOT OVERRIDE ]
53 * Checks the capabilities set in the $this->cando array and
54 * some pseudo capabilities (shortcutting access to multiple
57 * ususal capabilities start with lowercase letter
58 * shortcut capabilities start with uppercase letter
60 * @author Andreas Gohr <andi@splitbrain.org>
63 function canDo($cap) {
66 // can at least one of the user's properties be changed?
67 return ( $this->cando['modPass'] ||
68 $this->cando['modName'] ||
69 $this->cando['modMail'] );
72 // can at least anything be changed?
73 return ( $this->cando['modPass'] ||
74 $this->cando['modName'] ||
75 $this->cando['modMail'] ||
76 $this->cando['modLogin'] ||
77 $this->cando['modGroups'] ||
78 $this->cando['modMail'] );
81 // print a helping message for developers
82 if(!isset($this->cando[$cap])){
83 msg("Check for unknown capability '$cap' - Do you use an outdated Plugin?",-1);
85 return $this->cando[$cap];
90 * Trigger the AUTH_USERDATA_CHANGE event and call the modification function. [ DO NOT OVERRIDE ]
92 * You should use this function instead of calling createUser, modifyUser or
93 * deleteUsers directly. The event handlers can prevent the modification, for
94 * example for enforcing a user name schema.
96 * @author Gabriel Birke <birke@d-scribe.de>
97 * @param string $type Modification type ('create', 'modify', 'delete')
98 * @param array $params Parameters for the createUser, modifyUser or deleteUsers method. The content of this array depends on the modification type
99 * @return mixed Result from the modification function or false if an event handler has canceled the action
101 function triggerUserMod($type, $params) {
103 'create' => 'createUser',
104 'modify' => 'modifyUser',
105 'delete' => 'deleteUsers'
107 if(empty($validTypes[$type]))
109 $eventdata = array('type' => $type, 'params' => $params, 'modification_result' => null);
110 $evt = new Doku_Event('AUTH_USER_CHANGE', $eventdata);
111 if ($evt->advise_before(true)) {
112 $result = call_user_func_array(array($this, $validTypes[$type]), $params);
113 $evt->data['modification_result'] = $result;
115 $evt->advise_after();
121 * Log off the current user [ OPTIONAL ]
123 * Is run in addition to the ususal logoff method. Should
124 * only be needed when trustExternal is implemented.
127 * @author Andreas Gohr <andi@splitbrain.org>
133 * Do all authentication [ OPTIONAL ]
135 * Set $this->cando['external'] = true when implemented
137 * If this function is implemented it will be used to
138 * authenticate a user - all other DokuWiki internals
139 * will not be used for authenticating, thus
140 * implementing the checkPass() function is not needed
143 * The function can be used to authenticate against third
144 * party cookies or Apache auth mechanisms and replaces
145 * the auth_login() function
147 * The function will be called with or without a set
148 * username. If the Username is given it was called
149 * from the login form and the given credentials might
150 * need to be checked. If no username was given it
151 * the function needs to check if the user is logged in
152 * by other means (cookie, environment).
154 * The function needs to set some globals needed by
155 * DokuWiki like auth_login() does.
158 * @author Andreas Gohr <andi@splitbrain.org>
160 * @param string $user Username
161 * @param string $pass Cleartext Password
162 * @param bool $sticky Cookie should not expire
163 * @return bool true on successful auth
165 function trustExternal($user,$pass,$sticky=false){
170 $sticky ? $sticky = true : $sticky = false; //sanity check
172 // do the checking here
174 // set the globals if authed
175 $USERINFO['name'] = 'FIXME';
176 $USERINFO['mail'] = 'FIXME';
177 $USERINFO['grps'] = array('FIXME');
178 $_SERVER['REMOTE_USER'] = $user;
179 $_SESSION[DOKU_COOKIE]['auth']['user'] = $user;
180 $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass;
181 $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
188 * Check user+password [ MUST BE OVERRIDDEN ]
190 * Checks if the given user exists and the given
191 * plaintext password is correct
193 * May be ommited if trustExternal is used.
195 * @author Andreas Gohr <andi@splitbrain.org>
198 function checkPass($user,$pass){
199 msg("no valid authorisation system in use", -1);
204 * Return user info [ MUST BE OVERRIDDEN ]
206 * Returns info about the given user needs to contain
207 * at least these fields:
209 * name string full name of the user
210 * mail string email addres of the user
211 * grps array list of groups the user is in
213 * @author Andreas Gohr <andi@splitbrain.org>
214 * @return array containing user data or false
216 function getUserData($user) {
217 if(!$this->cando['external']) msg("no valid authorisation system in use", -1);
222 * Create a new User [implement only where required/possible]
224 * Returns false if the user already exists, null when an error
225 * occurred and true if everything went well.
227 * The new user HAS TO be added to the default group by this
230 * Set addUser capability when implemented
232 * @author Andreas Gohr <andi@splitbrain.org>
234 function createUser($user,$pass,$name,$mail,$grps=null){
235 msg("authorisation method does not allow creation of new users", -1);
240 * Modify user data [implement only where required/possible]
242 * Set the mod* capabilities according to the implemented features
244 * @author Chris Smith <chris@jalakai.co.uk>
245 * @param $user nick of the user to be changed
246 * @param $changes array of field/value pairs to be changed (password will be clear text)
249 function modifyUser($user, $changes) {
250 msg("authorisation method does not allow modifying of user data", -1);
255 * Delete one or more users [implement only where required/possible]
257 * Set delUser capability when implemented
259 * @author Chris Smith <chris@jalakai.co.uk>
260 * @param array $users
261 * @return int number of users deleted
263 function deleteUsers($users) {
264 msg("authorisation method does not allow deleting of users", -1);
269 * Return a count of the number of user which meet $filter criteria
270 * [should be implemented whenever retrieveUsers is implemented]
272 * Set getUserCount capability when implemented
274 * @author Chris Smith <chris@jalakai.co.uk>
276 function getUserCount($filter=array()) {
277 msg("authorisation method does not provide user counts", -1);
282 * Bulk retrieval of user data [implement only where required/possible]
284 * Set getUsers capability when implemented
286 * @author Chris Smith <chris@jalakai.co.uk>
287 * @param start index of first user to be returned
288 * @param limit max number of users to be returned
289 * @param filter array of field/pattern pairs, null for no filter
290 * @return array of userinfo (refer getUserData for internal userinfo details)
292 function retrieveUsers($start=0,$limit=-1,$filter=null) {
293 msg("authorisation method does not support mass retrieval of user data", -1);
298 * Define a group [implement only where required/possible]
300 * Set addGroup capability when implemented
302 * @author Chris Smith <chris@jalakai.co.uk>
305 function addGroup($group) {
306 msg("authorisation method does not support independent group creation", -1);
311 * Retrieve groups [implement only where required/possible]
313 * Set getGroups capability when implemented
315 * @author Chris Smith <chris@jalakai.co.uk>
318 function retrieveGroups($start=0,$limit=0) {
319 msg("authorisation method does not support group list retrieval", -1);
324 * Return case sensitivity of the backend [OPTIONAL]
326 * When your backend is caseinsensitive (eg. you can login with USER and
327 * user) then you need to overwrite this method and return false
329 function isCaseSensitive(){
334 * Sanitize a given username [OPTIONAL]
336 * This function is applied to any user name that is given to
337 * the backend and should also be applied to any user name within
338 * the backend before returning it somewhere.
340 * This should be used to enforce username restrictions.
342 * @author Andreas Gohr <andi@splitbrain.org>
343 * @param string $user - username
344 * @param string - the cleaned username
346 function cleanUser($user){
351 * Sanitize a given groupname [OPTIONAL]
353 * This function is applied to any groupname that is given to
354 * the backend and should also be applied to any groupname within
355 * the backend before returning it somewhere.
357 * This should be used to enforce groupname restrictions.
359 * Groupnames are to be passed without a leading '@' here.
361 * @author Andreas Gohr <andi@splitbrain.org>
362 * @param string $group - groupname
363 * @param string - the cleaned groupname
365 function cleanGroup($group){
371 * Check Session Cache validity [implement only where required/possible]
373 * DokuWiki caches user info in the user's session for the timespan defined
374 * in $conf['auth_security_timeout'].
376 * This makes sure slow authentication backends do not slow down DokuWiki.
377 * This also means that changes to the user database will not be reflected
378 * on currently logged in users.
380 * To accommodate for this, the user manager plugin will touch a reference
381 * file whenever a change is submitted. This function compares the filetime
382 * of this reference file with the time stored in the session.
384 * This reference file mechanism does not reflect changes done directly in
385 * the backend's database through other means than the user manager plugin.
387 * Fast backends might want to return always false, to force rechecks on
388 * each page load. Others might want to use their own checking here. If
389 * unsure, do not override.
391 * @param string $user - The username
392 * @author Andreas Gohr <andi@splitbrain.org>
395 function useSessionCache($user){
397 return ($_SESSION[DOKU_COOKIE]['auth']['time'] >= @filemtime($conf['cachedir'].'/sessionpurge'));
401 //Setup VIM: ex: et ts=2 :