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 Zend\Permissions\Acl\Acl; |
10
|
|
|
use Zend\Permissions\Acl\Assertion\AssertionInterface; |
11
|
|
|
use Zend\Permissions\Acl\Resource\ResourceInterface; |
12
|
|
|
use Zend\Permissions\Acl\Role\RoleInterface; |
13
|
|
|
|
14
|
|
|
class BookableAvailable implements AssertionInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Assert that the bookable of the given booking can be rented by the current user |
18
|
|
|
* |
19
|
|
|
* @param \Application\Acl\Acl $acl |
20
|
|
|
* @param RoleInterface $role |
21
|
|
|
* @param ResourceInterface $resource |
22
|
|
|
* @param string $privilege |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
4 |
|
public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null) |
27
|
|
|
{ |
28
|
|
|
/** @var Booking $booking */ |
29
|
4 |
|
$booking = $resource->getInstance(); |
|
|
|
|
30
|
|
|
|
31
|
4 |
|
if (!$booking) { |
32
|
|
|
return $acl->reject('the booking does not exist'); |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
if (!User::getCurrent()) { |
36
|
|
|
return $acl->reject('the user is not logged in'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
$bookable = $booking->getBookable(); |
40
|
|
|
|
41
|
4 |
|
if (!$bookable) { |
42
|
|
|
// Booking using user's own equipment is always allowed |
43
|
2 |
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
if (!$bookable->isActive()) { |
47
|
|
|
return $acl->reject('the bookable is not active'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Check that the user has ALL required licenses for the bookable |
51
|
2 |
|
if (!$bookable->getLicenses()->isEmpty() && User::getCurrent()->getRole() !== User::ROLE_BOOKING_ONLY) { |
52
|
1 |
|
$userLicenses = User::getCurrent()->getLicenses(); |
53
|
|
|
|
54
|
1 |
|
foreach ($bookable->getLicenses() as $requiredLicense) { |
55
|
1 |
|
if (!$userLicenses->contains($requiredLicense)) { |
56
|
1 |
|
return $acl->reject('the user does not have the required license: ' . $requiredLicense->getName()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
if ($bookable->getSimultaneousBookingMaximum() > 0) { |
62
|
|
|
// Check that the bookable has no more running bookings than its maximum |
63
|
1 |
|
$runningBookings = _em()->getRepository(Booking::class)->findBy([ |
64
|
1 |
|
'bookable' => $bookable, |
65
|
|
|
'endDate' => null, |
66
|
|
|
]); |
67
|
|
|
|
68
|
1 |
|
$count = count($runningBookings); |
69
|
1 |
|
if ($count >= $bookable->getSimultaneousBookingMaximum()) { |
70
|
|
|
return $acl->reject('the bookable limit of simultaneous bookings has been reached: ' . $count . '/' . $bookable->getSimultaneousBookingMaximum()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|