56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace EesyPHP\Auth;
|
|
|
|
use EesyPHP\App;
|
|
use EesyPHP\Auth\User;
|
|
use EesyPHP\Config;
|
|
use EesyPHP\Log;
|
|
use function EesyPHP\cast;
|
|
use function EesyPHP\vardump;
|
|
|
|
use phpCAS;
|
|
|
|
class Casuser extends Backend {
|
|
|
|
/**
|
|
* Retrieve CAS attribute value(s) from CAS authenticated user
|
|
* @param string $attr The CAS attribute name
|
|
* @param mixed $default The default value to return if the CAS attribute is undefined
|
|
* (optional, default: null)
|
|
* @param string|null $cast The expected type of value (optional, default: string)
|
|
*/
|
|
public static function get_attr($attr, $default=null, $cast=null) {
|
|
if (!phpCAS::hasAttribute($attr))
|
|
return $default;
|
|
return cast(phpCAS::getAttribute($attr), $cast?$cast:'string');
|
|
}
|
|
|
|
/**
|
|
* Retrieve a user by its username
|
|
* @param string $username
|
|
* @return \EesyPHP\Auth\User|null|false The user object if found, null it not, false in case of error
|
|
*/
|
|
public static function get_user($username) {
|
|
if (!phpCAS :: isAuthenticated()) {
|
|
Log::error("get_user(%s): phpCAS not authenticated, can't compute user");
|
|
return null;
|
|
}
|
|
|
|
$info = array();
|
|
foreach(Config::get('auth.cas.user_attributes') as $name => $attr_config) {
|
|
$cas_name = Config::get("cas_name", null, 'string', false, $attr_config);
|
|
$alt_cas_name = Config::get("alt_cas_name", $name, 'string', false, $attr_config);
|
|
if (!$cas_name || is_null(self :: get_attr($cas_name)))
|
|
$cas_name = $alt_cas_name;
|
|
$info[$name] = self :: get_attr(
|
|
$cas_name?$cas_name:$name,
|
|
Config::get("default", null, null, false, $attr_config)
|
|
);
|
|
}
|
|
Log::debug('User "%s" info computed from CAS attributes:\n%s', $username, vardump($info));
|
|
return new User($username, '\\EesyPHP\\Auth\\Casuser', $info);
|
|
}
|
|
|
|
}
|