Skip to content

Commit

Permalink
Fixed a crit bug in permissions checkers. Added updateGuildConf method
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaaz committed Aug 12, 2018
1 parent 47315d5 commit 280d21a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
64 changes: 40 additions & 24 deletions src/AxonClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,27 +810,6 @@ class AxonClient {
return guildConf.modules.find(m => m === command.module.label);
}

/**
* Get guildConfig from cache || DB
* Cache it if not cached
*
* @param {String} gID
* @returns {Promise} guildConf object fetched
* @memberof AxonClient
*/
async getGuildConf(gID) {
let guildConf = this.guildConfigs.get(gID);
if (!guildConf) {
try {
guildConf = await this.fetchGuildConf(gID); // retrieve DB get guildConf
} catch (err) {
throw new AxonCommandError('OnMessage', 'DB ERROR - guildConfig', `Guild: ${gID}`, err);
}
this.guildConfigs.set(gID, guildConf);
}
return guildConf;
}

//
// ****** DATABASE ******
// initialisation/fetch
Expand Down Expand Up @@ -945,6 +924,43 @@ class AxonClient {
return command;
}

/**
* Get guildConfig from cache || DB
* Cache it if not cached
*
* @param {String} gID
* @returns {Promise} guildConf object fetched
* @memberof AxonClient
*/
async getGuildConf(gID) {
let guildConf = this.guildConfigs.get(gID);
if (!guildConf) {
try {
guildConf = await this.fetchGuildConf(gID); // retrieve DB get guildConf
} catch (err) {
throw new AxonCommandError('OnMessage', 'DB ERROR - guildConfig', `Guild: ${gID}`, err);
}
this.guildConfigs.set(gID, guildConf);
}
return guildConf;
}

/**
* Update guild Conf in cache + DB
* Uses Json Object directly. (Client cache leaned Guild schema)
*
* @param {String} gID - Guild id
* @param {Object} guildSchema - Guild schema Object
* @returns {Promise} Updated guildSchema
* @memberof AxonClient
*/
updateGuildConf(gID, guildSchema) {
return this.DBprovider.saveGuildSchema(gID, guildSchema)
.then(() => {
this.guildConfigs.set(gID, guildSchema);
});
}

//
// ****** EXTERNAL ******
//
Expand Down Expand Up @@ -1037,7 +1053,7 @@ class AxonClient {
async updateStateModule(guildID, label, boolean) {
let conf;
try {
conf = this.guildConfigs.get(guildID) || this.fetchGuildConf(guildID); // get from cache or from DB if not found
conf = await this.getGuildConf(guildID); // get from cache or from DB if not found
} catch (err) {
throw err;
}
Expand Down Expand Up @@ -1068,7 +1084,7 @@ class AxonClient {
async updateStateCommand(guildID, label, boolean) {
let conf;
try {
conf = this.guildConfigs.get(guildID) || this.fetchGuildConf(guildID); // get from cache or from DB if not found
conf = await this.getGuildConf(guildID); // get from cache or from DB if not found
} catch (err) {
throw err;
}
Expand Down Expand Up @@ -1099,7 +1115,7 @@ class AxonClient {
async updateStateEvent(guildID, label, boolean) {
let conf;
try {
conf = this.guildConfigs.get(guildID) || this.fetchGuildConf(guildID); // get from cache or from DB if not found
conf = await this.getGuildConf(guildID); // get from cache or from DB if not found
} catch (err) {
throw err;
}
Expand Down
34 changes: 16 additions & 18 deletions src/Structures/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,35 +212,26 @@ class Command extends Base {
_execute(message) {
const { msg, args, guildConf } = message;

/** Test for Mod Only / serverMod command | serverAdmin command */
if (((guildConf.modOnly || this.permissions.serverMod) && !this.AxonUtils.isMod(msg.member, guildConf)) || (this.permissions.serverAdmin && !this.AxonUtils.isAdmin(msg.member))) {
/** Sends invalid perm message in case of invalid perm [option enabled] */
if (!guildConf.modOnly && this.options.invalidPermissionMessage) { // doesn't send back invalid perm if mod Only
return this.sendUserPerms(msg.channel);
}
return Promise.resolve();
}

/** Test for bot permissions */
if (!this._checkPermsBot(msg)) {
return this.sendBotPerms(msg.channel);
}

/** Test for Cooldown - Send Cooldown message */
const cd = this._shouldCooldown(msg);
if (typeof (cd) === 'number') {
return this.sendCooldown(msg.channel, cd);
}

/** Permissions checkers */
if (!this.canExecute(msg)) {
if (!this.canExecute(msg, guildConf)) {
/** Sends invalid perm message in case of invalid perm [option enabled] */
if (this.options.invalidPermissionMessage) {
if (!guildConf.modOnly && this.options.invalidPermissionMessage) {
return this.sendUserPerms(msg.channel);
}
return Promise.resolve();
}

/** Test for Cooldown - Send Cooldown message */
const cd = this._shouldCooldown(msg);
if (typeof (cd) === 'number') {
return this.sendCooldown(msg.channel, cd);
}

if (this.options.deleteCommand) { // delete input
msg.delete();
}
Expand Down Expand Up @@ -373,12 +364,14 @@ class Command extends Base {
* Permission checker - Does the user has perm to exec command/not
* Bypass - one of the perms (override) => doesn't go through others chercker
* Needed - all perms => still go through other checkers
* ServerMod
*
* @param {Object<Message>} msg - Message Object
* @param {Object} guildConf - Guild Config
* @returns {Boolean} true: user can execute command
* @memberof Command
*/
canExecute(msg) {
canExecute(msg, guildConf) {
/** Bypass: if one of the perm is true => Exec the command */
if (this._checkPermsUserBypass(msg)
|| this._checkUserBypass(msg)
Expand All @@ -388,6 +381,11 @@ class Command extends Base {
return true;
}

if (((guildConf.modOnly || this.permissions.serverMod) && !this.AxonUtils.isMod(msg.member, guildConf))
|| (this.permissions.serverAdmin && !this.AxonUtils.isAdmin(msg.member))) {
return false;
}

/** Needed: if one of the perms is false => doesn't exec the command */
if (!this._checkPermsUserNeeded(msg)
|| !this._checkUserNeeded(msg)
Expand Down

0 comments on commit 280d21a

Please sign in to comment.