saving users
This commit is contained in:
parent
5842448f1e
commit
05503ade05
@ -30,7 +30,7 @@ class Server extends EventEmitter {
|
|||||||
|
|
||||||
const { MARIA_HOST, MARIA_USER, MARIA_PORT, MARIA_PASS, MARIA_DB,
|
const { MARIA_HOST, MARIA_USER, MARIA_PORT, MARIA_PASS, MARIA_DB,
|
||||||
MONGO_HOST, MONGO_USER, MONGO_PORT, MONGO_PASS, MONGO_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;
|
const { discord, http: httpOpts, databases } = options;
|
||||||
|
|
||||||
this._options = 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.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.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, {
|
this.authenticator = new Authenticator(this, this.app, this.userDatabase, {
|
||||||
mongo: this.mongodb.client,
|
mongo: this.mongodb.client,
|
||||||
secret: SECRET,
|
secret: SECRET,
|
||||||
discordID: discord.id,
|
discordID: DISCORD_ID,
|
||||||
discordScope: discord.scope,
|
discordScope: discord.scope,
|
||||||
discordSecret: DISCORD_SECRET,
|
discordSecret: DISCORD_SECRET,
|
||||||
discordVersion: discord.version,
|
discordVersion: discord.version,
|
||||||
|
@ -5,12 +5,12 @@ const { User } = require("../structures");
|
|||||||
// MongoDB based user db
|
// MongoDB based user db
|
||||||
class UserDatabase extends AbstractUserDatabase {
|
class UserDatabase extends AbstractUserDatabase {
|
||||||
|
|
||||||
constructor (db, userColl = 'users') {
|
constructor (server, db, userColl = 'users') {
|
||||||
super();
|
super();
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.collectionName = userColl;
|
this.collectionName = userColl;
|
||||||
|
this.logger = server.createLogger(this);
|
||||||
this.cache = new Map();
|
this.cache = new Map();
|
||||||
|
|
||||||
this.collection = null;
|
this.collection = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -66,7 +66,9 @@ class UserDatabase extends AbstractUserDatabase {
|
|||||||
let user = this.cache.get(data._id);
|
let user = this.cache.get(data._id);
|
||||||
|
|
||||||
if (!data) {
|
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();
|
await user.save();
|
||||||
this.cache.set(user.id, user);
|
this.cache.set(user.id, user);
|
||||||
}
|
}
|
||||||
@ -74,6 +76,21 @@ class UserDatabase extends AbstractUserDatabase {
|
|||||||
return user;
|
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) {
|
_createUser (data) {
|
||||||
return new User(this, data);
|
return new User(this, data);
|
||||||
}
|
}
|
||||||
|
@ -4,17 +4,55 @@ class User {
|
|||||||
|
|
||||||
constructor (db, data) {
|
constructor (db, data) {
|
||||||
|
|
||||||
this.id = data._id;
|
|
||||||
this._db = db;
|
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 };
|
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) {
|
hasPermission (perm) {
|
||||||
|
|
||||||
return this.permissions[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
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user