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. The content of this array depends on the modification type 119 * @return mixed Result from the modification function or false if an event handler has canceled the action 120 */ 121 public function triggerUserMod($type, $params) { 122 $validTypes = array( 123 'create' => 'createUser', 124 'modify' => 'modifyUser', 125 'delete' => 'deleteUsers' 126 ); 127 if(empty($validTypes[$type])) 128 return false; 129 $eventdata = array('type' => $type, 'params' => $params, 'modification_result' => null); 130 $evt = new Doku_Event('AUTH_USER_CHANGE', $eventdata); 131 if($evt->advise_before(true)) { 132 $result = call_user_func_array(array($this, $validTypes[$type]), $params); 133 $evt->data['modification_result'] = $result; 134 } 135 $evt->advise_after(); 136 unset($evt); 137 return $result; 138 } 139 140 /** 141 * Log off the current user [ OPTIONAL ] 142 * 143 * Is run in addition to the ususal logoff method. Should 144 * only be needed when trustExternal is implemented. 145 * 146 * @see auth_logoff() 147 * @author Andreas Gohr <andi@splitbrain.org> 148 */ 149 public function logOff() { 150 } 151 152 /** 153 * Do all authentication [ OPTIONAL ] 154 * 155 * Set $this->cando['external'] = true when implemented 156 * 157 * If this function is implemented it will be used to 158 * authenticate a user - all other DokuWiki internals 159 * will not be used for authenticating, thus 160 * implementing the checkPass() function is not needed 161 * anymore. 162 * 163 * The function can be used to authenticate against third 164 * party cookies or Apache auth mechanisms and replaces 165 * the auth_login() function 166 * 167 * The function will be called with or without a set 168 * username. If the Username is given it was called 169 * from the login form and the given credentials might 170 * need to be checked. If no username was given it 171 * the function needs to check if the user is logged in 172 * by other means (cookie, environment). 173 * 174 * The function needs to set some globals needed by 175 * DokuWiki like auth_login() does. 176 * 177 * @see auth_login() 178 * @author Andreas Gohr <andi@splitbrain.org> 179 * 180 * @param string $user Username 181 * @param string $pass Cleartext Password 182 * @param bool $sticky Cookie should not expire 183 * @return bool true on successful auth 184 */ 185 public function trustExternal($user, $pass, $sticky = false) { 186 /* some example: 187 188 global $USERINFO; 189 global $conf; 190 $sticky ? $sticky = true : $sticky = false; //sanity check 191 192 // do the checking here 193 194 // set the globals if authed 195 $USERINFO['name'] = 'FIXME'; 196 $USERINFO['mail'] = 'FIXME'; 197 $USERINFO['grps'] = array('FIXME'); 198 $_SERVER['REMOTE_USER'] = $user; 199 $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 200 $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass; 201 $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 202 return true; 203 204 */ 205 } 206 207 /** 208 * Check user+password [ MUST BE OVERRIDDEN ] 209 * 210 * Checks if the given user exists and the given 211 * plaintext password is correct 212 * 213 * May be ommited if trustExternal is used. 214 * 215 * @author Andreas Gohr <andi@splitbrain.org> 216 * @param string $user the user name 217 * @param string $pass the clear text password 218 * @return bool 219 */ 220 public function checkPass($user, $pass) { 221 msg("no valid authorisation system in use", -1); 222 return false; 223 } 224 225 /** 226 * Return user info [ MUST BE OVERRIDDEN ] 227 * 228 * Returns info about the given user needs to contain 229 * at least these fields: 230 * 231 * name string full name of the user 232 * mail string email addres of the user 233 * grps array list of groups the user is in 234 * 235 * @author Andreas Gohr <andi@splitbrain.org> 236 * @param string $user the user name 237 * @return array containing user data or false 238 */ 239 public function getUserData($user) { 240 if(!$this->cando['external']) msg("no valid authorisation system in use", -1); 241 return false; 242 } 243 244 /** 245 * Create a new User [implement only where required/possible] 246 * 247 * Returns false if the user already exists, null when an error 248 * occurred and true if everything went well. 249 * 250 * The new user HAS TO be added to the default group by this 251 * function! 252 * 253 * Set addUser capability when implemented 254 * 255 * @author Andreas Gohr <andi@splitbrain.org> 256 * @param string $user 257 * @param string $pass 258 * @param string $name 259 * @param string $mail 260 * @param null|array $grps 261 * @return bool|null 262 */ 263 public function createUser($user, $pass, $name, $mail, $grps = null) { 264 msg("authorisation method does not allow creation of new users", -1); 265 return null; 266 } 267 268 /** 269 * Modify user data [implement only where required/possible] 270 * 271 * Set the mod* capabilities according to the implemented features 272 * 273 * @author Chris Smith <chris@jalakai.co.uk> 274 * @param string $user nick of the user to be changed 275 * @param array $changes array of field/value pairs to be changed (password will be clear text) 276 * @return bool 277 */ 278 public function modifyUser($user, $changes) { 279 msg("authorisation method does not allow modifying of user data", -1); 280 return false; 281 } 282 283 /** 284 * Delete one or more users [implement only where required/possible] 285 * 286 * Set delUser capability when implemented 287 * 288 * @author Chris Smith <chris@jalakai.co.uk> 289 * @param array $users 290 * @return int number of users deleted 291 */ 292 public function deleteUsers($users) { 293 msg("authorisation method does not allow deleting of users", -1); 294 return false; 295 } 296 297 /** 298 * Return a count of the number of user which meet $filter criteria 299 * [should be implemented whenever retrieveUsers is implemented] 300 * 301 * Set getUserCount capability when implemented 302 * 303 * @author Chris Smith <chris@jalakai.co.uk> 304 * @param array $filter array of field/pattern pairs, empty array for no filter 305 * @return int 306 */ 307 public function getUserCount($filter = array()) { 308 msg("authorisation method does not provide user counts", -1); 309 return 0; 310 } 311 312 /** 313 * Bulk retrieval of user data [implement only where required/possible] 314 * 315 * Set getUsers capability when implemented 316 * 317 * @author Chris Smith <chris@jalakai.co.uk> 318 * @param int $start index of first user to be returned 319 * @param int $limit max number of users to be returned 320 * @param array $filter array of field/pattern pairs, null for no filter 321 * @return array list of userinfo (refer getUserData for internal userinfo details) 322 */ 323 public function retrieveUsers($start = 0, $limit = -1, $filter = null) { 324 msg("authorisation method does not support mass retrieval of user data", -1); 325 return array(); 326 } 327 328 /** 329 * Define a group [implement only where required/possible] 330 * 331 * Set addGroup capability when implemented 332 * 333 * @author Chris Smith <chris@jalakai.co.uk> 334 * @param string $group 335 * @return bool 336 */ 337 public function addGroup($group) { 338 msg("authorisation method does not support independent group creation", -1); 339 return false; 340 } 341 342 /** 343 * Retrieve groups [implement only where required/possible] 344 * 345 * Set getGroups capability when implemented 346 * 347 * @author Chris Smith <chris@jalakai.co.uk> 348 * @param int $start 349 * @param int $limit 350 * @return array 351 */ 352 public function retrieveGroups($start = 0, $limit = 0) { 353 msg("authorisation method does not support group list retrieval", -1); 354 return array(); 355 } 356 357 /** 358 * Return case sensitivity of the backend [OPTIONAL] 359 * 360 * When your backend is caseinsensitive (eg. you can login with USER and 361 * user) then you need to overwrite this method and return false 362 * 363 * @return bool 364 */ 365 public function isCaseSensitive() { 366 return true; 367 } 368 369 /** 370 * Sanitize a given username [OPTIONAL] 371 * 372 * This function is applied to any user name that is given to 373 * the backend and should also be applied to any user name within 374 * the backend before returning it somewhere. 375 * 376 * This should be used to enforce username restrictions. 377 * 378 * @author Andreas Gohr <andi@splitbrain.org> 379 * @param string $user username 380 * @return string the cleaned username 381 */ 382 public function cleanUser($user) { 383 return $user; 384 } 385 386 /** 387 * Sanitize a given groupname [OPTIONAL] 388 * 389 * This function is applied to any groupname that is given to 390 * the backend and should also be applied to any groupname within 391 * the backend before returning it somewhere. 392 * 393 * This should be used to enforce groupname restrictions. 394 * 395 * Groupnames are to be passed without a leading '@' here. 396 * 397 * @author Andreas Gohr <andi@splitbrain.org> 398 * @param string $group groupname 399 * @return string the cleaned groupname 400 */ 401 public function cleanGroup($group) { 402 return $group; 403 } 404 405 /** 406 * Check Session Cache validity [implement only where required/possible] 407 * 408 * DokuWiki caches user info in the user's session for the timespan defined 409 * in $conf['auth_security_timeout']. 410 * 411 * This makes sure slow authentication backends do not slow down DokuWiki. 412 * This also means that changes to the user database will not be reflected 413 * on currently logged in users. 414 * 415 * To accommodate for this, the user manager plugin will touch a reference 416 * file whenever a change is submitted. This function compares the filetime 417 * of this reference file with the time stored in the session. 418 * 419 * This reference file mechanism does not reflect changes done directly in 420 * the backend's database through other means than the user manager plugin. 421 * 422 * Fast backends might want to return always false, to force rechecks on 423 * each page load. Others might want to use their own checking here. If 424 * unsure, do not override. 425 * 426 * @param string $user - The username 427 * @author Andreas Gohr <andi@splitbrain.org> 428 * @return bool 429 */ 430 public function useSessionCache($user) { 431 global $conf; 432 return ($_SESSION[DOKU_COOKIE]['auth']['time'] >= @filemtime($conf['cachedir'].'/sessionpurge')); 433 } 434 435 /** 436 * Overrides the standard config loading to integrate old auth module style configs 437 * 438 * @deprecated 2012-11-09 439 */ 440 public function loadConfig() { 441 global $conf; 442 $plugin = $this->getPluginName(); 443 $oldname = preg_replace('/^auth/', '', $plugin); 444 445 $default = $this->readDefaultSettings(); 446 $oldconf = array(); 447 if(isset($conf['auth'][$oldname])) $oldconf = (array) $conf['auth'][$oldname]; 448 $conf['plugin'][$plugin] = array_merge($default, $oldconf, (array) $conf['plugin'][$plugin]); 449 450 $this->conf =& $conf['plugin'][$plugin]; 451 $this->configloaded = true; 452 } 453} 454