Skip to content

Commit

Permalink
Merge pull request #106 from UIUCLibrary/authority-plugin-refactor
Browse files Browse the repository at this point in the history
Authority plugin refactoring
  • Loading branch information
alexdryden authored Jan 25, 2023
2 parents 5dd558c + d4dd494 commit fcf89ab
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 394 deletions.
Binary file removed .DS_Store
Binary file not shown.
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# OS generated files #
######################
**/.DS_Store
**/.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Logs and databases #
######################
*.log
*.sql
*.sqlite
*.dump

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

3 changes: 2 additions & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,8 @@ public function itemUpdate(Event $event)
$entity = $event->getParam('entity');
$request = $event->getParam('request');
$operation = $request->getOperation();
$teamAuth = new TeamAuth($em, $this->getUser());
$logger = $this->getServiceLocator()->get('Omeka\Logger');
$teamAuth = new TeamAuth($em, $logger);

if ($operation == 'update') {
if (array_key_exists('remove_team', $request->getContent()) ||
Expand Down
Binary file removed src/Controller/.DS_Store
Binary file not shown.
27 changes: 11 additions & 16 deletions src/Controller/AddController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function __construct(EntityManager $entityManager)

public function teamAddAction()
{

$all_u_array = array();
$all_u_collection = $this->api()->search('users')->getContent();
foreach ($all_u_collection as $u):
Expand Down Expand Up @@ -78,31 +79,27 @@ public function teamAddAction()
return $view;
}


//otherwise, set the data
if (! $this->teamAuth()->teamAuthorized($this->identity(), 'add', 'team')){
$this->messenger()->addError("You aren't authorized to add teams");
return $view;
}
//TODO: turn the section where user+role are added into a form so it can be populated below
$form->setData($request->getPost());
$userForm->setData($request->getPost());
$itemsetForm->setData($request->getPost());
$userRoleForm->setData($request->getPost());


//if the form isn't valid, return it

if (! $form->isValid()) {
return $view;
}

//get the data from the post
$data = $request->getPost('team');

$newTeam = $this->api($form)->create('team', $data);

//add the users, resources and sites to the team
if ($newTeam) {
//looks like this was a diagnostic i used to see what was in the data variable
$view->setVariable('post_data', $data);
$view->setVariable('team', $newTeam);

$team = $this->entityManager->getRepository('Teams\Entity\Team')
->findOneBy(['id' => (int)$newTeam->getContent()->id()]);
if ($request->getPost('user_role')) {
Expand Down Expand Up @@ -205,10 +202,6 @@ public function teamAddAction()
}
$view = new ViewModel;

// $userForm->setData($request->getPost());
// $itemsetForm->setData($request->getPost());
// $userRoleForm->setData($request->getPost());

$view->setVariable('form', $form);
$view->setVariable('userForm', $userForm);
$view->setVariable('itemsetForm', $itemsetForm);
Expand All @@ -231,17 +224,19 @@ public function roleAddAction()
return $view;
}

if (! $this->teamAuth()->teamAuthorized($this->identity(), 'add', 'role')){
$this->messenger()->addError("You aren't authorized to add roles");
return $view;
}

//otherwise, set the data
$form->setData($request->getPost());



//get the data from the post
$data = $request->getPost('role');

//if the form isn't valid, return it
if (! $form->isValid()) {
if (!$form->isValid()) {
return $view;
}

Expand Down
95 changes: 34 additions & 61 deletions src/Controller/DeleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,53 +39,29 @@ public function teamDeleteAction()
//is there an id?
$id = $this->params()->fromRoute('id');
if (! $id) {
return $this->redirect()->toRoute('admin');
$this->messenger()->addError("No team id found");
return $this->redirect()->toRoute('admin/teams');
}

//does a team have that id
try {
$team = $this->api()->search('team', ['id'=>$id]);
$team = $this->api()->searchOne('team', ['id'=>$id]);
} catch (InvalidArgumentException $exception) {
return $this->redirect()->toRoute('admin');
}

$criteria = ['id' => $id];

$qb = $this->entityManager->createQueryBuilder();
$entityClass = 'Teams\Entity\Team';

$qb->select('omeka_root')->from($entityClass, 'omeka_root');
foreach ($criteria as $field => $value) {
$qb->andWhere($qb->expr()->eq(
"omeka_root.$field",
$this->createNamedParameter($qb, $value)
));
$this->messenger()->addError("Invalid team id");
return $this->redirect()->toRoute('admin/teams');
}
$qb->setMaxResults(1);

$entity = $qb->getQuery()->getOneOrNullResult();


$request = new Request('delete', 'team');
$event = new Event('api.hydrate.pre', $this, [
'entity' => $entity,
'request' => $request,
]);
$this->getEventManager()->triggerEvent($event);


//is it a post request?
$request = $this->getRequest();
if (! $request->isPost()) {
return new ViewModel(['team'=>$team]);
}

//is it the right id and did they say confirm?
// if ($id != $request->getPost('id')
// || 'Delete' != $request->getPost('confirm')
// ) {
// return $this->redirect()->toRoute('admin/teams');
// }
if (! $this->teamAuth()->teamAuthorized($this->identity(), 'delete', 'team')){
$this->messenger()->addError("You aren't authorized to delete teams");
return $this->redirect()->toRoute('admin/teams');
}

if ($request->getPost('confirm') == 'Delete') {
$this->api()->delete('team', ['id'=>$id]);
return $this->redirect()->toRoute('admin/teams');
Expand All @@ -101,39 +77,36 @@ public function roleDeleteAction()
$id = $this->params()->fromRoute('id');
$role = $this->entityManager->getRepository('Teams\Entity\TeamRole')
->findOneBy(['id'=> $id]);

$request = $this->getRequest();

//test to see if anyone has this role. If they do, can't delete.
//test to see if anyone has this role. If they do, don't delete it.
$role_users = $this->entityManager->getRepository('Teams\Entity\TeamUser')
->findBy(['role'=>$id]);
$view = new ViewModel(
[
'role_users' => $role_users,
'user' => $user,
]
);

if (! $request->isPost()) {
return new ViewModel(
[
'role'=>$role,
'role_users' => $role_users,
'user' => $user,
]
);
return $view;
}
if ($request->isPost()) {
if (! $role_users) {
if ($this->identity()->getRole() == 'global_admin') {
if ($request->getPost('confirm') == 'Delete') {
$this->entityManager->remove($role);
$this->entityManager->flush();
$this->messenger()->addSuccess(sprintf('Successfully deleted role "%s"', $role->getName()));

return $this->redirect()->toRoute('admin/teams/roles');
} else {
return $this->redirect()->toRoute('admin/teams/roles');
}
} else {
$this->messenger()->addError('Only global admins can delete roles');
}
} else {
$this->messenger()->addError("Can't be deleted because teams are using the role");
}
if (! $this->teamAuth($user, 'delete', 'role')){
$this->messenger()->addError('You are not authorized to delete roles');
return $view;
}
if ($role_users){
$this->messenger()->addError('This role can not be deleted while users are assigned to it');
return $view;
}
if ($request->getPost('confirm') == 'Delete') {
$this->entityManager->remove($role);
$this->entityManager->flush();
$this->messenger()->addSuccess(sprintf('Successfully deleted role "%s"', $role->getName()));
}
return $this->redirect()->toRoute('admin/teams/roles');


}
}
76 changes: 9 additions & 67 deletions src/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ public function allAction()
$super_admin = $this->entityManager->getRepository('Omeka\Entity\User')
->findOneBy(['role' => 'global_admin']);
$user = $this->identity();


$view->setVariable('teams', $teams);
$view->setVariable('super_admin', $super_admin);
$view->setVariable('user', $user);
Expand Down Expand Up @@ -204,17 +202,10 @@ public function deleteAction()
}
endforeach;
$entityManager->flush();
$this->messenger()->addSuccess('Item successfully removed from your team.'); // @translate
$this->messenger()->addSuccess('Item remains available to other teams if they are linked to it.'); // @translate
$this->messenger()->addSuccess('Item will be deleted after x days '); // @translate
$this->messenger()->addSuccess('Item successfully removed from your team. Item remains available to other teams if they are linked to it.'); // @translate
} else {
$this->messenger()->addError('something went wrong'); // @translate
}

// $response = $this->api($form)->delete('items', $this->params('id'));
// if ($response) {
// $this->messenger()->addSuccess('Item successfully deleted'); // @translate
// }
} else {
$this->messenger()->addFormErrors($form);
}
Expand Down Expand Up @@ -314,44 +305,6 @@ public function indexAction()
return $view;
}

