77use App \Models \User ;
88use Illuminate \Foundation \Auth \Access \AuthorizesRequests ;
99use Illuminate \Http \Request ;
10- use Illuminate \Support \Facades \Auth ;
1110use Illuminate \Support \Facades \DB ;
1211
1312/**
1413 * Class incapsulates checkout logic for reuse in different controllers
14+ *
1515 * @author [D. Minaev.] [<dmitriy.minaev.v@gmail.com>]
1616 */
1717class PredefinedKitCheckoutService
1818{
1919 use AuthorizesRequests;
2020
2121 /**
22- * @param Request $request, this function works with fields: checkout_at, expected_checkin, note
23- * @param PredefinedKit $kit kit for checkout
24- * @param User $user checkout target
22+ * @param Request $request, this function works with fields: checkout_at, expected_checkin, note
23+ * @param PredefinedKit $kit kit for checkout
24+ * @param User $user checkout target
2525 * @return array Empty array if all ok, else [string_error1, string_error2...]
2626 */
2727 public function checkout (Request $ request , PredefinedKit $ kit , User $ user )
@@ -93,7 +93,7 @@ protected function getAssetsToAdd($kit, $user, &$errors)
9393 }
9494 }
9595 if ($ quantity > 0 ) {
96- $ errors [] = trans ('admin/kits/general.none_models ' , ['model ' => $ model ->name , 'qty ' => $ model ->pivot ->quantity ]);
96+ $ errors [] = trans ('admin/kits/general.none_models ' , ['model ' => $ model ->name , 'qty ' => $ model ->pivot ->quantity ]);
9797 }
9898 }
9999
@@ -107,9 +107,10 @@ protected function getLicenseSeatsToAdd($kit, &$errors)
107107 ->with ('freeSeats ' )
108108 ->get ();
109109 foreach ($ licenses as $ license ) {
110+ $ this ->authorize ('checkout ' , $ license );
110111 $ quantity = $ license ->pivot ->quantity ;
111112 if ($ quantity > count ($ license ->freeSeats )) {
112- $ errors [] = trans ('admin/kits/general.none_licenses ' , ['license ' => $ license ->name , 'qty ' => $ license ->pivot ->quantity ]);
113+ $ errors [] = trans ('admin/kits/general.none_licenses ' , ['license ' => $ license ->name , 'qty ' => $ license ->pivot ->quantity ]);
113114 }
114115 for ($ i = 0 ; $ i < $ quantity ; $ i ++) {
115116 $ seats_to_add [] = $ license ->freeSeats [$ i ];
@@ -123,8 +124,9 @@ protected function getConsumablesToAdd($kit, &$errors)
123124 {
124125 $ consumables = $ kit ->consumables ()->with ('users ' )->get ();
125126 foreach ($ consumables as $ consumable ) {
127+ $ this ->authorize ('checkout ' , $ consumable );
126128 if ($ consumable ->numRemaining () < $ consumable ->pivot ->quantity ) {
127- $ errors [] = trans ('admin/kits/general.none_consumables ' , ['consumable ' => $ consumable ->name , 'qty ' => $ consumable ->pivot ->quantity ]);
129+ $ errors [] = trans ('admin/kits/general.none_consumables ' , ['consumable ' => $ consumable ->name , 'qty ' => $ consumable ->pivot ->quantity ]);
128130 }
129131 }
130132
@@ -135,8 +137,9 @@ protected function getAccessoriesToAdd($kit, &$errors)
135137 {
136138 $ accessories = $ kit ->accessories ()->with ('users ' )->get ();
137139 foreach ($ accessories as $ accessory ) {
140+ $ this ->authorize ('checkout ' , $ accessory );
138141 if ($ accessory ->numRemaining () < $ accessory ->pivot ->quantity ) {
139- $ errors [] = trans ('admin/kits/general.none_accessory ' , ['accessory ' => $ accessory ->name , 'qty ' => $ accessory ->pivot ->quantity ]);
142+ $ errors [] = trans ('admin/kits/general.none_accessory ' , ['accessory ' => $ accessory ->name , 'qty ' => $ accessory ->pivot ->quantity ]);
140143 }
141144 }
142145
@@ -175,7 +178,7 @@ function () use ($user, $admin, $checkout_at, $expected_checkin, $errors, $asset
175178 ]);
176179 event (new CheckoutableCheckedOut ($ consumable , $ user , $ admin , $ note ));
177180 }
178- //accessories
181+ // accessories
179182 foreach ($ accessories_to_add as $ accessory ) {
180183 $ accessory ->assigned_to = $ user ->id ;
181184 $ accessory ->users ()->attach ($ accessory ->id , [
0 commit comments