<?php
/**
Copyright 2011-2014 Nick Korbel
Copyright 2012-2014 Alois Schloegl
This file is part of Booked Scheduler.
Booked Scheduler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Booked Scheduler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Booked Scheduler. If not, see <http://www.gnu.org/licenses/>.
*/
require_once(ROOT_DIR . 'lib/Application/Authentication/namespace.php');
class Customapp extends Authentication implements IAuthentication
{
private $authToDecorate;
private $_registration;
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $auth;
/**
* @var string
*/
private $password;
/** @var Customapp_User */
private $user;
public function SetRegistration($registration)
{
$this->_registration = $registration;
}
private function GetRegistration()
{
if ($this->_registration == null)
{
$this->_registration = new Registration();
}
return $this->_registration;
}
public function __construct(Authentication $authentication)
{
$this->authToDecorate = $authentication;
require_once(ROOT_DIR . 'plugins/Authentication/customapp/api.php');
if (!class_exists('Customapp_authenticate'))
{
throw new Exception('Could not load Customapp authentication.');
}
$this->auth = new customapp_authenticate;
if( !isset($_SESSION['login']['username']) || !isset($_SESSION['booked']) )
{
header('Location: /logout.php');
}
}
public function Validate($username, $password)
{
$username = $_SESSION['login']['username'];
if ($this->user = $this->auth->authenticate($username))
{
return true;
}
return false;
}
public function Login($username, $loginContext)
{
$username = $_SESSION['login']['username'];
if ($this->UserExists())
{
$this->Synchronize();
if($this->auth->isAdmin($username))
{
require_once(ROOT_DIR . 'plugins/Authentication/customapp/booked.php');
$bookedApi = new Customapp_booked;
$bookedApi->addToAdmin($bookedApi->getUserId($username));
}
}
return $this->authToDecorate->Login($username, $loginContext);
}
public function Logout(UserSession $user)
{
$this->authToDecorate->Logout($user);
}
public function AreCredentialsKnown()
{
return (bool)$_SESSION['login']['username'];
}
public function ShowUsernamePrompt()
{
return false;
}
public function ShowPasswordPrompt()
{
return false;
}
public function ShowPersistLoginPrompt()
{
return false;
}
public function ShowForgotPasswordPrompt()
{
return false;
}
private function UserExists()
{
return $this->user != null;
}
private function Synchronize()
{
$registration = $this->GetRegistration();
$email = $this->user->email != ''?$this->user->email:$this->user->f_name.'.'.$this->user->l_name.'@no-reply.com';
$registration->Synchronize(
new AuthenticatedUser(
$this->user->username,
$email,
$this->user->f_name,
$this->user->l_name,
'',
Configuration::Instance()->GetKey(ConfigKeys::LANGUAGE),
Configuration::Instance()->GetDefaultTimezone(),
null, null, null)
);
}
}
?>