public function teamResources($resource_type, $query, $user_id, $active = true, $team_id = null)
{
if ($team_id) {
$team_entity = $this->entityManager->getRepository('Teams\Entity\Team')->findOneBy(['id' => $team_id]);




$q = $this->entityManager->createQuery("SELECT resource FROM Omeka\Entity\Resource resource WHERE resource INSTANCE OF Omeka\Entity\Item");
$item_sets = $q->getArrayResult();
$team_resources = array();
foreach ($team_entity->getTeamResources() as $team_resource):
//obv here would be a place where you could just use the discriminator to see if it is an item
if (array_search($team_resource->getResource()->getId(), array_column($item_sets, 'id'))) {
$team_resources[] = $team_resource;
}
endforeach;
$per_page = 10;
$page = $query['page'];
$start_i = ($per_page * $page) - $per_page;
// $tr = $team_entity->getTeamResources();
$max_i = count($team_resources);
if ($max_i < $start_i + $per_page) {
$end_i = $max_i;
} else {
$end_i = $start_i + $per_page;
}
// $tr = $team_entity->getTeamResources();
for ($i = $start_i; $i < $end_i; $i++) {
$resources[] = $this->api()->read($resource_type, $team_resources[$i]->getResource()->getId())->getContent();
}
} else {
$team_resources=null;
}

return array('page_resources'=>$resources, 'team_resources'=>$team_resources);
}

