creates test taker and adds it to LDAP group when logging with ldap credentials for first time

This commit is contained in:
s396447 2018-01-17 19:43:52 +01:00
parent 5bcd1fda62
commit 8b75aafd53

View File

@ -36,6 +36,11 @@ use oat\generisHard\models\hardsql\Exception;
use oat\oatbox\user\auth\LoginAdapter;
use Zend\Authentication\Adapter\Ldap;
use common_persistence_Manager;
use oat\taoTestTaker\models\CrudService;
use core_kernel_classes_Class;
use oat\oatbox\user\LoginService;
use oat\taoGroups\models\CrudGroupsService;
use oat\taoGroups\models\GroupsService;
/**
* Adapter to authenticate users stored in the Ldap implementation
@ -123,10 +128,12 @@ class LdapAdapter implements LoginAdapter
$mapping = $this->hasOption(self::OPTION_USER_MAPPING)
? $this->getOption(self::OPTION_USER_MAPPING)
: array();
$factory = new LdapUserFactory($mapping);
$user = $factory->createUser($params);
return $user;
//TODO: change this
$user = $this->createTestTaker($this->getUsername(), $this->getPassword(), $params);
$this->addUserToGroup($user, 'LDAP');
return LoginService::authenticate($this->getUsername(), $this->getPassword());
} else {
throw new core_kernel_users_InvalidLoginException('User "'.$this->getUsername().'" failed LDAP authentication.');
@ -135,6 +142,57 @@ class LdapAdapter implements LoginAdapter
}
private function createTestTaker($login, $password, $params)
{
$testTakerCrudService = CrudService::singleton();
$testTakerClass = new core_kernel_classes_Class(('http://www.tao.lu/Ontologies/TAO.rdf#User'));
$data = [
PROPERTY_USER_LOGIN => $login,
PROPERTY_USER_PASSWORD => $password,
RDFS_LABEL => $login . ' - ' . $params['givenname'] . ' ' . $params['sn'],
PROPERTY_USER_FIRSTNAME => $params['givenname'],
PROPERTY_USER_LASTNAME => $params['sn'],
];
try {
$testTaker = $testTakerCrudService->createFromLdapData($data);
} catch (\common_exception_PreConditionFailure $e) {
//TODO: throw better exception
throw new core_kernel_users_InvalidLoginException('Error while creating test taker: ' . $login);
}
return $testTaker;
}
private function addUserToGroup($user, $groupLabel)
{
$groupCrudService = CrudGroupsService::singleton();
$groupService = GroupsService::singleton();
$groupClass = $groupService->getRootClass();
$instances = $groupClass->searchInstances(array(
RDFS_LABEL => $groupLabel
), array(
'recursive' => true,
'like' => false
));
if (count($instances)) {
$group = current($instances);
} else {
$group = $groupCrudService->createFromArray([
RDFS_LABEL => $groupLabel
]);
}
$groupService->addUser($user->getUri(), $group);
}
/**
* @param \Zend\Authentication\Adapter\Ldap $adapter
*/
@ -202,3 +260,4 @@ class LdapAdapter implements LoginAdapter
}