3030use App \Models \Device ;
3131use App \Models \Port ;
3232use App \Models \User ;
33+ use Illuminate \Database \Query \Builder ;
34+ use Illuminate \Support \Collection ;
3335use Illuminate \Support \Facades \Auth ;
3436use Illuminate \Support \Facades \DB ;
3537
3638class PermissionsCache
3739{
38- private $ devicePermissions ;
39- private $ portPermissions ;
40- private $ billPermissions ;
41- private $ deviceGroupMap ;
40+ /** @var array<int, Collection<object{user_id: int, device_id: int}>> */
41+ private array $ devicePermissions = [];
42+
43+ /** @var array<int, Collection<int>> */
44+ private array $ deviceGroupMap = [];
45+
46+ /** @var Collection<object{user_id: int, port_id: int}>|null */
47+ private ?Collection $ portPermissions = null ;
48+
49+ /** @var Collection<object{user_id: int, bill_id: int}>|null */
50+ private ?Collection $ billPermissions = null ;
4251
4352 /**
4453 * Check if a device can be accessed by user (non-global read/admin)
4554 * If no user is given, use the logged in user
46- *
47- * @param Device|int $device
48- * @param User|int $user
49- * @return bool
5055 */
51- public function canAccessDevice ($ device , $ user = null )
56+ public function canAccessDevice (Device | int $ device , User | int | null $ user = null ): bool
5257 {
5358 return $ this ->getDevicePermissions ($ user )
5459 ->contains ('device_id ' , $ this ->getDeviceId ($ device ));
@@ -57,12 +62,8 @@ public function canAccessDevice($device, $user = null)
5762 /**
5863 * Check if a access can be accessed by user (non-global read/admin)
5964 * If no user is given, use the logged in user
60- *
61- * @param Port|int $port
62- * @param User|int $user
63- * @return bool
6465 */
65- public function canAccessPort ($ port , $ user = null )
66+ public function canAccessPort (Port | int $ port , User | int | null $ user = null ): bool
6667 {
6768 return $ this ->getPortPermissions ()
6869 ->where ('user_id ' , $ this ->getUserId ($ user ))
@@ -73,41 +74,21 @@ public function canAccessPort($port, $user = null)
7374 /**
7475 * Check if a bill can be accessed by user (non-global read/admin)
7576 * If no user is given, use the logged in user
76- *
77- * @param Bill|int $bill
78- * @param User|int $user
79- * @return bool
8077 */
81- public function canAccessBill ($ bill , $ user = null )
78+ public function canAccessBill (Bill | int $ bill , User | int | null $ user = null ): bool
8279 {
8380 return $ this ->getBillPermissions ()
8481 ->where ('user_id ' , $ this ->getUserId ($ user ))
8582 ->where ('bill_id ' , $ this ->getBillId ($ bill ))
8683 ->isNotEmpty ();
8784 }
8885
89- /**
90- * Get the user_id of users that have been granted access to device
91- *
92- * @param Device|int $device
93- * @return \Illuminate\Support\Collection
94- */
95- /*
96- public function usersForDevice($device)
97- {
98- return $this->getDevicePermissions()
99- ->where('device_id', $this->getDeviceId($device))
100- ->pluck('user_id');
101- }
102- */
103-
10486 /**
10587 * Get the user_id of users that have been granted access to port
10688 *
107- * @param Port|int $port
108- * @return \Illuminate\Support\Collection
89+ * @return Collection<int>
10990 */
110- public function usersForPort ($ port )
91+ public function usersForPort (Port | int $ port ): Collection
11192 {
11293 return $ this ->getPortPermissions ()
11394 ->where ('port_id ' , $ this ->getPortId ($ port ))
@@ -117,10 +98,9 @@ public function usersForPort($port)
11798 /**
11899 * Get the user_id of users that have been granted access to bill
119100 *
120- * @param Bill|int $bill
121- * @return \Illuminate\Support\Collection
101+ * @return Collection<int>
122102 */
123- public function usersForBill ($ bill )
103+ public function usersForBill (Bill | int $ bill ): Collection
124104 {
125105 return $ this ->getBillPermissions ()
126106 ->where ('bill_id ' , $ this ->getBillId ($ bill ))
@@ -130,10 +110,9 @@ public function usersForBill($bill)
130110 /**
131111 * Get a list of device_id of all devices the user can access
132112 *
133- * @param User|int $user
134- * @return \Illuminate\Support\Collection
113+ * @return Collection<int>
135114 */
136- public function devicesForUser ($ user = null )
115+ public function devicesForUser (User | int | null $ user = null ): Collection
137116 {
138117 return $ this ->getDevicePermissions ($ user )
139118 ->pluck ('device_id ' );
@@ -142,10 +121,9 @@ public function devicesForUser($user = null)
142121 /**
143122 * Get a list of port_id of all ports the user can access directly
144123 *
145- * @param User|int $user
146- * @return \Illuminate\Support\Collection
124+ * @return Collection<int>
147125 */
148- public function portsForUser ($ user = null )
126+ public function portsForUser (User | int | null $ user = null ): Collection
149127 {
150128 return $ this ->getPortPermissions ()
151129 ->where ('user_id ' , $ this ->getUserId ($ user ))
@@ -155,10 +133,9 @@ public function portsForUser($user = null)
155133 /**
156134 * Get a list of bill_id of all bills the user can access directly
157135 *
158- * @param User|int $user
159- * @return \Illuminate\Support\Collection
136+ * @return Collection<int>
160137 */
161- public function billsForUser ($ user = null )
138+ public function billsForUser (User | int | null $ user = null ): Collection
162139 {
163140 return $ this ->getBillPermissions ()
164141 ->where ('user_id ' , $ this ->getUserId ($ user ))
@@ -168,10 +145,9 @@ public function billsForUser($user = null)
168145 /**
169146 * Get the ids of all device groups the user can access
170147 *
171- * @param User|int $user
172- * @return \Illuminate\Support\Collection
148+ * @return Collection<int>
173149 */
174- public function deviceGroupsForUser ($ user = null )
150+ public function deviceGroupsForUser (User | int | null $ user = null ): Collection
175151 {
176152 $ user_id = $ this ->getUserId ($ user );
177153
@@ -189,10 +165,9 @@ public function deviceGroupsForUser($user = null)
189165 /**
190166 * Get the cached data for device permissions. Use helpers instead.
191167 *
192- * @param User|int $user
193- * @return \Illuminate\Support\Collection
168+ * @return Collection<object{user_id: int, device_id: int}>
194169 */
195- public function getDevicePermissions ($ user = null )
170+ public function getDevicePermissions (User | int | null $ user = null ): Collection
196171 {
197172 $ user_id = $ this ->getUserId ($ user );
198173
@@ -210,9 +185,9 @@ public function getDevicePermissions($user = null)
210185 /**
211186 * Get the cached data for port permissions. Use helpers instead.
212187 *
213- * @return \Illuminate\Support\ Collection
188+ * @return Collection<object{user_id: int, port_id: int}>
214189 */
215- public function getPortPermissions ()
190+ public function getPortPermissions (): Collection
216191 {
217192 if (is_null ($ this ->portPermissions )) {
218193 $ this ->portPermissions = DB ::table ('ports_perms ' )
@@ -226,9 +201,9 @@ public function getPortPermissions()
226201 /**
227202 * Get the cached data for bill permissions. Use helpers instead.
228203 *
229- * @return \Illuminate\Support\ Collection
204+ * @return Collection<object{user_id: int, bill_id: int}>
230205 */
231- public function getBillPermissions ()
206+ public function getBillPermissions (): Collection
232207 {
233208 if (is_null ($ this ->billPermissions )) {
234209 $ this ->billPermissions = DB ::table ('bill_perms ' )
@@ -238,52 +213,42 @@ public function getBillPermissions()
238213 return $ this ->billPermissions ;
239214 }
240215
241- /**
242- * @param mixed $user
243- * @return int|null
244- */
245- private function getUserId ($ user )
216+ public function invalidateCache (): void
217+ {
218+ $ this ->devicePermissions = [];
219+ $ this ->deviceGroupMap = [];
220+ $ this ->portPermissions = null ;
221+ $ this ->billPermissions = null ;
222+ }
223+
224+ private function getUserId (User |int |null $ user ): ?int
246225 {
247226 return $ user instanceof User ? $ user ->user_id : (is_numeric ($ user ) ? (int ) $ user : Auth::id ());
248227 }
249228
250- /**
251- * @param mixed $device
252- * @return int
253- */
254- private function getDeviceId ($ device )
229+ private function getDeviceId (Device |int |null $ device ): int
255230 {
256231 return $ device instanceof Device ? $ device ->device_id : (is_numeric ($ device ) ? (int ) $ device : 0 );
257232 }
258233
259- /**
260- * @param mixed $port
261- * @return int
262- */
263- private function getPortId ($ port )
234+ private function getPortId (Port |int |null $ port ): int
264235 {
265236 return $ port instanceof Port ? $ port ->port_id : (is_numeric ($ port ) ? (int ) $ port : 0 );
266237 }
267238
268- /**
269- * @param mixed $bill
270- * @return int
271- */
272- private function getBillId ($ bill )
239+ private function getBillId (Bill |int |null $ bill ): int
273240 {
274241 return $ bill instanceof Bill ? $ bill ->bill_id : (is_numeric ($ bill ) ? (int ) $ bill : 0 );
275242 }
276243
277- /**
278- * @return \Illuminate\Database\Query\Builder
279- */
280- public function getDeviceGroupPermissionsQuery ()
244+ private function getDeviceGroupPermissionsQuery (): Builder
281245 {
282246 return DB ::table ('devices_group_perms ' )
283- ->select ('devices_group_perms.user_id ' , 'device_group_device.device_id ' )
284- ->join ('device_group_device ' , 'device_group_device.device_group_id ' , '= ' , 'devices_group_perms.device_group_id ' )
285- ->when (! LibrenmsConfig::get ('permission.device_group.allow_dynamic ' ), fn ($ query ) => $ query
286- ->join ('device_groups ' , 'device_groups.id ' , '= ' , 'devices_group_perms.device_group_id ' )
287- ->where ('device_groups.type ' , 'static ' ));
247+ ->select ('devices_group_perms.user_id ' , 'device_group_device.device_id ' )
248+ ->join ('device_group_device ' , 'device_group_device.device_group_id ' , '= ' ,
249+ 'devices_group_perms.device_group_id ' )
250+ ->when (! LibrenmsConfig::get ('permission.device_group.allow_dynamic ' ), fn ($ query ) => $ query
251+ ->join ('device_groups ' , 'device_groups.id ' , '= ' , 'devices_group_perms.device_group_id ' )
252+ ->where ('device_groups.type ' , 'static ' ));
288253 }
289254}
0 commit comments