From 21bd0a42d0b55ae695b82c59ab09fdf6cd26caa8 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 22 Nov 2020 13:49:08 +0100 Subject: [PATCH] Swaf upgrade: fix compilation errors --- src/App.ts | 74 ++++++++++++----------- src/SlugGenerator.ts | 6 +- src/controllers/AboutController.ts | 4 +- src/controllers/AuthController.ts | 6 +- src/controllers/AuthTokenController.ts | 19 +++--- src/controllers/FileController.ts | 55 ++++++++++------- src/controllers/LinkController.ts | 24 ++++---- src/controllers/MagicLinkActionType.ts | 6 +- src/controllers/MagicLinkController.ts | 20 +++--- src/controllers/URLRedirectController.ts | 21 ++++--- src/migrations/CreateAuthTokensTable.ts | 6 +- src/migrations/CreateFilesTable.ts | 6 +- src/migrations/CreateUrlRedirectsTable.ts | 6 +- src/migrations/IncreaseFilesSizeField.ts | 4 +- src/models/AuthToken.ts | 12 ++-- src/models/FileModel.ts | 6 +- src/models/URLRedirect.ts | 8 +-- 17 files changed, 153 insertions(+), 130 deletions(-) diff --git a/src/App.ts b/src/App.ts index 3a117cf..77a791d 100644 --- a/src/App.ts +++ b/src/App.ts @@ -1,45 +1,46 @@ -import Application from "wms-core/Application"; -import {Type} from "wms-core/Utils"; -import Migration from "wms-core/db/Migration"; -import CreateMigrationsTable from "wms-core/migrations/CreateMigrationsTable"; -import CreateLogsTable from "wms-core/migrations/CreateLogsTable"; -import ExpressAppComponent from "wms-core/components/ExpressAppComponent"; -import NunjucksComponent from "wms-core/components/NunjucksComponent"; -import MysqlComponent from "wms-core/components/MysqlComponent"; -import LogRequestsComponent from "wms-core/components/LogRequestsComponent"; -import RedisComponent from "wms-core/components/RedisComponent"; -import ServeStaticDirectoryComponent from "wms-core/components/ServeStaticDirectoryComponent"; -import MaintenanceComponent from "wms-core/components/MaintenanceComponent"; -import MailComponent from "wms-core/components/MailComponent"; -import SessionComponent from "wms-core/components/SessionComponent"; -import FormHelperComponent from "wms-core/components/FormHelperComponent"; -import CsrfProtectionComponent from "wms-core/components/CsrfProtectionComponent"; -import WebSocketServerComponent from "wms-core/components/WebSocketServerComponent"; +import Application from "swaf/Application"; +import Migration, {MigrationType} from "swaf/db/Migration"; +import CreateMigrationsTable from "swaf/migrations/CreateMigrationsTable"; +import ExpressAppComponent from "swaf/components/ExpressAppComponent"; +import NunjucksComponent from "swaf/components/NunjucksComponent"; +import MysqlComponent from "swaf/components/MysqlComponent"; +import LogRequestsComponent from "swaf/components/LogRequestsComponent"; +import RedisComponent from "swaf/components/RedisComponent"; +import ServeStaticDirectoryComponent from "swaf/components/ServeStaticDirectoryComponent"; +import MaintenanceComponent from "swaf/components/MaintenanceComponent"; +import MailComponent from "swaf/components/MailComponent"; +import SessionComponent from "swaf/components/SessionComponent"; +import FormHelperComponent from "swaf/components/FormHelperComponent"; +import CsrfProtectionComponent from "swaf/components/CsrfProtectionComponent"; +import WebSocketServerComponent from "swaf/components/WebSocketServerComponent"; import AboutController from "./controllers/AboutController"; -import AutoUpdateComponent from "wms-core/components/AutoUpdateComponent"; +import AutoUpdateComponent from "swaf/components/AutoUpdateComponent"; import AuthController from "./controllers/AuthController"; -import MagicLinkWebSocketListener from "wms-core/auth/magic_link/MagicLinkWebSocketListener"; +import MagicLinkWebSocketListener from "swaf/auth/magic_link/MagicLinkWebSocketListener"; import MagicLinkController from "./controllers/MagicLinkController"; -import MailController from "wms-core/auth/MailController"; +import MailController from "swaf/auth/MailController"; import FileController from "./controllers/FileController"; -import CreateUsersAndUserEmailsTable from "wms-core/auth/migrations/CreateUsersAndUserEmailsTable"; -import CreateMagicLinksTable from "wms-core/auth/migrations/CreateMagicLinksTable"; +import CreateUsersAndUserEmailsTable from "swaf/auth/migrations/CreateUsersAndUserEmailsTable"; +import CreateMagicLinksTable from "swaf/auth/migrations/CreateMagicLinksTable"; import CreateAuthTokensTable from "./migrations/CreateAuthTokensTable"; -import AuthComponent from "wms-core/auth/AuthComponent"; -import AuthGuard from "wms-core/auth/AuthGuard"; -import MagicLink from "wms-core/auth/models/MagicLink"; +import AuthComponent from "swaf/auth/AuthComponent"; +import AuthGuard from "swaf/auth/AuthGuard"; +import MagicLink from "swaf/auth/models/MagicLink"; import AuthToken from "./models/AuthToken"; import {MagicLinkActionType} from "./controllers/MagicLinkActionType"; import {Request} from "express"; import CreateFilesTable from "./migrations/CreateFilesTable"; import IncreaseFilesSizeField from "./migrations/IncreaseFilesSizeField"; -import AddApprovedFieldToUsersTable from "wms-core/auth/migrations/AddApprovedFieldToUsersTable"; +import AddApprovedFieldToUsersTable from "swaf/auth/migrations/AddApprovedFieldToUsersTable"; import CreateUrlRedirectsTable from "./migrations/CreateUrlRedirectsTable"; import AuthTokenController from "./controllers/AuthTokenController"; import URLRedirectController from "./controllers/URLRedirectController"; import LinkController from "./controllers/LinkController"; -import BackendController from "wms-core/helpers/BackendController"; -import RedirectBackComponent from "wms-core/components/RedirectBackComponent"; +import BackendController from "swaf/helpers/BackendController"; +import RedirectBackComponent from "swaf/components/RedirectBackComponent"; +import DummyMigration from "swaf/migrations/DummyMigration"; +import DropLegacyLogsTable from "swaf/migrations/DropLegacyLogsTable"; +import {Session} from "express-session"; import packageJson = require('../package.json'); export default class App extends Application { @@ -53,7 +54,7 @@ export default class App extends Application { protected getMigrations(): MigrationType[] { return [ CreateMigrationsTable, - CreateLogsTable, + DummyMigration, CreateUsersAndUserEmailsTable, CreateMagicLinksTable, CreateAuthTokensTable, @@ -61,6 +62,7 @@ export default class App extends Application { IncreaseFilesSizeField, AddApprovedFieldToUsersTable, CreateUrlRedirectsTable, + DropLegacyLogsTable, ]; } @@ -102,8 +104,11 @@ export default class App extends Application { this.use(redisComponent); this.use(new SessionComponent(redisComponent)); this.use(new AuthComponent(new class extends AuthGuard { - public async getProofForSession(session: Express.Session): Promise { - return await MagicLink.bySessionID(session.id, [MagicLinkActionType.LOGIN, MagicLinkActionType.REGISTER]); + public async getProofForSession(session: Session): Promise { + return await MagicLink.bySessionId( + session.id, + [MagicLinkActionType.LOGIN, MagicLinkActionType.REGISTER], + ); } public async getProofForRequest(req: Request): Promise { @@ -119,7 +124,7 @@ export default class App extends Application { return super.getProofForRequest(req); } - })); + }(this))); // Utils this.use(new FormHelperComponent()); @@ -132,8 +137,7 @@ export default class App extends Application { } private registerWebSocketListeners() { - this.magicLinkWebSocketListener = new MagicLinkWebSocketListener(); - this.use(this.magicLinkWebSocketListener); + this.use(new MagicLinkWebSocketListener()); } private registerControllers() { @@ -142,7 +146,7 @@ export default class App extends Application { // Priority this.use(new AuthController()); - this.use(new MagicLinkController(this.magicLinkWebSocketListener!)); + this.use(new MagicLinkController(this.as>(MagicLinkWebSocketListener))); this.use(new BackendController()); // Core functionality diff --git a/src/SlugGenerator.ts b/src/SlugGenerator.ts index b95997a..e0e92b1 100644 --- a/src/SlugGenerator.ts +++ b/src/SlugGenerator.ts @@ -1,7 +1,7 @@ -import {cryptoRandomDictionary} from "wms-core/Utils"; +import {cryptoRandomDictionary} from "swaf/Utils"; import config from "config"; import FileModel from "./models/FileModel"; -import {ServerError} from "wms-core/HttpError"; +import {ServerError} from "swaf/HttpError"; const SLUG_DICTIONARY = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; export default async function generateSlug(tries: number): Promise { @@ -14,4 +14,4 @@ export default async function generateSlug(tries: number): Promise { i++; } while (i < tries); throw new ServerError('Failed to generate slug; newly generated slug size should be increased by 1.'); -}; \ No newline at end of file +}; diff --git a/src/controllers/AboutController.ts b/src/controllers/AboutController.ts index a4b3e3e..281c3be 100644 --- a/src/controllers/AboutController.ts +++ b/src/controllers/AboutController.ts @@ -1,4 +1,4 @@ -import Controller from "wms-core/Controller"; +import Controller from "swaf/Controller"; import {Request, Response} from "express"; export default class AboutController extends Controller { @@ -10,4 +10,4 @@ export default class AboutController extends Controller { private async getAbout(req: Request, res: Response) { res.render('about'); } -} \ No newline at end of file +} diff --git a/src/controllers/AuthController.ts b/src/controllers/AuthController.ts index 6f30199..a8ead95 100644 --- a/src/controllers/AuthController.ts +++ b/src/controllers/AuthController.ts @@ -1,8 +1,8 @@ -import MagicLinkAuthController from "wms-core/auth/magic_link/MagicLinkAuthController"; -import {MAGIC_LINK_MAIL} from "wms-core/Mails"; +import MagicLinkAuthController from "swaf/auth/magic_link/MagicLinkAuthController"; +import {MAGIC_LINK_MAIL} from "swaf/Mails"; export default class AuthController extends MagicLinkAuthController { public constructor() { super(MAGIC_LINK_MAIL); } -} \ No newline at end of file +} diff --git a/src/controllers/AuthTokenController.ts b/src/controllers/AuthTokenController.ts index 297b3bb..974f543 100644 --- a/src/controllers/AuthTokenController.ts +++ b/src/controllers/AuthTokenController.ts @@ -1,18 +1,19 @@ -import Controller from "wms-core/Controller"; -import {REQUIRE_AUTH_MIDDLEWARE} from "wms-core/auth/AuthComponent"; +import Controller from "swaf/Controller"; +import {RequireAuthMiddleware} from "swaf/auth/AuthComponent"; import {Request, Response} from "express"; import AuthToken from "../models/AuthToken"; -import {BadRequestError, ForbiddenHttpError, NotFoundHttpError} from "wms-core/HttpError"; +import {BadRequestError, ForbiddenHttpError, NotFoundHttpError} from "swaf/HttpError"; export default class AuthTokenController extends Controller { routes(): void { - this.post('/gen-auth-token', this.postGenAuthToken, 'generate-token', REQUIRE_AUTH_MIDDLEWARE); - this.post('/revoke-auth-token/:id', this.postRevokeAuthToken, 'revoke-token', REQUIRE_AUTH_MIDDLEWARE); + this.post('/gen-auth-token', this.postGenAuthToken, 'generate-token', RequireAuthMiddleware); + this.post('/revoke-auth-token/:id', this.postRevokeAuthToken, 'revoke-token', RequireAuthMiddleware); } protected async postGenAuthToken(req: Request, res: Response): Promise { + const user = req.as(RequireAuthMiddleware).getUser(); const authToken = AuthToken.create({ - user_id: req.models.user!.id, + user_id: user.id, ttl: req.body.ttl ? parseInt(req.body.ttl) : 365 * 24 * 3600, }); await authToken.save(); @@ -26,11 +27,13 @@ export default class AuthTokenController extends Controller { const authToken = await AuthToken.getById(parseInt(id)); if (!authToken) throw new NotFoundHttpError('Auth token', req.url); - if (!authToken.canDelete(req.models.user!.id!)) throw new ForbiddenHttpError('auth token', req.url); + + const user = req.as(RequireAuthMiddleware).getUser(); + if (!authToken.canDelete(user.getOrFail('id'))) throw new ForbiddenHttpError('auth token', req.url); await authToken.delete(); req.flash('success', 'Successfully deleted auth token.'); res.redirectBack(Controller.route('file-upload')); } -} \ No newline at end of file +} diff --git a/src/controllers/FileController.ts b/src/controllers/FileController.ts index 6ccd81c..332b371 100644 --- a/src/controllers/FileController.ts +++ b/src/controllers/FileController.ts @@ -1,31 +1,32 @@ -import Controller from "wms-core/Controller"; -import {REQUIRE_AUTH_MIDDLEWARE} from "wms-core/auth/AuthComponent"; +import Controller from "swaf/Controller"; +import {RequireAuthMiddleware} from "swaf/auth/AuthComponent"; import {NextFunction, Request, Response} from "express"; -import {BadRequestError, ForbiddenHttpError, ServerError} from "wms-core/HttpError"; +import {BadRequestError, ForbiddenHttpError, ServerError} from "swaf/HttpError"; import FileModel from "../models/FileModel"; import config from "config"; import * as fs from "fs"; import AuthToken from "../models/AuthToken"; import {IncomingForm} from "formidable"; import generateSlug from "../SlugGenerator"; -import Logger from "wms-core/Logger"; -import FileUploadMiddleware from "wms-core/FileUploadMiddleware"; +import {log} from "swaf/Logger"; +import FileUploadMiddleware from "swaf/FileUploadMiddleware"; export default class FileController extends Controller { routes(): void { - this.get('/files/upload', this.getFileUploader, 'file-upload', REQUIRE_AUTH_MIDDLEWARE); + this.get('/files/upload', this.getFileUploader, 'file-upload', RequireAuthMiddleware); this.get('/files/upload/script', this.downloadLinuxScript, 'file-linux-script'); - this.post('/files/post', this.postFileFrontend, 'post-file-frontend', REQUIRE_AUTH_MIDDLEWARE, FILE_UPLOAD_FORM_MIDDLEWARE); - this.get('/files/:page([0-9]+)?', this.getFileManager, 'file-manager', REQUIRE_AUTH_MIDDLEWARE); - this.post('/files/delete/:slug', FileController.deleteFileRoute, 'delete-file-frontend', REQUIRE_AUTH_MIDDLEWARE); + this.post('/files/post', this.postFileFrontend, 'post-file-frontend', RequireAuthMiddleware, FileUploadFormMiddleware); + this.get('/files/:page([0-9]+)?', this.getFileManager, 'file-manager', RequireAuthMiddleware); + this.post('/files/delete/:slug', FileController.deleteFileRoute, 'delete-file-frontend', RequireAuthMiddleware); } protected async getFileUploader(req: Request, res: Response): Promise { const allowedDomains = config.get('allowed_url_domains'); + const user = req.as(RequireAuthMiddleware).getUser(); res.render('file-upload', { max_upload_size: config.get('max_upload_size'), - auth_tokens: await AuthToken.select().where('user_id', req.models.user!.id!).get(), + auth_tokens: await AuthToken.select().where('user_id', user.id).get(), allowed_domains: allowedDomains, default_domain: allowedDomains[config.get('default_url_domain_for_files')], }); @@ -36,8 +37,9 @@ export default class FileController extends Controller { } protected async getFileManager(req: Request, res: Response): Promise { + const user = req.as(RequireAuthMiddleware).getUser(); res.render('file-manager', { - files: await FileModel.paginateForUser(req, 100, req.models.user!.id!), + files: await FileModel.paginateForUser(req, 100, user.getOrFail('id')), }); } @@ -52,7 +54,7 @@ export default class FileController extends Controller { throw new BadRequestError('No file received.', 'You must upload exactly one (1) file.', req.url); } - let upload = req.files['upload']; + const upload = req.files['upload']; // TTL let ttl = config.get('default_file_ttl'); @@ -60,8 +62,10 @@ export default class FileController extends Controller { if (req.body.ttl !== undefined) ttl = parseInt(req.body.ttl); else if (req.body.expire_after_days !== undefined) ttl = parseInt(req.body.expire_after_days) * 24 * 3600; + const user = req.as(RequireAuthMiddleware).getUser(); + const file = FileModel.create({ - user_id: req.models.user!.id, + user_id: user.id, slug: slug, real_name: upload.name, storage_type: 'local', @@ -93,7 +97,9 @@ export default class FileController extends Controller { const file = await FileModel.getBySlug(req.params.slug); if (!file) return next(); - if (!file.canDelete(req.models.user!.id!)) throw new ForbiddenHttpError('file', req.url); + + const user = req.as(RequireAuthMiddleware).getUser(); + if (!file.canDelete(user.getOrFail('id'))) throw new ForbiddenHttpError('file', req.url); switch (file.storage_type) { case 'local': @@ -118,13 +124,20 @@ export default class FileController extends Controller { public static async deleteFile(file: FileModel): Promise { fs.unlinkSync(file.storage_path!); await file.delete(); - Logger.info('Deleted', file.storage_path, `(${file.real_name})`); + log.info('Deleted', file.storage_path, `(${file.real_name})`); } } -export const FILE_UPLOAD_FORM_MIDDLEWARE = new FileUploadMiddleware(() => { - const form = new IncomingForm(); - form.uploadDir = 'storage/tmp'; - form.maxFileSize = config.get('max_upload_size') * 1024 * 1024; - return form; -}, 'upload'); +export class FileUploadFormMiddleware extends FileUploadMiddleware { + protected getDefaultField(): string { + return 'upload'; + } + + protected makeForm(): IncomingForm { + const form = new IncomingForm(); + form.uploadDir = 'storage/tmp'; + form.maxFileSize = config.get('max_upload_size') * 1024 * 1024; + return form; + } + +} diff --git a/src/controllers/LinkController.ts b/src/controllers/LinkController.ts index 2e70526..60680e7 100644 --- a/src/controllers/LinkController.ts +++ b/src/controllers/LinkController.ts @@ -1,29 +1,29 @@ -import Controller from "wms-core/Controller"; +import Controller from "swaf/Controller"; import {NextFunction, Request, Response} from "express"; -import {BadRequestError, NotFoundHttpError, ServerError} from "wms-core/HttpError"; +import {BadRequestError, NotFoundHttpError, ServerError} from "swaf/HttpError"; import config from "config"; -import {REQUIRE_REQUEST_AUTH_MIDDLEWARE} from "wms-core/auth/AuthComponent"; +import {RequireRequestAuthMiddleware} from "swaf/auth/AuthComponent"; import URLRedirect from "../models/URLRedirect"; import URLRedirectController from "./URLRedirectController"; import FileModel from "../models/FileModel"; import generateSlug from "../SlugGenerator"; -import FileController, {FILE_UPLOAD_FORM_MIDDLEWARE} from "./FileController"; +import FileController, {FileUploadFormMiddleware} from "./FileController"; import * as fs from "fs"; import {encodeRFC5987ValueChars} from "../Utils"; import {promisify} from "util"; -import Logger from "wms-core/Logger"; +import {log} from "swaf/Logger"; export default class LinkController extends Controller { routes(): void { - this.post('/', this.postFile, 'post-file', REQUIRE_REQUEST_AUTH_MIDDLEWARE, FILE_UPLOAD_FORM_MIDDLEWARE); - this.delete('/:slug', FileController.deleteFileRoute, 'delete-file', REQUIRE_REQUEST_AUTH_MIDDLEWARE); + this.post('/', this.postFile, 'post-file', RequireRequestAuthMiddleware, FileUploadFormMiddleware); + this.delete('/:slug', FileController.deleteFileRoute, 'delete-file', RequireRequestAuthMiddleware); this.get('/:slug', this.getFile, 'get-file'); - this.put('/:slug', this.putFile, 'put-file', REQUIRE_REQUEST_AUTH_MIDDLEWARE, FILE_UPLOAD_FORM_MIDDLEWARE); + this.put('/:slug', this.putFile, 'put-file', RequireRequestAuthMiddleware, FileUploadFormMiddleware); - this.post('/', URLRedirectController.addURL, 'post-url', REQUIRE_REQUEST_AUTH_MIDDLEWARE); - this.delete('/:slug', this.deleteURL, 'delete-url', REQUIRE_REQUEST_AUTH_MIDDLEWARE); + this.post('/', URLRedirectController.addURL, 'post-url', RequireRequestAuthMiddleware); + this.delete('/:slug', this.deleteURL, 'delete-url', RequireRequestAuthMiddleware); this.get('/:slug', this.getURLRedirect, 'get-url'); - this.put('/:slug', URLRedirectController.addURL, 'put-url', REQUIRE_REQUEST_AUTH_MIDDLEWARE); + this.put('/:slug', URLRedirectController.addURL, 'put-url', RequireRequestAuthMiddleware); this.get(/(.*)/, this.domainFilter); } @@ -45,7 +45,7 @@ export default class LinkController extends Controller { // If file is bigger than max hotlink size, fallback to express download if (stats.size > config.get('max_hotlink_size') * 1024 * 1024) { - Logger.info(`Fallback to express download for file of size ${stats.size}`); + log.info(`Fallback to express download for file of size ${stats.size}`); return res.download(file.storage_path!, fileName); } diff --git a/src/controllers/MagicLinkActionType.ts b/src/controllers/MagicLinkActionType.ts index 77a6481..fa2e69b 100644 --- a/src/controllers/MagicLinkActionType.ts +++ b/src/controllers/MagicLinkActionType.ts @@ -1,4 +1,4 @@ -import MagicLink from "wms-core/auth/models/MagicLink"; +import MagicLink from "swaf/auth/models/MagicLink"; export enum MagicLinkActionType { LOGIN = 'Login', @@ -6,7 +6,7 @@ export enum MagicLinkActionType { } export function getActionMessage(magicLink: MagicLink): string { - switch (magicLink.getActionType()) { + switch (magicLink.action_type) { case MagicLinkActionType.LOGIN: return 'You have been authenticated.'; case MagicLinkActionType.REGISTER: @@ -14,4 +14,4 @@ export function getActionMessage(magicLink: MagicLink): string { } return ''; -} \ No newline at end of file +} diff --git a/src/controllers/MagicLinkController.ts b/src/controllers/MagicLinkController.ts index f3e7503..16fe536 100644 --- a/src/controllers/MagicLinkController.ts +++ b/src/controllers/MagicLinkController.ts @@ -1,22 +1,24 @@ -import _MagicLinkController from "wms-core/auth/magic_link/MagicLinkController"; +import _MagicLinkController from "swaf/auth/magic_link/MagicLinkController"; import {Request, Response} from "express"; -import Controller from "wms-core/Controller"; -import MagicLinkWebSocketListener from "wms-core/auth/magic_link/MagicLinkWebSocketListener"; -import MagicLink from "wms-core/auth/models/MagicLink"; +import Controller from "swaf/Controller"; +import MagicLinkWebSocketListener from "swaf/auth/magic_link/MagicLinkWebSocketListener"; +import MagicLink from "swaf/auth/models/MagicLink"; import AuthController from "./AuthController"; import {MagicLinkActionType} from "./MagicLinkActionType"; +import App from "../App"; +import AuthComponent from "swaf/auth/AuthComponent"; -export default class MagicLinkController extends _MagicLinkController { - constructor(magicLinkWebSocketListener: MagicLinkWebSocketListener) { +export default class MagicLinkController extends _MagicLinkController { + constructor(magicLinkWebSocketListener: MagicLinkWebSocketListener) { super(magicLinkWebSocketListener); } protected async performAction(magicLink: MagicLink, req: Request, res: Response): Promise { - switch (magicLink.getActionType()) { + switch (magicLink.action_type) { case MagicLinkActionType.LOGIN: case MagicLinkActionType.REGISTER: await AuthController.checkAndAuth(req, res, magicLink); - const proof = await req.authGuard.isAuthenticated(req.session!); + const proof = await this.getApp().as(AuthComponent).getAuthGuard().isAuthenticated(req.session!); const user = await proof?.getResource(); if (!res.headersSent && user) { @@ -27,4 +29,4 @@ export default class MagicLinkController extends _MagicLinkController { break; } } -} \ No newline at end of file +} diff --git a/src/controllers/URLRedirectController.ts b/src/controllers/URLRedirectController.ts index 5586248..cf42e2c 100644 --- a/src/controllers/URLRedirectController.ts +++ b/src/controllers/URLRedirectController.ts @@ -1,23 +1,24 @@ -import Controller from "wms-core/Controller"; +import Controller from "swaf/Controller"; import {NextFunction, Request, Response} from "express"; import URLRedirect from "../models/URLRedirect"; -import {REQUIRE_AUTH_MIDDLEWARE} from "wms-core/auth/AuthComponent"; +import {RequireAuthMiddleware} from "swaf/auth/AuthComponent"; import generateSlug from "../SlugGenerator"; import config from "config"; import AuthToken from "../models/AuthToken"; export default class URLRedirectController extends Controller { routes(): void { - this.get('/url/shrink', this.getURLShrinker, 'url-shrinker', REQUIRE_AUTH_MIDDLEWARE); + this.get('/url/shrink', this.getURLShrinker, 'url-shrinker', RequireAuthMiddleware); this.get('/url/shrink/script', this.downloadLinuxScript, 'url-linux-script'); - this.post('/url/shrink', this.addURLFrontend, 'shrink-url', REQUIRE_AUTH_MIDDLEWARE); - this.get('/urls/:page([0-9]+)?', this.getURLRedirectManager, 'url-manager', REQUIRE_AUTH_MIDDLEWARE); + this.post('/url/shrink', this.addURLFrontend, 'shrink-url', RequireAuthMiddleware); + this.get('/urls/:page([0-9]+)?', this.getURLRedirectManager, 'url-manager', RequireAuthMiddleware); } protected async getURLShrinker(req: Request, res: Response): Promise { + const user = req.as(RequireAuthMiddleware).getUser(); const allowedDomains = config.get('allowed_url_domains'); res.render('url-shrinker', { - auth_tokens: await AuthToken.select().where('user_id', req.models.user!.id!).get(), + auth_tokens: await AuthToken.select().where('user_id', user.id).get(), allowed_domains: allowedDomains, default_domain: allowedDomains[config.get('default_url_domain_for_urls')], }); @@ -28,8 +29,9 @@ export default class URLRedirectController extends Controller { } protected async getURLRedirectManager(req: Request, res: Response): Promise { + const user = req.as(RequireAuthMiddleware).getUser(); res.render('url-manager', { - urls: await URLRedirect.paginateForUser(req, 100, req.models.user!.id!), + urls: await URLRedirect.paginateForUser(req, 100, user.getOrFail('id')), }); } @@ -41,9 +43,10 @@ export default class URLRedirectController extends Controller { public static async addURL(req: Request, res: Response, next: NextFunction, slug?: string): Promise { if (req.body.type !== 'url') return next(); + const user = req.as(RequireAuthMiddleware).getUser(); slug = slug || req.params.slug || req.body.slug || await generateSlug(10); const urlRedirect = URLRedirect.create({ - user_id: req.models.user!.id, + user_id: user.id, slug: slug, target_url: req.body.target_url, }); @@ -63,4 +66,4 @@ export default class URLRedirectController extends Controller { }, }); } -} \ No newline at end of file +} diff --git a/src/migrations/CreateAuthTokensTable.ts b/src/migrations/CreateAuthTokensTable.ts index 4bb09a1..2194a3a 100644 --- a/src/migrations/CreateAuthTokensTable.ts +++ b/src/migrations/CreateAuthTokensTable.ts @@ -1,6 +1,6 @@ import {Connection} from "mysql"; -import Migration from "wms-core/db/Migration"; -import ModelFactory from "wms-core/db/ModelFactory"; +import Migration from "swaf/db/Migration"; +import ModelFactory from "swaf/db/ModelFactory"; import AuthToken from "../models/AuthToken"; export default class CreateAuthTokensTable extends Migration { @@ -25,4 +25,4 @@ export default class CreateAuthTokensTable extends Migration { ModelFactory.register(AuthToken); } -} \ No newline at end of file +} diff --git a/src/migrations/CreateFilesTable.ts b/src/migrations/CreateFilesTable.ts index f810e08..a0a8baa 100644 --- a/src/migrations/CreateFilesTable.ts +++ b/src/migrations/CreateFilesTable.ts @@ -1,6 +1,6 @@ import {Connection} from "mysql"; -import Migration from "wms-core/db/Migration"; -import ModelFactory from "wms-core/db/ModelFactory"; +import Migration from "swaf/db/Migration"; +import ModelFactory from "swaf/db/ModelFactory"; import FileModel from "../models/FileModel"; export default class CreateFilesTable extends Migration { @@ -27,4 +27,4 @@ export default class CreateFilesTable extends Migration { public registerModels(): void { ModelFactory.register(FileModel); } -} \ No newline at end of file +} diff --git a/src/migrations/CreateUrlRedirectsTable.ts b/src/migrations/CreateUrlRedirectsTable.ts index a6d808e..c27db4a 100644 --- a/src/migrations/CreateUrlRedirectsTable.ts +++ b/src/migrations/CreateUrlRedirectsTable.ts @@ -1,6 +1,6 @@ -import Migration from "wms-core/db/Migration"; +import Migration from "swaf/db/Migration"; import {Connection} from "mysql"; -import ModelFactory from "wms-core/db/ModelFactory"; +import ModelFactory from "swaf/db/ModelFactory"; import URLRedirect from "../models/URLRedirect"; export default class CreateUrlRedirectsTable extends Migration { @@ -23,4 +23,4 @@ export default class CreateUrlRedirectsTable extends Migration { public registerModels(): void { ModelFactory.register(URLRedirect); } -} \ No newline at end of file +} diff --git a/src/migrations/IncreaseFilesSizeField.ts b/src/migrations/IncreaseFilesSizeField.ts index b4b62bd..52cef57 100644 --- a/src/migrations/IncreaseFilesSizeField.ts +++ b/src/migrations/IncreaseFilesSizeField.ts @@ -1,4 +1,4 @@ -import Migration from "wms-core/db/Migration"; +import Migration from "swaf/db/Migration"; import {Connection} from "mysql"; export default class IncreaseFilesSizeField extends Migration { @@ -12,4 +12,4 @@ export default class IncreaseFilesSizeField extends Migration { public registerModels(): void { } -} \ No newline at end of file +} diff --git a/src/models/AuthToken.ts b/src/models/AuthToken.ts index 97f010b..c6a234f 100644 --- a/src/models/AuthToken.ts +++ b/src/models/AuthToken.ts @@ -1,7 +1,7 @@ -import Model from "wms-core/db/Model"; -import AuthProof from "wms-core/auth/AuthProof"; -import User from "wms-core/auth/models/User"; -import {cryptoRandomDictionary} from "wms-core/Utils"; +import Model from "swaf/db/Model"; +import AuthProof from "swaf/auth/AuthProof"; +import User from "swaf/auth/models/User"; +import {cryptoRandomDictionary} from "swaf/Utils"; export default class AuthToken extends Model implements AuthProof { public id?: number = undefined; @@ -18,8 +18,6 @@ export default class AuthToken extends Model implements AuthProof { } protected async autoFill(): Promise { - await super.autoFill(); - // @ts-ignore if (!this.secret) this['secret'] = cryptoRandomDictionary(64, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'); } @@ -53,4 +51,4 @@ export default class AuthToken extends Model implements AuthProof { public async revoke(): Promise { await this.delete(); } -} \ No newline at end of file +} diff --git a/src/models/FileModel.ts b/src/models/FileModel.ts index d8a2af6..5ac6ac0 100644 --- a/src/models/FileModel.ts +++ b/src/models/FileModel.ts @@ -1,7 +1,7 @@ -import Model from "wms-core/db/Model"; -import Controller from "wms-core/Controller"; +import Model from "swaf/db/Model"; +import Controller from "swaf/Controller"; import config from "config"; -import User from "wms-core/auth/models/User"; +import User from "swaf/auth/models/User"; import {Request} from "express"; import URLRedirect from "./URLRedirect"; diff --git a/src/models/URLRedirect.ts b/src/models/URLRedirect.ts index 2b754ed..77281aa 100644 --- a/src/models/URLRedirect.ts +++ b/src/models/URLRedirect.ts @@ -1,9 +1,9 @@ -import Model from "wms-core/db/Model"; -import User from "wms-core/auth/models/User"; +import Model from "swaf/db/Model"; +import User from "swaf/auth/models/User"; import FileModel from "./FileModel"; import {Request} from "express"; import config from "config"; -import Controller from "wms-core/Controller"; +import Controller from "swaf/Controller"; export default class URLRedirect extends Model { public static get table(): string { @@ -35,4 +35,4 @@ export default class URLRedirect extends Model { slug: this.slug!, }); } -} \ No newline at end of file +}