|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Acl\Assertion; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\Booking; |
|
8
|
|
|
use Application\Model\User; |
|
9
|
|
|
use Ecodev\Felix\Acl\Assertion\NamedAssertion; |
|
10
|
|
|
use Laminas\Permissions\Acl\Acl; |
|
11
|
|
|
use Laminas\Permissions\Acl\Resource\ResourceInterface; |
|
12
|
|
|
use Laminas\Permissions\Acl\Role\RoleInterface; |
|
13
|
|
|
|
|
14
|
|
|
class BookableAvailable implements NamedAssertion |
|
15
|
|
|
{ |
|
16
|
|
|
public function getName(): string |
|
17
|
|
|
{ |
|
18
|
|
|
return 'le réservable est est disponible'; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Assert that the bookable of the given booking can be rented by the current user. |
|
23
|
|
|
* |
|
24
|
|
|
* @param \Application\Acl\Acl $acl |
|
25
|
|
|
* @param RoleInterface $role |
|
26
|
|
|
* @param ResourceInterface $resource |
|
27
|
|
|
* @param string $privilege |
|
28
|
|
|
* |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function assert(Acl $acl, ?RoleInterface $role = null, ?ResourceInterface $resource = null, $privilege = null) |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var null|Booking $booking */ |
|
34
|
3 |
|
$booking = $resource->getInstance(); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
3 |
|
if (!$booking) { |
|
37
|
|
|
return $acl->reject('the booking does not exist'); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
if (!User::getCurrent()) { |
|
41
|
|
|
return $acl->reject('the user is not logged in'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
$bookable = $booking->getBookable(); |
|
45
|
|
|
|
|
46
|
3 |
|
if (!$bookable) { |
|
47
|
|
|
// Booking using user's own equipment is always allowed |
|
48
|
1 |
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
if (!$bookable->isActive()) { |
|
52
|
|
|
return $acl->reject('the bookable is not active'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Check that the user has ALL required licenses for the bookable |
|
56
|
2 |
|
if (!$bookable->getLicenses()->isEmpty() && User::getCurrent()->getRole() !== User::ROLE_BOOKING_ONLY) { |
|
57
|
1 |
|
$userLicenses = User::getCurrent()->getLicenses(); |
|
58
|
|
|
|
|
59
|
1 |
|
foreach ($bookable->getLicenses() as $requiredLicense) { |
|
60
|
1 |
|
if (!$userLicenses->contains($requiredLicense)) { |
|
61
|
1 |
|
return $acl->reject('the user does not have the required license: ' . $requiredLicense->getName()); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
if ($bookable->getSimultaneousBookingMaximum() > 0) { |
|
67
|
|
|
// Check that the bookable has no more running bookings than its maximum |
|
68
|
1 |
|
$runningBookings = _em()->getRepository(Booking::class)->findBy([ |
|
69
|
1 |
|
'bookable' => $bookable, |
|
70
|
|
|
'endDate' => null, |
|
71
|
|
|
]); |
|
72
|
|
|
|
|
73
|
1 |
|
$count = count($runningBookings); |
|
74
|
1 |
|
if ($count >= $bookable->getSimultaneousBookingMaximum()) { |
|
75
|
|
|
return $acl->reject('the bookable limit of simultaneous bookings has been reached: ' . $count . '/' . $bookable->getSimultaneousBookingMaximum()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.