Skip to content

Commit

Permalink
Merge pull request #155 from UIUCLibrary/api-create-team-reasource
Browse files Browse the repository at this point in the history
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
alexdryden authored Jul 11, 2024
2 parents 3cac31b + 8c3c8c7 commit d7fde8c
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 55 deletions.
30 changes: 14 additions & 16 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -1980,10 +1980,8 @@ public function itemCreate(Event $event)
$operation = $request->getOperation();
$em = $this->getServiceLocator()->get('Omeka\EntityManager');


if ($operation == 'create') {
$response = $event->getParam('response');

$resource = $response->getContent();
$team_key = '';
if (array_key_exists('team', $request->getContent())){
Expand All @@ -1994,24 +1992,24 @@ public function itemCreate(Event $event)

if ($team_key) {
$teams = $request->getContent()[$team_key];

if (!is_array($teams)){
$teams = [$teams];
}
//add items to team
foreach ($teams as $team_id):
foreach ($teams as $team_id){
$team = $em->getRepository('Teams\Entity\Team')->findOneBy(['id'=>$team_id]);
$tr = new TeamResource($team, $resource);
$em->persist($tr);

//if there is media, add those to the team as well

$media = $resource->getMedia();

if (count($media) > 0) {
foreach ($media as $m):
$tr = new TeamResource($team, $m);
$tr = new TeamResource($team, $resource);
$em->persist($tr);
endforeach;

//if there is media, add those to the team as well
$media = $resource->getMedia();
if (count($media) > 0) {
foreach ($media as $m):
$tr = new TeamResource($team, $m);
$em->persist($tr);
endforeach;
}
}
endforeach;
$em->flush();
}
}
Expand Down
210 changes: 210 additions & 0 deletions src/Api/Adapter/AbstractTeamEntityAdapter.php
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);
}





}
Loading

0 comments on commit d7fde8c

Please sign in to comment.