1
0
Fork 0
MySQLDumper/library/Msd/User.php

252 Zeilen
6,2 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 Users
* @version SVN: $Rev$
* @author $Author$
*/
/**
* Class for user login and logout actions.
*
* @package MySQLDumper
* @subpackage Users
*/
class Msd_User
{
/**
* The executed process was successfully completed.
*
* @var int
*/
2012-08-04 10:40:48 +00:00
const SUCCESS = 0x00;
2011-06-10 21:55:32 +00:00
/**
* There is no file with user identities and credentials.
*
* @var int
*/
2012-08-04 10:40:48 +00:00
const NO_USER_FILE = 0x01;
2011-06-10 21:55:32 +00:00
/**
* The user file doesn't contain any valid user logins.
*
* @var int
*/
2012-08-04 10:40:48 +00:00
const NO_VALID_USER = 0x02;
2011-06-10 21:55:32 +00:00
/**
* The given identity is unknown or the password is wrong.
*
* @var int
*/
2012-08-04 10:40:48 +00:00
const UNKNOWN_IDENTITY = 0x03;
2011-06-10 21:55:32 +00:00
/**
* An unknown error occured.
*
* @var int
*/
2012-08-04 10:40:48 +00:00
const GENERAL_FAILURE = 0xFF;
2011-06-10 21:55:32 +00:00
/**
* Path and filename of the user ini file.
*
* @var string
*/
private $_usersFile;
/**
* Instance to authentication storage.
*
* @var Zend_Auth_Storage_Session
*/
private $_authStorage = null;
/**
* Id of currently loggedin user.
*
* @var int
*/
private $_userId = null;
/**
* Name of currently loggedin user.
*
* @var string
*/
private $_userName = null;
/**
* Current login status.
*
* @var boolean
*/
private $_isLoggedIn = false;
/**
* Messages from Zend_Auth_Result.
*
* @var array
*/
private $_authMessages = array();
/**
* Constructor
*
2012-08-04 10:40:48 +00:00
* @return Msd_User
2011-06-10 21:55:32 +00:00
*/
public function __construct()
{
2012-08-04 10:40:48 +00:00
$this->_usersFile = APPLICATION_PATH . '/configs/users.ini';
2011-06-10 21:55:32 +00:00
$this->_authStorage = new Zend_Auth_Storage_Session();
2012-08-04 10:40:48 +00:00
$auth = $this->_authStorage->read();
2011-06-10 21:55:32 +00:00
if (!empty($auth)) {
if (isset($auth['name'])) {
$this->_userName = $auth['name'];
}
if (isset($auth['id'])) {
$this->_userId = $auth['id'];
}
if ($this->_userName !== null && $this->_userId !== null) {
$this->_isLoggedIn = true;
}
} else {
$this->_loginByCookie();
}
}
/**
* Returns the messages which comes from Zend_Auth_Result.
*
* @return array
*/
public function getAuthMessages()
{
return $this->_authMessages;
}
/**
* Return the loggedin status.
*
* @return boolean
*/
public function isLoggedIn()
{
return $this->_isLoggedIn;
}
/**
* Login the user with the given identity and credentials.
* Set cookie if automatic login is wanted.
*
* Returns true if login was successful, otherwise false.
*
* @param string $username Identity for login process.
* @param string $password Credentials for login procress.
* @param boolean $autoLogin Set cookie for automatic login?
*
* @return int
*/
public function login($username, $password, $autoLogin = false)
{
if (!file_exists($this->_usersFile)) {
return self::NO_USER_FILE;
}
2012-08-04 10:40:48 +00:00
$usersConfig = new Msd_Ini($this->_usersFile);
$users = $usersConfig->get('user');
2011-06-10 21:55:32 +00:00
$hasValidUser = false;
foreach ($users as $user) {
if (isset($user['name']) && isset($user['pass'])) {
$hasValidUser = true;
break;
}
}
if (!$hasValidUser) {
return self::NO_VALID_USER;
}
$authAdapter = new Msd_Auth_Adapter_Ini($this->_usersFile);
$authAdapter->setUsername($username);
$authAdapter->setPassword($password);
2012-08-04 10:40:48 +00:00
$auth = Zend_Auth::getInstance();
$authResult = $auth->authenticate($authAdapter);
2011-06-10 21:55:32 +00:00
$this->_authMessages = $authResult->getMessages();
if ($authResult->isValid()) {
$this->_isLoggedIn = true;
if ($autoLogin) {
Zend_Session::regenerateId();
2012-08-04 10:40:48 +00:00
$crypt = Msd_Crypt::getInstance('MySQLDumper27112010');
2011-06-10 21:55:32 +00:00
$identity = $crypt->encrypt(
$username . ':' . $password
);
if (PHP_SAPI != 'cli') {
setcookie(
'msd_autologin',
$identity . ':' . md5($identity),
time() + 365 * 24 * 60 * 60,
'/'
);
}
}
$this->setDefaultConfiguration();
return self::SUCCESS;
}
return self::UNKNOWN_IDENTITY;
}
private function _loginByCookie()
{
$request = Zend_Controller_Front::getInstance()->getRequest();
2012-08-04 10:40:48 +00:00
$cookie = $request->get('msd_autologin');
2011-06-10 21:55:32 +00:00
if ($cookie === null || $cookie == '') {
// no cookie found
return false;
}
list($authInfo, $checksum) = explode(':', $cookie);
if (md5($authInfo) != $checksum) {
// autologin not valid - return
return false;
}
$crypt = Msd_Crypt::getInstance('MySQLDumper27112010');
list($username, $pass) = explode(':', $crypt->decrypt($authInfo));
// Try to login the user and refresh the cookie. Because you want
// to stay logged in until you logout.
$this->login($username, $pass, true);
}
2012-08-04 10:40:48 +00:00
2011-06-10 21:55:32 +00:00
/**
* Clear the user identity and logout the user.
*
* @return void
*/
public function logout()
{
Zend_Auth::getInstance()->clearIdentity();
$this->_isLoggedIn = false;
$this->setDefaultConfiguration();
}
/**
* Set default configuration for user
*
2012-08-04 10:40:48 +00:00
* @return void
2011-06-10 21:55:32 +00:00
*/
public function setDefaultConfiguration()
{
$configFile = 'defaultConfig';
if ($this->_isLoggedIn) {
$files = Msd_File::getConfigNames();
if (isset($files[0])) {
$configFile = $files[0];
}
}
Msd_Configuration::getInstance($configFile, true);
}
}