8
8
use Illuminate \Support \Facades \Auth ;
9
9
use Illuminate \Validation \ValidationException ;
10
10
use Matteoc99 \LaravelPreference \Contracts \PreferenceGroup ;
11
+ use Matteoc99 \LaravelPreference \Contracts \PreferencePolicy ;
11
12
use Matteoc99 \LaravelPreference \Enums \PolicyAction ;
12
13
use Matteoc99 \LaravelPreference \Exceptions \PreferenceNotFoundException ;
13
14
use Matteoc99 \LaravelPreference \Models \Preference ;
@@ -40,9 +41,8 @@ private function userPreferences(): MorphMany
40
41
public function getPreference (PreferenceGroup |Preference $ preference , mixed $ default = null ): mixed
41
42
{
42
43
43
- $ this ->authorize (PolicyAction::GET );
44
44
45
- $ preference = $ this ->validateAndRetrievePreference ($ preference );
45
+ $ preference = $ this ->validateAndRetrievePreference ($ preference, PolicyAction:: GET );
46
46
47
47
$ userPreference = $ this ->userPreferences ()->where ('preference_id ' , $ preference ->id )->first ();
48
48
@@ -65,9 +65,8 @@ public function getPreference(PreferenceGroup|Preference $preference, mixed $def
65
65
*/
66
66
public function setPreference (PreferenceGroup |Preference $ preference , mixed $ value ): void
67
67
{
68
- $ this ->authorize (PolicyAction::UPDATE );
69
68
70
- $ preference = $ this ->validateAndRetrievePreference ($ preference );
69
+ $ preference = $ this ->validateAndRetrievePreference ($ preference, PolicyAction:: UPDATE );
71
70
72
71
ValidationHelper::validateValue (
73
72
$ value ,
@@ -90,9 +89,8 @@ public function setPreference(PreferenceGroup|Preference $preference, mixed $val
90
89
*/
91
90
public function removePreference (PreferenceGroup |Preference $ preference ): int
92
91
{
93
- $ this ->authorize (PolicyAction::DELETE );
94
92
95
- $ preference = $ this ->validateAndRetrievePreference ($ preference );
93
+ $ preference = $ this ->validateAndRetrievePreference ($ preference, PolicyAction:: DELETE );
96
94
97
95
98
96
return $ this ->userPreferences ()->where ('preference_id ' , $ preference ->id )->delete ();
@@ -123,13 +121,17 @@ public function getPreferences(string $group = null): Collection
123
121
* Validate existence of a preference and retrieve it.
124
122
*
125
123
* @param PreferenceGroup|Preference $preference Preference name.
124
+ * @param PolicyAction $action
126
125
*
127
126
* @return Preference
128
- * @throws PreferenceNotFoundException If preference not found.
127
+ * @throws AuthorizationException
128
+ * @throws PreferenceNotFoundException
129
129
*/
130
- private function validateAndRetrievePreference (PreferenceGroup |Preference $ preference ): Preference
130
+ private function validateAndRetrievePreference (PreferenceGroup |Preference $ preference, PolicyAction $ action ): Preference
131
131
{
132
132
133
+ $ this ->authorize ($ action );
134
+
133
135
if (!$ preference instanceof Preference) {
134
136
135
137
SerializeHelper::conformNameAndGroup ($ preference , $ group );
@@ -141,7 +143,27 @@ private function validateAndRetrievePreference(PreferenceGroup|Preference $prefe
141
143
throw new PreferenceNotFoundException ("Preference not found: $ preference in group $ group " );
142
144
}
143
145
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
+ }
145
167
146
168
return $ preference ;
147
169
}
0 commit comments