From 03100e0bd3711d049cc82e7512a744fdcf339ae7 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 21 Mar 2020 18:05:17 +0100 Subject: [PATCH] Can set (update) general settings --- src/controllers/Routes.ts | 4 ++- src/controllers/SettingsController.ts | 52 +++++++++++++++++++++++++++ src/entities/User.ts | 21 ++++++++--- src/helpers/AccountHelper.ts | 28 +++++++++++++++ 4 files changed, 100 insertions(+), 5 deletions(-) diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index ea34f59..c2dea1e 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -82,7 +82,9 @@ export const Routes : Route[] = [ // Settings controller {path: "/settings/get_general", cb: (h) => SettingsController.GetGeneral(h)}, - + + {path: "/settings/set_general", cb: (h) => SettingsController.SetGeneral(h)}, + // Friends controller {path: "/friends/getList", cb: (h) => FriendsController.GetList(h)}, diff --git a/src/controllers/SettingsController.ts b/src/controllers/SettingsController.ts index 3905c26..af1dab0 100644 --- a/src/controllers/SettingsController.ts +++ b/src/controllers/SettingsController.ts @@ -7,6 +7,11 @@ import { RequestHandler } from "../entities/RequestHandler"; import { UserHelper } from "../helpers/UserHelper"; import { UserController } from "./UserController"; +import { GeneralSettings, User, UserPageStatus } from "../entities/User"; +import { removeHTMLNodes, checkURL } from "../utils/StringUtils"; +import { VirtualDirectoryController } from "./VirtualDirectoryController"; +import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils"; +import { AccountHelper } from "../helpers/AccountHelper"; export class SettingsController { @@ -36,4 +41,51 @@ export class SettingsController { }) } + /** + * Update (set) general account settings + * + * @param h Request handler + */ + public static async SetGeneral(h: RequestHandler) { + + // Determine page status + const pageStatus = h.postBool("isPublic") ? ( + h.postBool("isOpen") ? UserPageStatus.OPEN : UserPageStatus.PUBLIC + ) : UserPageStatus.PRIVATE; + + // Check personnal website + const personnalWebsite = h.postString("personnalWebsite", 0); + if(personnalWebsite.length > 0 && !checkURL(personnalWebsite)) + h.error(401, "Invalid personnal website supplied!"); + + // Check virtual directory + let virtualDirectory = h.postString("virtualDirectory", 0); + if(virtualDirectory.length > 0) { + virtualDirectory = h.postVirtualDirectory("virtualDirectory"); + + if(!checkVirtualDirectoryAvailability(virtualDirectory, h.getUserId(), VirtualDirType.USER)) + h.error(401, "The specified virtual directory is not available!"); + } + + // Construct new settings object + const newSettings : GeneralSettings = { + id: h.getUserId(), + firstName: removeHTMLNodes(h.postString("firstName", 3)), + lastName: removeHTMLNodes(h.postString("lastName", 3)), + pageStatus: pageStatus, + blockComments: !h.postBool("allowComments"), + allowPostsFromFriends: h.postBool("allowPostsFromFriends"), + friendsListPublic: h.postBool("publicFriendsList"), + personnalWebsite: personnalWebsite, + virtualDirectory: virtualDirectory, + allowMails: h.postBool("allow_comunic_mails"), + publicNote: removeHTMLNodes(h.postString("publicNote", 0)) + }; + + await AccountHelper.SetGeneral(newSettings); + + h.success(); + + } + } \ No newline at end of file diff --git a/src/entities/User.ts b/src/entities/User.ts index 298000a..8bcb99c 100644 --- a/src/entities/User.ts +++ b/src/entities/User.ts @@ -16,7 +16,6 @@ export enum UserPageStatus { export interface UserInfo { id: number, - email: string; firstName: string, lastName: string, timeCreate: number, @@ -28,7 +27,21 @@ export interface UserInfo { publicNote ?: string, blockComments : boolean, allowPostsFromFriends: boolean, - allowMails: boolean +} + +export interface GeneralSettings { + id: number, + email ?: string, + firstName: string, + lastName: string, + pageStatus: UserPageStatus, + blockComments: boolean, + allowPostsFromFriends: boolean, + friendsListPublic: boolean, + personnalWebsite ?: string, + virtualDirectory: string, + allowMails: boolean, + publicNote ?: string } export interface SecuritySettings { @@ -40,7 +53,7 @@ export interface SecuritySettings { } -export interface UserBuilder extends UserInfo, SecuritySettings { +export interface UserBuilder extends UserInfo, SecuritySettings, GeneralSettings { } @@ -75,7 +88,7 @@ export class User implements UserBuilder { get isPublic() : boolean { - return this.pageStatus == UserPageStatus.PUBLIC; + return this.pageStatus != UserPageStatus.PRIVATE; } get isOpen() : boolean { diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 7639859..ddb645d 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -5,6 +5,7 @@ import { DatabaseHelper } from "./DatabaseHelper"; import { UserHelper } from "./UserHelper"; import { time, mysql_date } from "../utils/DateUtils"; import { NewAccount } from "../entities/NewAccount"; +import { GeneralSettings, UserPageStatus } from "../entities/User"; /** * Account helper @@ -309,4 +310,31 @@ export class AccountHelper { } }); } + + /** + * Set (save) new general settings + * + * @param settings New settings + */ + public static async SetGeneral(settings: GeneralSettings) { + await DatabaseHelper.UpdateRows({ + table: USER_TABLE, + where: { + ID: settings.id + }, + set: { + prenom: settings.firstName, + nom: settings.lastName, + public: settings.pageStatus != UserPageStatus.PRIVATE ? 1 : 0, + pageouverte: settings.pageStatus == UserPageStatus.OPEN ? 1 : 0, + bloquecommentaire: settings.blockComments ? 1 : 0, + autoriser_post_amis: settings.allowPostsFromFriends ? 1 : 0, + autorise_mail: settings.allowMails ? 1 : 0, + liste_amis_publique: settings.friendsListPublic ? 1 : 0, + sous_repertoire: settings.virtualDirectory, + site_web: settings.personnalWebsite, + public_note: settings.publicNote + } + }); + } } \ No newline at end of file