Skip to content

Commit

Permalink
added api guild perm support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon1320 committed Oct 11, 2018
1 parent e262037 commit 48c8175
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/db/schemas/guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ const guildScriptSchema = new Schema({
_id: false
});

const memberPermSchema = new Schema({

member_id: { type: String, required: true },
allow_list: { type: Boolean, default: true },
list: [ String ]
}, {
_id: false
});

const GuildSchema = new Schema({

// Id generated by mongo, this is here so I remember it exists.
// _id: ObjectId,

discord_id: { type: String, required: true, unique: true },
prefix: { type: String, default: "-" },
log_channel_id: { type: String, default: null },
log_events: [ String ],
member_perms: [ memberPermSchema ],
scripts: [ guildScriptSchema ],
});

Expand Down
16 changes: 7 additions & 9 deletions src/web/routes/api/guilds.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ router.route("/").post(authAdmin, (req, res) => {
const params = {};
params.discord_id = req.body.discord_id;
params.prefix = req.body.prefix;
params.log_channel_id = req.body.log_channel_id;
params.log_events = req.body.log_events;
params.member_perms = req.body.member_perms;
params.scripts = [];

const guild = new schemas.GuildSchema(params);
Expand Down Expand Up @@ -237,8 +236,7 @@ router.route("/:discord_id").get(authUser, (req, res) => {

const params = {};
params.prefix = req.body.prefix;
params.log_channel_id = req.body.log_channel_id;
params.log_events = req.body.log_events;
params.member_perms = req.body.member_perms;

schemas.GuildSchema
.findOne({
Expand All @@ -258,7 +256,7 @@ router.route("/:discord_id").get(authUser, (req, res) => {
return e.id === req.params.discord_id;
});

if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000))) {
if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000) && current_guild.member_perms.includes("patch_guild") === false)) {

return res.json({ status: 403 });
}
Expand Down Expand Up @@ -307,7 +305,7 @@ router.route("/:discord_id").get(authUser, (req, res) => {
return e.id === req.params.discord_id;
});

if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000))) {
if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000) && current_guild.member_perms.includes("delete_guild") === false)) {

return res.json({ status: 403 });
}
Expand Down Expand Up @@ -510,7 +508,7 @@ router.route("/:discord_id/scripts").get(authUser, (req, res) => {
return e.id === req.params.discord_id;
});

if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000))) {
if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000) && current_guild.member_perms.includes("post_script") === false)) {

return res.json({ status: 403 });
}
Expand Down Expand Up @@ -634,7 +632,7 @@ router.route("/:discord_id/scripts/:object_id").get(authUser, (req, res) => {
return e.id === req.params.discord_id;
});

if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000))) {
if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000) && current_guild.member_perms.includes("patch_script") === false)) {

return res.json({ status: 403 });
}
Expand Down Expand Up @@ -726,7 +724,7 @@ router.route("/:discord_id/scripts/:object_id").get(authUser, (req, res) => {
return e.id === req.params.discord_id;
});

if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000))) {
if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000) && current_guild.member_perms.includes("delete_script") === false)) {

return res.json({ status: 403 });
}
Expand Down
16 changes: 16 additions & 0 deletions src/web/templates/dashboard/marketplace.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,14 @@
throw `failed to parse script add json with error: ${error}`;
}
if (scriptAddYay.status !== 200) {
toastr["error"](`failed to add script: ${scriptAddYay.response.status}`);
throw `failed to add script: ${scriptAddYay.response.status}`;
return;
}
btn.classList.remove("add-script");
btn.classList.remove("blue");
btn.classList.remove("lighten-2");
Expand Down Expand Up @@ -898,6 +906,14 @@
throw `failed to parse script remove json with error: ${error}`;
}
if (scriptRemoveYay.status !== 200) {
toastr["error"](`failed to remove script: ${scriptRemoveYay.response.status}`);
throw `failed to remove script: ${scriptRemoveYay.response.status}`;
return;
}
btn.classList.remove("remove-script");
btn.classList.remove("red");
btn.classList.remove("lighten-1");
Expand Down

0 comments on commit 48c8175

Please sign in to comment.