1<?php 2// must be run within Dokuwiki 3if(!defined('DOKU_INC')) die(); 4 5/** 6 * Auth Plugin Prototype 7 * 8 * foundation authorisation class 9 * all auth classes should inherit from this class 10 * 11 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 12 * @author Chris Smith <chris@jalakai.co.uk> 13 * @author Jan Schumann <js@jschumann-it.com> 14 */ 15class DokuWiki_Auth_Plugin extends DokuWiki_Plugin { 16 public $success = true; 17 18 /** 19 * Possible things an auth backend module may be able to 20 * do. The things a backend can do need to be set to true 21 * in the constructor. 22 */ 23 protected $cando = array( 24 'addUser' => false, // can Users be created? 25 'delUser' => false, // can Users be deleted? 26 'modLogin' => false, // can login names be changed? 27 'modPass' => false, // can passwords be changed? 28 'modName' => false, // can real names be changed? 29 'modMail' => false, // can emails be changed? 30 'modGroups' => false, // can groups be changed? 31 'getUsers' => false, // can a (filtered) list of users be retrieved? 32 'getUserCount' => false, // can the number of users be retrieved? 33 'getGroups' => false, // can a list of available groups be retrieved? 34 'external' => false, // does the module do external auth checking? 35 'logout' => true, // can the user logout again? (eg. not possible with HTTP auth) 36 ); 37 38 /** 39 * Constructor. 40 * 41 * Carry out sanity checks to ensure the object is 42 * able to operate. Set capabilities in $this->cando 43 * array here 44 * 45 * For future compatibility, sub classes should always include a call 46 * to parent::__constructor() in their constructors! 47 * 48 * Set $this->success to false if checks fail 49 * 50 * @author Christopher Smith <chris@jalakai.co.uk> 51 */ 52 public function __construct() { 53 // the base class constructor does nothing, derived class 54 // constructors do the real work 55 } 56 57 /** 58 * Available Capabilities. [ DO NOT OVERRIDE ] 59 * 60 * For introspection/debugging 61 * 62 * @author Christopher Smith <chris@jalakai.co.uk> 63 * @return array 64 */ 65 public function getCapabilities(){ 66 return array_keys($this->cando); 67 } 68 69 /** 70 * Capability check. [ DO NOT OVERRIDE ] 71 * 72 * Checks the capabilities set in the $this->cando array and 73 * some pseudo capabilities (shortcutting access to multiple 74 * ones) 75 * 76 * ususal capabilities start with lowercase letter 77 * shortcut capabilities start with uppercase letter 78 * 79 * @author Andreas Gohr <andi@splitbrain.org> 80 * @param string $cap the capability to check 81 * @return bool 82 */ 83 public function canDo($cap) { 84 switch($cap) { 85 case 'Profile': 86 // can at least one of the user's properties be changed? 87 return ($this->cando['modPass'] || 88 $this->cando['modName'] || 89 $this->cando['modMail']); 90 break; 91 case 'UserMod': 92 // can at least anything be changed? 93 return ($this->cando['modPass'] || 94 $this->cando['modName'] || 95 $this->cando['modMail'] || 96 $this->cando['modLogin'] || 97 $this->cando['modGroups'] || 98 $this->cando['modMail']); 99 break; 100 default: 101 // print a helping message for developers 102 if(!isset($this->cando[$cap])) { 103 msg("Check for unknown capability '$cap' - Do you use an outdated Plugin?", -1); 104 } 105 return $this->cando[$cap]; 106 } 107 } 108 109 /** 110 * Trigger the AUTH_USERDATA_CHANGE event and call the modification function. [ DO NOT OVERRIDE ] 111 * 112 * You should use this function instead of calling createUser, modifyUser or 113 * deleteUsers directly. The event handlers can prevent the modification, for 114 * example for enforcing a user name schema. 115 * 116 * @author Gabriel Birke <birke@d-scribe.de> 117 * @param string $type Modification type ('create', 'modify', 'delete') 118 * @param array $params Parameters for the createUser, modifyUser or deleteUsers method. 119 * The content of this array depends on the modification type 120 * @return bool|null|int Result from the modification function or false if an event handler has canceled the action 121 */ 122 public function triggerUserMod($type, $params) { 123 $validTypes = array( 124 'create' => 'createUser', 125 'modify' => 'modifyUser', 126 'delete' => 'deleteUsers' 127 ); 128 if(empty($validTypes[$type])) { 129 return false; 130 } 131 132 $result = false; 133 $eventdata = array('type' => $type, 'params' => $params, 'modification_result' => null); 134 $evt = new Doku_Event('AUTH_USER_CHANGE', $eventdata); 135 if($evt->advise_before(true)) { 136 $result = call_user_func_array(array($this, $validTypes[$type]), $evt->data['params']); 137 $evt->data['modification_result'] = $result; 138 } 139 $evt->advise_after(); 140 unset($evt); 141 return $result; 142 } 143 144 /** 145 * Log off the current user [ OPTIONAL ] 146 * 147 * Is run in addition to the ususal logoff method. Should 148 * only be needed when trustExternal is implemented. 149 * 150 * @see auth_logoff() 151 * @author Andreas Gohr <andi@splitbrain.org> 152 */ 153 public function logOff() { 154 } 155 156 /** 157 * Do all authentication [ OPTIONAL ] 158 * 159 * Set $this->cando['external'] = true when implemented 160 * 161 * If this function is implemented it will be used to 162 * authenticate a user - all other DokuWiki internals 163 * will not be used for authenticating, thus 164 * implementing the checkPass() function is not needed 165 * anymore. 166 * 167 * The function can be used to authenticate against third 168 * party cookies or Apache auth mechanisms and replaces 169 * the auth_login() function 170 * 171 * The function will be called with or without a set 172 * username. If the Username is given it was called 173 * from the login form and the given credentials might 174 * need to be checked. If no username was given it 175 * the function needs to check if the user is logged in 176 * by other means (cookie, environment). 177 * 178 * The function needs to set some globals needed by 179 * DokuWiki like auth_login() does. 180 * 181 * @see auth_login() 182 * @author Andreas Gohr <andi@splitbrain.org> 183 * 184 * @param string $user Username 185 * @param string $pass Cleartext Password 186 * @param bool $sticky Cookie should not expire 187 * @return bool true on successful auth 188 */ 189 public function trustExternal($user, $pass, $sticky = false) { 190 /* some example: 191 192 global $USERINFO; 193 global $conf; 194 $sticky ? $sticky = true : $sticky = false; //sanity check 195 196 // do the checking here 197 198 // set the globals if authed 199 $USERINFO['name'] = 'FIXME'; 200 $USERINFO['mail'] = 'FIXME'; 201 $USERINFO['grps'] = array('FIXME'); 202 $_SERVER['REMOTE_USER'] = $user; 203 $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 204 $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass; 205 $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 206 return true; 207 208 */ 209 } 210 211 /** 212 * Check user+password [ MUST BE OVERRIDDEN ] 213 * 214 * Checks if the given user exists and the given 215 * plaintext password is correct 216 * 217 * May be ommited if trustExternal is used. 218 * 219 * @author Andreas Gohr <andi@splitbrain.org> 220 * @param string $user the user name 221 * @param string $pass the clear text password 222 * @return bool 223 */ 224 public function checkPass($user, $pass) { 225 msg("no valid authorisation system in use", -1); 226 return false; 227 } 228 229 /** 230 * Return user info [ MUST BE OVERRIDDEN ] 231 * 232 * Returns info about the given user needs to contain 233 * at least these fields: 234 * 235 * name string full name of the user 236 * mail string email address of the user 237 * grps array list of groups the user is in 238 * 239 * @author Andreas Gohr <andi@splitbrain.org> 240 * @param string $user the user name 241 * @param bool $requireGroups whether or not the returned data must include groups 242 * @return false|array containing user data or false 243 */ 244 public function getUserData($user, $requireGroups=true) { 245 if(!$this->cando['external']) msg("no valid authorisation system in use", -1); 246 return false; 247 } 248 249 /** 250 * Create a new User [implement only where required/possible] 251 * 252 * Returns false if the user already exists, null when an error 253 * occurred and true if everything went well. 254 * 255 * The new user HAS TO be added to the default group by this 256 * function! 257 * 258 * Set addUser capability when implemented 259 * 260 * @author Andreas Gohr <andi@splitbrain.org> 261 * @param string $user 262 * @param string $pass 263 * @param string $name 264 * @param string $mail 265 * @param null|array $grps 266 * @return bool|null 267 */ 268 public function createUser($user, $pass, $name, $mail, $grps = null) { 269 msg("authorisation method does not allow creation of new users", -1); 270 return null; 271 } 272 273 /** 274 * Modify user data [implement only where required/possible] 275 * 276 * Set the mod* capabilities according to the implemented features 277 * 278 * @author Chris Smith <chris@jalakai.co.uk> 279 * @param string $user nick of the user to be changed 280 * @param array $changes array of field/value pairs to be changed (password will be clear text) 281 * @return bool 282 */ 283 public function modifyUser($user, $changes) { 284 msg("authorisation method does not allow modifying of user data", -1); 285 return false; 286 } 287 288 /** 289 * Delete one or more users [implement only where required/possible] 290 * 291 * Set delUser capability when implemented 292 * 293 * @author Chris Smith <chris@jalakai.co.uk> 294 * @param array $users 295 * @return int number of users deleted 296 */ 297 public function deleteUsers($users) { 298 msg("authorisation method does not allow deleting of users", -1); 299 return 0; 300 } 301 302 /** 303 * Return a count of the number of user which meet $filter criteria 304 * [should be implemented whenever retrieveUsers is implemented] 305 * 306 * Set getUserCount capability when implemented 307 * 308 * @author Chris Smith <chris@jalakai.co.uk> 309 * @param array $filter array of field/pattern pairs, empty array for no filter 310 * @return int 311 */ 312 public function getUserCount($filter = array()) { 313 msg("authorisation method does not provide user counts", -1); 314 return 0; 315 } 316 317 /** 318 * Bulk retrieval of user data [implement only where required/possible] 319 * 320 * Set getUsers capability when implemented 321 * 322 * @author Chris Smith <chris@jalakai.co.uk> 323 * @param int $start index of first user to be returned 324 * @param int $limit max number of users to be returned, 0 for unlimited 325 * @param array $filter array of field/pattern pairs, null for no filter 326 * @return array list of userinfo (refer getUserData for internal userinfo details) 327 */ 328 public function retrieveUsers($start = 0, $limit = 0, $filter = null) { 329 msg("authorisation method does not support mass retrieval of user data", -1); 330 return array(); 331 } 332 333 /** 334 * Define a group [implement only where required/possible] 335 * 336 * Set addGroup capability when implemented 337 * 338 * @author Chris Smith <chris@jalakai.co.uk> 339 * @param string $group 340 * @return bool 341 */ 342 public function addGroup($group) { 343 msg("authorisation method does not support independent group creation", -1); 344 return false; 345 } 346 347 /** 348 * Retrieve groups [implement only where required/possible] 349 * 350 * Set getGroups capability when implemented 351 * 352 * @author Chris Smith <chris@jalakai.co.uk> 353 * @param int $start 354 * @param int $limit 355 * @return array 356 */ 357 public function retrieveGroups($start = 0, $limit = 0) { 358 msg("authorisation method does not support group list retrieval", -1); 359 return array(); 360 } 361 362 /** 363 * Return case sensitivity of the backend [OPTIONAL] 364 * 365 * When your backend is caseinsensitive (eg. you can login with USER and 366 * user) then you need to overwrite this method and return false 367 * 368 * @return bool 369 */ 370 public function isCaseSensitive() { 371 return true; 372 } 373 374 /** 375 * Sanitize a given username [OPTIONAL] 376 * 377 * This function is applied to any user name that is given to 378 * the backend and should also be applied to any user name within 379 * the backend before returning it somewhere. 380 * 381 * This should be used to enforce username restrictions. 382 * 383 * @author Andreas Gohr <andi@splitbrain.org> 384 * @param string $user username 385 * @return string the cleaned username 386 */ 387 public function cleanUser($user) { 388 return $user; 389 } 390 391 /** 392 * Sanitize a given groupname [OPTIONAL] 393 * 394 * This function is applied to any groupname that is given to 395 * the backend and should also be applied to any groupname within 396 * the backend before returning it somewhere. 397 * 398 * This should be used to enforce groupname restrictions. 399 * 400 * Groupnames are to be passed without a leading '@' here. 401 * 402 * @author Andreas Gohr <andi@splitbrain.org> 403 * @param string $group groupname 404 * @return string the cleaned groupname 405 */ 406 public function cleanGroup($group) { 407 return $group; 408 } 409 410 /** 411 * Check Session Cache validity [implement only where required/possible] 412 * 413 * DokuWiki caches user info in the user's session for the timespan defined 414 * in $conf['auth_security_timeout']. 415 * 416 * This makes sure slow authentication backends do not slow down DokuWiki. 417 * This also means that changes to the user database will not be reflected 418 * on currently logged in users. 419 * 420 * To accommodate for this, the user manager plugin will touch a reference 421 * file whenever a change is submitted. This function compares the filetime 422 * of this reference file with the time stored in the session. 423 * 424 * This reference file mechanism does not reflect changes done directly in 425 * the backend's database through other means than the user manager plugin. 426 * 427 * Fast backends might want to return always false, to force rechecks on 428 * each page load. Others might want to use their own checking here. If 429 * unsure, do not override. 430 * 431 * @param string $user - The username 432 * @author Andreas Gohr <andi@splitbrain.org> 433 * @return bool 434 */ 435 public function useSessionCache($user) { 436 global $conf; 437 return ($_SESSION[DOKU_COOKIE]['auth']['time'] >= @filemtime($conf['cachedir'].'/sessionpurge')); 438 } 439} 440