Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 473a21d

Browse files
author
Jamie Snape
committed
Tidy producer model and controller
1 parent 74af1c9 commit 473a21d

File tree

3 files changed

+141
-85
lines changed

3 files changed

+141
-85
lines changed

core/models/base/CommunityModelBase.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,16 @@ public function delete($communityDao)
272272
$communityDao->saved = false;
273273
}
274274

275-
/** check if the policy is valid
275+
/**
276+
* Check whether the given policy is valid for the given community and user.
276277
*
277-
* @param FolderDao $folderDao
278-
* @param UserDao $userDao
279-
* @param type $policy
280-
* @return boolean
278+
* @param CommunityDao $communityDao community DAO
279+
* @param null|UserDao $userDao user DAO
280+
* @param int $policy policy
281+
* @return bool true if the given policy is valid for the given community and user
282+
* @throws Zend_Exception
281283
*/
282-
public function policyCheck($communityDao, $userDao = null, $policy = 0)
284+
public function policyCheck($communityDao, $userDao = null, $policy = MIDAS_POLICY_READ)
283285
{
284286
if (!$communityDao instanceof CommunityDao || !is_numeric($policy)) {
285287
throw new Zend_Exception(

modules/tracker/controllers/ProducerController.php

Lines changed: 89 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,23 @@ public function listAction()
4848
{
4949
$this->disableLayout();
5050

51-
/** @var int $commId */
52-
$commId = $this->getParam('communityId');
53-
if (!isset($commId)) {
54-
throw new Zend_Exception('Must pass communityId parameter');
51+
/** @var int $communityId */
52+
$communityId = $this->getParam('communityId');
53+
54+
if (!isset($communityId)) {
55+
throw new Zend_Exception('The required communityId parameter is missing');
5556
}
5657

57-
/** @var CommunityDao $comm */
58-
$comm = $this->Community->load($commId);
59-
if (!$comm || !$this->Community->policyCheck($comm, $this->userSession->Dao, MIDAS_POLICY_READ)
58+
/** @var CommunityDao $communityDao */
59+
$communityDao = $this->Community->load($communityId);
60+
61+
if ($communityDao === false || $this->Community->policyCheck($communityDao, $this->userSession->Dao, MIDAS_POLICY_READ) === false
6062
) {
61-
throw new Zend_Exception('Read permission required on the community', 403);
63+
throw new Zend_Exception('The community does not exist or you do not have the necessary permission', 403);
6264
}
63-
$this->view->community = $comm;
64-
$this->view->producers = $this->Tracker_Producer->getByCommunityId($commId);
65+
66+
$this->view->community = $communityDao;
67+
$this->view->producers = $this->Tracker_Producer->getByCommunityId($communityId);
6568
}
6669

6770
/**
@@ -76,27 +79,36 @@ public function viewAction()
7679
{
7780
/** @var int $producerId */
7881
$producerId = $this->getParam('producerId');
82+
7983
if (!isset($producerId)) {
80-
throw new Zend_Exception('Must pass producerId parameter');
84+
throw new Zend_Exception('The required producerId parameter is missing');
8185
}
8286

83-
/** @var Tracker_ProducerDao $producer */
84-
$producer = $this->Tracker_Producer->load($producerId);
85-
$comm = $producer->getCommunity();
86-
if (!$producer || !$this->Community->policyCheck($comm, $this->userSession->Dao, MIDAS_POLICY_READ)
87+
/** @var Tracker_ProducerDao $producerDao */
88+
$producerDao = $this->Tracker_Producer->load($producerId);
89+
90+
if ($this->Tracker_Producer->policyCheck($producerDao, $this->userSession->Dao, MIDAS_POLICY_READ) === false
8791
) {
88-
throw new Zend_Exception('Read permission required on the community', 403);
89-
}
90-
$this->view->producer = $producer;
91-
$this->view->trendGroups = $this->Tracker_Trend->getTrendsGroupByDatasets($producer);
92-
$this->view->isAdmin = $this->Community->policyCheck($comm, $this->userSession->Dao, MIDAS_POLICY_ADMIN);
93-
$this->view->json['tracker']['producer'] = $producer;
94-
95-
$breadcrumbs = array(array('type' => 'community', 'object' => $comm, 'tab' => 'Trackers'));
96-
$breadcrumbs[] = array(
97-
'type' => 'custom',
98-
'text' => $producer->getDisplayName(),
99-
'icon' => $this->view->coreWebroot.'/public/images/icons/cog_go.png',
92+
throw new Zend_Exception('The producer does not exist or you do not have the necessary permission on its community', 403);
93+
}
94+
95+
$this->view->producer = $producerDao;
96+
$this->view->trendGroups = $this->Tracker_Trend->getTrendsGroupByDatasets($producerDao);
97+
$this->view->isAdmin = $this->Tracker_Producer->policyCheck($producerDao, $this->userSession->Dao, MIDAS_POLICY_ADMIN);
98+
$this->view->json['tracker']['producer'] = $producerDao;
99+
100+
$breadcrumbs = array(
101+
array(
102+
'type' => 'community',
103+
'object' => $producerDao->getCommunity(),
104+
'tab' => 'Trackers',
105+
),
106+
array(
107+
'type' => 'custom',
108+
'text' => $producerDao->getDisplayName(),
109+
'icon' => $this->view->baseUrl('core/public/images/icons/cog_go.png'),
110+
),
111+
100112
);
101113
$this->Component->Breadcrumb->setBreadcrumbHeader($breadcrumbs, $this->view);
102114
}
@@ -116,18 +128,20 @@ public function deleteAction()
116128

117129
/** @var int $producerId */
118130
$producerId = $this->getParam('producerId');
131+
119132
if (!isset($producerId)) {
120-
throw new Zend_Exception('Must pass producerId parameter');
133+
throw new Zend_Exception('The required producerId parameter is missing');
121134
}
122135

123-
/** @var Tracker_ProducerDao $producer */
124-
$producer = $this->Tracker_Producer->load($producerId);
125-
$comm = $producer->getCommunity();
126-
if (!$producer || !$this->Community->policyCheck($comm, $this->userSession->Dao, MIDAS_POLICY_ADMIN)
136+
/** @var Tracker_ProducerDao $producerDao */
137+
$producerDao = $this->Tracker_Producer->load($producerId);
138+
139+
if ($this->Tracker_Producer->policyCheck($producerDao, $this->userSession->Dao, MIDAS_POLICY_ADMIN) === false
127140
) {
128-
throw new Zend_Exception('Admin permission required on the community', 403);
141+
throw new Zend_Exception('The producer does not exist or you do not have the necessary permission on its community', 403);
129142
}
130-
$this->Tracker_Producer->delete($producer);
143+
144+
$this->Tracker_Producer->delete($producerDao);
131145
}
132146

133147
/**
@@ -141,20 +155,19 @@ public function editAction()
141155
$producerId = $this->getParam('producerId');
142156

143157
if (!isset($producerId)) {
144-
throw new Zend_Exception('Must pass producerId parameter');
158+
throw new Zend_Exception('The required producerId parameter is missing');
145159
}
146160

147-
/** @var Tracker_ProducerDao $producer */
148-
$producer = $this->Tracker_Producer->load($producerId);
149-
if (!$producer) {
150-
throw new Zend_Exception('Invalid producerId', 404);
151-
}
152-
if (!$this->Community->policyCheck($producer->getCommunity(), $this->userSession->Dao, MIDAS_POLICY_ADMIN)
161+
/** @var Tracker_ProducerDao $producerDao */
162+
$producerDao = $this->Tracker_Producer->load($producerId);
163+
164+
if ($this->Tracker_Producer->policyCheck($producerDao, $this->userSession->Dao, MIDAS_POLICY_ADMIN) === false
153165
) {
154-
throw new Zend_Exception('Admin permission required on the community', 403);
166+
throw new Zend_Exception('The producer does not exist or you do not have the necessary permission on its community', 403);
155167
}
168+
156169
$this->disableLayout();
157-
$this->view->producer = $producer;
170+
$this->view->producer = $producerDao;
158171
}
159172

160173
/**
@@ -177,34 +190,50 @@ public function editsubmitAction()
177190
throw new Zend_Exception('Must pass producerId parameter');
178191
}
179192

180-
/** @var Tracker_ProducerDao $producer */
181-
$producer = $this->Tracker_Producer->load($producerId);
182-
if (!$this->Community->policyCheck($producer->getCommunity(), $this->userSession->Dao, MIDAS_POLICY_ADMIN)
193+
/** @var Tracker_ProducerDao $producerDao */
194+
$producerDao = $this->Tracker_Producer->load($producerId);
195+
196+
if ($this->Tracker_Producer->policyCheck($producerDao, $this->userSession->Dao, MIDAS_POLICY_ADMIN) === false
183197
) {
184-
throw new Zend_Exception('Admin permission required on the community', 403);
198+
throw new Zend_Exception('The producer does not exist or you do not have the necessary permission on its community', 403);
185199
}
200+
201+
/** @var string $displayName */
186202
$displayName = $this->getParam('displayName');
187-
$description = $this->getParam('description');
188-
$repository = $this->getParam('repository');
189-
$revisionUrl = $this->getParam('revisionUrl');
190-
$executableName = $this->getParam('executableName');
191203

192204
if (isset($displayName)) {
193-
$producer->setDisplayName($displayName);
205+
$producerDao->setDisplayName($displayName);
194206
}
207+
208+
/** @var string $description */
209+
$description = $this->getParam('description');
210+
195211
if (isset($description)) {
196-
$producer->setDescription($description);
212+
$producerDao->setDescription($description);
197213
}
214+
215+
/** @var string $repository */
216+
$repository = $this->getParam('repository');
217+
198218
if (isset($repository)) {
199-
$producer->setRepository($repository);
200-
}
201-
if (isset($executableName)) {
202-
$producer->setExecutableName($executableName);
219+
$producerDao->setRepository($repository);
203220
}
221+
222+
/** @var string $revisionUrl */
223+
$revisionUrl = $this->getParam('revisionUrl');
224+
204225
if (isset($revisionUrl)) {
205-
$producer->setRevisionUrl($revisionUrl);
226+
$producerDao->setRevisionUrl($revisionUrl);
206227
}
207-
$this->Tracker_Producer->save($producer);
208-
echo JsonComponent::encode(array('status' => 'ok', 'message' => 'Changes saved', 'producer' => $producer));
228+
229+
/** @var string $executableName */
230+
$executableName = $this->getParam('executableName');
231+
232+
if (isset($executableName)) {
233+
$producerDao->setExecutableName($executableName);
234+
}
235+
236+
$this->Tracker_Producer->save($producerDao);
237+
echo JsonComponent::encode(array('status' => 'ok', 'message' => 'Changes saved', 'producer' => $producerDao));
209238
}
210239
}

modules/tracker/models/base/ProducerModelBase.php

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ abstract class Tracker_ProducerModelBase extends Tracker_AppModel
2929
public function __construct()
3030
{
3131
parent::__construct();
32+
3233
$this->_name = 'tracker_producer';
3334
$this->_key = 'producer_id';
3435
$this->_mainData = array(
@@ -53,6 +54,7 @@ public function __construct()
5354
'child_column' => 'producer_id',
5455
),
5556
);
57+
5658
$this->initialize();
5759
}
5860

@@ -84,37 +86,60 @@ abstract public function getByCommunityIdAndName($communityId, $displayName);
8486
*/
8587
public function createIfNeeded($communityId, $displayName)
8688
{
87-
$producer = $this->getByCommunityIdAndName($communityId, $displayName);
88-
if (!$producer) {
89-
/** @var Tracker_ProducerDao $producer */
90-
$producer = MidasLoader::newDao('ProducerDao', $this->moduleName);
91-
$producer->setCommunityId($communityId);
92-
$producer->setDisplayName($displayName);
93-
$producer->setDescription('');
94-
$producer->setExecutableName('');
95-
$producer->setRepository('');
96-
$producer->setRevisionUrl('');
97-
$this->save($producer);
89+
$producerDao = $this->getByCommunityIdAndName($communityId, $displayName);
90+
91+
if ($producerDao === false) {
92+
/** @var Tracker_ProducerDao $producerDao */
93+
$producerDao = MidasLoader::newDao('ProducerDao', $this->moduleName);
94+
$producerDao->setCommunityId($communityId);
95+
$producerDao->setDisplayName($displayName);
96+
$producerDao->setDescription('');
97+
$producerDao->setExecutableName('');
98+
$producerDao->setRepository('');
99+
$producerDao->setRevisionUrl('');
100+
$this->save($producerDao);
98101
}
99102

100-
return $producer;
103+
return $producerDao;
101104
}
102105

103106
/**
104107
* Delete the given producer and all associated trends.
105108
*
106-
* @param Tracker_ProducerDao $producer producer DAO
109+
* @param Tracker_ProducerDao $producerDao producer DAO
107110
*/
108-
public function delete($producer)
111+
public function delete($producerDao)
109112
{
110113
/** @var Tracker_TrendModel $trendModel */
111114
$trendModel = MidasLoader::loadModel('Trend', $this->moduleName);
112-
$trends = $producer->getTrends();
115+
$trendDaos = $producerDao->getTrends();
113116

114-
/** @var Tracker_TrendDao $trend */
115-
foreach ($trends as $trend) {
116-
$trendModel->delete($trend);
117+
/** @var Tracker_TrendDao $trendDao */
118+
foreach ($trendDaos as $trendDao) {
119+
$trendModel->delete($trendDao);
117120
}
118-
parent::delete($producer);
121+
122+
parent::delete($producerDao);
123+
}
124+
125+
/**
126+
* Check whether the given policy is valid for the given producer and user.
127+
*
128+
* @param Tracker_ProducerDao $producerDao producer DAO
129+
* @param null|UserDao $userDao user DAO
130+
* @param int $policy policy
131+
* @return bool true if the given policy is valid for the given producer and user
132+
*/
133+
public function policyCheck($producerDao, $userDao = null, $policy = MIDAS_POLICY_READ)
134+
{
135+
if (is_null($producerDao) || $producerDao === false) {
136+
return false;
137+
}
138+
139+
/** @var CommunityModel $communityModel */
140+
$communityModel = MidasLoader::loadModel('Community');
141+
$communityDao = $producerDao->getCommunity();
142+
143+
return $communityDao !== false && $communityModel->policyCheck($communityDao, $userDao, $policy);
119144
}
120145
}

0 commit comments

Comments
 (0)