/**
* This function transform the old authentication system to the new one
* based on APIs defined. It reads the old configuration and generates an
* authentication mapping for each API and version.
*
* @return boolean|string Boolean false if nothing was performed; string
* adapter name otherwise.
*/
public function transformAuthPerApis()
{
$oldAuth = $this->fetch();
if (!$oldAuth) {
return false;
}
$oldAuth = $oldAuth->getArrayCopy();
switch ($oldAuth['type']) {
case 'http_basic':
$adapter = ['name' => 'http_basic', 'type' => AuthenticationEntity::TYPE_BASIC, 'realm' => $oldAuth['realm'], 'htpasswd' => $oldAuth['htpasswd']];
break;
case 'http_digest':
$adapter = ['name' => 'http_digest', 'type' => AuthenticationEntity::TYPE_DIGEST, 'realm' => $oldAuth['realm'], 'htdigest' => $oldAuth['htdigest'], 'digest_domains' => $oldAuth['digest_domains'], 'nonce_timeout' => $oldAuth['nonce_timeout']];
break;
case AuthenticationEntity::TYPE_OAUTH2:
$adapter = ['type' => AuthenticationEntity::TYPE_OAUTH2, 'oauth2_type' => $oldAuth['dsn_type'], 'oauth2_dsn' => $oldAuth['dsn'], 'oauth2_route' => $oldAuth['route_match']];
switch ($oldAuth['dsn_type']) {
case AuthenticationEntity::DSN_PDO:
$adapter['name'] = 'oauth2_pdo';
$adapter['oauth2_username'] = $oldAuth['username'];
$adapter['oauth2_password'] = $oldAuth['password'];
break;
case AuthenticationEntity::DSN_MONGO:
$adapter['name'] = 'oauth2_mongo';
$adapter['oauth2_database'] = $oldAuth['database'];
break;
}
break;
}
// Save the authentication adapter
$this->saveAuthenticationAdapter($adapter);
// Create the authentication map for each API
$modules = $this->modules->getModules();
foreach ($modules as $module) {
foreach ($module->getVersions() as $version) {
$this->saveAuthenticationMap($adapter['name'], $module->getName(), $version);
}
}
// Remove the old configuration
$this->removeOldAuthentication();
return $adapter['name'];
}