Skip to content

Commit

Permalink
prepare for live
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoerlitz committed Mar 17, 2024
1 parent 08cd6cf commit 81643be
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ if (Config.APP_DEBUG) {
process.on("uncaughtException", (err, origin) => handleUncaughtException(err, origin));
}

console.log(Config.EMAIL_CONFIG.DEBUG_EMAIL);

initializeApplication()
.then(() => {
// Basic server configuration
Expand Down
3 changes: 3 additions & 0 deletions src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ router.use(
"/role",
routerGroup((r: Router) => {
r.get("/", RoleAdministrationController.getAll);
r.post("/", RoleAdministrationController.create);
r.post("/user", RoleAdministrationController.addUser);
r.delete("/user", RoleAdministrationController.removeUser);
r.get("/:role_id", RoleAdministrationController.getByID);
r.patch("/:role_id", RoleAdministrationController.update);

Expand Down
75 changes: 74 additions & 1 deletion src/controllers/permission/RoleAdminController.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Request, Response } from "express";
import { NextFunction, Request, Response } from "express";
import { Role } from "../../models/Role";
import { RoleHasPermissions } from "../../models/through/RoleHasPermissions";
import PermissionHelper from "../../utility/helper/PermissionHelper";
import { User } from "../../models/User";
import Validator, { ValidationTypeEnum } from "../../utility/Validator";
import { HttpStatusCode } from "axios";
import { RoleBelongsToUsers } from "../../models/through/RoleBelongsToUsers";

/**
* Gets all roles
Expand All @@ -15,6 +17,74 @@ async function getAll(request: Request, response: Response) {
response.send(roles);
}

async function create(request: Request, response: Response, next: NextFunction) {
try {
const user: User = response.locals.user;
const body = request.body as { name: string };
PermissionHelper.checkUserHasPermission(user, "tech.permissions.role.edit");

Validator.validate(body, {
name: [ValidationTypeEnum.NON_NULL],
});

const role = await Role.create({
name: body.name,
});

response.status(HttpStatusCode.Created).send(role);
} catch (e) {
next(e);
}
}

async function addUser(request: Request, response: Response, next: NextFunction) {
try {
const user: User = response.locals.user;
const body = request.body as { role_id: string; user_id: string };
PermissionHelper.checkUserHasPermission(user, "tech.permissions.role.edit");

Validator.validate(body, {
role_id: [ValidationTypeEnum.NON_NULL, ValidationTypeEnum.NUMBER],
user_id: [ValidationTypeEnum.NON_NULL, ValidationTypeEnum.NUMBER],
});

await RoleBelongsToUsers.create({
role_id: Number(body.role_id),
user_id: Number(body.user_id),
});

const addedUser = await User.findByPk(body.user_id);

response.status(HttpStatusCode.Created).send(addedUser);
} catch (e) {
next(e);
}
}

async function removeUser(request: Request, response: Response, next: NextFunction) {
try {
const user: User = response.locals.user;
const body = request.body as { role_id: string; user_id: string };
PermissionHelper.checkUserHasPermission(user, "tech.permissions.role.edit");

Validator.validate(body, {
role_id: [ValidationTypeEnum.NON_NULL, ValidationTypeEnum.NUMBER],
user_id: [ValidationTypeEnum.NON_NULL, ValidationTypeEnum.NUMBER],
});

await RoleBelongsToUsers.destroy({
where: {
role_id: Number(body.role_id),
user_id: Number(body.user_id),
},
});

response.sendStatus(HttpStatusCode.Ok);
} catch (e) {
next(e);
}
}

/**
* Gets a role (and its information) from the role's ID
* @param request
Expand Down Expand Up @@ -154,6 +224,9 @@ async function addPermission(request: Request, response: Response) {

export default {
getAll,
create,
addUser,
removeUser,
getByID,
update,
removePermission,
Expand Down
1 change: 0 additions & 1 deletion src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export const Config = {

// Read from .env
APP_DEBUG: process.env.APP_DEBUG?.toLowerCase() == "true",
DEBUG_EMAIL: process.env.DEBUG_EMAIL,
APP_LOG_SQL: process.env.APP_LOG_SQL?.toLowerCase() == "true",
APP_CORS_ALLOW: process.env.APP_CORS_ALLOW,

Expand Down
2 changes: 1 addition & 1 deletion src/exceptions/VatsimConnectException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class VatsimConnectException extends Error {
return;

case ConnectLibraryErrors.ERR_SUSPENDED:
response.status(403).send({ code: "ERR_SUSP", message: "Account suspended", hint: "Nevermind, bin gebannt" });
response.status(500).send({ code: "ERR_SUSP", message: "Account suspended" });
return;

case ConnectLibraryErrors.ERR_INV_SCOPES:
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/EmailLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function sendMail(options: SendMailOptions, nonPooled = true) {
const template = handlebars.compile(file);

let message = {
to: Config.APP_DEBUG ? Config.EMAIL_CONFIG.DEBUG_EMAIL : options.recipient,
to: Config.EMAIL_CONFIG.DEBUG_EMAIL ? Config.EMAIL_CONFIG.DEBUG_EMAIL : options.recipient,
from: Config.EMAIL_CONFIG.SMTP_USERNAME,
subject: options.subject,
html: template({ ...options.replacements, date_now: dayjs.utc().format(Config.DATETIME_FORMAT) }),
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/vatsim/ConnectLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class VatsimConnectLibrary {
*/
private async _checkIsUserAllowed() {
if (this.m_userData == undefined) return null;
const allowed_cids = [10000001, 10000002, 10000003, 10000010];
const allowed_cids = [1373921, 1450775, 1331358, 1357290, 1439797, 1583954, 1438611, 1432304, 1439600, 1463320, 1238939];

if (!allowed_cids.includes(Number(this.m_userData.data.cid))) {
throw new VatsimConnectException(ConnectLibraryErrors.ERR_SUSPENDED);
Expand Down

0 comments on commit 81643be

Please sign in to comment.