1
0
Fork 0
MySQLDumper/library/Msd/Auth/Adapter/Ini.php

122 Zeilen
2,7 KiB
PHP

2011-06-10 21:55:32 +00:00
<?php
/**
* This file is part of MySQLDumper released under the GNU/GPL 2 license
* http://www.mysqldumper.net
*
* @package MySQLDumper
* @subpackage Auth_adapter
* @version SVN: $Rev$
* @author $Author$
*/
/**
* Adapter to use Zend_Auth with INI files.
*
* @package MySQLDumper
* @subpackage Auth_adapter
*/
class Msd_Auth_Adapter_Ini implements Zend_Auth_Adapter_Interface
{
/**
* User database
*
* @var array
*/
private $_users = null;
/**
* Username for authentication.
*
* @var string
*/
private $_username = null;
/**
* Password for authentication.
*
* @var string
*/
private $_password = null;
/**
* Class constructor
*
* @param string $iniFilename Filename for registered users
*
2012-08-04 10:40:48 +00:00
* @throws Msd_Exception
*
* @return Msd_Auth_Adapter_Ini
2011-06-10 21:55:32 +00:00
*/
public function __construct($iniFilename)
{
if (!file_exists($iniFilename)) {
throw new Msd_Exception(
'INI file with authentication information doesn\'t exists!'
);
}
$this->_users = parse_ini_file($iniFilename, true);
}
/**
* set the username, which is used for authentication.
*
2012-08-04 10:40:48 +00:00
* @param string $username The username
*
2011-06-10 21:55:32 +00:00
* @return void
*/
public function setUsername($username)
{
$this->_username = (string)$username;
2011-06-10 21:55:32 +00:00
}
/**
* Set the password, which is used for authentication.
*
2012-08-04 10:40:48 +00:00
* @param string $password The password
*
2011-06-10 21:55:32 +00:00
* @return void
*/
public function setPassword($password)
{
$this->_password = (string)$password;
2011-06-10 21:55:32 +00:00
}
/**
* Authenticate with the given credentials.
*
2012-08-04 10:40:48 +00:00
* @throws Msd_Exception
*
2011-06-10 21:55:32 +00:00
* @return Zend_Auth_Result
*/
public function authenticate()
{
if ($this->_username == null || $this->_password == null) {
throw new Msd_Exception(
'You must set the username and password first!'
);
}
$authResult = false;
foreach ($this->_users['users']['user'] as $name => $pass) {
if ($this->_username == $name && md5($this->_password) == $pass) {
2011-06-10 21:55:32 +00:00
$authResult = array(
'id' => $name,
'name' => $name,
2011-06-10 21:55:32 +00:00
);
}
}
if ($authResult === false) {
return new Zend_Auth_Result(
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
array(),
array('L_LOGIN_INVALID_USER')
);
}
return new Zend_Auth_Result(
Zend_Auth_Result::SUCCESS,
$authResult
);
}
}