88use Illuminate \Support \Facades \Auth ;
99use Illuminate \Validation \ValidationException ;
1010use Matteoc99 \LaravelPreference \Contracts \PreferenceGroup ;
11+ use Matteoc99 \LaravelPreference \Contracts \PreferencePolicy ;
1112use Matteoc99 \LaravelPreference \Enums \PolicyAction ;
1213use Matteoc99 \LaravelPreference \Exceptions \PreferenceNotFoundException ;
1314use Matteoc99 \LaravelPreference \Models \Preference ;
@@ -40,9 +41,8 @@ private function userPreferences(): MorphMany
4041 public function getPreference (PreferenceGroup |Preference $ preference , mixed $ default = null ): mixed
4142 {
4243
43- $ this ->authorize (PolicyAction::GET );
4444
45- $ preference = $ this ->validateAndRetrievePreference ($ preference );
45+ $ preference = $ this ->validateAndRetrievePreference ($ preference, PolicyAction:: GET );
4646
4747 $ userPreference = $ this ->userPreferences ()->where ('preference_id ' , $ preference ->id )->first ();
4848
@@ -65,9 +65,8 @@ public function getPreference(PreferenceGroup|Preference $preference, mixed $def
6565 */
6666 public function setPreference (PreferenceGroup |Preference $ preference , mixed $ value ): void
6767 {
68- $ this ->authorize (PolicyAction::UPDATE );
6968
70- $ preference = $ this ->validateAndRetrievePreference ($ preference );
69+ $ preference = $ this ->validateAndRetrievePreference ($ preference, PolicyAction:: UPDATE );
7170
7271 ValidationHelper::validateValue (
7372 $ value ,
@@ -90,9 +89,8 @@ public function setPreference(PreferenceGroup|Preference $preference, mixed $val
9089 */
9190 public function removePreference (PreferenceGroup |Preference $ preference ): int
9291 {
93- $ this ->authorize (PolicyAction::DELETE );
9492
95- $ preference = $ this ->validateAndRetrievePreference ($ preference );
93+ $ preference = $ this ->validateAndRetrievePreference ($ preference, PolicyAction:: DELETE );
9694
9795
9896 return $ this ->userPreferences ()->where ('preference_id ' , $ preference ->id )->delete ();
@@ -123,13 +121,17 @@ public function getPreferences(string $group = null): Collection
123121 * Validate existence of a preference and retrieve it.
124122 *
125123 * @param PreferenceGroup|Preference $preference Preference name.
124+ * @param PolicyAction $action
126125 *
127126 * @return Preference
128- * @throws PreferenceNotFoundException If preference not found.
127+ * @throws AuthorizationException
128+ * @throws PreferenceNotFoundException
129129 */
130- private function validateAndRetrievePreference (PreferenceGroup |Preference $ preference ): Preference
130+ private function validateAndRetrievePreference (PreferenceGroup |Preference $ preference, PolicyAction $ action ): Preference
131131 {
132132
133+ $ this ->authorize ($ action );
134+
133135 if (!$ preference instanceof Preference) {
134136
135137 SerializeHelper::conformNameAndGroup ($ preference , $ group );
@@ -141,7 +143,27 @@ private function validateAndRetrievePreference(PreferenceGroup|Preference $prefe
141143 throw new PreferenceNotFoundException ("Preference not found: $ preference in group $ group " );
142144 }
143145
144- //Todo Gate
146+ if (!empty ($ preference ->policy )) {
147+ $ policy = $ preference ->policy ;
148+ $ authorized = false ;
149+
150+ $ enum = SerializeHelper::reversePreferenceToEnum ($ preference );
151+
152+ if ($ policy instanceof PreferencePolicy) {
153+ $ authorized = match ($ action ) {
154+ PolicyAction::INDEX => $ policy ->index (Auth::user (), $ this , $ enum ),
155+ PolicyAction::GET => $ policy ->get (Auth::user (), $ this , $ enum ),
156+ PolicyAction::UPDATE => $ policy ->update (Auth::user (), $ this , $ enum ),
157+ PolicyAction::DELETE => $ policy ->delete (Auth::user (), $ this , $ enum ),
158+ default => throw new AuthorizationException ("Unknown Policy: " . $ action ->name ),
159+ };
160+ }
161+
162+ if (!$ authorized ) {
163+ throw new AuthorizationException ("The user is not authorized to perform the action: " . $ action ->name );
164+ }
165+
166+ }
145167
146168 return $ preference ;
147169 }
0 commit comments