From 99fb8213db758cc457b0c2f9f5c8b5f213102c7a Mon Sep 17 00:00:00 2001 From: Chris Brame Date: Wed, 31 Oct 2018 00:37:12 -0400 Subject: [PATCH] chore(roles): role orders --- src/models/role.js | 68 ++++++++---------------------------- src/models/roleorder.js | 32 +++++++++++++++++ src/settings/settingsUtil.js | 21 +++++++++++ 3 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 src/models/roleorder.js diff --git a/src/models/role.js b/src/models/role.js index b7a648686..5a627f004 100644 --- a/src/models/role.js +++ b/src/models/role.js @@ -7,81 +7,43 @@ 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b. "888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o ======================================================================== - Created: 12/28/2015 + Created: 10/28/2018 Author: Chris Brame **/ var mongoose = require('mongoose'); -var COLLECTION = 'tags'; +var COLLECTION = 'roles'; -/** - * Tag Schema - * @module models/tag - * @class Tag - - * - * @property {object} _id ```Required``` ```unique``` MongoDB Object ID - * @property {String} name ```Required``` ```unique``` Name of Tag - */ -var tagSchema = mongoose.Schema({ - name: { type: String, required: true, unique: true }, - normalized: String +var roleSchema = mongoose.Schema({ + name: { type: String, required: true, unique: true }, + normalized: String, + description: String, + grants: { type: Object, required: true, default: {} } }); -tagSchema.pre('save', function(next) { +roleSchema.pre('save', function(next) { this.name = this.name.trim(); this.normalized = this.name.toLowerCase().trim(); return next(); }); -tagSchema.statics.getTag = function(id, callback) { - var q = this.model(COLLECTION).findOne({_id: id}); - - return q.exec(callback); -}; - -/** - * Return all Tags - * - * @memberof Tag - * @static - * @method getTags - * - * @param {QueryCallback} callback MongoDB Query Callback - */ -tagSchema.statics.getTags = function(callback) { - var q = this.model(COLLECTION).find({}).sort('normalized'); - - return q.exec(callback); +roleSchema.statics.getRoles = function(callback) { + return this.model(COLLECTION).find({}).exec(callback); }; -tagSchema.statics.getTagsWithLimit = function(limit, page, callback) { - return this.model(COLLECTION).find({}) - .limit(limit) - .skip(page*limit) - .sort('normalized') - .exec(callback); -}; - -tagSchema.statics.getTagByName = function(tagName, callback) { - var q = this.model(COLLECTION).find({name: tagName}).limit(1); - - return q.exec(callback); -}; - -tagSchema.statics.tagExist = function(tagName, callback) { - var q = this.model(COLLECTION).countDocuments({name: tagName}); +roleSchema.statics.getRole = function(id, callback) { + var q = this.model(COLLECTION).findOne({_id: id}); return q.exec(callback); }; -tagSchema.statics.getTagCount = function(callback) { - var q = this.model(COLLECTION).countDocuments({}).lean(); +roleSchema.statics.getRoleByName = function(name, callback) { + var q = this.model(COLLECTION).findOne({ normalized: new RegExp('^' + name.trim() + '$', 'i') }); return q.exec(callback); }; -module.exports = mongoose.model(COLLECTION, tagSchema); \ No newline at end of file +module.exports = mongoose.model(COLLECTION, roleSchema); \ No newline at end of file diff --git a/src/models/roleorder.js b/src/models/roleorder.js new file mode 100644 index 000000000..0ad5f07a4 --- /dev/null +++ b/src/models/roleorder.js @@ -0,0 +1,32 @@ +/* + . .o8 oooo + .o8 "888 `888 + .o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo + 888 `888""8P `888 `888 d88' `888 d88' `88b d88( "8 888 .8P' + 888 888 888 888 888 888 888ooo888 `"Y88b. 888888. + 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b. + "888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o + ======================================================================== + Created: 10/30/2018 + Author: Chris Brame + + **/ + +var mongoose = require('mongoose'); + +var COLLECTION = 'role_order'; + +var roleOrder = mongoose.Schema({ + order: [ mongoose.Schema.Types.ObjectId ] +}); + +roleOrder.statics.getOrder = function(callback) { + return this.model(COLLECTION).findOne({}).exec(callback); +}; + +roleOrder.methods.updateOrder = function(order, callback) { + this.order = order; + this.save(callback); +}; + +module.exports = mongoose.model(COLLECTION, roleOrder, COLLECTION); \ No newline at end of file diff --git a/src/settings/settingsUtil.js b/src/settings/settingsUtil.js index e37319b29..a4f733291 100644 --- a/src/settings/settingsUtil.js +++ b/src/settings/settingsUtil.js @@ -156,6 +156,27 @@ util.getSettings = function (callback) { return done() }) + }, + function (done) { + var roleSchema = require('../models/role') + var roleOrderSchema = require('../models/roleorder') + roleSchema.getRoles(function (err, roles) { + if (err) return done(err) + roleOrderSchema.getOrder(function (err, roleOrder) { + if (err) return done(err) + roleOrder = roleOrder.order + + if (_.size(roleOrder) > 0) { + var arr = _.map(roleOrder, function (roID) { + return _.find(roles, { _id: roID }) + }) + + content.data.roles = arr + } else content.data.roles = roles + + return done() + }) + }) } ], function (err) {