Rename class Credentials to Credential

refs #4641
This commit is contained in:
Marius Hein 2013-08-30 10:24:05 +02:00
parent 83d20a6175
commit 7c8252a983
11 changed files with 80 additions and 79 deletions

View File

@ -27,7 +27,7 @@
# namespace Icinga\Application\Controllers;
use \Icinga\Web\Controller\ActionController;
use \Icinga\Authentication\Credentials;
use \Icinga\Authentication\Credential;
use \Icinga\Authentication\Manager as AuthManager;
use \Icinga\Form\Authentication\LoginForm;
use \Icinga\Exception\ConfigurationError;
@ -60,7 +60,7 @@ class AuthenticationController extends ActionController
public function loginAction()
{
$this->replaceLayout = true;
$credentials = new Credentials();
$credentials = new Credential();
$this->view->form = new LoginForm();
$this->view->form->setRequest($this->_request);
$this->view->title = "Icinga Web Login";

View File

@ -37,7 +37,7 @@ use \Icinga\Application\DbAdapterFactory;
use \Icinga\Exception\ProgrammingError;
use \Icinga\User;
use \Icinga\Authentication\UserBackend;
use \Icinga\Authentication\Credentials;
use \Icinga\Authentication\Credential;
use \Icinga\Authentication;
use \Icinga\Application\Logger;
use \Icinga\Exception\ConfigurationError;
@ -135,11 +135,11 @@ class DbUserBackend implements UserBackend
/**
* Check if the user identified by the given credentials is available
*
* @param Credentials $credential
* @param Credential $credential
*
* @return boolean True when the username is known and currently active.
*/
public function hasUsername(Credentials $credential)
public function hasUsername(Credential $credential)
{
if ($this->db === null) {
Logger::warn('Ignoring hasUsername in database as no connection is available');
@ -152,11 +152,11 @@ class DbUserBackend implements UserBackend
/**
* Authenticate a user with the given credentials
*
* @param Credentials $credential
* @param Credential $credential
*
* @return User|null The authenticated user or Null.
*/
public function authenticate(Credentials $credential)
public function authenticate(Credential $credential)
{
if ($this->db === null) {
Logger::warn('Ignoring database authentication as no connection is available');

View File

@ -32,7 +32,7 @@ use \stdClass;
use \Zend_Config;
use \Icinga\User;
use \Icinga\Authentication\UserBackend;
use \Icinga\Authentication\Credentials;
use \Icinga\Authentication\Credential;
use \Icinga\Protocol\Ldap;
use Icinga\Protocol\Ldap\Connection;
use \Icinga\Application\Config as IcingaConfig;
@ -88,11 +88,11 @@ class LdapUserBackend implements UserBackend
/**
* Test if the username exists
*
* @param Credentials $credential
* @param Credential $credential
*
* @return bool
*/
public function hasUsername(Credentials $credential)
public function hasUsername(Credential $credential)
{
return $this->connection->fetchOne(
$this->selectUsername($credential->getUsername())
@ -136,11 +136,11 @@ class LdapUserBackend implements UserBackend
/**
* Authenticate
*
* @param Credentials $credentials
* @param Credential $credentials
*
* @return User
*/
public function authenticate(Credentials $credentials)
public function authenticate(Credential $credentials)
{
if (!$this->connection->testCredentials(
$this->connection->fetchDN($this->selectUsername($credentials->getUsername())),

View File

@ -35,7 +35,7 @@ namespace Icinga\Authentication;
* passwords as primitives in order to allow additional information
* to be provided (like the domain) when needed.
*/
class Credentials
class Credential
{
/**
* Username

View File

@ -269,12 +269,12 @@ class Manager
/**
* Find a backend for a credential
*
* @param Credentials $credentials
* @param Credential $credentials
*
* @return UserBackend|null
* @throws ConfigurationError
*/
private function getBackendForCredentials(Credentials $credentials)
private function getBackendForCredential(Credential $credentials)
{
$authErrors = 0;
@ -324,16 +324,16 @@ class Manager
}
/**
* Try to authenticate the current user with the Credentials (@see Credentials).
* Try to authenticate the current user with the Credential (@see Credential).
*
* @param Credentials $credentials The credentials to use for authentication
* @param Credential $credentials The credentials to use for authentication
* @param Boolean $persist Whether to persist the authentication result
* in the current session
*
* @return Boolean true on success, otherwise false
* @throws ConfigError
*/
public function authenticate(Credentials $credentials, $persist = true)
public function authenticate(Credential $credentials, $persist = true)
{
if (count($this->userBackends) === 0) {
Logger::error('AuthManager: No authentication backend provided, your users will never be able to login.');
@ -344,7 +344,7 @@ class Manager
);
}
$userBackend = $this->getBackendForCredentials($credentials);
$userBackend = $this->getBackendForCredential($credentials);
if ($userBackend === null) {
Logger::info('AuthManager: Unknown user %s tried to log in', $credentials->getUsername());

View File

@ -30,6 +30,7 @@ namespace Icinga\Authentication;
use \Zend_Config;
use \Icinga\User;
use Icinga\Authentication\Credential;
/**
* Public api for an user backend object
@ -46,20 +47,20 @@ interface UserBackend
/**
* Test if the username exists
*
* @param Credentials $credentials
* @param Credential $credentials
*
* @return bool
*/
public function hasUsername(Credentials $credentials);
public function hasUsername(Credential $credentials);
/**
* Authenticate
*
* @param Credentials $credentials
* @param Credential $credentials
*
* @return User
*/
public function authenticate(Credentials $credentials);
public function authenticate(Credential $credentials);
/**
* Name of the backend

View File

@ -12,20 +12,20 @@ use Icinga\Test\BaseTestCase;
// @codingStandardsIgnoreStart
require_once 'Zend/Config.php';
require_once BaseTestCase::$libDir . '/Authentication/Credentials.php';
require_once BaseTestCase::$libDir . '/Authentication/Credential.php';
require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php';
require_once BaseTestCase::$libDir . '/User.php';
// @codingStandardsIgnoreEnd
use \Zend_Config;
use \Icinga\Authentication\Credentials as Credentials;
use \Icinga\Authentication\Credential;
use \Icinga\Authentication\UserBackend as UserBackend;
use \Icinga\User;
/**
* Simple backend mock that takes an config object
* with the property "credentials", which is an array
* of Credentials this backend authenticates
* of Credential this backend authenticates
**/
class BackendMock implements UserBackend
{
@ -49,7 +49,7 @@ class BackendMock implements UserBackend
}
}
public function hasUsername(Credentials $userCredentials)
public function hasUsername(Credential $userCredentials)
{
foreach ($this->allowedCredentials as $credential) {
if ($credential->getUsername() == $userCredentials->getUsername()) {
@ -79,12 +79,12 @@ class BackendMock implements UserBackend
'user@test.local'
);
}
public function getUserCount() {
return count($this->allowedCredentials);
}
public function authenticate(Credentials $credentials)
public function authenticate(Credential $credentials)
{
if (!in_array($credentials, $this->allowedCredentials)) {
return false;

View File

@ -45,7 +45,7 @@ require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php';
require_once BaseTestCase::$libDir . '/Protocol/Ldap/Exception.php';
require_once BaseTestCase::$libDir . '/Application/DbAdapterFactory.php';
require_once BaseTestCase::$libDir . '/Application/Config.php';
require_once BaseTestCase::$libDir . '/Authentication/Credentials.php';
require_once BaseTestCase::$libDir . '/Authentication/Credential.php';
require_once BaseTestCase::$libDir . '/Authentication/Backend/DbUserBackend.php';
require_once BaseTestCase::$libDir . '/User.php';
require_once BaseTestCase::$libDir . '/Application/Logger.php';
@ -56,7 +56,7 @@ use \Zend_Db_Adapter_Pdo_Abstract;
use \Zend_Config;
use \Icinga\Authentication\Backend\DbUserBackend;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\Authentication\Credentials;
use \Icinga\Authentication\Credential;
use \Icinga\User;
use \Icinga\Application\Config;
@ -189,7 +189,7 @@ class DbUserBackendTest extends BaseTestCase
// Known user
$this->assertTrue(
$backend->hasUsername(
new Credentials(
new Credential(
$this->userData[0][self::USER_NAME_COLUMN],
$this->userData[0][self::PASSWORD_COLUMN]
)
@ -200,7 +200,7 @@ class DbUserBackendTest extends BaseTestCase
// Unknown user
$this->assertFalse(
$backend->hasUsername(
new Credentials(
new Credential(
'unknown user',
'secret'
)
@ -211,7 +211,7 @@ class DbUserBackendTest extends BaseTestCase
// Inactive user
$this->assertFalse(
$backend->hasUsername(
new Credentials(
new Credential(
$this->userData[2][self::USER_NAME_COLUMN],
$this->userData[2][self::PASSWORD_COLUMN]
)
@ -230,7 +230,7 @@ class DbUserBackendTest extends BaseTestCase
// Known user
$this->assertNotNull(
$backend->authenticate(
new Credentials(
new Credential(
$this->userData[0][self::USER_NAME_COLUMN],
$this->userData[0][self::PASSWORD_COLUMN]
)
@ -241,7 +241,7 @@ class DbUserBackendTest extends BaseTestCase
// Wrong password
$this->assertNull(
$backend->authenticate(
new Credentials(
new Credential(
$this->userData[1][self::USER_NAME_COLUMN],
'wrongpassword'
)
@ -252,7 +252,7 @@ class DbUserBackendTest extends BaseTestCase
// Nonexisting user
$this->assertNull(
$backend->authenticate(
new Credentials(
new Credential(
'nonexisting user',
$this->userData[1][self::PASSWORD_COLUMN]
)
@ -263,7 +263,7 @@ class DbUserBackendTest extends BaseTestCase
// Inactive user
$this->assertNull(
$backend->authenticate(
new Credentials(
new Credential(
$this->userData[2][self::USER_NAME_COLUMN],
$this->userData[2][self::PASSWORD_COLUMN]
)

View File

@ -12,21 +12,21 @@ use Icinga\Test\BaseTestCase;
// @codingStandardsIgnoreStart
require_once 'Zend/Config.php';
require_once BaseTestCase::$libDir . '/Authentication/Credentials.php';
require_once BaseTestCase::$libDir . '/Authentication/Credential.php';
require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php';
require_once BaseTestCase::$libDir . '/User.php';
// @codingStandardsIgnoreEnd
use \Exception;
use \Zend_Config;
use \Icinga\Authentication\Credentials as Credentials;
use \Icinga\Authentication\Credential;
use \Icinga\Authentication\UserBackend as UserBackend;
use \Icinga\User;
/**
* Simple backend mock that takes an config object
* with the property "credentials", which is an array
* of Credentials this backend authenticates
* of Credential this backend authenticates
**/
class ErrorProneBackendMock implements UserBackend
{
@ -56,12 +56,12 @@ class ErrorProneBackendMock implements UserBackend
/**
* Test if the username exists
*
* @param Credentials $credentials
* @param Credential $credentials
*
* @return bool
* @throws Exception
*/
public function hasUsername(Credentials $credentials)
public function hasUsername(Credential $credentials)
{
throw new Exception('hasUsername error: ' . $credentials->getUsername());
}
@ -69,12 +69,12 @@ class ErrorProneBackendMock implements UserBackend
/**
* Authenticate
*
* @param Credentials $credentials
* @param Credential $credentials
*
* @return User
* @throws Exception
*/
public function authenticate(Credentials $credentials)
public function authenticate(Credential $credentials)
{
throw new Exception('authenticate error: ' . $credentials->getUsername());
}

View File

@ -32,14 +32,14 @@ namespace Tests\Icinga\Authentication;
require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCase.php');
// @codingStandardsIgnoreEnd
use Icinga\Authentication\Credentials;
use Icinga\Authentication\Credential;
use \Icinga\Test\BaseTestCase;
// @codingStandardsIgnoreStart
require_once 'Zend/Config.php';
require_once BaseTestCase::$libDir . '/Protocol/Ldap/Connection.php';
require_once BaseTestCase::$libDir . '/Protocol/Ldap/Query.php';
require_once BaseTestCase::$libDir . '/Authentication/Credentials.php';
require_once BaseTestCase::$libDir . '/Authentication/Credential.php';
require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php';
require_once BaseTestCase::$libDir . '/Authentication/Backend/LdapUserBackend.php';
// @codingStandardsIgnoreEnd
@ -183,9 +183,9 @@ class LdapUserBackendTest extends BaseTestCase
public function testHasUsername()
{
$backend = new LdapUserBackend($this->createBackendConfig());
$this->assertTrue($backend->hasUsername(new Credentials('jwoe')));
$this->assertTrue($backend->hasUsername(new Credentials('rmiles')));
$this->assertFalse($backend->hasUsername(new Credentials('DoesNotExist')));
$this->assertTrue($backend->hasUsername(new Credential('jwoe')));
$this->assertTrue($backend->hasUsername(new Credential('rmiles')));
$this->assertFalse($backend->hasUsername(new Credential('DoesNotExist')));
}
/**
@ -197,17 +197,17 @@ class LdapUserBackendTest extends BaseTestCase
$this->assertInstanceOf(
'\Icinga\User',
$backend->authenticate(new Credentials('jwoe', 'passjwoe'))
$backend->authenticate(new Credential('jwoe', 'passjwoe'))
);
$this->assertFalse($backend->authenticate(new Credentials('jwoe', 'passjwoe22')));
$this->assertFalse($backend->authenticate(new Credential('jwoe', 'passjwoe22')));
$this->assertInstanceOf(
'\Icinga\User',
$backend->authenticate(new Credentials('rmiles', 'passrmiles'))
$backend->authenticate(new Credential('rmiles', 'passrmiles'))
);
$this->assertFalse($backend->authenticate(new Credentials('rmiles', 'passrmiles33')));
$this->assertFalse($backend->authenticate(new Credential('rmiles', 'passrmiles33')));
}
/**
@ -217,6 +217,6 @@ class LdapUserBackendTest extends BaseTestCase
public function testAuthenticateUnknownUser()
{
$backend = new LdapUserBackend($this->createBackendConfig());
$this->assertFalse($backend->authenticate(new Credentials('unknown123', 'passunknown123')));
$this->assertFalse($backend->authenticate(new Credential('unknown123', 'passunknown123')));
}
}

View File

@ -40,7 +40,7 @@ require_once 'Zend/Log.php';
require_once 'Zend/Config.php';
require_once BaseTestCase::$libDir . '/Application/Logger.php';
require_once BaseTestCase::$libDir . '/Authentication/Manager.php';
require_once BaseTestCase::$libDir . '/Authentication/Credentials.php';
require_once BaseTestCase::$libDir . '/Authentication/Credential.php';
require_once BaseTestCase::$libDir . '/Exception/ConfigurationError.php';
require_once 'BackendMock.php';
require_once 'ErrorProneBackendMock.php';
@ -49,7 +49,7 @@ require_once 'SessionMock.php';
use \Zend_Config;
use \Icinga\Authentication\Manager as AuthManager;
use \Icinga\Authentication\Credentials;
use \Icinga\Authentication\Credential;
use \Icinga\Exception\ConfigurationError;
/**
@ -60,9 +60,9 @@ class ManagerTest extends BaseTestCase
public function getTestCredentials()
{
return array(
new Credentials("jdoe", "passjdoe"),
new Credentials("root", "passroot"),
new Credentials("test", "passtest")
new Credential("jdoe", "passjdoe"),
new Credential("root", "passroot"),
new Credential("test", "passtest")
);
}
@ -115,7 +115,7 @@ class ManagerTest extends BaseTestCase
$this->assertTrue(
$authMgr->authenticate(
new Credentials('jdoe', 'passjdoe')
new Credential('jdoe', 'passjdoe')
)
);
@ -138,19 +138,19 @@ class ManagerTest extends BaseTestCase
$auth = $this->getManagerInstance();
$this->assertFalse(
$auth->authenticate(
new Credentials("jhoe", "passjdoe"),
new Credential("jhoe", "passjdoe"),
false
)
);
$this->assertFalse(
$auth->authenticate(
new Credentials("joe", "passjhoe"),
new Credential("joe", "passjhoe"),
false
)
);
$this->assertTrue(
$auth->authenticate(
new Credentials("jdoe", "passjdoe"),
new Credential("jdoe", "passjdoe"),
false
)
);
@ -161,7 +161,7 @@ class ManagerTest extends BaseTestCase
$session = new SessionMock();
$auth = $this->getManagerInstance($session, true);
$this->assertFalse($auth->isAuthenticated(true));
$auth->authenticate(new Credentials("jdoe", "passjdoe"));
$auth->authenticate(new Credential("jdoe", "passjdoe"));
$this->assertNotEquals(null, $session->get("user"));
$user = $session->get("user");
$this->assertEquals("Username", $user->getUsername());
@ -186,7 +186,7 @@ class ManagerTest extends BaseTestCase
{
$auth = $this->getManagerInstance($session, false);
$this->assertFalse($auth->isAuthenticated(true));
$auth->authenticate(new Credentials("jdoe", "passjdoe"));
$auth->authenticate(new Credential("jdoe", "passjdoe"));
}
/**
@ -213,7 +213,7 @@ class ManagerTest extends BaseTestCase
);
$authManager->authenticate(
new Credentials('jdoe', 'passjdoe')
new Credential('jdoe', 'passjdoe')
);
}
@ -250,7 +250,7 @@ class ManagerTest extends BaseTestCase
);
$authManager->authenticate(
new Credentials('jdoe', 'passjdoe')
new Credential('jdoe', 'passjdoe')
);
}
@ -272,20 +272,20 @@ class ManagerTest extends BaseTestCase
$authManager->getUserBackend('provider1')->setCredentials(
array(
new Credentials('p1-user1', 'p1-passwd1'),
new Credentials('p1-user2', 'p1-passwd2')
new Credential('p1-user1', 'p1-passwd1'),
new Credential('p1-user2', 'p1-passwd2')
)
);
$authManager->getUserBackend('provider2')->setCredentials(
array(
new Credentials('p2-user1', 'p2-passwd1'),
new Credentials('p2-user2', 'p2-passwd2')
new Credential('p2-user1', 'p2-passwd1'),
new Credential('p2-user2', 'p2-passwd2')
)
);
$this->assertTrue(
$authManager->authenticate(new Credentials('p2-user2', 'p2-passwd2'))
$authManager->authenticate(new Credential('p2-user2', 'p2-passwd2'))
);
}
@ -323,27 +323,27 @@ class ManagerTest extends BaseTestCase
$authManager->getUserBackend('provider4')->setCredentials(
array(
new Credentials('p4-user1', 'p4-passwd1'),
new Credentials('p4-user2', 'p4-passwd2')
new Credential('p4-user1', 'p4-passwd1'),
new Credential('p4-user2', 'p4-passwd2')
)
);
$session->isOpen = true;
$this->assertTrue(
$authManager->authenticate(new Credentials('p4-user2', 'p4-passwd2'))
$authManager->authenticate(new Credential('p4-user2', 'p4-passwd2'))
);
$session->isOpen = true;
$this->assertTrue(
$authManager->authenticate(new Credentials('p4-user1', 'p4-passwd1'))
$authManager->authenticate(new Credential('p4-user1', 'p4-passwd1'))
);
$session->isOpen = true;
$this->assertFalse(
$authManager->authenticate(new Credentials('p4-user2', 'p4-passwd1-WRONG123123'))
$authManager->authenticate(new Credential('p4-user2', 'p4-passwd1-WRONG123123'))
);
}