openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

AuthRunner.class.php (2191B)


      1 <?php
      2 
      3 
      4 namespace cms\auth;
      5 
      6 
      7 use cms\base\Configuration;
      8 use logger\Logger;
      9 
     10 /**
     11  * Executes multiple authentication modules.
     12  */
     13 class AuthRunner
     14 {
     15 	/**
     16 	 * Executes the callback with all modules of this section
     17 	 *
     18 	 * @param $section string a configuration setting under security/modules which must contain an array of strings
     19 	 * @param $callback callable anonymous function with a auth module as first parameter
     20 	 * @param $emptyResult mixed if the callback returns this value, the next value is executed.
     21 	 * @return mixed
     22 	 */
     23 	protected static function getModulesFor($section, $callback, $emptyResult )
     24 	{
     25 		$securityConfig = Configuration::subset('security');
     26 
     27 		$modules = $securityConfig->subset($section )->get('modules',[]);
     28 
     29 		foreach ($modules as $module) {
     30 
     31 			$moduleClass = Auth::NS . '\\' . $module . 'Auth';
     32 
     33 			if (!class_exists($moduleClass)) {
     34 				throw new \LogicException('module is not available: ' . $moduleClass );
     35 			}
     36 
     37 			/** @var \cms\auth\Auth $auth */
     38 			$auth = new $moduleClass;
     39 			$result = $callback( $auth );
     40 
     41 			if  ( $result != $emptyResult )
     42 				return $result;
     43 
     44 			// next module.
     45 		}
     46 
     47 		return $emptyResult;
     48 	}
     49 
     50 
     51 	/**
     52 	 * Search for a username in all modules of this section.
     53 	 *
     54 	 * @param $section
     55 	 * @return string username of null (if none found)
     56 	 */
     57 	public static function getUsername( $section ) {
     58 
     59 		return self::getModulesFor(/**
     60 		 * @param $auth Auth
     61 		 */ $section, function($auth) {
     62 			$username = $auth->username();
     63 
     64 			if ($username)
     65 				Logger::debug('Preselecting User ' . $username . ' from ' . get_class($auth) );
     66 
     67 			return $username;
     68 		},null);
     69 	}
     70 
     71 
     72 	/**
     73 	 * Makes an autorization through all modules of this section.
     74 	 *
     75 	 * @param $section
     76 	 * @param $user
     77 	 * @param $password
     78 	 * @param $token
     79 	 * @return int a bitmask of Auth::STATUS_*
     80 	 */
     81 	public static function checkLogin( $section, $user,$password,$token ) {
     82 
     83 		return self::getModulesFor($section, /**
     84 		 * @param $auth Auth
     85 		 * @return null|boolean|int
     86 		 */ function($auth) use ($token, $password, $user) {
     87 				Logger::info('Trying to login with module '.get_class($auth));
     88 
     89 				return $auth->login($user,$password,$token);
     90 			}, Auth::STATUS_FAILED
     91 		);
     92 	}
     93 }