diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 6bd3b51..aa7af45 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -93,6 +93,8 @@ export const Routes : Route[] = [ {path: "/settings/get_security", cb: (h) => SettingsController.GetSecurity(h)}, + {path: "/settings/set_security", cb: (h) => SettingsController.SetSecurity(h)}, + // Friends controller {path: "/friends/getList", cb: (h) => FriendsController.GetList(h)}, diff --git a/src/controllers/SettingsController.ts b/src/controllers/SettingsController.ts index 70f39f5..a72f1d9 100644 --- a/src/controllers/SettingsController.ts +++ b/src/controllers/SettingsController.ts @@ -6,7 +6,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { UserHelper } from "../helpers/UserHelper"; -import { GeneralSettings, UserPageStatus, SupportedLanguages, LangSettings } from "../entities/User"; +import { GeneralSettings, UserPageStatus, SupportedLanguages, LangSettings, SecuritySettings } from "../entities/User"; import { removeHTMLNodes, checkURL, fixEncoding } from "../utils/StringUtils"; import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils"; import { AccountHelper } from "../helpers/AccountHelper"; @@ -148,10 +148,31 @@ export class SettingsController { h.send({ id: userInfo.id, - security_question_1: userInfo.hasSecurityQuestion1 ? fixEncoding(userInfo.security_question_1) : "", - security_answer_1: userInfo.hasSecurityAnswer1 ? fixEncoding(userInfo.security_answer_1) : "", - security_question_2: userInfo.hasSecurityQuestion2 ? fixEncoding(userInfo.security_question_2) : "", - security_answer_2: userInfo.hasSecurityAnswer2 ? fixEncoding(userInfo.security_answer_2) : "", + security_question_1: userInfo.hasSecurityQuestion1 ? userInfo.security_question_1 : "", + security_answer_1: userInfo.hasSecurityAnswer1 ? userInfo.security_answer_1 : "", + security_question_2: userInfo.hasSecurityQuestion2 ? userInfo.security_question_2 : "", + security_answer_2: userInfo.hasSecurityAnswer2 ? userInfo.security_answer_2 : "", }); } + + /** + * Set security settings + * + * @param h Request handler + */ + public static async SetSecurity(h: RequestHandler) { + await h.needUserPostPassword("password"); + + const newSettings: SecuritySettings = { + id: h.getUserId(), + security_question_1: removeHTMLNodes(h.postString("security_question_1", 0)), + security_answer_1: removeHTMLNodes(h.postString("security_answer_1", 0)), + security_question_2: removeHTMLNodes(h.postString("security_question_2", 0)), + security_answer_2: removeHTMLNodes(h.postString("security_answer_2", 0)) + } + + await AccountHelper.SetSecuritySettings(newSettings); + + h.success(); + } } \ No newline at end of file diff --git a/src/helpers/AccountHelper.ts b/src/helpers/AccountHelper.ts index 96d47c4..6eb3726 100644 --- a/src/helpers/AccountHelper.ts +++ b/src/helpers/AccountHelper.ts @@ -5,7 +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, LangSettings } from "../entities/User"; +import { GeneralSettings, UserPageStatus, LangSettings, SecuritySettings } from "../entities/User"; /** * Account helper @@ -354,4 +354,24 @@ export class AccountHelper { } }); } + + /** + * Update (set) new security settings + * + * @param settings New settings + */ + public static async SetSecuritySettings(settings: SecuritySettings) { + await DatabaseHelper.UpdateRows({ + table: USER_TABLE, + where: { + ID: settings.id + }, + set: { + question1: settings.security_question_1, + reponse1: settings.security_answer_1, + question2: settings.security_question_2, + reponse2: settings.security_answer_2 + } + }); + } } \ No newline at end of file