2020-10-03 20:36:35 +00:00
|
|
|
<?php
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2020 Miguel Nogueira
|
|
|
|
*
|
|
|
|
* This file is part of Raspberry Staff Manager.
|
|
|
|
*
|
|
|
|
* Raspberry Staff Manager is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Raspberry Staff Manager is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
use App\Exceptions\InvalidInviteException;
|
|
|
|
use App\Exceptions\PublicTeamInviteException;
|
|
|
|
use App\Exceptions\UserAlreadyInvitedException;
|
2020-10-03 20:36:35 +00:00
|
|
|
use App\Http\Requests\EditTeamRequest;
|
|
|
|
use App\Http\Requests\NewTeamRequest;
|
|
|
|
use App\Http\Requests\SendInviteRequest;
|
|
|
|
use App\Mail\InviteToTeam;
|
2021-07-25 21:54:15 +00:00
|
|
|
use App\Services\TeamService;
|
2020-10-03 20:36:35 +00:00
|
|
|
use App\Team;
|
|
|
|
use App\User;
|
2020-10-08 18:19:10 +00:00
|
|
|
use App\Vacancy;
|
2021-07-25 21:54:15 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2020-10-03 20:36:35 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
2020-10-08 18:19:10 +00:00
|
|
|
use Mpociot\Teamwork\Exceptions\UserNotInTeamException;
|
2020-10-03 20:36:35 +00:00
|
|
|
use Mpociot\Teamwork\Facades\Teamwork;
|
|
|
|
use Mpociot\Teamwork\TeamInvite;
|
|
|
|
|
|
|
|
class TeamController extends Controller
|
|
|
|
{
|
2021-07-25 21:54:15 +00:00
|
|
|
private $teamService;
|
|
|
|
|
|
|
|
public function __construct(TeamService $teamService) {
|
|
|
|
$this->teamService = $teamService;
|
|
|
|
}
|
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2020-12-21 01:02:05 +00:00
|
|
|
$this->authorize('index', Team::class);
|
2020-12-08 02:58:10 +00:00
|
|
|
|
2020-10-08 18:19:10 +00:00
|
|
|
$teams = Team::with('users.roles')->get();
|
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
return view('dashboard.teams.teams')
|
2020-10-08 18:19:10 +00:00
|
|
|
->with('teams', $teams);
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
2020-12-21 01:02:05 +00:00
|
|
|
* @param NewTeamRequest $request
|
2021-07-25 21:54:15 +00:00
|
|
|
* @return RedirectResponse
|
2020-12-21 01:02:05 +00:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2020-10-03 20:36:35 +00:00
|
|
|
*/
|
|
|
|
public function store(NewTeamRequest $request)
|
|
|
|
{
|
2020-12-21 01:02:05 +00:00
|
|
|
$this->authorize('create', Team::class);
|
2021-07-25 21:54:15 +00:00
|
|
|
$this->teamService->createTeam($request->teamName, Auth::user()->id);
|
2020-12-08 02:58:10 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success', __('Team successfully created.'));
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
2020-12-21 01:02:05 +00:00
|
|
|
* @param Team $team
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2020-10-03 20:36:35 +00:00
|
|
|
*/
|
|
|
|
public function edit(Team $team)
|
|
|
|
{
|
2020-12-08 02:58:10 +00:00
|
|
|
$this->authorize('update', $team);
|
2020-10-03 20:36:35 +00:00
|
|
|
return view('dashboard.teams.edit-team')
|
2020-12-21 01:02:05 +00:00
|
|
|
->with([
|
|
|
|
'team' => $team,
|
|
|
|
'users' => User::all(),
|
|
|
|
'vacancies' => Vacancy::with('teams')->get()->all()
|
|
|
|
]);
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
2020-12-21 01:02:05 +00:00
|
|
|
* @param EditTeamRequest $request
|
|
|
|
* @param Team $team
|
2021-07-25 21:54:15 +00:00
|
|
|
* @return RedirectResponse
|
2020-12-21 01:02:05 +00:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2020-10-03 20:36:35 +00:00
|
|
|
*/
|
2021-07-25 21:54:15 +00:00
|
|
|
public function update(EditTeamRequest $request, Team $team): RedirectResponse
|
2020-10-03 20:36:35 +00:00
|
|
|
{
|
2020-12-08 02:58:10 +00:00
|
|
|
$this->authorize('update', $team);
|
2021-07-25 21:54:15 +00:00
|
|
|
$team = $this->teamService->updateTeam($team, $request->teamDescription, $team->joinType);
|
2020-12-08 02:58:10 +00:00
|
|
|
|
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
if ($team) {
|
|
|
|
return redirect()
|
|
|
|
->to(route('teams.index'))
|
|
|
|
->with('success', __('Team updated.'));
|
|
|
|
}
|
2020-10-10 16:30:26 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('error', __('An error ocurred while trying to update this team.'));
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2020-12-21 01:02:05 +00:00
|
|
|
// wip
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
public function invite(SendInviteRequest $request, Team $team): RedirectResponse
|
2020-10-09 21:27:36 +00:00
|
|
|
{
|
2020-12-08 02:58:10 +00:00
|
|
|
$this->authorize('invite', $team);
|
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
try {
|
2020-10-03 20:36:35 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
$this->teamService->inviteUser($team, $request->user);
|
2020-10-09 21:27:36 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success', __('User invited successfully!'));
|
2020-10-03 20:36:35 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
} catch (UserAlreadyInvitedException | PublicTeamInviteException $ex) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('error', $ex->getMessage());
|
|
|
|
}
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
public function processInviteAction(Request $request, $action, $token): RedirectResponse
|
2020-10-03 20:36:35 +00:00
|
|
|
{
|
2021-07-25 21:54:15 +00:00
|
|
|
try {
|
2020-10-09 21:27:36 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
$this->teamService->processInvite(Auth::user(), $action, $token);
|
2020-10-03 20:36:35 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
return redirect()
|
|
|
|
->to(route('teams.index'))
|
|
|
|
->with('success', __('Invite processed successfully!'));
|
2020-10-03 20:36:35 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
} catch (InvalidInviteException $e) {
|
2020-10-09 21:27:36 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('error', $e->getMessage());
|
2020-10-09 21:27:36 +00:00
|
|
|
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-08 18:19:10 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
public function switchTeam(Request $request, Team $team): RedirectResponse
|
2020-10-08 18:19:10 +00:00
|
|
|
{
|
2020-12-08 02:58:10 +00:00
|
|
|
$this->authorize('switchTeam', $team);
|
|
|
|
|
2020-10-10 16:30:26 +00:00
|
|
|
try {
|
2020-10-08 18:19:10 +00:00
|
|
|
Auth::user()->switchTeam($team);
|
|
|
|
|
2021-07-19 23:35:03 +00:00
|
|
|
$request->session()->flash('success', __('Switched teams! Your team dashboard will now use this context.'));
|
2020-10-10 16:30:26 +00:00
|
|
|
} catch (UserNotInTeamException $ex) {
|
2021-07-19 23:35:03 +00:00
|
|
|
$request->session()->flash('error', __('You can\'t switch to a team you don\'t belong to.'));
|
2020-10-08 18:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
2020-10-09 21:27:36 +00:00
|
|
|
|
|
|
|
// Since it's a separate form, we shouldn't use the same update method
|
2021-07-25 21:54:15 +00:00
|
|
|
public function assignVacancies(Request $request, Team $team): RedirectResponse
|
2020-10-09 21:27:36 +00:00
|
|
|
{
|
2020-12-08 02:58:10 +00:00
|
|
|
$this->authorize('update', $team);
|
2021-07-25 21:54:15 +00:00
|
|
|
$message = $this->teamService->updateVacancies($team, $request->assocVacancies);
|
2020-12-08 02:58:10 +00:00
|
|
|
|
2021-07-25 21:54:15 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success', $message);
|
2020-10-09 21:27:36 +00:00
|
|
|
}
|
2020-10-03 20:36:35 +00:00
|
|
|
}
|