Skip to content

Commit 85f9ca5

Browse files
authored
Bugfix/sponsor role binding (#288)
* Fix sponsor role binding addition * Fix sponsor role binding addition * Fix Sponsor account creation * comments, docs * make JWT optional
1 parent 5b95230 commit 85f9ca5

File tree

7 files changed

+32
-5
lines changed

7 files changed

+32
-5
lines changed

docs/api/api_data.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ define({
7171
"field": "phoneNumber",
7272
"description": "<p>the user's phone number, represented as a string.</p>"
7373
}
74-
]
74+
],
75+
"header": [{
76+
"group": "header",
77+
"type": "JWT",
78+
"optional": true,
79+
"field": "token",
80+
"description": "<p>the user's invite token.</p>"
81+
}]
7582
},
7683
"examples": [{
7784
"title": "Request-Example:",

docs/api/api_data.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@
7070
"field": "phoneNumber",
7171
"description": "<p>the user's phone number, represented as a string.</p>"
7272
}
73-
]
73+
],
74+
"header": [{
75+
"group": "header",
76+
"type": "JWT",
77+
"optional": true,
78+
"field": "token",
79+
"description": "<p>the user's invite token.</p>"
80+
}]
7481
},
7582
"examples": [{
7683
"title": "Request-Example:",

middlewares/auth.middleware.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,17 @@ function createRoleBindings(roleName = undefined) {
423423
});
424424
}
425425

426+
/**
427+
* Adds a rolebinding between the user and the role with the name stored in 'accountType'.
428+
* @param {{user: {id: ObjectId, accountType: string}}} req
429+
* @param {*} res
430+
* @param {(err?) => void} next
431+
*/
432+
async function addAccountTypeRoleBinding(req, res, next) {
433+
await Services.RoleBinding.createRoleBindingByRoleName(req.user.id, req.user.accountType);
434+
return next();
435+
}
436+
426437
/**
427438
* Middleware to retrieve all the roles in the database
428439
* @param {*} req
@@ -450,6 +461,7 @@ module.exports = {
450461
getAccountTypeFromConfirmationToken: Middleware.Util.asyncMiddleware(getAccountTypeFromConfirmationToken),
451462
validateConfirmationTokenWithoutAccount: Middleware.Util.asyncMiddleware(validateConfirmationTokenWithoutAccount),
452463
createRoleBindings: createRoleBindings,
464+
addAccountTypeRoleBinding: Middleware.Util.asyncMiddleware(addAccountTypeRoleBinding),
453465
addCreationRoleBindings: Middleware.Util.asyncMiddleware(addCreationRoleBindings),
454466
resendConfirmAccountEmail: Middleware.Util.asyncMiddleware(resendConfirmAccountEmail),
455467
retrieveRoleBindings: Middleware.Util.asyncMiddleware(retrieveRoleBindings),

middlewares/validators/account.validator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
VALIDATOR.alphaArrayValidator("body", "dietaryRestrictions", false),
1212
VALIDATOR.enumValidator("body", "shirtSize", Constants.SHIRT_SIZES, false),
1313
VALIDATOR.passwordValidator("body", "password", false),
14-
VALIDATOR.jwtValidator("param", "token", process.env.JWT_CONFIRM_ACC_SECRET, true),
14+
VALIDATOR.jwtValidator("header", "token", process.env.JWT_CONFIRM_ACC_SECRET, true),
1515
VALIDATOR.dateValidator("body", "birthDate", false),
1616
VALIDATOR.phoneNumberValidator("body", "phoneNumber", false)
1717
],

models/account.model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const AccountSchema = new mongoose.Schema({
4242
},
4343
accountType: {
4444
type: String,
45-
enum: Constants.USER_TYPES,
45+
enum: Constants.EXTENDED_USER_TYPES,
4646
default: Constants.HACKER
4747
},
4848
birthDate: {

routes/api/account.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = {
7575
* @apiParam (body) {String} password The password of the account.
7676
* @apiParam (body) {String} birthDate a Date parsable string.
7777
* @apiParam (body) {Number} phoneNumber the user's phone number, represented as a string.
78+
* @apiParam (header) {JWT} [token] the user's invite token.
7879
*
7980
* @apiParamExample {json} Request-Example:
8081
* {

routes/api/sponsor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = {
9898

9999
Middleware.Sponsor.parseSponsor,
100100

101-
Middleware.Auth.createRoleBindings(CONSTANTS.SPONSOR),
101+
Middleware.Auth.addAccountTypeRoleBinding,
102102

103103
Middleware.Sponsor.createSponsor,
104104
Controllers.Sponsor.createdSponsor

0 commit comments

Comments
 (0)