Skip to content

Commit

Permalink
Merge pull request #113 from UIUCLibrary/api-cleanup
Browse files Browse the repository at this point in the history
Api cleanup
  • Loading branch information
alexdryden authored Jan 28, 2023
2 parents 4d5529f + 77f4b43 commit c8da17e
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 598 deletions.
4 changes: 2 additions & 2 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,7 @@ public function itemUpdate(Event $event)

foreach ($request->getContent()['add_team'] as $team_id) {
//if the user is authorized to add items to that team
if ($teamAuth->teamAuthorized('add', 'resource', $team_id)) {
if ($teamAuth->teamAuthorized($this->getUser(),'add', 'resource', $team_id)) {
$team = $em->getRepository('Teams\Entity\Team')->findOneBy(['id' => $team_id]);
if ($team) {
foreach (array_keys($resource_ids) as $resource_id) {
Expand All @@ -1844,7 +1844,7 @@ public function itemUpdate(Event $event)
$em->flush();

foreach ($request->getContent()['remove_team'] as $team_id) {
if ($teamAuth->teamAuthorized('delete', 'resource', $team_id)) {
if ($teamAuth->teamAuthorized($this->getUser(),'delete', 'resource', $team_id)) {
foreach (array_keys($resource_ids) as $resource_id) {
$team_resource = $em->getRepository('Teams\Entity\TeamResource')
->findOneBy(['team' => $team_id, 'resource'=>$resource_id]);
Expand Down
170 changes: 0 additions & 170 deletions src/Api/Adapter/TeamAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@
use Doctrine\ORM\QueryBuilder;
use Teams\Api\Representation\TeamRepresentation;
use Teams\Entity\Team;
use Teams\Entity\TeamRole;
use Teams\Entity\TeamUser;
use Teams\Entity\TeamResource;
use Omeka\Api\Adapter\AbstractEntityAdapter;
use Omeka\Api\Request;
use Omeka\Entity\EntityInterface;
use Omeka\Entity\Item;
use Omeka\Entity\ItemSet;
use Omeka\Entity\Media;
use Omeka\Entity\Resource;
use Omeka\Entity\User;
use Omeka\Stdlib\ErrorStore;
use Omeka\Stdlib\Message;
use Laminas\Validator\EmailAddress;

class TeamAdapter extends AbstractEntityAdapter
{
Expand Down Expand Up @@ -59,67 +50,6 @@ public function hydrate(
}
}

// public function validateRequest(Request $request, ErrorStore $errorStore)
// {
// $data = $request->getContent();
// if (array_key_exists('o:name', $data)) {
// $result = $this->validateName($data['o:name'], $errorStore);
// }
// }

// public function validateEntity(EntityInterface $entity, ErrorStore $errorStore)
// {
// $name = $entity->getName();
// $result = $this->validateName($name, $errorStore);
// if (!$this->isUnique($entity, ['name' => $name])) {
// $errorStore->addError('o:name', new Message(
// 'The name "%s" is already taken.', // @translate
// $name
// ));
// }
// }
//
// /**
// * Validate a name.
// *
// * @param string $name
// * @param ErrorStore $errorStore
// * @return bool
// */
// protected function validateName($name, ErrorStore $errorStore)
// {
// $result = true;
// $sanitized = $this->sanitizeLightString($name);
// if (is_string($name) && $sanitized !== '') {
// $name = $sanitized;
// $sanitized = $this->sanitizeString($sanitized);
// if ($name !== $sanitized) {
// $errorStore->addError('o:name', new Message(
// 'The name "%s" contains forbidden characters.', // @translate
// $name
// ));
// $result = false;
// }
// if (preg_match('~^[\d]+$~', $name)) {
// $errorStore->addError('o:name', 'A name can’t contain only numbers.'); // @translate
// $result = false;
// }
// $reserved = [
// 'id', 'name', 'description',
// 'show', 'browse', 'add', 'edit', 'delete', 'delete-confirm', 'batch-edit', 'batch-edit-all',
// ];
// if (in_array(strtolower($name), $reserved)) {
// $errorStore->addError('o:name', 'A name cannot be a reserved word.'); // @translate
// $result = false;
// }
// } else {
// $errorStore->addError('o:name', 'A team must have a name.'); // @translate
// $result = false;
// }
// return $result;
// }

///3 ifs permit single return via specified column
public function buildQuery(QueryBuilder $qb, array $query)
{
if (isset($query['id'])) {
Expand All @@ -133,106 +63,6 @@ public function buildQuery(QueryBuilder $qb, array $query)
if (isset($query['description'])) {
$this->buildQueryValuesItself($qb, $query['description'], 'description');
}
//
// // All teams for these entities ("OR"). If multiple, mixed with "AND",
// // so, for mixed resources, use "resource_id".
// $mapResourceTypes = [
// 'user_id' => User::class,
// 'resource_id' => Resource::class,
// 'item_set_id' => ItemSet::class,
// 'item_id' => Item::class,
// 'media_id' => Media::class,
// ];
// $subQueryKeys = array_intersect_key($mapResourceTypes, $query);
// foreach ($subQueryKeys as $queryKey => $resourceType) {
// if ($queryKey === 'user_id') {
// $teamEntity = TeamUser::class;
// $teamEntityColumn = 'user';
// } else {
// $teamEntity = TEamResource::class;
// $teamEntityColumn = 'resource';
// }
// $entities = is_array($query[$queryKey]) ? $query[$queryKey] : [$query[$queryKey]];
// $entities = array_filter($entities, 'is_numeric');
// if (empty($entities)) {
// continue;
// }
// $teamEntityAlias = $this->createAlias();
// $entityAlias = $this->createAlias();
// $qb
// // Note: This query may be used if the annotation is set in
// // core on Resource. In place, the relation is recreated.
// // ->innerJoin(
// // $this->getEntityClass() . ($queryKey === 'user_id' ? '.users' : '.resources'),
// // $entityAlias, 'WITH',
// // $qb->expr()->in("$entityAlias.id", $this->createNamedParameter($qb, $entities))
// // );
// ->innerJoin(
// $teamEntity,
// $teamEntityAlias,
// 'WITH',
// $qb->expr()->andX(
// $qb->expr()->eq($teamEntityAlias . '.team', $this->getEntityClass() . '.id'),
// $qb->expr()->in(
// $teamEntityAlias . '.' . $teamEntityColumn,
// $this->createNamedParameter($qb, $entities)
// )
// )
// );
// // This check avoids bad result for bad request mixed ids.
// if (!in_array($queryKey, ['user_id', 'resource_id'])) {
// $resourceAlias = $this->createAlias();
// $qb
// ->innerJoin(
// $resourceType,
// $resourceAlias,
// 'WITH',
// $qb->expr()->eq(
// $teamEntityAlias . '.resource',
// $resourceAlias . '.id'
// )
// );
// }
// }
//
// if (array_key_exists('resource_type', $query)) {
// $mapResourceTypes = [
// 'users' => User::class,
// 'resources' => Resource::class,
// 'item_sets' => ItemSet::class,
// 'items' => Item::class,
// 'media' => Media::class,
// ];
// if (isset($mapResourceTypes[$query['resource_type']])) {
// $entityJoinClass = $query['resource_type'] === 'users'
// ? TeamUser::class
// : TeamResource::class;
// $entityJoinAlias = $this->createAlias();
// $qb
// ->linnerJoin(
// $entityJoinClass,
// $entityJoinAlias,
// 'WITH',
// $qb->expr()->eq($entityJoinAlias . '.team', Team::class)
// );
// if (!in_array($query['resource_type'], ['users', 'resources'])) {
// $entityAlias = $this->createAlias();
// $qb
// ->innerJoin(
// $mapResourceTypes[$query['resource_type']],
// $entityAlias,
// 'WITH',
// $qb->expr()->eq(
// $entityJoinClass . '.resource',
// $entityAlias . '.id'
// )
// );
// }
// } elseif ($query['resource_type'] !== '') {
// $qb
// ->andWhere('1 = 0');
// }
// }
}

public function sortQuery(QueryBuilder $qb, array $query)
Expand Down
Loading

0 comments on commit c8da17e

Please sign in to comment.