Skip to content

Commit 331482f

Browse files
committed
feat(auth): restrict generic routes for auth models
- Added "unsupported" permission type - Updated auth model permissions - Added check in middleware
1 parent 5cdf64a commit 331482f

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lib/src/middlewares/authorization_middleware.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ Middleware authorizationMiddleware() {
8484
'You do not have permission to perform this action.',
8585
);
8686
}
87+
case RequiredPermissionType.unsupported:
88+
// This action is explicitly marked as not supported via this generic route.
89+
// Return Method Not Allowed.
90+
print(
91+
'[AuthorizationMiddleware] Action for model "$modelName", method "$method" '
92+
'is marked as unsupported via generic route.',
93+
);
94+
// Throw ForbiddenException to be caught by the errorHandler
95+
throw ForbiddenException(
96+
'Method "$method" is not supported for model "$modelName" '
97+
'via this generic data endpoint.',
98+
);
8799
}
88100

89101
// If all checks pass, proceed to the next handler in the chain.

lib/src/registry/model_registry.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ enum RequiredPermissionType {
1818

1919
/// Requires the user to have a specific permission string.
2020
specificPermission,
21+
22+
/// This action is not supported via this generic route.
23+
/// It is typically handled by a dedicated service or route.
24+
unsupported,
2125
}
2226

2327
/// Configuration for the authorization requirements of a single HTTP method
@@ -196,17 +200,15 @@ final modelRegistry = <String, ModelConfig<dynamic>>{
196200
requiresOwnershipCheck: true, // Must be the owner
197201
),
198202
postPermission: const ModelActionPermission(
199-
type: RequiredPermissionType.none, // User creation handled by auth routes
203+
type: RequiredPermissionType.unsupported, // User creation handled by auth routes
200204
),
201205
putPermission: const ModelActionPermission(
202206
type: RequiredPermissionType.specificPermission,
203207
permission: Permissions.userUpdateOwned, // User can update their own
204208
requiresOwnershipCheck: true, // Must be the owner
205209
),
206210
deletePermission: const ModelActionPermission(
207-
type: RequiredPermissionType.specificPermission,
208-
permission: Permissions.userDeleteOwned, // User can delete their own
209-
requiresOwnershipCheck: true, // Must be the owner
211+
type: RequiredPermissionType.unsupported, // User can delete their own
210212
),
211213
),
212214
// Configuration for UserAppSettings (user-owned)
@@ -220,7 +222,7 @@ final modelRegistry = <String, ModelConfig<dynamic>>{
220222
requiresOwnershipCheck: true,
221223
),
222224
postPermission: const ModelActionPermission(
223-
type: RequiredPermissionType.none,
225+
type: RequiredPermissionType.unsupported,
224226
// Creation of UserAppSettings is handled by the authentication service
225227
// during user creation, not via a direct POST to /api/v1/data.
226228
),
@@ -230,7 +232,7 @@ final modelRegistry = <String, ModelConfig<dynamic>>{
230232
requiresOwnershipCheck: true,
231233
),
232234
deletePermission: const ModelActionPermission(
233-
type: RequiredPermissionType.none,
235+
type: RequiredPermissionType.unsupported,
234236
// Deletion of UserAppSettings is handled by the authentication service
235237
// during account deletion, not via a direct DELETE to /api/v1/data.
236238
),
@@ -246,7 +248,7 @@ final modelRegistry = <String, ModelConfig<dynamic>>{
246248
requiresOwnershipCheck: true,
247249
),
248250
postPermission: const ModelActionPermission(
249-
type: RequiredPermissionType.none,
251+
type: RequiredPermissionType.unsupported,
250252
// Creation of UserContentPreferences is handled by the authentication
251253
// service during user creation, not via a direct POST to /api/v1/data.
252254
),
@@ -256,7 +258,7 @@ final modelRegistry = <String, ModelConfig<dynamic>>{
256258
requiresOwnershipCheck: true,
257259
),
258260
deletePermission: const ModelActionPermission(
259-
type: RequiredPermissionType.none,
261+
type: RequiredPermissionType.unsupported,
260262
// Deletion of UserContentPreferences is handled by the authentication
261263
// service during account deletion, not via a direct DELETE to /api/v1/data.
262264
),

0 commit comments

Comments
 (0)