controller services changes

This commit is contained in:
Yuri Kuznetsov
2013-11-27 12:44:36 +02:00
parent fb3985fb2e
commit eb00602c6d
7 changed files with 128 additions and 46 deletions

View File

@@ -0,0 +1,9 @@
<?php
namespace Espo\Controllers;
class Account extends \Espo\Core\Controllers\Record
{
}

View File

@@ -13,7 +13,7 @@ abstract class Base
private $serviceFactory;
private $serviceClassName = null;
protected $serviceClassName = null;
private $service = null;
@@ -28,6 +28,9 @@ abstract class Base
}
$this->name = $name;
//echo $this->name;
//die;
if (empty($this->serviceClassName)) {
$moduleName = $this->getMetadata()->getScopeModuleName($this->name);
if ($moduleName) {
@@ -37,7 +40,7 @@ abstract class Base
}
}
}
protected function getContainer()
{
return $this->container;
@@ -68,12 +71,17 @@ abstract class Base
return $this->serviceFactory;
}
protected function loadService()
{
$this->service = $this->getServiceFactory()->createByClassName($this->serviceClassName);
}
protected function getService()
{
if (!empty($this->service)) {
return $this->service;
}
$this->service = $this->getServiceFactory()->createByClassName($this->serviceClassName);
$this->loadService();
return $this->service;
}

View File

@@ -7,34 +7,41 @@ use \Espo\Core\Exceptions\Forbidden;
abstract class Record extends Base
{
protected $serviceClassName = '\\Espo\\Services\\Record';
protected function loadService()
{
parent::loadService();
$this->service->setEntityName($this->name);
}
protected function actionRead($params)
{
$id = $params['id'];
$service = $this->getService();
$entity = $service->getEntity($id);
if (!$this->getAcl()->check($entity, 'read')) {
throw new Forbidden();
}
return $entity;
}
protected function actionUpdate($params, $data)
{
$id = $params['id'];
$service = $this->getService();
$entity = $service->getEntity($id);
if (!$this->getAcl()->check($entity, 'edit')) {
throw new Forbidden();
}
if ($service->updateEntity($entity, $data)) {
return $entity;
}
throw new Error();
}
@@ -43,29 +50,29 @@ abstract class Record extends Base
if (!$this->getAcl()->check($this->name, 'edit')) {
throw new Forbidden();
}
$service = $this->getService();
if ($entity = $service->postEntity($data)) {
return $entity;
}
throw new Error();
}
protected function actionList($params, $where)
{
if (!$this->getAcl()->check($this->name, 'read')) {
throw new Forbidden();
}
$service = $this->getService();
$where = $data['where'];
$offset = $data['offset'];
$limit = $data['limit'];
$asc = $data['asc'];
$sortBy = $data['sortBy'];
$entityList = $service->findEntities({
'where' => $where,
'offset' => $offset,
@@ -73,66 +80,66 @@ abstract class Record extends Base
'asc' => $asc,
'sortBy' => $sortBy,
});
return $entityList;
}
protected function actionDelete($params)
{
$id = $params['id'];
$service = $this->getService();
$entity = $service->getEntity($id);
$service = $this->getService();
$entity = $service->getEntity($id);
if (!$this->getAcl()->check($entity, 'delete')) {
throw new Forbidden();
}
if ($service->deleteEntity($entity)) {
return true;
}
throw new Error();
}
protected function actionMassUpdate($params, $data)
{
if (!$this->getAcl()->check($this->name, 'edit')) {
throw new Forbidden();
}
$service = $this->getService();
$ids = $data['ids'];
$where = $data['where'];
$idsUpdated = $service->massUpdate($ids, $where);
return $idsUpdated;
}
protected function actionMassDelete($params, $data)
{
if (!$this->getAcl()->check($this->name, 'delete')) {
throw new Forbidden();
}
$service = $this->getService();
$ids = $data['ids'];
$where = $data['where'];
$idsDeleted = $service->massDelete($ids, $where);
return $idsDeleted;
}
protected function actionListLinked($params, $data)
{
$id = $params['id'];
$link = $params['link'];
$service = $this->getService();
$entity = $service->getEntity($id);
$foreignEntityName = $entity->defs['links'][$link]['entity'];
if (!$this->getAcl()->check($entity, 'read')) {
throw new Forbidden();
}
@@ -145,36 +152,36 @@ abstract class Record extends Base
$limit = $data['limit'];
$asc = $data['asc'];
$sortBy = $data['sortBy'];
$entityList = $service->findLinkedEntities($entity, $link, {
'where' => $where,
'offset' => $offset,
'limit' => $limit,
'asc' => $asc,
'sortBy' => $sortBy,
});
});
return $entityList;
}
protected function actionCreateLink($params)
{
$id = $params['id'];
$link = $params['link'];
$foreignId = $params['foreignId'];
$service = $this->getService();
$entity = $service->getEntity($id);
$foreignEntityName = $entity->defs['links'][$link]['entity'];
if (!$this->getAcl()->check($entity, 'edit')) {
throw new Forbidden();
}
if ($service->linkEntity($entity, $link, $foreignId)) {
return true;
}
throw new Error();
}
}
}

View File

@@ -20,7 +20,7 @@ class ServiceFactory
{
if (class_exists($className)) {
$service = new $className();
$dependencies = $service->dependencies;
$dependencies = $service::dependencies;
foreach ($dependencies as $name) {
$setMethod = 'set' . ucfirst($name);
$service->$setMethod($this->container->get($name));
@@ -29,5 +29,4 @@ class ServiceFactory
}
throw new Error("Class '$className' does not exist");
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace Espo\Core\Services;
abstract class Base
{
static public $dependencies = array();
}

View File

@@ -48,6 +48,7 @@ class Auth extends \Slim\Middleware
$username = $authKey;
$password = $authSec;
$user = $this->entityManager->getRepository('\Espo\Entities\User')->findOneBy(array('username' => $username));
if ($user) {
if ($password == $user->getPassword()) {

View File

@@ -0,0 +1,49 @@
<?php
namespace Espo\Services;
class Record extends \Espo\Core\Services\Base
{
static public $dependencies = array(
'entityManager',
'user',
);
private $user;
private $entityManager;
protected $entityName;
public function setEntityName($entityName)
{
$this->entityName = $entityName;
}
public function setEntityManager($entityManager)
{
$this->entityManager = $entityManager;
}
public function setUser($user)
{
$this->user = $user;
}
public function getEntityManager()
{
return $this->entityManager;
}
public function getUser()
{
return $this->user;
}
public function getEntity($id)
{
}
}