Skip to content

Commit 2c42f93

Browse files
committed
feat(data): support unauthenticated requests and improve documentation
- Update authenticatedUser to be nullable to support unauthenticated requests - Add comments to explain user ownership and authentication checks - Refactor user ID determination logic for repository calls
1 parent b46bfca commit 2c42f93

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

routes/api/v1/data/index.dart

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Future<Response> onRequest(RequestContext context) async {
2929
Future<Response> _handleGet(RequestContext context) async {
3030
final modelName = context.read<String>();
3131
final modelConfig = context.read<ModelConfig<dynamic>>();
32-
final authenticatedUser = context.read<User>();
32+
// Read authenticatedUser as nullable, as per configurable authentication.
33+
final authenticatedUser = context.read<User?>();
3334
final params = context.request.uri.queryParameters;
3435

3536
_logger
@@ -73,8 +74,12 @@ Future<Response> _handleGet(RequestContext context) async {
7374
)
7475
: null;
7576

76-
final userIdForRepoCall =
77-
(modelConfig.getOwnerId != null &&
77+
// Determine userId for repository call.
78+
// If the model is user-owned and the user is authenticated and not an admin,
79+
// then the operation should be scoped to the authenticated user's ID.
80+
// Otherwise, it's a global operation or an admin bypass.
81+
final userIdForRepoCall = (modelConfig.getOwnerId != null &&
82+
authenticatedUser != null &&
7883
!context.read<PermissionService>().isAdmin(authenticatedUser))
7984
? authenticatedUser.id
8085
: null;
@@ -101,7 +106,8 @@ Future<Response> _handleGet(RequestContext context) async {
101106
Future<Response> _handlePost(RequestContext context) async {
102107
final modelName = context.read<String>();
103108
final modelConfig = context.read<ModelConfig<dynamic>>();
104-
final authenticatedUser = context.read<User>();
109+
// Read authenticatedUser as nullable, as per configurable authentication.
110+
final authenticatedUser = context.read<User?>();
105111

106112
_logger.info('Handling POST request for model "$modelName".');
107113

@@ -124,8 +130,12 @@ Future<Response> _handlePost(RequestContext context) async {
124130
);
125131
}
126132

127-
final userIdForRepoCall =
128-
(modelConfig.getOwnerId != null &&
133+
// Determine userId for repository call.
134+
// If the model is user-owned and the user is authenticated and not an admin,
135+
// then the operation should be scoped to the authenticated user's ID.
136+
// Otherwise, it's a global operation or an admin bypass.
137+
final userIdForRepoCall = (modelConfig.getOwnerId != null &&
138+
authenticatedUser != null &&
129139
!context.read<PermissionService>().isAdmin(authenticatedUser))
130140
? authenticatedUser.id
131141
: null;

0 commit comments

Comments
 (0)