Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Support for username in params
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jul 22, 2019
1 parent d6dbfa1 commit 9b502a0
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 73 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.0.97",
"version": "1.0.98",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -135,5 +135,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.0.97"
"staart-version": "1.0.98"
}
188 changes: 148 additions & 40 deletions src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import {
verify2FAForUser,
getBackupCodesForUser,
regenerateBackupCodesForUser,
updatePasswordForUser
updatePasswordForUser,
deleteAccessTokenForUser,
updateAccessTokenForUser,
getUserAccessTokenForUser,
createAccessTokenForUser,
getUserAccessTokensForUser
} from "../rest/user";
import {
Get,
Expand All @@ -36,7 +41,7 @@ import {
} from "../rest/email";
import { CREATED } from "http-status-codes";
import asyncHandler from "express-async-handler";
import { joiValidate } from "../helpers/utils";
import { joiValidate, userUsernameToId } from "../helpers/utils";
import Joi from "@hapi/joi";

@Controller("users")
Expand All @@ -45,8 +50,7 @@ import Joi from "@hapi/joi";
export class UserController {
@Get(":id")
async get(req: Request, res: Response) {
let id = req.body.id || req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand Down Expand Up @@ -80,8 +84,7 @@ export class UserController {
)
)
async patch(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -92,8 +95,7 @@ export class UserController {

@Delete(":id")
async delete(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -116,8 +118,7 @@ export class UserController {
)
)
async updatePassword(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const oldPassword = req.body.oldPassword;
const newPassword = req.body.newPassword;
joiValidate(
Expand All @@ -138,8 +139,7 @@ export class UserController {

@Get(":id/events")
async getRecentEvents(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -149,8 +149,7 @@ export class UserController {

@Get(":id/memberships")
async getMemberships(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -160,8 +159,7 @@ export class UserController {

@Get(":id/data")
async getUserData(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -171,8 +169,7 @@ export class UserController {

@Get(":id/emails")
async getEmails(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -182,8 +179,7 @@ export class UserController {

@Put(":id/emails")
async putEmails(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const email = req.body.email;
joiValidate(
{
Expand All @@ -200,8 +196,7 @@ export class UserController {

@Get(":id/emails/:emailId")
async getEmail(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const emailId = req.params.emailId;
joiValidate(
{
Expand All @@ -215,8 +210,7 @@ export class UserController {

@Post(":id/emails/:emailId/resend")
async postResend(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const emailId = req.params.emailId;
joiValidate(
{
Expand All @@ -231,8 +225,7 @@ export class UserController {

@Delete(":id/emails/:emailId")
async deleteEmail(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const emailId = req.params.emailId;
joiValidate(
{
Expand All @@ -252,8 +245,7 @@ export class UserController {

@Get(":id/notifications")
async getUserNotifications(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -263,8 +255,7 @@ export class UserController {

@Patch(":id/notifications/:notificationId")
async updateUserNotification(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const notificationId = req.params.notificationId;
joiValidate(
{
Expand All @@ -285,8 +276,7 @@ export class UserController {

@Get(":id/2fa/enable")
async getEnable2FA(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -296,8 +286,7 @@ export class UserController {

@Post(":id/2fa/verify")
async postVerify2FA(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const code = req.body.code;
joiValidate(
{
Expand All @@ -313,8 +302,7 @@ export class UserController {

@Delete(":id/2fa")
async delete2FA(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -324,8 +312,7 @@ export class UserController {

@Get(":id/backup-codes")
async getBackupCodes(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
Expand All @@ -335,12 +322,133 @@ export class UserController {

@Get(":id/backup-codes/regenerate")
async getRegenerateBackupCodes(req: Request, res: Response) {
let id = req.params.id;
if (id === "me") id = res.locals.token.id;
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
);
res.json(await regenerateBackupCodesForUser(res.locals.token.id, id));
}

@Get(":id/access-tokens")
async getUserAccessTokens(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
);
const accessTokenParams = { ...req.query };
joiValidate(
{
start: Joi.string(),
itemsPerPage: Joi.number()
},
accessTokenParams
);
res.json(
await getUserAccessTokensForUser(
res.locals.token.id,
id,
accessTokenParams
)
);
}

@Put(":id/access-tokens")
@Middleware(
validator(
{
scopes: Joi.string(),
name: Joi.string(),
description: Joi.string()
},
"body"
)
)
async putUserAccessTokens(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
joiValidate(
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
);
res
.status(CREATED)
.json(
await createAccessTokenForUser(
res.locals.token.id,
id,
req.body,
res.locals
)
);
}

@Get(":id/access-tokens/:accessTokenId")
async getUserAccessToken(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const accessTokenId = req.params.accessTokenId;
joiValidate(
{
id: [Joi.string().required(), Joi.number().required()],
accessTokenId: Joi.number().required()
},
{ id, accessTokenId }
);
res.json(
await getUserAccessTokenForUser(res.locals.token.id, id, accessTokenId)
);
}

@Patch(":id/access-tokens/:accessTokenId")
@Middleware(
validator(
{
scopes: Joi.string().allow(""),
name: Joi.string().allow(""),
description: Joi.string().allow("")
},
"body"
)
)
async patchUserAccessToken(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const accessTokenId = req.params.accessTokenId;
joiValidate(
{
id: [Joi.string().required(), Joi.number().required()],
accessTokenId: Joi.number().required()
},
{ id, accessTokenId }
);
res.json(
await updateAccessTokenForUser(
res.locals.token.id,
id,
accessTokenId,
req.body,
res.locals
)
);
}

@Delete(":id/access-tokens/:accessTokenId")
async deleteUserAccessToken(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const accessTokenId = req.params.accessTokenId;
joiValidate(
{
id: [Joi.string().required(), Joi.number().required()],
accessTokenId: Joi.number().required()
},
{ id, accessTokenId }
);
res.json(
await deleteAccessTokenForUser(
res.locals.token.id,
id,
accessTokenId,
res.locals
)
);
}
}
Loading

0 comments on commit 9b502a0

Please sign in to comment.