Skip to content

Commit d7fde8c

Browse files
authored
Merge pull request #155 from UIUCLibrary/api-create-team-reasource
Api: create team resource. This fixes #154 by adding a way to create team-resources via the API. Note: this is not a way to edit teams directly. This expects a POST request to the team-resource api
2 parents 3cac31b + 8c3c8c7 commit d7fde8c

File tree

4 files changed

+408
-55
lines changed

4 files changed

+408
-55
lines changed

Module.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,10 +1980,8 @@ public function itemCreate(Event $event)
19801980
$operation = $request->getOperation();
19811981
$em = $this->getServiceLocator()->get('Omeka\EntityManager');
19821982

1983-
19841983
if ($operation == 'create') {
19851984
$response = $event->getParam('response');
1986-
19871985
$resource = $response->getContent();
19881986
$team_key = '';
19891987
if (array_key_exists('team', $request->getContent())){
@@ -1994,24 +1992,24 @@ public function itemCreate(Event $event)
19941992

19951993
if ($team_key) {
19961994
$teams = $request->getContent()[$team_key];
1997-
1995+
if (!is_array($teams)){
1996+
$teams = [$teams];
1997+
}
19981998
//add items to team
1999-
foreach ($teams as $team_id):
1999+
foreach ($teams as $team_id){
20002000
$team = $em->getRepository('Teams\Entity\Team')->findOneBy(['id'=>$team_id]);
2001-
$tr = new TeamResource($team, $resource);
2002-
$em->persist($tr);
2003-
2004-
//if there is media, add those to the team as well
2005-
2006-
$media = $resource->getMedia();
2007-
2008-
if (count($media) > 0) {
2009-
foreach ($media as $m):
2010-
$tr = new TeamResource($team, $m);
2001+
$tr = new TeamResource($team, $resource);
20112002
$em->persist($tr);
2012-
endforeach;
2003+
2004+
//if there is media, add those to the team as well
2005+
$media = $resource->getMedia();
2006+
if (count($media) > 0) {
2007+
foreach ($media as $m):
2008+
$tr = new TeamResource($team, $m);
2009+
$em->persist($tr);
2010+
endforeach;
2011+
}
20132012
}
2014-
endforeach;
20152013
$em->flush();
20162014
}
20172015
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
3+
namespace Teams\Api\Adapter;
4+
5+
use Omeka\Api\Adapter\AbstractAdapter;
6+
use Omeka\Api\Request;
7+
use Omeka\Entity\EntityInterface;
8+
use Omeka\Stdlib\ErrorStore;
9+
use Omeka\Stdlib\Message;
10+
use Teams\Entity\Team;
11+
use Teams\Mvc\Controller\Plugin\TeamAuth;
12+
use Omeka\Api\Exception;
13+
14+
15+
abstract class AbstractTeamEntityAdapter extends \Omeka\Api\Adapter\AbstractEntityAdapter
16+
{
17+
18+
/**
19+
* @inheritDoc
20+
*/
21+
public function getRepresentationClass()
22+
{
23+
// TODO: Implement getRepresentationClass() method.
24+
}
25+
26+
/**
27+
* @inheritDoc
28+
*/
29+
public function hydrate(Request $request, EntityInterface $entity, ErrorStore $errorStore)
30+
{
31+
// TODO: Implement hydrate() method.
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function getResourceName()
38+
{
39+
// TODO: Implement getResourceName() method.
40+
}
41+
42+
/**
43+
* @inheritDoc
44+
*/
45+
public function getEntityClass()
46+
{
47+
// TODO: Implement getEntityClass() method.
48+
}
49+
abstract public function getMappedEntityClass();
50+
51+
abstract public function getMappedEntityName();
52+
53+
public function validateRequest(Request $request, ErrorStore $errorStore)
54+
{
55+
// if (Request::CREATE === $request->getOperation()){
56+
// //validate correct payload data exists
57+
// if(!$request->getValue('team') || !is_int($request->getValue('team'))){
58+
// $errorStore->addError('o-module-teams:team', 'Your payload needs to indicate team with a numeric value');
59+
// } else {
60+
// $team = $this->getEntityManager()
61+
// ->getRepository('Teams\Entity\Team')
62+
// ->findOneBy(['id'=>$request->getValue('team')]);
63+
// if($team){
64+
// $errorStore->addError('o-module-teams:team', new Message(
65+
// 'A team with id %s does not exist.', // @translate
66+
// $request->getValue('team') ));
67+
//
68+
// }
69+
// }
70+
// if(!$request->getValue('resource') || !is_int($request->getValue('resource'))){
71+
// $errorStore->addError('o-module-teams:team', 'Your payload needs to indicate resource with a numeric value');
72+
// }
73+
//
74+
// //validate team and resource exist
75+
//
76+
//
77+
//
78+
//
79+
// }
80+
// if ($errorStore->hasErrors()) {
81+
// $validationException = new Exception\ValidationException;
82+
// $validationException->setErrorStore($errorStore);
83+
// throw $validationException;
84+
// }
85+
//
86+
// $entity_index = 'o:' . $this->getMappedEntityName();
87+
// $services = $this->getServiceLocator();
88+
// $logger = $services->get('Omeka\Logger');
89+
// //does the request contain a team and resource
90+
// $data = [];
91+
// if (Request::CREATE === $request->getOperation()){
92+
// $data = $request->getContent();
93+
// } elseif (Request::DELETE === $request->getOperation()) {
94+
// $data = $request->getId();
95+
// }
96+
// if (!is_array($data)){
97+
// $errorStore->addError('o:id', new Message('The %s id must be an array.', $this->getResourceName())); // @translate
98+
// return;
99+
// }
100+
// if (!array_key_exists('o:team',$data)){
101+
// $errorStore->addError('o:team', 'The request lacks a team id.'); // @translate
102+
// ->err('T$loggerhe request lacks a team id.');
103+
//
104+
// }
105+
// if (!array_key_exists($entity_index,$data)){
106+
// $errorStore->addError($entity_index, new Message('The request lacks a %s id.',$this->getMappedEntityName())); // @translate
107+
// }
108+
//
109+
//
110+
// //is that id a team
111+
//
112+
// $team = $this->getEntityManager()
113+
// ->getRepository('Teams\Entity\Team')
114+
// ->findOneBy(['id'=>$data['o:team']]);
115+
// if (! $team) {
116+
// $errorStore->addError('o:team', new Message(
117+
// 'A team with id = "%s" can not be found', // @translate
118+
// $data['o:team']
119+
// ));
120+
// }
121+
//
122+
// //is that a resource
123+
// $mapped_entity = $this->getEntityManager()
124+
// ->find($this->getMappedEntityClass(), $data[$entity_index]);
125+
//
126+
// if (! $mapped_entity) {
127+
// $errorStore->addError($entity_index, new Message(
128+
// 'A %1$s with id = "%2$s" can not be found', // @translate
129+
// $this->getMappedEntityName(),
130+
// $data[$entity_index]
131+
// ));
132+
// }
133+
//
134+
// //does the team resource already exist
135+
// if ($team && $mapped_entity){
136+
// if (Request::CREATE === $request->getOperation() && $this->teamEntityExists($team, $mapped_entity)){
137+
// $errorStore->addError('o:resource', 'That team resource already exists.'); // @translate
138+
// } elseif (Request::DELETE === $request->getOperation() && ! $this->teamEntityExists($team, $mapped_entity)){
139+
// $errorStore->addError('o:resource', 'That team resource you are trying to delete does not exists.'); // @translate
140+
// }
141+
// }
142+
143+
}
144+
145+
//PHP 8 can implement multiple types as type hint: Resource|User|ResourceTemplate|Asset|Site
146+
public function teamEntityExists(Team $team, EntityInterface $entity )
147+
{
148+
$entity_name = $this->getMappedEntityName();
149+
return $this->getEntityManager()
150+
->getRepository($this->getEntityClass())
151+
->findOneBy(['team'=>$team->getId(), $entity_name => $entity->getId()]);
152+
153+
}
154+
155+
public function teamAuthority($request, $team, $user, $resource=null)
156+
{
157+
$em = $this->getEntityManager();
158+
$user = $this->getServiceLocator()->get('Omeka\AuthenticationService')->getIdentity();
159+
$operation = $request->getOperation();
160+
$services = $this->getServiceLocator();
161+
$logger = $services->get('Omeka\Logger');
162+
$teamAuth = new TeamAuth($em, $logger);
163+
$teamId = 0;
164+
if (array_key_exists('team',$request->getContent())){
165+
$teamId = $request->getContent()['team'];
166+
} elseif (array_key_exists('o:team', $request->getContent())){
167+
$teamId = $request->getContent()['o:team'];
168+
}
169+
170+
if (! $teamAuth->teamAuthorized($user, $operation, 'resource', $teamId)){
171+
throw new Exception\PermissionDeniedException(sprintf(
172+
$this->getTranslator()->translate(
173+
'Permission denied for the current user to %1$s a team resource in team_id = %2$s.'
174+
),
175+
$operation, $request->getContent()['o:team'])
176+
);
177+
}
178+
}
179+
180+
public function read(Request $request)
181+
{
182+
AbstractAdapter::read($request);
183+
}
184+
185+
public function batchCreate(Request $request)
186+
{
187+
AbstractAdapter::batchCreate($request);
188+
}
189+
190+
public function batchDelete(Request $request)
191+
{
192+
AbstractAdapter::batchDelete($request);
193+
}
194+
195+
196+
public function update(Request $request)
197+
{
198+
AbstractAdapter::update($request);
199+
}
200+
201+
public function batchUpdate(Request $request)
202+
{
203+
AbstractAdapter::batchUpdate($request);
204+
}
205+
206+
207+
208+
209+
210+
}

0 commit comments

Comments
 (0)