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 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
|
4 |
|
$booking = $resource->getInstance(); |
|
|
|
|
29
|
|
|
|
30
|
4 |
|
if (!$booking) { |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
if (!User::getCurrent()) { |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
4 |
|
$bookable = $booking->getBookable(); |
39
|
|
|
|
40
|
4 |
|
if (!$bookable) { |
41
|
|
|
// Booking using user's own equipment is always allowed |
42
|
2 |
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
if (!$bookable->isActive()) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Check that the user has ALL required licenses for the bookable |
50
|
2 |
|
if (!$bookable->getLicenses()->isEmpty() && User::getCurrent()->getRole() !== User::ROLE_BOOKING_ONLY) { |
51
|
1 |
|
$userLicenses = User::getCurrent()->getLicenses(); |
52
|
|
|
|
53
|
1 |
|
foreach ($bookable->getLicenses() as $requiredLicense) { |
54
|
1 |
|
if (!$userLicenses->contains($requiredLicense)) { |
55
|
1 |
|
return false; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
if ($bookable->getSimultaneousBookingMaximum() > 0) { |
61
|
|
|
// Check that the bookable has no more running bookings than its maximum |
62
|
1 |
|
$runningBookings = _em()->getRepository(Booking::class)->findBy([ |
63
|
1 |
|
'bookable' => $bookable, |
64
|
|
|
'endDate' => null, |
65
|
|
|
]); |
66
|
|
|
|
67
|
1 |
|
if (count($runningBookings) >= $bookable->getSimultaneousBookingMaximum()) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return true; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|