1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-02-16 22:22:39 +00:00
comunicapiv2/src/helpers/AccountHelper.ts

535 lines
13 KiB
TypeScript
Raw Normal View History

2019-11-23 13:16:28 +01:00
import { crypt, sha1, randomStr } from "../utils/CryptUtils";
import { APIClient } from "../entities/APIClient";
import { UserLoginTokens } from "../entities/UserLoginTokens";
import { DatabaseHelper } from "./DatabaseHelper";
2019-12-26 13:49:17 +01:00
import { UserHelper } from "./UserHelper";
2019-12-30 13:44:54 +01:00
import { time, mysql_date } from "../utils/DateUtils";
import { NewAccount } from "../entities/NewAccount";
2020-03-22 14:18:59 +01:00
import { GeneralSettings, UserPageStatus, LangSettings, SecuritySettings } from "../entities/User";
2020-03-25 18:20:43 +01:00
import { AccountExport } from "../entities/AccountExport";
2020-03-25 18:44:51 +01:00
import { PostsHelper } from "./PostsHelper";
2020-03-25 18:55:25 +01:00
import { CommentsHelper } from "./CommentsHelper";
2020-03-27 14:52:56 +01:00
import { LikesHelper, LikesType } from "./LikesHelper";
import { SurveyHelper } from "./SurveyHelper";
2020-03-26 12:13:53 +01:00
import { MoviesHelper } from "./MoviesHelper";
2020-03-26 13:05:14 +01:00
import { ConversationsHelper } from "./ConversationsHelper";
2020-03-26 13:29:39 +01:00
import { FriendsHelper } from "./FriendsHelper";
2020-03-26 13:59:30 +01:00
import { GroupsHelper } from "./GroupsHelper";
import { NotificationsHelper } from "./NotificationsHelper";
2020-03-27 14:44:27 +01:00
import { AccountImageHelper } from "./AccountImageHelper";
import { BackgroundImageHelper } from "./BackgroundImageHelper";
2020-03-30 14:20:33 +02:00
import { EventsHelper } from "./EventsHelper";
2019-11-23 13:16:28 +01:00
/**
* Account helper
*
* @author Pierre HUBERT
*/
2019-12-28 16:28:38 +01:00
export const USER_TABLE = "utilisateurs";
2019-11-23 13:16:28 +01:00
const USERS_TOKENS_TABLE = "comunic_api_users_tokens";
export class AccountHelper {
2019-12-30 13:44:54 +01:00
/**
* Create a new account
*
* @param info Information about the new account
*/
public static async Create(info: NewAccount) {
const data = {
nom: info.lastName,
prenom: info.firstName,
date_creation: mysql_date(),
mail: info.email,
password: this.CryptPassword(info.password)
};
await DatabaseHelper.InsertRow(USER_TABLE, data);
}
2019-11-23 13:16:28 +01:00
/**
* Given email address and password, try to sign in user
*
* @param email The email of the user
* @param password User password
* @param client Information about associated client / null if none found
*/
static async LoginUser(email: string, password: string, client: APIClient) : Promise<UserLoginTokens | null> {
// Perform a request on the database
const row = await DatabaseHelper.QueryRow({
table: USER_TABLE,
fields: ["ID"],
where: {
mail: email,
password: this.CryptPassword(password)
}
});
// Check if user was found
if(row == null)
return null;
const userID = row.ID;
// Check for existing tokens
let tokens = await this.GetClientTokens(userID, client);
if(tokens != null)
return tokens;
const newTokens : UserLoginTokens = {
userID: userID,
clientID: client.id,
token1: randomStr(150),
token2: "dummy_data"
}
// Save new tokens
await DatabaseHelper.InsertRow(USERS_TOKENS_TABLE, {
user_id: newTokens.userID,
service_id: newTokens.clientID,
token1: newTokens.token1,
token2: newTokens.token2
});
return newTokens;
}
/**
* Get user client tokens (if it exists)
*
* @param userID Target user ID
* @param client Information about associated client / null if none found
*/
private static async GetClientTokens(userID: number, client: APIClient): Promise<UserLoginTokens | null> {
const row = await DatabaseHelper.QueryRow({
table: USERS_TOKENS_TABLE,
where: {
user_id: userID,
service_id: client.id
}
});
return row == null ? null : this.DBToUserTokens(row);
}
2019-11-23 13:47:06 +01:00
/**
* Find a user using its tokens
*
* @param client Information about the client
* @param token1 First token
* @param token2 Second token
* @returns The ID of the target user / -1 if none found
*/
public static async GetUserIdFromTokens(client : APIClient,
token1: string, token2: string) : Promise<number> {
const row = await DatabaseHelper.QueryRow({
table: USERS_TOKENS_TABLE,
fields: ["user_id"],
where: {
service_id: client.id,
token1: token1,
token2: token2
}
});
if(!row)
return -1;
return Number(row.user_id);
}
2019-11-23 14:03:14 +01:00
/**
* Destroy user tokens
*
* @param client Information about the client
* @param userID Target user ID
*/
public static async DestroyUserTokens(client: APIClient, userID: number) {
2020-03-30 14:20:33 +02:00
// Push the event
EventsHelper.Emit("destroyed_login_tokens", {
client: client,
userID: userID
});
2019-11-23 14:03:14 +01:00
return DatabaseHelper.DeleteRows(USERS_TOKENS_TABLE, {
service_id: client.id,
user_id: userID
});
}
2019-12-27 18:49:40 +01:00
/**
* Check out whether the password of a user is valid
* or not
*
* @param userID Target user ID
* @param password Target password
*/
public static async CheckUserPassword(userID: number, password: string) : Promise<boolean> {
const crypt_pass = this.CryptPassword(password);
return await DatabaseHelper.Count({
table: USER_TABLE,
where: {
ID: userID,
password: crypt_pass
}
}) > 0;
}
2019-11-23 13:16:28 +01:00
/**
* Crypt a password
*
* @param pass The password to crypt
* @return Encrypted string
*/
private static CryptPassword(pass: string) : string {
return crypt(sha1(pass), sha1(pass));
}
private static DBToUserTokens(row : any) : UserLoginTokens {
return {
userID: row.user_id,
clientID: row.service_id,
token1: row.token1,
token2: row.token2
};
}
2019-12-26 13:49:17 +01:00
/**
* Check out whether an email address exists or not
*
* @param email Email address to check
*/
public static async ExistsEmail(email: string) : Promise<boolean> {
return await DatabaseHelper.Count({
table: USER_TABLE,
where: {
mail: email
}
}) > 0;
}
/**
* Find user ID from its email address
*
* @param email Email address
* @returns The ID of the matching user / -1 if none found
*/
public static async FindIDFromEmail(email: string) : Promise<number> {
const result = await DatabaseHelper.QueryRow({
table: USER_TABLE,
where: {
mail: email,
},
fields: ["ID"]
});
return result == null ? -1 : result.ID;
}
2020-03-28 15:21:06 +01:00
/**
* Update the last activity time of the user
*
* @param userID Target user ID
*/
public static async UpdateLastActivity(userID: number) {
await DatabaseHelper.UpdateRows({
table: USER_TABLE,
where: {
ID: userID
},
set: {
last_activity: time()
}
})
}
2019-12-26 13:49:17 +01:00
/**
* Check out whether a virtual directory is available or not
*
* @param dir Virtual directory to check
* @param userID Target user ID
*/
public static async CheckUserDirectoryAvailability(dir: string, userID: number = -1) : Promise<boolean> {
const foundUser = await UserHelper.FindByFolder(dir);
return foundUser < 1 || userID == foundUser;
}
2019-12-30 08:36:55 +01:00
/**
* Generate a new token to reset an account password
*
* @param userID Target user ID
* @returns Generated token
*/
public static async GenerateNewPasswordResetToken(userID: number) : Promise<string> {
// Generate a token
const token = randomStr(255);
await DatabaseHelper.UpdateRows({
table: USER_TABLE,
where: {
ID: userID
},
set: {
password_reset_token: token,
password_reset_token_time_create: time()
}
});
return token;
}
2019-12-30 12:11:33 +01:00
2019-12-30 13:20:24 +01:00
/**
* Destroy password reset token for a given user
*
* @param userID Target user ID
*/
public static async DestroyPasswordResetTokenForUser(userID: number) {
await DatabaseHelper.UpdateRows({
table: USER_TABLE,
where: {
ID: userID
},
set: {
password_reset_token: "",
password_reset_token_time_create: 85 // Value too low to be valid
}
});
}
2020-03-27 15:13:16 +01:00
/**
* Delete all user login tokens
*
* This method has for immediate effect to disconnect
* the user from all its devices
*
* @param userID Target user ID
*/
public static async DeleteAllUserLoginTokens(userID: number) {
await DatabaseHelper.DeleteRows(USERS_TOKENS_TABLE, {
user_id: userID
})
}
2019-12-30 12:11:33 +01:00
/**
* Get the ID of a user from a password reset token
*
* @param token The token to use
* @returns The ID of the user associated to the token, if it is valid / -1 else
*/
public static async GetUserIDFromPasswordResetToken(token: string) : Promise<number> {
// Query the database
const result = await DatabaseHelper.QueryRow({
table: USER_TABLE,
where: {
password_reset_token: token,
},
customWhere: "password_reset_token_time_create > ?",
customWhereArgs:[(time()-60*60*24).toString()] // Tokens are valid for 24 hours
});
if(result == null)
return -1;
return result.ID;
2019-12-30 13:20:24 +01:00
}
2019-12-30 12:11:33 +01:00
2019-12-30 13:20:24 +01:00
/**
* Change the password of the user
*
* @param userID Target user ID
* @param password Target password
*/
public static async ChangePassword(userID: number, password: string) {
await DatabaseHelper.UpdateRows({
table: USER_TABLE,
where: {
ID: userID
},
set: {
password: this.CryptPassword(password)
}
});
2019-12-30 12:11:33 +01:00
}
2020-03-21 18:05:17 +01:00
/**
* 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
}
});
}
2020-03-21 18:54:30 +01:00
/**
* Set (save) new language settings
*
* @param settings New settings
*/
public static async SetLanguageSettings(settings: LangSettings) {
await DatabaseHelper.UpdateRows({
table: USER_TABLE,
where: {
ID: settings.id
},
set: {
lang: settings.lang
}
});
}
2020-03-22 14:18:59 +01:00
/**
* 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
}
});
}
2020-03-25 18:20:43 +01:00
/**
* Export all the data of an account
*
* @param userID Target user ID
*/
public static async Export(userID: number) : Promise<AccountExport> {
const data = new AccountExport({
2020-03-25 18:44:51 +01:00
// General info
2020-03-25 18:24:46 +01:00
userID: userID,
2020-03-25 18:44:51 +01:00
userInfo: await UserHelper.GetUserInfo(userID),
// Export the list of posts
2020-03-25 18:55:25 +01:00
postsList: await PostsHelper.ExportAllPostsUser(userID),
// Export the list of comments
2020-03-25 19:08:43 +01:00
comments: await CommentsHelper.ExportAllUser(userID),
// Export user likes
likes: await LikesHelper.ExportAllUser(userID),
// Export all responses of user to surveys
2020-03-26 12:13:53 +01:00
surveyResponses: await SurveyHelper.ExportAllUserResponses(userID),
// User movies
movies: await MoviesHelper.GetListUser(userID),
2020-03-26 13:05:14 +01:00
// Conversation messages
2020-03-26 13:26:05 +01:00
allConversationMessages: await ConversationsHelper.ExportAllMessages(userID),
2020-03-26 13:09:15 +01:00
// Conversations list
conversations: await ConversationsHelper.GetListUser(userID),
2020-03-26 13:26:05 +01:00
// Conversations messages (completed after)
2020-03-26 13:29:39 +01:00
conversationsMessages: new Map(),
// Friends
2020-03-26 13:44:57 +01:00
friendsList: await FriendsHelper.GetList(userID),
2020-03-26 13:59:30 +01:00
// Groups membership
groups: await GroupsHelper.GetListUser(userID),
2020-03-25 18:20:43 +01:00
})
2020-03-26 13:26:05 +01:00
// Process conversation messages
for(const conv of data.conversations) {
data.conversationsMessages[conv.id]
= await ConversationsHelper.GetAllMessages(conv.id);
}
2020-03-25 18:20:43 +01:00
return data;
}
2020-03-26 17:08:56 +01:00
/**
* Remove completely a user account
*
* @param userID Target user ID
*/
public static async Delete(userID: number) {
2020-03-27 11:35:54 +01:00
/*// Delete all groups memberships
2020-03-26 17:08:56 +01:00
await GroupsHelper.DeleteAllUsersGroups(userID);
2020-03-27 11:35:54 +01:00
// Delete all user comments
await CommentsHelper.DeleteAllUser(userID);
2020-03-27 11:42:18 +01:00
// Delete all user posts
await PostsHelper.DeleteAllUser(userID);
2020-03-27 11:48:00 +01:00
// Delete all responses of user to surveys
await SurveyHelper.DeleteAllUserResponses(userID);
// Delete all the likes created by the user
await LikesHelper.DeleteAllUser(userID);
2020-03-27 13:57:36 +01:00
// Delete all user movies
await MoviesHelper.DeleteAllUser(userID);
2020-03-27 11:48:00 +01:00
// Delete conversation messages
await ConversationsHelper.DeleteAllUserMessages(userID);
// Remove the user from all its conversations
await ConversationsHelper.DeleteAllUserConversations(userID);
// Delete all the notifications related with the user
await NotificationsHelper.DeleteAllRelatedWithUser(userID);
2020-03-27 14:41:18 +01:00
// Delete all user friends, including friendship requests
await FriendsHelper.DeleteAllUser(userID);
2020-03-27 14:44:27 +01:00
// Delete user account image
await AccountImageHelper.Delete(userID);
2020-03-27 14:52:56 +01:00
// Delete all the likes on the user page
await LikesHelper.DeleteAll(userID, LikesType.USER);
// Delete user background image
await BackgroundImageHelper.Delete(userID);
2020-04-03 16:43:38 +02:00
// TODO : Delete custom user emojies
2020-03-27 15:13:16 +01:00
// Delete connections to all services
await this.DeleteAllUserLoginTokens(userID);
*/
2020-03-27 15:13:53 +01:00
// Delete user from the database
// WILL BE IMPLEMENTED LATER
2020-03-26 17:08:56 +01:00
}
2019-11-23 13:16:28 +01:00
}