public function teamDetailAction()
{
$view = new ViewModel;
Expand All @@ -371,22 +324,20 @@ public function teamDetailAction()
foreach ($resources as $key => $resource):
//I imagine this as like a subquery that gets the list of item ids
$sub_query = $em->createQueryBuilder();
$sub_query->select('r.id')
$sub_query->select('r.id')
->from('Omeka\Entity\\' . $resource['entity'], 'r');

$ids = $sub_query->getQuery()->getArrayResult();
$ids = $sub_query->getQuery()->getArrayResult();

//get the count of the total number of team items
$qb = $em->createQueryBuilder();

$qb->select('count(tr.' . $resource['fk'] . ')')
//get the count of the total number of team items
$qb = $em->createQueryBuilder();
$qb->select('count(tr.' . $resource['fk'] . ')')
->from('Teams\Entity\\' . $resource['team_entity'], 'tr')
->where('tr.team = ?1')
->andWhere('tr.' . $resource['fk'] . ' in (:ids)')
->setParameter('ids', $ids)
;
$qb->setParameter(1, $this->params('id'));
$resources[$key]['count'] += $qb->getQuery()->getSingleScalarResult();
->setParameter('ids', $ids);
$qb->setParameter(1, $this->params('id'));
$resources[$key]['count'] += $qb->getQuery()->getSingleScalarResult();
endforeach;

$view->setVariable('resources', $resources);
Expand Down Expand Up @@ -440,13 +391,4 @@ public function roleIndexAction()
return $view;
}


public function usersAction()
{
$team_users = $this->api()->search('team-user');
$users = $this->api()->search('users');
$view = new ViewModel(['users'=> $users, 'team_users'=>$team_users]);
return $view;
// $view->setVariable('response', $response);
}
}
Loading

0 comments on commit fcf89ab

Please sign in to comment.