Swaf upgrade: fix compilation errors
This commit is contained in:
parent
973110f1a4
commit
21bd0a42d0
74
src/App.ts
74
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<Migration>[] {
|
||||
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<MagicLink | AuthToken> {
|
||||
public async getProofForSession(session: Express.Session): Promise<any | null> {
|
||||
return await MagicLink.bySessionID(session.id, [MagicLinkActionType.LOGIN, MagicLinkActionType.REGISTER]);
|
||||
public async getProofForSession(session: Session): Promise<any | null> {
|
||||
return await MagicLink.bySessionId(
|
||||
session.id,
|
||||
[MagicLinkActionType.LOGIN, MagicLinkActionType.REGISTER],
|
||||
);
|
||||
}
|
||||
|
||||
public async getProofForRequest(req: Request): Promise<MagicLink | AuthToken | null> {
|
||||
@ -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>>(MagicLinkWebSocketListener)));
|
||||
this.use(new BackendController());
|
||||
|
||||
// Core functionality
|
||||
|
@ -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<string> {
|
||||
|
@ -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 {
|
||||
|
@ -1,5 +1,5 @@
|
||||
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() {
|
||||
|
@ -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<void> {
|
||||
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,7 +27,9 @@ export default class AuthTokenController extends Controller {
|
||||
|
||||
const authToken = await AuthToken.getById<AuthToken>(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();
|
||||
|
||||
|
@ -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<void> {
|
||||
const allowedDomains = config.get<string[]>('allowed_url_domains');
|
||||
const user = req.as(RequireAuthMiddleware).getUser();
|
||||
res.render('file-upload', {
|
||||
max_upload_size: config.get<string>('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<number>('default_url_domain_for_files')],
|
||||
});
|
||||
@ -36,8 +37,9 @@ export default class FileController extends Controller {
|
||||
}
|
||||
|
||||
protected async getFileManager(req: Request, res: Response): Promise<void> {
|
||||
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<number>('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<void> {
|
||||
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<number>('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<number>('max_upload_size') * 1024 * 1024;
|
||||
return form;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<number>('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);
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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<App> {
|
||||
constructor(magicLinkWebSocketListener: MagicLinkWebSocketListener<App>) {
|
||||
super(magicLinkWebSocketListener);
|
||||
}
|
||||
|
||||
protected async performAction(magicLink: MagicLink, req: Request, res: Response): Promise<void> {
|
||||
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) {
|
||||
|
@ -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<void> {
|
||||
const user = req.as(RequireAuthMiddleware).getUser();
|
||||
const allowedDomains = config.get<string[]>('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<number>('default_url_domain_for_urls')],
|
||||
});
|
||||
@ -28,8 +29,9 @@ export default class URLRedirectController extends Controller {
|
||||
}
|
||||
|
||||
protected async getURLRedirectManager(req: Request, res: Response): Promise<void> {
|
||||
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<void> {
|
||||
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,
|
||||
});
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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<User> {
|
||||
public id?: number = undefined;
|
||||
@ -18,8 +18,6 @@ export default class AuthToken extends Model implements AuthProof<User> {
|
||||
}
|
||||
|
||||
protected async autoFill(): Promise<void> {
|
||||
await super.autoFill();
|
||||
|
||||
// @ts-ignore
|
||||
if (!this.secret) this['secret'] = cryptoRandomDictionary(64, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_');
|
||||
}
|
||||
|
@ -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";
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user