saving users

This commit is contained in:
Erik 2022-11-09 13:13:03 +02:00
parent 5842448f1e
commit 05503ade05
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
3 changed files with 64 additions and 9 deletions

View File

@ -30,7 +30,7 @@ class Server extends EventEmitter {
const { MARIA_HOST, MARIA_USER, MARIA_PORT, MARIA_PASS, MARIA_DB,
MONGO_HOST, MONGO_USER, MONGO_PORT, MONGO_PASS, MONGO_DB,
NODE_ENV, SECRET, DISCORD_SECRET } = process.env; // Secret used for cookies, discord secret is for discord oauth
NODE_ENV, SECRET, DISCORD_SECRET, DISCORD_ID } = process.env; // Secret used for cookies, discord secret is for discord oauth
const { discord, http: httpOpts, databases } = options;
this._options = options;
@ -52,11 +52,11 @@ class Server extends EventEmitter {
this.mariadb = new MariaDB(this, { options: databases.mariadb, MARIA_HOST, MARIA_USER, MARIA_PORT, MARIA_PASS, MARIA_DB });
this.mongodb = new MongoDB(this, { options: databases.mongodb, MONGO_HOST, MONGO_USER, MONGO_PORT, MONGO_PASS, MONGO_DB });
this.userDatabase = new UserDatabase(this.mongodb);
this.userDatabase = new UserDatabase(this, this.mongodb);
this.authenticator = new Authenticator(this, this.app, this.userDatabase, {
mongo: this.mongodb.client,
secret: SECRET,
discordID: discord.id,
discordID: DISCORD_ID,
discordScope: discord.scope,
discordSecret: DISCORD_SECRET,
discordVersion: discord.version,

View File

@ -5,12 +5,12 @@ const { User } = require("../structures");
// MongoDB based user db
class UserDatabase extends AbstractUserDatabase {
constructor (db, userColl = 'users') {
constructor (server, db, userColl = 'users') {
super();
this.db = db;
this.collectionName = userColl;
this.logger = server.createLogger(this);
this.cache = new Map();
this.collection = null;
}
@ -66,7 +66,9 @@ class UserDatabase extends AbstractUserDatabase {
let user = this.cache.get(data._id);
if (!data) {
user = this._createUser({ discord: profile });
this.logger.info(`Creating new user from Discord profile: ${profile.username} (${profile.id})`);
user = this._createUser({ type: 'user', name: profile.username });
user.addExternalProfile('discord', profile);
await user.save();
this.cache.set(user.id, user);
}
@ -74,6 +76,21 @@ class UserDatabase extends AbstractUserDatabase {
return user;
}
/**
* Updates user entry
*
* @param {*} user
* @memberof UserDatabase
*/
async updateUser (user) {
if (user.id) await this.collection.updateOne({ _id: ObjectId(user.id) }, user.json);
else {
const result = await this.collection.insertOne(user.json);
user.id = result.insertedId;
}
return user;
}
_createUser (data) {
return new User(this, data);
}

View File

@ -4,17 +4,55 @@ class User {
constructor (db, data) {
this.id = data._id;
this._db = db;
this.id = data._id || null;
if (!data.name) throw new Error('Missing name for user');
this.name = data.name;
if (!data.type) throw new Error('Missing type for user');
this.type = data.type;
this.externalProfiles = data.externalProfiles;
this.permissions = { ...User.defaultPermissions, ...data.permissions };
}
/**
* Added as a method in case any kind of validation or whatever is needed
*
* @param {string} platform
* @param {object} profile
* @memberof User
*/
addExternalProfile (platform, profile) {
this.externalProfiles[platform] = profile;
}
/**
*
*
* @param {string} perm Permission to check for
* @return {boolean}
* @memberof User
*/
hasPermission (perm) {
return this.permissions[perm];
}
save () {
return this._db.updateUser(this);
}
get json () {
return {
id: this.id,
name: this.name,
type: this.type,
permissions: this.permissions,
externalProfiles: this.externalProfiles
};
}
}