entryPoints, version folder

This commit is contained in:
Yuri Kuznetsov
2014-01-27 15:03:06 +02:00
parent a2d83d2fd4
commit b1214f9418
13 changed files with 179 additions and 157 deletions
View File
+1 -5
View File
@@ -1,11 +1,7 @@
<?php <?php
require_once('../bootstrap.php'); require_once('../../bootstrap.php');
$app = new \Espo\Core\Application(); $app = new \Espo\Core\Application();
$app->run(); $app->run();
?>
+51 -20
View File
@@ -10,6 +10,8 @@ class Application
private $container; private $container;
private $slim; private $slim;
private $auth;
/** /**
* Constructor * Constructor
@@ -23,23 +25,32 @@ class Application
set_error_handler(array($this->getLog(), 'catchError'), E_ALL); set_error_handler(array($this->getLog(), 'catchError'), E_ALL);
set_exception_handler(array($this->getLog(), 'catchException')); set_exception_handler(array($this->getLog(), 'catchException'));
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
$this->slim = $this->container->get('slim');
$this->metadata = $this->container->get('metadata');
$this->initMetadata();
} }
public function getSlim() public function getSlim()
{ {
if (empty($this->slim)) {
$this->slim = $this->container->get('slim');
}
return $this->slim; return $this->slim;
} }
public function getMetadata() public function getMetadata()
{ {
if (empty($this->metadata)) {
$this->metadata = $this->container->get('metadata');
}
return $this->metadata; return $this->metadata;
} }
protected function getAuth()
{
if (empty($this->auth)) {
$this->auth = new \Espo\Core\Utils\Auth($this->container);
}
return $this->auth;
}
public function getContainer() public function getContainer()
{ {
@@ -57,22 +68,44 @@ class Application
$this->initRoutes(); $this->initRoutes();
$this->getSlim()->run(); $this->getSlim()->run();
} }
public function runEntryPoint($entryPoint)
protected function initMetadata() {
{ if (empty($entryPoint)) {
$isNotCached = !$this->getMetadata()->isCached(); throw new \Error();
}
$this->getMetadata()->init($isNotCached);
} $slim = $this->getSlim();
$container = $this->getContainer();
$slim->get('/', function() {});
$entryPointManager = new \Espo\Core\EntryPointManager($container);
$auth = $this->getAuth();
$apiAuth = new \Espo\Core\Utils\Api\Auth($auth, $entryPointManager->checkAuthRequired($entryPoint), true);
$slim->add($apiAuth);
$slim->hook('slim.before.dispatch', function () use ($entryPoint, $entryPointManager, $container) {
try {
$entryPointManager->run($entryPoint);
} catch (\Exception $e) {
$container->get('output')->processError($e->getMessage(), $e->getCode());
}
});
$slim->run();
}
protected function routeHooks() protected function routeHooks()
{ {
$container = $this->getContainer(); $container = $this->getContainer();
$slim = $this->getSlim(); $slim = $this->getSlim();
$auth = new \Espo\Core\Utils\Api\Auth($container->get('entityManager'), $container); $auth = $this->getAuth();
$this->getSlim()->add($auth);
$apiAuth = new \Espo\Core\Utils\Api\Auth($auth);
$this->getSlim()->add($apiAuth);
$this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container) { $this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container) {
@@ -119,7 +152,6 @@ class Application
} catch (\Exception $e) { } catch (\Exception $e) {
$container->get('output')->processError($e->getMessage(), $e->getCode()); $container->get('output')->processError($e->getMessage(), $e->getCode());
} }
}); });
$this->getSlim()->hook('slim.after.router', function () use (&$slim) { $this->getSlim()->hook('slim.after.router', function () use (&$slim) {
@@ -150,6 +182,5 @@ class Application
} }
} }
} }
} }
-7
View File
@@ -87,13 +87,6 @@ class Container
); );
} }
private function loadEntryPointManager()
{
return new \Espo\Core\EntryPointManager(
$this
);
}
private function loadLog() private function loadLog()
{ {
return new \Espo\Core\Utils\Log( return new \Espo\Core\Utils\Log(
+16 -16
View File
@@ -9,6 +9,7 @@ use \Espo\Core\Exceptions\NotFound,
class EntryPointManager class EntryPointManager
{ {
private $container; private $container;
private $fileManager; private $fileManager;
protected $data = null; protected $data = null;
@@ -33,26 +34,33 @@ class EntryPointManager
private $customPaths = array( private $customPaths = array(
'corePath' => 'application/Espo/Custom/EntryPoints', 'corePath' => 'application/Espo/Custom/EntryPoints',
'modulePath' => 'application/Espo/Custom/Modules/{*}/EntryPoints', 'modulePath' => 'application/Espo/Custom/Modules/{*}/EntryPoints',
); );
public function __construct(\Espo\Core\Container $container) public function __construct(\Espo\Core\Container $container)
{ {
$this->container = $container; $this->container = $container;
$this->fileManager = $this->getContainer()->get('fileManager'); $this->fileManager = $container->get('fileManager');
} }
protected function getContainer() protected function getContainer()
{ {
return $this->container; return $this->container;
} }
protected function getFileManager() protected function getFileManager()
{ {
return $this->fileManager; return $this->fileManager;
} }
public function checkAuthRequired($name)
{
$className = $this->get($name);
if ($className === false) {
throw new NotFound();
}
return $className::$authRequired;
}
public function run($name) public function run($name)
{ {
@@ -60,14 +68,12 @@ class EntryPointManager
if ($className === false) { if ($className === false) {
throw new NotFound(); throw new NotFound();
} }
$entryPoint = new $className($this->container); $entryPoint = new $className($this->container);
$entryPoint->run(); $entryPoint->run();
} }
protected function get($name = '')
public function get($name = '')
{ {
if (!isset($this->data)) { if (!isset($this->data)) {
$this->init(); $this->init();
@@ -86,12 +92,11 @@ class EntryPointManager
} }
public function getAll() protected function getAll()
{ {
return $this->get(); return $this->get();
} }
protected function init() protected function init()
{ {
if (file_exists($this->cacheFile) && $this->getContainer()->get('config')->get('useCache')) { if (file_exists($this->cacheFile) && $this->getContainer()->get('config')->get('useCache')) {
@@ -142,12 +147,7 @@ class EntryPointManager
} }
return $entryPoints; return $entryPoints;
} }
}
}
+4 -15
View File
@@ -10,7 +10,7 @@ abstract class Base
{ {
private $container; private $container;
protected $authRequired = true; public static $authRequired = true;
protected function getContainer() protected function getContainer()
{ {
@@ -29,12 +29,12 @@ abstract class Base
protected function getEntityManager() protected function getEntityManager()
{ {
$this->getContainer()->get('entityManager'); return $this->getContainer()->get('entityManager');
} }
protected function getServiceFactory() protected function getServiceFactory()
{ {
$this->getContainer()->get('serviceFactory'); return $this->getContainer()->get('serviceFactory');
} }
protected function getConfig() protected function getConfig()
@@ -45,22 +45,11 @@ abstract class Base
protected function getMetadata() protected function getMetadata()
{ {
return $this->getContainer()->get('metadata'); return $this->getContainer()->get('metadata');
} }
protected function checkAccess()
{
if ($this->authRequired) {
return $this->getUser()->isFetched();
}
return false;
}
public function __construct(Container $container) public function __construct(Container $container)
{ {
$this->container = $container; $this->container = $container;
if (!$this->checkAccess()) {
throw new Forbidden();
}
} }
abstract public function run(); abstract public function run();
+41 -39
View File
@@ -6,34 +6,41 @@ use \Espo\Core\Utils\Api\Slim;
class Auth extends \Slim\Middleware class Auth extends \Slim\Middleware
{ {
private $entityManager; protected $auth;
private $container; protected $authRequired = null;
protected $showDialog = false;
public function __construct(\Espo\Core\ORM\EntityManager $entityManager, \Espo\Core\Container $container) public function __construct(\Espo\Core\Utils\Auth $auth, $authRequired = null, $showDialog = false)
{ {
$this->entityManager = $entityManager; $this->auth = $auth;
$this->container = $container; $this->authRequired = $authRequired;
} $this->showDialog = $showDialog;
}
function call() function call()
{ {
$req = $this->app->request(); $req = $this->app->request();
$res = $this->app->response();
$uri = $req->getResourceUri(); $uri = $req->getResourceUri();
$httpMethod = $req->getMethod(); $httpMethod = $req->getMethod();
/** if (is_null($this->authRequired)) {
* Check if user credentials are required for current route $routes = $this->app->router()->getMatchedRoutes($httpMethod, $uri);
*/
$routes = $this->app->router()->getMatchedRoutes($httpMethod, $uri);
if (!empty($routes[0])) { if (!empty($routes[0])) {
$routeConditions = $routes[0]->getConditions(); $routeConditions = $routes[0]->getConditions();
if (isset($routeConditions['auth']) && $routeConditions['auth'] === false) { if (isset($routeConditions['auth']) && $routeConditions['auth'] === false) {
$this->container->setUser($this->entityManager->getEntity('User')); $this->auth->useNoAuth();
$this->next->call(); $this->next->call();
return;
}
}
} else {
if (!$this->authRequired) {
$this->auth->useNoAuth();
$this->next->call();
return; return;
} }
} }
@@ -41,39 +48,34 @@ class Auth extends \Slim\Middleware
$authKey = $req->headers('PHP_AUTH_USER'); $authKey = $req->headers('PHP_AUTH_USER');
$authSec = $req->headers('PHP_AUTH_PW'); $authSec = $req->headers('PHP_AUTH_PW');
$GLOBALS['log']->add('DEBUG', 'AUTH: Try to authenticate');
if ($authKey && $authSec) { if ($authKey && $authSec) {
$isAuthenticated = false; $isAuthenticated = false;
$username = $authKey; $username = $authKey;
$password = $authSec; $password = $authSec;
$user = $this->entityManager->getRepository('User')->findOne(array( $isAuthenticated = $this->auth->login($username, $password);
'whereClause' => array(
'userName' => $username,
'password' => md5($password)
),
));
if ($user instanceof \Espo\Entities\User) {
$this->entityManager->setUser($user);
$this->container->setUser($user);
$isAuthenticated = true;
}
$GLOBALS['log']->add('DEBUG', 'AUTH: Result of authenticate =['.$isAuthenticated.']');
if ($isAuthenticated) { if ($isAuthenticated) {
$this->next->call(); $this->next->call();
} else { } else {
$res->header('WWW-Authenticate'); $this->processUnauthorized();
$res->status(401);
} }
} else { } else {
$res->header('WWW-Authenticate'); $this->processUnauthorized();
$res->status(401);
} }
} }
protected function processUnauthorized()
{
$res = $this->app->response();
if ($this->showDialog) {
$res->header('WWW-Authenticate', sprintf('Basic realm="%s"', ''));
} else {
$res->header('WWW-Authenticate');
}
$res->status(401);
}
} }
+1 -3
View File
@@ -31,7 +31,6 @@ class Output
ob_clean(); ob_clean();
echo $data; echo $data;
//$this->getSlim()->stop();
} }
public function processError($message = 'Error', $code = 500) public function processError($message = 'Error', $code = 500)
@@ -56,7 +55,7 @@ class Output
{ {
$GLOBALS['log']->add('INFO', 'Display Error: '.$text.', Code: '.$statusCode.' URL: '.$_SERVER['REQUEST_URI']); $GLOBALS['log']->add('INFO', 'Display Error: '.$text.', Code: '.$statusCode.' URL: '.$_SERVER['REQUEST_URI']);
if ( !empty( $this->slim) ) { if (!empty( $this->slim)) {
$this->getSlim()->response()->status($statusCode); $this->getSlim()->response()->status($statusCode);
$this->getSlim()->response()->header('X-Status-Reason', $text); $this->getSlim()->response()->header('X-Status-Reason', $text);
$this->getSlim()->stop(); $this->getSlim()->stop();
@@ -69,4 +68,3 @@ class Output
} }
?>
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace Espo\Core\Utils;
class Auth
{
protected $container;
public function __construct(\Espo\Core\Container $container)
{
$this->container = $container;
}
public function useNoAuth()
{
$entityManager = $this->container->get('entityManager');
$this->container->setUser($entityManager->getEntity('User'));
}
public function login($username, $password)
{
$GLOBALS['log']->add('DEBUG', 'AUTH: Try to authenticate');
$entityManager = $this->container->get('entityManager');
$user = $entityManager->getRepository('User')->findOne(array(
'whereClause' => array(
'userName' => $username,
'password' => md5($password)
),
));
if ($user instanceof \Espo\Entities\User) {
$entityManager->setUser($user);
$this->container->setUser($user);
$GLOBALS['log']->add('DEBUG', 'AUTH: Result of authenticate =[' . $isAuthenticated . ']');
return true;
}
}
}
+2
View File
@@ -26,6 +26,8 @@ class Metadata
$this->fileManager = $fileManager; $this->fileManager = $fileManager;
$this->converter = new \Espo\Core\Utils\Database\Converter($this, $this->fileManager); $this->converter = new \Espo\Core\Utils\Database\Converter($this, $this->fileManager);
$this->init(!$this->isCached());
} }
protected function getConfig() protected function getConfig()
+9 -7
View File
@@ -8,10 +8,11 @@ use \Espo\Core\Exceptions\BadRequest;
class Download extends \Espo\Core\EntryPoints\Base class Download extends \Espo\Core\EntryPoints\Base
{ {
protected $authRequired = true; public static $authRequired = true;
public function run() public function run()
{ {
$id = $_GET['id']; $id = $_GET['id'];
if (empty($id)) { if (empty($id)) {
throw new BadRequest(); throw new BadRequest();
@@ -19,21 +20,22 @@ class Download extends \Espo\Core\EntryPoints\Base
$attachment = $this->getEntityManager()->getEntity('Attachment', $id); $attachment = $this->getEntityManager()->getEntity('Attachment', $id);
if ($attachment) { if (!$attachment) {
throw new NotFound(); throw new NotFound();
} }
if ($enity->get('parentId') && $enity->get('parentType')) { if ($attachment->get('parentId') && $attachment->get('parentType')) {
$parent = $this->getEntityManager()->getEntity($enity->get('parentType'), $enity->get('parentId')); $parent = $this->getEntityManager()->getEntity($attachment->get('parentType'), $attachment->get('parentId'));
if (!$this->getAcl()->check($parent)) { if (!$this->getAcl()->check($parent)) {
throw new Forbidden(); throw new Forbidden();
} }
} }
$fileName = "data/upload/{$attachment->id}"; $fileName = "data/upload/{$attachment->id}";
if (!file_exists($fileName)) {
if (!file_exists($fileName)) {
throw new NotFound(); throw new NotFound();
} }
header('Content-Description: File Transfer'); header('Content-Description: File Transfer');
if ($attachment->get('type')) { if ($attachment->get('type')) {
+2 -45
View File
@@ -1,50 +1,7 @@
<?php <?php
chdir(dirname(__FILE__)); chdir(dirname(__FILE__));
set_include_path( dirname(__FILE__) ); set_include_path(dirname(__FILE__));
require_once "vendor/autoload.php"; require_once "vendor/autoload.php";
//error_reporting(-1);
/*
use Doctrine\ORM\Tools\Setup;
$isDevMode = true;
//JSON Driver for Doctrine
use Doctrine\ORM\Mapping\Driver\JsonDriver;
$config = Setup::createConfiguration($isDevMode, null, null);
$config->setMetadataDriverImpl(new JsonDriver(array(__DIR__."/data/cache/doctrine/metadata")));
//END: JSON Driver for Doctrine
*/
// database configuration parameters
/*$conn = array(
'driver' => 'pdo_sqlite',
'path' => __DIR__ . '/db.sqlite',
); */
/*$conn = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'jet_doctrine_last',
); */
/*$conn = array(
'driver' => 'pdo_mysql',
//'driver' => 'mysqli',
'host' => 'localhost',
'dbname' => 'jetcrm',
'user' => 'root',
'password' => ''
);
// obtaining the entity manager
//$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
$em = \Doctrine\ORM\EntityManager::create($conn, $config);
*/
?>
+11
View File
@@ -0,0 +1,11 @@
<?php
if (empty($_GET['entryPoint'])) {
include "main.html";
} else {
include "bootstrap.php";
$app = new \Espo\Core\Application();
$app->runEntryPoint($_GET['entryPoint']);
}