mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
controller services changes
This commit is contained in:
9
application/Espo/Controllers/Account.php
Normal file
9
application/Espo/Controllers/Account.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Controllers;
|
||||
|
||||
|
||||
class Account extends \Espo\Core\Controllers\Record
|
||||
{
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
9
application/Espo/Core/Services/Base.php
Normal file
9
application/Espo/Core/Services/Base.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Espo\Core\Services;
|
||||
|
||||
abstract class Base
|
||||
{
|
||||
static public $dependencies = array();
|
||||
|
||||
}
|
||||
@@ -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()) {
|
||||
|
||||
49
application/Espo/Services/Record.php
Normal file
49
application/Espo/Services/Record.php
Normal 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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user