Skip to content

Commit e80a969

Browse files
chore: minor type, name, and structural edits
1 parent 79570d0 commit e80a969

File tree

4 files changed

+20
-40
lines changed

4 files changed

+20
-40
lines changed

server/src/middleware/auth.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextFunction, Response } from 'express';
33
import User from '@/models/users';
44
import { AuthenticatedRequest } from '@/types/auth';
55
import { verifyAuthToken } from '@/utils/authTokenHandler';
6-
import authorizeUser from '@/utils/roleBasedAccess';
6+
import defineAbilitiesForUser from '@/utils/roleBasedAccess';
77

88
// Middleware for verifying token signature and storing token info in response
99
// If this call passes to the next handler, it means the user is atleast a volunteer
@@ -33,8 +33,6 @@ export async function auth(
3333

3434
// Add the decoded token to the request object
3535
req.user = {
36-
// REVIEW: Why do we need "id" here? Where are we using it?
37-
id: decodedAuthToken.id ?? decodedAuthToken.employeeId,
3836
employeeId: decodedAuthToken.employeeId,
3937
role: decodedAuthToken.role,
4038
firstName: decodedAuthToken.firstName
@@ -65,7 +63,7 @@ export async function auth(
6563
}
6664

6765
// Add role authorization to the request
68-
req.authorization = authorizeUser(req, user.id, user.permissions);
66+
req.authorization = defineAbilitiesForUser(req, user.id, user.permissions);
6967

7068
next();
7169
} catch (err: any) {

server/src/models/users.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
import mongoose, { Model, Schema } from 'mongoose';
22

3-
import { IPermission, IUser } from '@/types/models';
4-
import {
5-
ACTION_ENUM,
6-
RESOURCE_ENUM,
7-
SCOPE_ENUM
8-
} from '@/utils/roleBasedAccess';
9-
10-
// Permission schema definition
11-
// This schema defines the structure of the permission data assigned to a user for a single operation.
12-
// It includes fields for action, resource, and scope.
13-
// These permissions are used in addition to the default permissions for each role
14-
// REVIEW: Why is this Schema? Maybe an interface would work just fine.
15-
// REVIEW: Update property names to match CASL terminology? Action, subject, field, conditions?
16-
const permissionSchema = new Schema<IPermission>({
17-
action: { type: String, enum: ACTION_ENUM, required: true },
18-
resource: { type: String, enum: RESOURCE_ENUM, required: true },
19-
scope: { type: String, enum: SCOPE_ENUM, required: true }
20-
});
3+
import { IUser } from '@/types/models';
4+
import { ACTION_ENUM, SUBJECT_ENUM, CONDITION_ENUM } from '@/utils/roleDefinitions';
215

226
// User schema definition
237
// This schema defines the structure of the user data in the MongoDB database.
@@ -46,7 +30,11 @@ const userSchema = new Schema<IUser>(
4630
default: 'Pending'
4731
},
4832
permissions: {
49-
type: [permissionSchema],
33+
type: [{
34+
action: { type: String, enum: ACTION_ENUM, required: true },
35+
subject: { type: String, enum: SUBJECT_ENUM, required: true },
36+
condition: { type: String, enum: CONDITION_ENUM, required: true }
37+
}],
5038
default: []
5139
}
5240
},

server/src/routes/auth.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
SignupRequest
1313
} from '@/types/auth';
1414
import { generateAuthToken } from '@/utils/authTokenHandler';
15-
import { ACTIONS } from '@/utils/roleBasedAccess';
15+
import { ACTIONS, SUBJECTS } from '@/utils/roleDefinitions';
1616

1717
const router = express.Router();
1818

@@ -193,7 +193,7 @@ router.get(
193193
}
194194
try {
195195
const users = await User.find(
196-
accessibleBy(req.authorization).ofType('User'),
196+
accessibleBy(req.authorization).ofType(SUBJECTS.USER),
197197
'firstName lastName role approvalStatus'
198198
);
199199
res.json(users);
@@ -208,15 +208,12 @@ router.get(
208208

209209
router.put(
210210
'/users/:id/approve',
211-
auth, // REVIEW: Why auth and not [auth]. What's the difference?
211+
[auth],
212212
async (req: AuthenticatedRequest, res: Response): Promise<void> => {
213-
// REVIEW: Should we replace 'User' with the const RESOURCES.USER?
214-
// REVIEW: What is 'approvalStatus' here?
215213
if (
216214
!req.authorization?.can(
217215
ACTIONS.CUSTOM.APPROVE,
218-
subject('User', { _id: req.params.id }),
219-
'approvalStatus'
216+
subject(SUBJECTS.USER, { _id: req.params.id }),
220217
)
221218
) {
222219
res.sendStatus(403);
@@ -252,7 +249,7 @@ router.post(
252249
'/preapprove',
253250
auth,
254251
async (req: AuthenticatedRequest, res: Response): Promise<void> => {
255-
if (!req.authorization?.can(ACTIONS.CUSTOM.PREAPPROVE, 'User')) {
252+
if (!req.authorization?.can(ACTIONS.CUSTOM.PREAPPROVE, SUBJECTS.USER)) {
256253
res.sendStatus(403);
257254
return;
258255
}
@@ -291,7 +288,7 @@ router.get(
291288
if (
292289
!req.authorization?.can(
293290
ACTIONS.CASL.READ,
294-
subject('User', { employeeId: req.params.employeeId })
291+
subject(SUBJECTS.USER, { employeeId: req.params.employeeId })
295292
)
296293
) {
297294
res.sendStatus(403);
@@ -325,7 +322,7 @@ router.put(
325322
if (
326323
!req.authorization?.can(
327324
ACTIONS.CASL.UPDATE,
328-
subject('User', { employeeId: req.params.employeeId })
325+
subject(SUBJECTS.USER, { employeeId: req.params.employeeId })
329326
)
330327
) {
331328
res.sendStatus(403);
@@ -366,7 +363,7 @@ router.get(
366363
if (
367364
!req.authorization?.can(
368365
ACTIONS.CASL.READ,
369-
subject('User', { _id: req.params.id })
366+
subject(SUBJECTS.USER, { _id: req.params.id })
370367
)
371368
) {
372369
res.sendStatus(403);
@@ -398,7 +395,7 @@ router.put(
398395
if (
399396
!req.authorization?.can(
400397
ACTIONS.CASL.UPDATE,
401-
subject('User', { _id: req.params.id })
398+
subject(SUBJECTS.USER, { _id: req.params.id })
402399
)
403400
) {
404401
res.sendStatus(403);

server/src/types/auth.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
import { MongoAbility } from '@casl/ability';
1+
import { Ability } from '@/utils/roleDefinitions';
22
import { Request } from 'express';
33
import { JwtPayload } from 'jsonwebtoken';
44

55
export interface AuthenticatedRequest extends Request {
66
user?: {
7-
id: string;
87
employeeId: string;
98
role: string;
109
firstName: string;
1110
};
12-
// REVIEW: Do we need <any> here?
13-
authorization?: MongoAbility<any>;
11+
authorization?: Ability;
1412
}
1513

1614
export interface JWTPayload extends JwtPayload {
17-
id: string;
1815
employeeId: string;
1916
role: string;
2017
}

0 commit comments

Comments
 (0)