Skip to content

Commit

Permalink
#457 - fix: added permission to displaying inactive users on index views
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilpiech97 committed Jul 10, 2024
1 parent ce0c013 commit 2e73d01
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 87 deletions.
9 changes: 8 additions & 1 deletion app/Domain/EmployeesMilestonesRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@

class EmployeesMilestonesRetriever
{
protected bool $hasPermissionToViewInactiveUsers;

public function __construct(
protected VacationTypeConfigRetriever $configRetriever,
) {}

public function getResults(?string $searchText, ?string $sort): Collection
public function getResults(User $user, ?string $searchText, ?string $sort): Collection
{
$this->hasPermissionToViewInactiveUsers = $user->hasPermissionTo("showInactiveUsers");

return match ($sort) {
"birthday-asc" => $this->getUpcomingBirthdays($searchText),
"birthday-desc" => $this->getUpcomingBirthdays($searchText, "desc"),
"seniority-asc" => $this->getSeniority($searchText),
"seniority-desc" => $this->getSeniority($searchText, "desc"),
default => User::query()
->withTrashed($this->hasPermissionToViewInactiveUsers)
->search($searchText)
->orderByProfileField("last_name")
->orderByProfileField("first_name")
Expand All @@ -34,6 +39,7 @@ public function getResults(?string $searchText, ?string $sort): Collection
public function getUpcomingBirthdays(?string $searchText, string $direction = "asc"): Collection
{
$users = User::query()
->withTrashed($this->hasPermissionToViewInactiveUsers)
->search($searchText)
->get();

Expand All @@ -43,6 +49,7 @@ public function getUpcomingBirthdays(?string $searchText, string $direction = "a
public function getSeniority(?string $searchText, string $direction = "asc"): Collection
{
return User::query()
->withTrashed($this->hasPermissionToViewInactiveUsers)
->search($searchText)
->orderBy(
UserHistory::query()
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/AssignedBenefitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Inertia\Response;
use Toby\Enums\Month;
Expand All @@ -22,11 +23,12 @@ class AssignedBenefitController extends Controller
/**
* @throws AuthorizationException
*/
public function index(): Response
public function index(Request $request): Response
{
$this->authorize("manageBenefits");

$users = User::query()
->withTrashed($request->user()->hasPermissionTo("showInactiveUsers"))
->orderByProfileField("last_name")
->orderByProfileField("first_name")
->get();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/EmployeesMilestonesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function index(Request $request, EmployeesMilestonesRetriever $employeesM
$searchText = $request->query("search");
$sort = $request->query("sort");

$users = $employeesMilestoneRetriever->getResults($searchText, $sort);
$users = $employeesMilestoneRetriever->getResults($request->user(), $searchText, $sort);

return inertia("EmployeesMilestones", [
"users" => EmployeeMilestoneResource::collection($users),
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/EquipmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function index(Request $request): RedirectResponse|Response
$searchQuery = $request->query("search");

$equipmentItems = EquipmentItem::query()
->with("assignee")
->with(["assignee" => fn($query) => $query->withTrashed()])
->search($searchQuery)
->when(
$request->query("assignee") && $request->query("assignee") !== "unassigned",
Expand All @@ -49,6 +49,7 @@ public function index(Request $request): RedirectResponse|Response
->withQueryString();

$users = User::query()
->withTrashed()
->orderByProfileField("last_name")
->orderByProfileField("first_name")
->get();
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/KeysController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function index(Request $request): Response
->sortBy("id");

$users = User::query()
->withTrashed($request->user()->hasPermissionTo("showInactiveUsers"))
->orderByProfileField("last_name")
->orderByProfileField("first_name")
->get();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/MonthlyUsageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __invoke(
$currentUser = $request->user();

$users = User::query()
->withTrashed()
->withTrashed($currentUser->hasPermissionTo("showInactiveUsers"))
->withVacationLimitIn($currentYearPeriod)
->where("id", "!=", $currentUser->id)
->orderByProfileField("last_name")
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/OvertimeRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ public function indexForApprovers(
$yearPeriod = $yearPeriodRetriever->selected();
$status = $request->get("status", "all");
$user = $request->get("user");
$withTrashedUsers = $request->boolean("withTrashedUsers") ?? false;
$authUser = $request->user();
$withTrashedUsers = $authUser->can("showInactiveUsers");

$overtimeRequests = OvertimeRequest::query()
->with(["user.permissions", "user.profile"])
->whereBelongsTo($yearPeriod)
->whereRelation("user", fn(Builder $query): Builder => $query->withTrashed($withTrashedUsers))
->when($user !== null, fn(Builder $query): Builder => $query->where("user_id", $user))
->states(OvertimeRequestStatesRetriever::filterByStatusGroup($status, $request->user()))
->states(OvertimeRequestStatesRetriever::filterByStatusGroup($status, $authUser))
->latest()
->paginate();

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/ResumeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Toby\Http\Controllers;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
Expand All @@ -24,6 +25,7 @@ public function index(): Response

$resumes = Resume::query()
->with("user")
->whereRelation("user", fn(Builder $query): Builder => $query->withTrashed(false))
->latest("updated_at")
->paginate();

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/VacationCalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function index(
): Response {
$month = Month::fromNameOrCurrent((string)$month);
$currentUser = $request->user();
$withTrashedUsers = $request->boolean("withBlockedUsers");
$withTrashedUsers = $currentUser->hasPermissionTo("showInactiveUsers");

$yearPeriod = $yearPeriodRetriever->selected();
$carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber());
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/VacationLimitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Toby\Http\Controllers;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Response;
use Toby\Domain\UserVacationStatsRetriever;
use Toby\Helpers\YearPeriodRetriever;
Expand All @@ -16,7 +18,7 @@

class VacationLimitController extends Controller
{
public function edit(YearPeriodRetriever $yearPeriodRetriever, UserVacationStatsRetriever $statsRetriever): Response
public function edit(Request $request, YearPeriodRetriever $yearPeriodRetriever, UserVacationStatsRetriever $statsRetriever): Response
{
$this->authorize("manageVacationLimits");

Expand All @@ -25,6 +27,7 @@ public function edit(YearPeriodRetriever $yearPeriodRetriever, UserVacationStats

$limits = $yearPeriod
->vacationLimits()
->whereRelation("user", fn(Builder $query): Builder => $query->withTrashed($request->user()->hasPermissionTo("showInactiveUsers")))
->with("user.profile")
->has("user")
->get()
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/VacationRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,16 @@ public function indexForApprovers(
$status = $request->get("status", "all");
$user = $request->get("user");
$type = $request->get("type");
$withTrashedUsers = $request->boolean("withTrashedUsers") ?? false;
$authUser = $request->user();
$withTrashedUsers = $authUser->can("showInactiveUsers");

$vacationRequests = VacationRequest::query()
->with(["vacations.user.profile", "user.permissions", "user.profile"])
->whereBelongsTo($yearPeriod)
->whereRelation("user", fn(Builder $query): Builder => $query->withTrashed($withTrashedUsers))
->when($user !== null, fn(Builder $query): Builder => $query->where("user_id", $user))
->when($type !== null, fn(Builder $query): Builder => $query->where("type", $type))
->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $request->user()))
->states(VacationRequestStatesRetriever::filterByStatusGroup($status, $authUser))
->latest()
->paginate();

Expand All @@ -131,7 +132,6 @@ public function indexForApprovers(
"status" => $status,
"user" => (int)$user,
"type" => $type,
"withTrashedUsers" => $withTrashedUsers,
],
]);
}
Expand Down
1 change: 1 addition & 0 deletions config/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"managePermissions",
"manageHolidays",
"manageUsers",
"showInactiveUsers",
"manageKeys",
"manageTechnologies",
"manageResumes",
Expand Down
5 changes: 5 additions & 0 deletions resources/js/Composables/permissionInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const permissionsInfo = [
'value': 'manageUsers',
'section': 'Użytkownicy',
},
{
'name': 'Widoczność zablokowanych użytkowników',
'value': 'showInactiveUsers',
'section': 'Użytkownicy',
},
{
'name': 'Zarządzanie kluczami',
'value': 'manageKeys',
Expand Down
11 changes: 6 additions & 5 deletions resources/js/Pages/AssignedBenefits/AssignedBenefits.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ function isBenefitHasCompanion(benefitId) {
<tr
v-for="(item, index) in form.items"
:key="item.user.id"
class="group hover:bg-blumilk-25 divide-x divide-gray-300"
:class="[item.user.isActive ? '' : 'bg-gray-100', 'group hover:bg-blumilk-25 divide-x divide-gray-300']"
>
<th class="group p-2 sticky left-0 outline outline-1 outline-offset-0 outline-gray-300 bg-white hover:bg-blumilk-25 group-hover:bg-blumilk-25">
<th class="group p-2 sticky left-0 outline outline-1 outline-offset-0 outline-gray-300 hover:bg-blumilk-25 group-hover:bg-blumilk-25">
<div class="flex justify-start items-center">
<span class="inline-flex justify-center items-center w-8 h-8 rounded-full">
<img :src="item.user.avatar">
Expand All @@ -198,7 +198,8 @@ function isBenefitHasCompanion(benefitId) {
:name="`${benefit.id}-employer-${index}`"
type="number"
step="0.01"
class="w-full h-full sm:text-sm appearance-none border-none text-right p-0 px-3 m-0 ring-inset hover:bg-blumilk-25 group-hover:bg-blumilk-25 focus:bg-blumilk-25 focus:ring-2 focus:ring-blumilk-300"
class=""
:class="[item.user.isActive ? '' : 'bg-gray-100', 'w-full h-full sm:text-sm appearance-none border-none text-right p-0 px-3 m-0 ring-inset hover:bg-blumilk-25 group-hover:bg-blumilk-25 focus:bg-blumilk-25 focus:ring-2 focus:ring-blumilk-300']"
title="Wprowadź kwotę."
min="0"
>
Expand All @@ -209,7 +210,7 @@ function isBenefitHasCompanion(benefitId) {
:name="`${benefit.id}-employee-${index}`"
type="number"
step="0.01"
class="w-full h-full sm:text-sm appearance-none border-none text-right p-0 px-3 m-0 ring-inset hover:bg-blumilk-25 group-hover:bg-blumilk-25 focus:bg-blumilk-25 focus:ring-2 focus:ring-blumilk-300"
:class="[item.user.isActive ? '' : 'bg-gray-100', 'w-full h-full sm:text-sm appearance-none border-none text-right p-0 px-3 m-0 ring-inset hover:bg-blumilk-25 group-hover:bg-blumilk-25 focus:bg-blumilk-25 focus:ring-2 focus:ring-blumilk-300']"
title="Wprowadź kwotę."
min="0"
>
Expand All @@ -226,7 +227,7 @@ function isBenefitHasCompanion(benefitId) {
<TextArea
v-model="item.comment"
:resize="true"
class="w-full sm:text-sm border-none appearance-none mt-1 focus:ring-0 focus:bg-blumilk-25 group-hover:bg-blumilk-25 resize-y h-full"
:class="[item.user.isActive ? '' : 'bg-gray-100', 'w-full sm:text-sm border-none appearance-none mt-1 focus:ring-0 focus:bg-blumilk-25 group-hover:bg-blumilk-25 resize-y h-full']"
style="min-height: 40px"
/>
</td>
Expand Down
41 changes: 4 additions & 37 deletions resources/js/Pages/Calendar.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<script setup>
import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/vue/24/solid'
import { computed, reactive, ref, watch } from 'vue'
import { computed, ref } from 'vue'
import { useMonthInfo } from '@/Composables/monthInfo.js'
import VacationTypeCalendarIcon from '@/Shared/VacationTypeCalendarIcon.vue'
import CalendarDay from '@/Shared/CalendarDay.vue'
import { debounce } from 'lodash'
import { Inertia } from '@inertiajs/inertia'
const props = defineProps({
users: Object,
Expand All @@ -14,11 +12,6 @@ const props = defineProps({
current: String,
selected: String,
years: Object,
withBlockedUsers: Boolean,
})
const form = reactive({
withTrashedUsers: props.withBlockedUsers ?? false,
})
let activeElement = ref(undefined)
Expand Down Expand Up @@ -52,15 +45,6 @@ function linkParameters(user, day) {
function linkVacationRequest(user){
return props.auth.user.id === user.id || props.auth.can.manageRequestsAsTechnicalApprover || props.auth.can.manageRequestsAsAdministrativeApprover
}
watch(form, debounce(() => {
Inertia.get('', {
withBlockedUsers: form.withTrashedUsers,
}, {
preserveState: true,
replace: true,
})
}, 150))
</script>

<template>
Expand All @@ -75,7 +59,7 @@ watch(form, debounce(() => {
<InertiaLink
v-if="previousMonth"
as="button"
:href="`/calendar/${previousMonth.value}?withBlockedUsers=${form.withTrashedUsers}`"
:href="`/calendar/${previousMonth.value}`"
class="flex focus:relative justify-center items-center p-2 text-gray-400 hover:text-gray-500 bg-white rounded-l-md border border-r-0 border-gray-300 focus:outline-blumilk-500 md:px-2 md:w-9 md:hover:bg-gray-50"
>
<ChevronLeftIcon class="w-5 h-5" />
Expand All @@ -89,15 +73,15 @@ watch(form, debounce(() => {
<InertiaLink
v-if="years.current.year === years.selected.year"
as="button"
:href="`/calendar/${currentMonth.value}?withBlockedUsers=${form.withTrashedUsers}`"
:href="`/calendar/${currentMonth.value}`"
class="hidden focus:relative items-center p-2 text-sm font-medium text-gray-700 hover:text-gray-900 bg-white hover:bg-gray-50 border-y border-gray-300 focus:outline-blumilk-500 md:flex"
>
Dzisiaj
</InertiaLink>
<InertiaLink
v-if="nextMonth"
as="button"
:href="`/calendar/${nextMonth.value}?withBlockedUsers=${form.withTrashedUsers}`"
:href="`/calendar/${nextMonth.value}`"
class="flex focus:relative justify-center items-center p-2 text-gray-400 hover:text-gray-500 bg-white rounded-r-md border border-l-0 border-gray-300 focus:outline-blumilk-500 md:px-2 md:w-9 md:hover:bg-gray-50"
>
<ChevronRightIcon class="w-5 h-5" />
Expand Down Expand Up @@ -129,23 +113,6 @@ watch(form, debounce(() => {
</a>
</div>
</div>
<div
v-if="auth.can.manageRequestsAsAdministrativeApprover"
class="flex items-center space-x-2 pb-2 px-4 sm:px-6"
>
<input
id="withTrashedUsers"
v-model="form.withTrashedUsers"
class="left-6 top-1/2 h-4 w-4 rounded border-gray-300 text-blumilk-600 focus:ring-blumilk-500"
type="checkbox"
>
<label
class="block text-sm font-medium text-gray-700"
for="withTrashedUsers"
>
Zablokowani użytkownicy
</label>
</div>
<div class="overflow-x-auto">
<table class="w-full text-sm text-center border border-gray-300">
<thead>
Expand Down
1 change: 1 addition & 0 deletions resources/js/Pages/EmployeesMilestones.vue
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ watch(form, debounce(() => {
<tr
v-for="user in users.data"
:key="user.id"
:class="[user.user.isActive ? '' : 'bg-gray-100']"
>
<td class="p-4 text-sm text-gray-500 whitespace-nowrap">
<div class="flex">
Expand Down
1 change: 1 addition & 0 deletions resources/js/Pages/Equipment/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ watch(form, debounce(() => {
v-for="item in equipmentItems.data"
:key="item.id"
class="hover:bg-blumilk-25"
:class="[item.assignee.isActive ? '' : 'bg-gray-100', 'hover:bg-blumilk-25']"
>
<td class="p-4 text-sm text-gray-500 font-semibold whitespace-nowrap">
{{ item.idNumber }}
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Keys.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function submitGiveKey() {
<tr
v-for="key in keys.data"
:key="key.id"
class="hover:bg-blumilk-25"
:class="[key.user.isActive ? '' : 'bg-gray-100', 'hover:bg-blumilk-25']"
>
<td class="p-4 text-sm text-gray-500 whitespace-nowrap">
Klucz nr {{ key.id }}
Expand Down
Loading

0 comments on commit 2e73d01

Please sign in to comment.