|
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
|
3 |
|
public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null) |
|
27
|
|
|
{ |
|
28
|
3 |
|
$booking = $resource->getInstance(); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
3 |
|
if (!$booking) { |
|
31
|
|
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
if (!User::getCurrent()) { |
|
35
|
|
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
$bookable = $booking->getBookable(); |
|
39
|
|
|
|
|
40
|
3 |
|
if (!$bookable) { |
|
41
|
|
|
// Booking using user's own equipment is always allowed |
|
42
|
2 |
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
if (!$bookable->isActive()) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Check that the user has ALL required licenses for the bookable |
|
50
|
1 |
|
if (!$bookable->getLicenses()->isEmpty()) { |
|
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
|
|
|
if ($bookable->getSimultaneousBookingMaximum() > 0) { |
|
61
|
|
|
// Check that the bookable has no more running bookings than its maximum |
|
62
|
|
|
$runningBookings = _em()->getRepository(Booking::class)->findBy([ |
|
63
|
|
|
'bookable' => $bookable, |
|
64
|
|
|
'endDate' => null, |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
if (count($runningBookings) >= $bookable->getSimultaneousBookingMaximum()) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|