From eb00602c6db66dc73e914f3b4a682fdda37acbfb Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 27 Nov 2013 12:44:36 +0200 Subject: [PATCH] controller services changes --- application/Espo/Controllers/Account.php | 9 ++ application/Espo/Core/Controllers/Base.php | 14 ++- application/Espo/Core/Controllers/Record.php | 89 +++++++++++--------- application/Espo/Core/ServiceFactory.php | 3 +- application/Espo/Core/Services/Base.php | 9 ++ application/Espo/Core/Utils/Api/Auth.php | 1 + application/Espo/Services/Record.php | 49 +++++++++++ 7 files changed, 128 insertions(+), 46 deletions(-) create mode 100644 application/Espo/Controllers/Account.php create mode 100644 application/Espo/Core/Services/Base.php create mode 100644 application/Espo/Services/Record.php diff --git a/application/Espo/Controllers/Account.php b/application/Espo/Controllers/Account.php new file mode 100644 index 0000000000..73d6acbc73 --- /dev/null +++ b/application/Espo/Controllers/Account.php @@ -0,0 +1,9 @@ +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; } diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index 1513361476..ca6b927016 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -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(); - } -} + } + diff --git a/application/Espo/Core/ServiceFactory.php b/application/Espo/Core/ServiceFactory.php index b9f44fec0e..21d9b6f86e 100644 --- a/application/Espo/Core/ServiceFactory.php +++ b/application/Espo/Core/ServiceFactory.php @@ -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"); } - } diff --git a/application/Espo/Core/Services/Base.php b/application/Espo/Core/Services/Base.php new file mode 100644 index 0000000000..c4b1f88601 --- /dev/null +++ b/application/Espo/Core/Services/Base.php @@ -0,0 +1,9 @@ +entityManager->getRepository('\Espo\Entities\User')->findOneBy(array('username' => $username)); if ($user) { if ($password == $user->getPassword()) { diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php new file mode 100644 index 0000000000..e1880db513 --- /dev/null +++ b/application/Espo/Services/Record.php @@ -0,0 +1,49 @@ +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) + { + + } +}