Commit 1a21a389 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add user group attachment from groups view

parent d07f3100
......@@ -7,6 +7,8 @@ namespace App\Http\Controllers\Web\Admin;
use Illuminate\View\View;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Source\UseCases\GroupUser\AddUserToGroup\APIPresenter as UserToGroupPresenter;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupSearch;
use Source\UseCases\GroupSchedule\AddSchedulesToGroups\ApiPresenter as ScheduleGroupPresenter;
use Source\UseCases\GroupSchedule\AddSchedulesToGroups\AddSchedulesToGroupsUseCase;
use Source\UseCases\GroupSchedule\GetGroupSchedules\WebPresenter as GroupSchedulesPresenter;
......@@ -71,9 +73,23 @@ class GroupRelationController extends Controller
}
public function addUser(): RedirectResponse
/**
* @param string $groupId
* @param \Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupSearch $addUserToGroupSearch
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Validation\ValidationException
*/
public function addUser(string $groupId, AddUserToGroupSearch $addUserToGroupSearch): RedirectResponse
{
$this->validate($this->request, [
'user' => 'required|string',
]);
$presenter = new UserToGroupPresenter();
$addUserToGroupSearch->addUserToGroup([$this->request->input('user')], [$groupId], $presenter);
return redirect()->back()->with($presenter->getViewModel());
}
/**
......
<?php
namespace Source\UseCases\GroupUser\AddUserToGroup;
use Source\Authorization\Authorizer;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\AuthorizationException;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\GroupUser\GroupUserRepository;
class AddUserToGroupSearch implements AddUserToGroupUseCase
{
/**
* @var \Source\Gateways\GroupUser\GroupUserRepository
*/
protected GroupUserRepository $repository;
/**
* @var \Source\Authorization\Authorizer
*/
protected Authorizer $authorizer;
/**
* @var \Source\Gateways\Users\UsersRepository
*/
protected UsersRepository $users;
/**
* @param \Source\Authorization\Authorizer $authorizer
* @param \Source\Gateways\GroupUser\GroupUserRepository $repository
* @param \Source\Gateways\Users\UsersRepository $users
*/
public function __construct(Authorizer $authorizer, GroupUserRepository $repository, UsersRepository $users)
{
$this->repository = $repository;
$this->authorizer = $authorizer;
$this->users = $users;
}
/**
* @inheritDoc
*/
public function addUserToGroup(array $userIds, array $groupIds, Presenter $presenter): void
{
$response = new ResponseModel();
foreach ($groupIds as $groupId) {
/*
* Not actually a userID, it could be... But it also could be an emplid or email
* So, user is emplid, email, or id
* Check all of them in this order:
* - email
* - emplid
* - id
*
* emplid before id simply because in the case of a collision where
* id == emplid, it's more likely that they are typing in an emplid
*/
foreach ($userIds as $query) {
try {
$user = $this->users->findByEmail($query);
if (!$user) {
$user = $this->users->findByEmplid($query);
}
$id = $user ? $user->getId() : $query;
$this->authorizer->protectAdminRights($id, $groupId);
$this->repository->addUserToGroup($id, $groupId);
$response->addMessage("Attached user $query to group $groupId");
} catch (EntityNotFoundException $e) {
$response->addError("Failed to attach user $query to group $groupId. " . $e->getMessage());
} catch (AuthorizationException $e) {
$response->addError("Failed to attach user $query to group $groupId. " . $e->getMessage());
}
}
}
$presenter->present($response);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment