entryPoints, version folder
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
<?php
|
||||
|
||||
require_once('../bootstrap.php');
|
||||
|
||||
require_once('../../bootstrap.php');
|
||||
|
||||
$app = new \Espo\Core\Application();
|
||||
|
||||
$app->run();
|
||||
|
||||
|
||||
?>
|
||||
@@ -11,6 +11,8 @@ class Application
|
||||
|
||||
private $slim;
|
||||
|
||||
private $auth;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
@@ -24,23 +26,32 @@ class Application
|
||||
set_exception_handler(array($this->getLog(), 'catchException'));
|
||||
|
||||
date_default_timezone_set('UTC');
|
||||
|
||||
$this->slim = $this->container->get('slim');
|
||||
$this->metadata = $this->container->get('metadata');
|
||||
|
||||
$this->initMetadata();
|
||||
}
|
||||
|
||||
public function getSlim()
|
||||
{
|
||||
if (empty($this->slim)) {
|
||||
$this->slim = $this->container->get('slim');
|
||||
}
|
||||
return $this->slim;
|
||||
}
|
||||
|
||||
public function getMetadata()
|
||||
{
|
||||
if (empty($this->metadata)) {
|
||||
$this->metadata = $this->container->get('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()
|
||||
{
|
||||
return $this->container;
|
||||
@@ -58,12 +69,32 @@ class Application
|
||||
$this->getSlim()->run();
|
||||
}
|
||||
|
||||
|
||||
protected function initMetadata()
|
||||
public function runEntryPoint($entryPoint)
|
||||
{
|
||||
$isNotCached = !$this->getMetadata()->isCached();
|
||||
if (empty($entryPoint)) {
|
||||
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()
|
||||
@@ -71,8 +102,10 @@ class Application
|
||||
$container = $this->getContainer();
|
||||
$slim = $this->getSlim();
|
||||
|
||||
$auth = new \Espo\Core\Utils\Api\Auth($container->get('entityManager'), $container);
|
||||
$this->getSlim()->add($auth);
|
||||
$auth = $this->getAuth();
|
||||
|
||||
$apiAuth = new \Espo\Core\Utils\Api\Auth($auth);
|
||||
$this->getSlim()->add($apiAuth);
|
||||
|
||||
$this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container) {
|
||||
|
||||
@@ -119,7 +152,6 @@ class Application
|
||||
} catch (\Exception $e) {
|
||||
$container->get('output')->processError($e->getMessage(), $e->getCode());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
$this->getSlim()->hook('slim.after.router', function () use (&$slim) {
|
||||
@@ -150,6 +182,5 @@ class Application
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -87,13 +87,6 @@ class Container
|
||||
);
|
||||
}
|
||||
|
||||
private function loadEntryPointManager()
|
||||
{
|
||||
return new \Espo\Core\EntryPointManager(
|
||||
$this
|
||||
);
|
||||
}
|
||||
|
||||
private function loadLog()
|
||||
{
|
||||
return new \Espo\Core\Utils\Log(
|
||||
|
||||
@@ -9,6 +9,7 @@ use \Espo\Core\Exceptions\NotFound,
|
||||
class EntryPointManager
|
||||
{
|
||||
private $container;
|
||||
|
||||
private $fileManager;
|
||||
|
||||
protected $data = null;
|
||||
@@ -36,11 +37,10 @@ class EntryPointManager
|
||||
);
|
||||
|
||||
|
||||
|
||||
public function __construct(\Espo\Core\Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->fileManager = $this->getContainer()->get('fileManager');
|
||||
$this->fileManager = $container->get('fileManager');
|
||||
}
|
||||
|
||||
protected function getContainer()
|
||||
@@ -53,6 +53,14 @@ class EntryPointManager
|
||||
return $this->fileManager;
|
||||
}
|
||||
|
||||
public function checkAuthRequired($name)
|
||||
{
|
||||
$className = $this->get($name);
|
||||
if ($className === false) {
|
||||
throw new NotFound();
|
||||
}
|
||||
return $className::$authRequired;
|
||||
}
|
||||
|
||||
public function run($name)
|
||||
{
|
||||
@@ -60,14 +68,12 @@ class EntryPointManager
|
||||
if ($className === false) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
$entryPoint = new $className($this->container);
|
||||
|
||||
$entryPoint->run();
|
||||
}
|
||||
|
||||
|
||||
public function get($name = '')
|
||||
protected function get($name = '')
|
||||
{
|
||||
if (!isset($this->data)) {
|
||||
$this->init();
|
||||
@@ -86,12 +92,11 @@ class EntryPointManager
|
||||
}
|
||||
|
||||
|
||||
public function getAll()
|
||||
protected function getAll()
|
||||
{
|
||||
return $this->get();
|
||||
}
|
||||
|
||||
|
||||
protected function init()
|
||||
{
|
||||
if (file_exists($this->cacheFile) && $this->getContainer()->get('config')->get('useCache')) {
|
||||
@@ -144,10 +149,5 @@ class EntryPointManager
|
||||
return $entryPoints;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ abstract class Base
|
||||
{
|
||||
private $container;
|
||||
|
||||
protected $authRequired = true;
|
||||
public static $authRequired = true;
|
||||
|
||||
protected function getContainer()
|
||||
{
|
||||
@@ -29,12 +29,12 @@ abstract class Base
|
||||
|
||||
protected function getEntityManager()
|
||||
{
|
||||
$this->getContainer()->get('entityManager');
|
||||
return $this->getContainer()->get('entityManager');
|
||||
}
|
||||
|
||||
protected function getServiceFactory()
|
||||
{
|
||||
$this->getContainer()->get('serviceFactory');
|
||||
return $this->getContainer()->get('serviceFactory');
|
||||
}
|
||||
|
||||
protected function getConfig()
|
||||
@@ -47,20 +47,9 @@ abstract class Base
|
||||
return $this->getContainer()->get('metadata');
|
||||
}
|
||||
|
||||
protected function checkAccess()
|
||||
{
|
||||
if ($this->authRequired) {
|
||||
return $this->getUser()->isFetched();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
if (!$this->checkAccess()) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
}
|
||||
|
||||
abstract public function run();
|
||||
|
||||
@@ -6,33 +6,40 @@ use \Espo\Core\Utils\Api\Slim;
|
||||
|
||||
class Auth extends \Slim\Middleware
|
||||
{
|
||||
private $entityManager;
|
||||
protected $auth;
|
||||
|
||||
private $container;
|
||||
protected $authRequired = null;
|
||||
|
||||
public function __construct(\Espo\Core\ORM\EntityManager $entityManager, \Espo\Core\Container $container)
|
||||
protected $showDialog = false;
|
||||
|
||||
public function __construct(\Espo\Core\Utils\Auth $auth, $authRequired = null, $showDialog = false)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->container = $container;
|
||||
$this->auth = $auth;
|
||||
$this->authRequired = $authRequired;
|
||||
$this->showDialog = $showDialog;
|
||||
}
|
||||
|
||||
function call()
|
||||
{
|
||||
$req = $this->app->request();
|
||||
$res = $this->app->response();
|
||||
|
||||
$uri = $req->getResourceUri();
|
||||
$httpMethod = $req->getMethod();
|
||||
|
||||
/**
|
||||
* Check if user credentials are required for current route
|
||||
*/
|
||||
if (is_null($this->authRequired)) {
|
||||
$routes = $this->app->router()->getMatchedRoutes($httpMethod, $uri);
|
||||
|
||||
if (!empty($routes[0])) {
|
||||
$routeConditions = $routes[0]->getConditions();
|
||||
if (isset($routeConditions['auth']) && $routeConditions['auth'] === false) {
|
||||
$this->container->setUser($this->entityManager->getEntity('User'));
|
||||
$this->auth->useNoAuth();
|
||||
$this->next->call();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!$this->authRequired) {
|
||||
$this->auth->useNoAuth();
|
||||
$this->next->call();
|
||||
return;
|
||||
}
|
||||
@@ -41,39 +48,34 @@ class Auth extends \Slim\Middleware
|
||||
$authKey = $req->headers('PHP_AUTH_USER');
|
||||
$authSec = $req->headers('PHP_AUTH_PW');
|
||||
|
||||
$GLOBALS['log']->add('DEBUG', 'AUTH: Try to authenticate');
|
||||
|
||||
if ($authKey && $authSec) {
|
||||
$isAuthenticated = false;
|
||||
|
||||
$username = $authKey;
|
||||
$password = $authSec;
|
||||
|
||||
$user = $this->entityManager->getRepository('User')->findOne(array(
|
||||
'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.']');
|
||||
$isAuthenticated = $this->auth->login($username, $password);
|
||||
|
||||
if ($isAuthenticated) {
|
||||
$this->next->call();
|
||||
} else {
|
||||
$res->header('WWW-Authenticate');
|
||||
$res->status(401);
|
||||
$this->processUnauthorized();
|
||||
}
|
||||
} else {
|
||||
$this->processUnauthorized();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,6 @@ class Output
|
||||
|
||||
ob_clean();
|
||||
echo $data;
|
||||
//$this->getSlim()->stop();
|
||||
}
|
||||
|
||||
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']);
|
||||
|
||||
if ( !empty( $this->slim) ) {
|
||||
if (!empty( $this->slim)) {
|
||||
$this->getSlim()->response()->status($statusCode);
|
||||
$this->getSlim()->response()->header('X-Status-Reason', $text);
|
||||
$this->getSlim()->stop();
|
||||
@@ -69,4 +68,3 @@ class Output
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ class Metadata
|
||||
$this->fileManager = $fileManager;
|
||||
|
||||
$this->converter = new \Espo\Core\Utils\Database\Converter($this, $this->fileManager);
|
||||
|
||||
$this->init(!$this->isCached());
|
||||
}
|
||||
|
||||
protected function getConfig()
|
||||
|
||||
@@ -8,10 +8,11 @@ use \Espo\Core\Exceptions\BadRequest;
|
||||
|
||||
class Download extends \Espo\Core\EntryPoints\Base
|
||||
{
|
||||
protected $authRequired = true;
|
||||
public static $authRequired = true;
|
||||
|
||||
public function run()
|
||||
{
|
||||
|
||||
$id = $_GET['id'];
|
||||
if (empty($id)) {
|
||||
throw new BadRequest();
|
||||
@@ -19,18 +20,19 @@ class Download extends \Espo\Core\EntryPoints\Base
|
||||
|
||||
$attachment = $this->getEntityManager()->getEntity('Attachment', $id);
|
||||
|
||||
if ($attachment) {
|
||||
if (!$attachment) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
if ($enity->get('parentId') && $enity->get('parentType')) {
|
||||
$parent = $this->getEntityManager()->getEntity($enity->get('parentType'), $enity->get('parentId'));
|
||||
if ($attachment->get('parentId') && $attachment->get('parentType')) {
|
||||
$parent = $this->getEntityManager()->getEntity($attachment->get('parentType'), $attachment->get('parentId'));
|
||||
if (!$this->getAcl()->check($parent)) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
}
|
||||
|
||||
$fileName = "data/upload/{$attachment->id}";
|
||||
|
||||
if (!file_exists($fileName)) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
+2
-45
@@ -1,50 +1,7 @@
|
||||
<?php
|
||||
|
||||
chdir(dirname(__FILE__));
|
||||
set_include_path( dirname(__FILE__) );
|
||||
set_include_path(dirname(__FILE__));
|
||||
|
||||
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);
|
||||
*/
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user