-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
408 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
<?php | ||
|
||
namespace Teams\Api\Adapter; | ||
|
||
use Omeka\Api\Adapter\AbstractAdapter; | ||
use Omeka\Api\Request; | ||
use Omeka\Entity\EntityInterface; | ||
use Omeka\Stdlib\ErrorStore; | ||
use Omeka\Stdlib\Message; | ||
use Teams\Entity\Team; | ||
use Teams\Mvc\Controller\Plugin\TeamAuth; | ||
use Omeka\Api\Exception; | ||
|
||
|
||
abstract class AbstractTeamEntityAdapter extends \Omeka\Api\Adapter\AbstractEntityAdapter | ||
{ | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getRepresentationClass() | ||
{ | ||
// TODO: Implement getRepresentationClass() method. | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function hydrate(Request $request, EntityInterface $entity, ErrorStore $errorStore) | ||
{ | ||
// TODO: Implement hydrate() method. | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getResourceName() | ||
{ | ||
// TODO: Implement getResourceName() method. | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getEntityClass() | ||
{ | ||
// TODO: Implement getEntityClass() method. | ||
} | ||
abstract public function getMappedEntityClass(); | ||
|
||
abstract public function getMappedEntityName(); | ||
|
||
public function validateRequest(Request $request, ErrorStore $errorStore) | ||
{ | ||
// if (Request::CREATE === $request->getOperation()){ | ||
// //validate correct payload data exists | ||
// if(!$request->getValue('team') || !is_int($request->getValue('team'))){ | ||
// $errorStore->addError('o-module-teams:team', 'Your payload needs to indicate team with a numeric value'); | ||
// } else { | ||
// $team = $this->getEntityManager() | ||
// ->getRepository('Teams\Entity\Team') | ||
// ->findOneBy(['id'=>$request->getValue('team')]); | ||
// if($team){ | ||
// $errorStore->addError('o-module-teams:team', new Message( | ||
// 'A team with id %s does not exist.', // @translate | ||
// $request->getValue('team') )); | ||
// | ||
// } | ||
// } | ||
// if(!$request->getValue('resource') || !is_int($request->getValue('resource'))){ | ||
// $errorStore->addError('o-module-teams:team', 'Your payload needs to indicate resource with a numeric value'); | ||
// } | ||
// | ||
// //validate team and resource exist | ||
// | ||
// | ||
// | ||
// | ||
// } | ||
// if ($errorStore->hasErrors()) { | ||
// $validationException = new Exception\ValidationException; | ||
// $validationException->setErrorStore($errorStore); | ||
// throw $validationException; | ||
// } | ||
// | ||
// $entity_index = 'o:' . $this->getMappedEntityName(); | ||
// $services = $this->getServiceLocator(); | ||
// $logger = $services->get('Omeka\Logger'); | ||
// //does the request contain a team and resource | ||
// $data = []; | ||
// if (Request::CREATE === $request->getOperation()){ | ||
// $data = $request->getContent(); | ||
// } elseif (Request::DELETE === $request->getOperation()) { | ||
// $data = $request->getId(); | ||
// } | ||
// if (!is_array($data)){ | ||
// $errorStore->addError('o:id', new Message('The %s id must be an array.', $this->getResourceName())); // @translate | ||
// return; | ||
// } | ||
// if (!array_key_exists('o:team',$data)){ | ||
// $errorStore->addError('o:team', 'The request lacks a team id.'); // @translate | ||
// ->err('T$loggerhe request lacks a team id.'); | ||
// | ||
// } | ||
// if (!array_key_exists($entity_index,$data)){ | ||
// $errorStore->addError($entity_index, new Message('The request lacks a %s id.',$this->getMappedEntityName())); // @translate | ||
// } | ||
// | ||
// | ||
// //is that id a team | ||
// | ||
// $team = $this->getEntityManager() | ||
// ->getRepository('Teams\Entity\Team') | ||
// ->findOneBy(['id'=>$data['o:team']]); | ||
// if (! $team) { | ||
// $errorStore->addError('o:team', new Message( | ||
// 'A team with id = "%s" can not be found', // @translate | ||
// $data['o:team'] | ||
// )); | ||
// } | ||
// | ||
// //is that a resource | ||
// $mapped_entity = $this->getEntityManager() | ||
// ->find($this->getMappedEntityClass(), $data[$entity_index]); | ||
// | ||
// if (! $mapped_entity) { | ||
// $errorStore->addError($entity_index, new Message( | ||
// 'A %1$s with id = "%2$s" can not be found', // @translate | ||
// $this->getMappedEntityName(), | ||
// $data[$entity_index] | ||
// )); | ||
// } | ||
// | ||
// //does the team resource already exist | ||
// if ($team && $mapped_entity){ | ||
// if (Request::CREATE === $request->getOperation() && $this->teamEntityExists($team, $mapped_entity)){ | ||
// $errorStore->addError('o:resource', 'That team resource already exists.'); // @translate | ||
// } elseif (Request::DELETE === $request->getOperation() && ! $this->teamEntityExists($team, $mapped_entity)){ | ||
// $errorStore->addError('o:resource', 'That team resource you are trying to delete does not exists.'); // @translate | ||
// } | ||
// } | ||
|
||
} | ||
|
||
//PHP 8 can implement multiple types as type hint: Resource|User|ResourceTemplate|Asset|Site | ||
public function teamEntityExists(Team $team, EntityInterface $entity ) | ||
{ | ||
$entity_name = $this->getMappedEntityName(); | ||
return $this->getEntityManager() | ||
->getRepository($this->getEntityClass()) | ||
->findOneBy(['team'=>$team->getId(), $entity_name => $entity->getId()]); | ||
|
||
} | ||
|
||
public function teamAuthority($request, $team, $user, $resource=null) | ||
{ | ||
$em = $this->getEntityManager(); | ||
$user = $this->getServiceLocator()->get('Omeka\AuthenticationService')->getIdentity(); | ||
$operation = $request->getOperation(); | ||
$services = $this->getServiceLocator(); | ||
$logger = $services->get('Omeka\Logger'); | ||
$teamAuth = new TeamAuth($em, $logger); | ||
$teamId = 0; | ||
if (array_key_exists('team',$request->getContent())){ | ||
$teamId = $request->getContent()['team']; | ||
} elseif (array_key_exists('o:team', $request->getContent())){ | ||
$teamId = $request->getContent()['o:team']; | ||
} | ||
|
||
if (! $teamAuth->teamAuthorized($user, $operation, 'resource', $teamId)){ | ||
throw new Exception\PermissionDeniedException(sprintf( | ||
$this->getTranslator()->translate( | ||
'Permission denied for the current user to %1$s a team resource in team_id = %2$s.' | ||
), | ||
$operation, $request->getContent()['o:team']) | ||
); | ||
} | ||
} | ||
|
||
public function read(Request $request) | ||
{ | ||
AbstractAdapter::read($request); | ||
} | ||
|
||
public function batchCreate(Request $request) | ||
{ | ||
AbstractAdapter::batchCreate($request); | ||
} | ||
|
||
public function batchDelete(Request $request) | ||
{ | ||
AbstractAdapter::batchDelete($request); | ||
} | ||
|
||
|
||
public function update(Request $request) | ||
{ | ||
AbstractAdapter::update($request); | ||
} | ||
|
||
public function batchUpdate(Request $request) | ||
{ | ||
AbstractAdapter::batchUpdate($request); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
Oops, something went wrong.