Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sails-adminpanel

Package Overview
Dependencies
Maintainers
1
Versions
189
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sails-adminpanel - npm Package Compare versions

Comparing version 1.2.13 to 2.0.0-pre.9

.github/workflows/dev.yml

51

docs/Menu.md

@@ -56,25 +56,7 @@ # Menu configuration

## Menu groups
## Menu
To group your menues you can use `groups` key in `menu` configuration.
Key `groups` - is an array of `objects`. Every object represent one menu group. Is will be a droupdown menu item.
```javascript
module.exports.adminpanel = {
menu: {
// List of menu groups
groups: [
{
key: 'users',
title: 'Users'
},
{
key: 'content',
title: 'Content items'
}
]
},
// ...
instances: {

@@ -102,3 +84,3 @@ users: {

This configuratio will create a 2 dropdown menu items.
This configuration will create a 2 dropdown menu items.

@@ -108,23 +90,9 @@ ## Menu icons

For now only [Glyphicons](http://getbootstrap.com/components/#glyphicons) icons are supported.
For now only [Line awesome](https://icons8.com/line-awesome) icons are supported.
You can set icon to menu item/group using `icon` key in config file.
But you should use icon class without `glyphicon-` prefix. **It will be added automatically !**
But you should use icon class without `la-` prefix. **It will be added automatically !**
```javascript
module.exports.adminpanel = {
menu: {
groups: [
{
key: 'content',
title: 'Content',
icon: 'inbox' // glyphicon-ibox will be set to menu item.
}
]
}
};
```
```javascript
module.exports.adminpanel = {
instances: {

@@ -149,8 +117,2 @@ user: {

menu: {
groups: [
{
key: 'customers',
title: 'Customer actions'
}
],
actions: [

@@ -164,4 +126,3 @@ {

link: '/some/action',
title: 'Some new action',
menuGroup: 'customers' // add to customer menu dropdown
title: 'Some new action'
}

@@ -168,0 +129,0 @@ ]

@@ -16,3 +16,3 @@ # Navigation editor

type: "menu",
opts: {
options: {
maxNestedItems: number,

@@ -19,0 +19,0 @@ displacementControl: boolean,

@@ -13,3 +13,4 @@ # Schedule editor

type: "worktime",
opts: {
options: {
supportOldVersion: true | false,
propertyList: {},

@@ -64,3 +65,8 @@ permutations: {}

```
####Support old version
Old version does not have dateStart or dateStop, timeStart or timeStop.
It only has start and stop fields, which means that tou can only set
timeStart and timeStop

@@ -1,73 +0,60 @@

'use strict';
var _ = require('lodash');
module.exports = function(sails) {
var config = sails.config.adminpanel;
var ConfigHelper = {
/**
* Checks if given field is identifier of model
*
* @param {Object} field
* @param {Object|string=} modelOrName
* @returns {boolean}
*/
getId: function(modelOrName) {
return (field.config.key == this.getIdentifierField(modelOrName));
},
isId: function(field, modelOrName) {
return (field.config.key == this.getIdentifierField(modelOrName));
},
/**
* Get configured `identifierField` from adminpanel configuration.
*
* If not configured and model passed try to guess it using `primaryKey` field in model.
* If system couldn't guess will return 'id`.
* Model could be object or just name (string).
*
* **Warning** If you will pass record - method will return 'id'
*
* @param {Object|string=} [model]
* @returns {string}
*/
getConfig: function() {
return sails.config.adminpanel
},
getIdentifierField: function(modelName) {
if (!modelName) {
throw new Error("Model name is not defined");
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigHelper = void 0;
class ConfigHelper {
static getConfig() {
return sails.config.adminpanel;
}
/**
* Checks if given field is identifier of model
*
* @param {Object} field
* @param {Object|string=} modelOrName
* @returns {boolean}
*/
static isId(field, modelOrName) {
return (field.config.key == this.getIdentifierField(modelOrName));
}
/**
* Get configured `identifierField` from adminpanel configuration.
*
* If not configured and model passed try to guess it using `primaryKey` field in model.
* If system couldn't guess will return 'id`.
* Model could be object or just name (string).
*
* **Warning** If you will pass record - method will return 'id'
*
* @returns {string}
* @param modelName
*/
static getIdentifierField(modelName) {
if (!modelName) {
throw new Error("Model name is not defined");
}
let config = sails.config.adminpanel;
let instanceConfig;
Object.keys(config.instances).forEach((instanceName) => {
if (config.instances[instanceName].model === modelName.toLowerCase()) {
instanceConfig = config.instances[instanceName];
}
let config = sails.config.adminpanel;
let instanceConfig;
Object.keys(config.instances).forEach((instanceName) => {
if (config.instances[instanceName].model === modelName.toLowerCase()) {
instanceConfig = config.instances[instanceName];
}
});
if (instanceConfig && instanceConfig.identifierField) {
return instanceConfig.identifierField;
}
else if (sails.models[modelName.toLowerCase()].primaryKey) {
return sails.models[modelName.toLowerCase()].primaryKey;
}
else {
throw new Error("ConfigHelper > Identifier field was not found");
}
},
/**
* Checks if CSRF protection enabled in website
*
* @returns {boolean}
*/
isCsrfEnabled: function() {
return (sails.config.security.csrf !== false);
});
if (instanceConfig && instanceConfig.identifierField) {
return instanceConfig.identifierField;
}
};
return ConfigHelper;
};
else if (sails.models[modelName.toLowerCase()].primaryKey) {
return sails.models[modelName.toLowerCase()].primaryKey;
}
else {
throw new Error("ConfigHelper > Identifier field was not found");
}
}
/**
* Checks if CSRF protection enabled in website
*
* @returns {boolean}
*/
static isCsrfEnabled() {
return (sails.config.security.csrf !== false);
}
}
exports.ConfigHelper = ConfigHelper;

@@ -1,3 +0,125 @@

declare var util: any;
declare var async: any;
declare var _: any;
export declare class FieldsHelper {
/**
* Will normalize a field configuration that will be loaded from config file.
*
* Input parameters should be different.
*
* For now AdminPanel hook supports such notations into field configurations:
*
* + Boolean notation
*
* ```
* fieldName: true // will enable field showing/editing
* fieldName: false // will remove field from showing. Could be useful for actions like edit
* ```
*
* + String notation
*
* ```
* fieldName: "Field title"
* ```
*
* + Object notation
*
* ```
* fieldName: {
* title: "Field title", // You can overwrite field title
* type: "string", //you can overwrite default field type in admin panel
* required: true, // you can mark field required or not
* editor: true, // you can add WYSTYG editor for the field in admin panel
* }
* ```
*
* There are several places for field config definition and an inheritance of field configs.
*
* 1. You could use a global `fields` property into `config/adminpanel.js` file into `instances` section.
* 2. You could use `fields` property into `instances:action` configuration. This config will overwrite global one
*
* ```
* module.exports.adminpanel = {
* instances: {
* users: {
* title: 'Users', //Menu title for instance
* model: 'User', // Model definition for instance
*
* fields: {
* email: 'User Email', //it will define title for this field in all actions (list/add/edit/view)
* createdAt: false, // Will hide createdAt field in all actions
* bio: {
* title: 'User bio',
* editor: true
* } // will set title `User bio` for the field and add editor into add/edit actions
* },
* // Action level config
* list: {
* bio: false // will hide bio field into list view
* },
*
* edit: {
* createdAt: 'Created at' //will enable field `createdAt` and set title to `Created at`
* }
* }
* }
* }
* ```
*
* @example
*
* //default field config should look like:
* var fieldConfig = {
* key: 'fieldKeyFromModel'
* title: "Field title",
* type: "string", //Or any other type. Will be fetched from model if not defined in config
* // ... Other config will be added here
* };
*
* @throws {Error} if no config or key passed
* @param {*} config
* @param {string} key
* @param modelField
* @returns {boolean|Object}
* @private
*/
private static _normalizeFieldConfig;
/**
* Load list of records for all associations into `fields`
*
* @param {Object} fields
* @param {function=} [cb]
*/
static loadAssociations(fields: any): Promise<any>;
/**
* Create list of populated models
*
* @param {Object} fields
* @returns {Array}
*/
static getFieldsToPopulate(fields: any): any[];
/**
* Basically it will fetch all attributes without functions
*
* Result will be object with list of fields and its config.<br/>
* <code>
* {
* "fieldName": {
* config: {
* key: 'fieldKeyFromModel'
* title: "Field title",
* type: "string", //Or any other type. Will be fetched from model if not defined in config
* // ... Other config will be added here
* },
* model: {
* // Here will be list of properties from your model
* type: 'string' //...
* }
* }
* }
* </code>
*
* @param {Request} req Sails.js req object
* @param {Object} instance Instance object with `name`, `config`, `model` {@link AdminUtil.findInstanceObject}
* @param {string=} [type] Type of action that config should be loaded for. Example: list, edit, add, remove, view. Defaut: list
* @returns {Object} Empty object or pbject with list of properties
*/
static getFields(req: any, instance: any, type: any): {};
}

@@ -1,8 +0,8 @@

'use strict';
var util = require('../lib/adminUtil');
var async = require('async');
var _ = require('lodash');
module.exports = {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldsHelper = void 0;
const adminUtil_1 = require("../lib/adminUtil");
class FieldsHelper {
/**
* Will normalize a field configuration that willl be loaded fro config file.
* Will normalize a field configuration that will be loaded from config file.
*

@@ -17,9 +17,9 @@ * Input parameters should be different.

* fieldName: true // will enable field showing/editing
* fieldName: false // will remove field from showing. Could be usefull for actions like edit
* fieldName: false // will remove field from showing. Could be useful for actions like edit
* ```
*
* + String natation
* + String notation
*
* ```
* fieldName: "Field Ttitle"
* fieldName: "Field title"
* ```

@@ -41,3 +41,3 @@ *

* 1. You could use a global `fields` property into `config/adminpanel.js` file into `instances` section.
* 2. You could use `fields` property into `instances:action` confguration. This config will overwrite global one
* 2. You could use `fields` property into `instances:action` configuration. This config will overwrite global one
*

@@ -74,3 +74,3 @@ * ```

*
* //default field config should llok like:
* //default field config should look like:
* var fieldConfig = {

@@ -86,7 +86,8 @@ * key: 'fieldKeyFromModel'

* @param {string} key
* @param modelField
* @returns {boolean|Object}
* @private
*/
_normalizeFieldConfig: function (config, key, modelField) {
if (_.isUndefined(config) || _.isUndefined(key)) {
static _normalizeFieldConfig(config, key, modelField) {
if (typeof config === "undefined" || typeof key === "undefined") {
throw new Error('No `config` or `key` passed !');

@@ -97,3 +98,3 @@ }

*/
if (_.isBoolean(config)) {
if (typeof config === "boolean") {
if (!config) {

@@ -110,3 +111,3 @@ return false;

// check for string notation
if (_.isString(config)) {
if (typeof config === "string") {
return {

@@ -117,4 +118,4 @@ key: key,

}
//check for object natation
if (_.isPlainObject(config)) {
//check for object notation
if (typeof config === "object") {
// make required checks

@@ -130,7 +131,7 @@ if (!config.key) {

if (config.type === 'association' || config.type === 'association-many') {
var associatedModelAtrubutes = {};
var displayField;
let associatedModelAttributes = {};
let displayField;
if (config.type === 'association') {
try {
associatedModelAtrubutes = util.getModel(modelField.model.toLowerCase()).attributes;
associatedModelAttributes = adminUtil_1.AdminUtil.getModel(modelField.model.toLowerCase()).attributes;
}

@@ -143,4 +144,4 @@ catch (e) {

try {
// console.log('admin > helper > collection > ', util.getModel(modelField.collection.toLowerCase()).attributes);
associatedModelAtrubutes = util.getModel(modelField.collection.toLowerCase()).attributes;
// console.log('admin > helper > collection > ', AdminUtil.getModel(modelField.collection.toLowerCase()).attributes);
associatedModelAttributes = adminUtil_1.AdminUtil.getModel(modelField.collection.toLowerCase()).attributes;
}

@@ -151,7 +152,7 @@ catch (e) {

}
// console.log('admin > helper > model > ', associatedModelAtrubutes);
if (associatedModelAtrubutes.hasOwnProperty('name')) {
// console.log('admin > helper > model > ', associatedModelAttributes);
if (associatedModelAttributes.hasOwnProperty('name')) {
displayField = 'name';
}
else if (associatedModelAtrubutes.hasOwnProperty('label')) {
else if (associatedModelAttributes.hasOwnProperty('label')) {
displayField = 'label';

@@ -162,3 +163,3 @@ }

}
_.defaults(config, {
config = Object.assign(config, {
identifierField: 'id',

@@ -171,3 +172,3 @@ displayField: displayField

return false;
},
}
/**

@@ -179,4 +180,3 @@ * Load list of records for all associations into `fields`

*/
loadAssociations: function (fields, cb) {
cb = cb || function () { };
static async loadAssociations(fields) {
/**

@@ -188,31 +188,36 @@ * Load all associated records for given field key

*/
var loadAssoc = function (key, cb) {
let loadAssoc = async function (key) {
if (fields[key].config.type !== 'association' && fields[key].config.type !== 'association-many') {
return cb();
return;
}
fields[key].config.records = [];
var modelName = fields[key].model.model || fields[key].model.collection;
let modelName = fields[key].model.model || fields[key].model.collection;
if (!modelName) {
sails.log.error('No model found for field: ', fields[key]);
return cb();
return;
}
var Model = util.getModel(modelName);
let Model = adminUtil_1.AdminUtil.getModel(modelName);
if (!Model) {
return cb();
return;
}
Model.find().exec(function (err, list) {
if (err) {
return cb();
}
fields[key].config.records = list;
cb();
});
let list;
try {
list = await Model.find();
}
catch (e) {
throw new Error("FieldsHelper > Models not found");
}
fields[key].config.records = list;
};
async.each(_.keys(fields), loadAssoc, function (err) {
if (err) {
return cb(err);
for await (let key of Object.keys(fields)) {
try {
await loadAssoc(key);
}
return cb(null, fields);
});
},
catch (e) {
sails.log.error(e);
return e;
}
}
return fields;
}
/**

@@ -224,5 +229,5 @@ * Create list of populated models

*/
getFieldsToPopulate: function (fields) {
var result = [];
_.forEach(fields, function (field, key) {
static getFieldsToPopulate(fields) {
let result = [];
Object.entries(fields).forEach(function ([key, field]) {
if (field.config.type === 'association' || field.config.type === 'association-many') {

@@ -233,5 +238,5 @@ result.push(key);

return result;
},
}
/**
* Basicaly it will fetch all attributes without functions
* Basically it will fetch all attributes without functions
*

@@ -261,3 +266,3 @@ * Result will be object with list of fields and its config.<br/>

*/
getFields: function (req, instance, type) {
static getFields(req, instance, type) {
if (!instance.model || !instance.model.attributes) {

@@ -269,11 +274,9 @@ return {};

//get field config for actions
var actionConfig = util.findActionConfig(instance, type);
var fieldsConfig = instance.config.fields || {};
let actionConfig = adminUtil_1.AdminUtil.findActionConfig(instance, type);
let fieldsConfig = instance.config.fields || {};
//Get keys from config
//var actionConfigFields = _.keys(actionConfig.fields);
//Getting list of fields from model
let modelAttributes = instance.model.attributes
var that = this;
let modelAttributes = instance.model.attributes;
let that = this;
/**

@@ -286,3 +289,3 @@ * Iteration function for every field

*/
var _prepareField = function (modelField, key) {
let _prepareField = function ([key, modelField]) {
/**

@@ -292,3 +295,3 @@ * Checks for short type in waterline:

*/
if (_.isString(modelField)) {
if (typeof modelField === "string") {
modelField = {

@@ -298,6 +301,6 @@ type: modelField

}
if (_.isObject(modelField) && modelField.model) {
if (typeof modelField === "object" && modelField.model) {
modelField.type = 'association';
}
if (_.isObject(modelField) && modelField.collection) {
if (typeof modelField === "object" && modelField.collection) {
modelField.type = 'association-many';

@@ -309,4 +312,4 @@ }

//Getting config form configuration file
var fldConfig = { key: key, title: key };
var ignoreField = false; // if set to true, field will be removed from editor/list
let fldConfig = { key: key, title: key };
let ignoreField = false; // if set to true, field will be removed from editor/list
//Checking global instance fields configuration

@@ -319,4 +322,4 @@ if (fieldsConfig[key] || fieldsConfig[key] === false) {

else {
var tmpCfg = that._normalizeFieldConfig(fieldsConfig[key], key, modelField);
_.merge(fldConfig, tmpCfg);
let tmpCfg = that._normalizeFieldConfig(fieldsConfig[key], key, modelField);
fldConfig = { ...fldConfig, ...tmpCfg };
}

@@ -331,5 +334,5 @@ }

else {
var tmpCfg = that._normalizeFieldConfig(actionConfig.fields[key], key, modelField);
let tmpCfg = that._normalizeFieldConfig(actionConfig.fields[key], key, modelField);
ignoreField = false;
_.merge(fldConfig, tmpCfg);
fldConfig = { ...fldConfig, ...tmpCfg };
}

@@ -349,5 +352,5 @@ }

fldConfig.type = fldConfig.type.toLowerCase();
//nomalizing configs
//normalizing configs
fldConfig = that._normalizeFieldConfig(fldConfig, key, modelField);
//Adding new field to resultset
//Adding new field to result set
result[key] = {

@@ -359,6 +362,7 @@ config: fldConfig,

// creating result
var result = {};
_.forEach(modelAttributes, _prepareField);
let result = {};
Object.entries(modelAttributes).forEach(_prepareField);
return result;
}
};
}
exports.FieldsHelper = FieldsHelper;

@@ -1,228 +0,175 @@

'use strict';
var _ = require('lodash');
/**
* Menu helper
*
* @param {Object} config
* @constructor
*/
module.exports = function menuHelper(config) {
var module = {
/**
* Checks if brand exists
*
* @returns {boolean}
*/
hasBrand: function () {
return Boolean(config.menu && config.menu.brand);
},
/**
* Get menu brand link
*
* @returns {string}
*/
getBrandLink: function() {
if (!config.menu || !config.menu.brand || !_.isObject(config.menu.brand) || !config.menu.brand.link) {
return '/admin';
}
return config.menu.brand.link;
},
/**
* Get menu brand title
*
* @returns {string}
*/
getBrandTitle: function() {
if (!config.menu || !config.menu.brand) {
return 'Sails-adminpanel';
}
if (_.isString(config.menu.brand)) {
return config.menu.brand;
}
if (_.isObject(config.menu.brand) && _.isString(config.menu.brand.title)) {
return config.menu.brand.title;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MenuHelper = void 0;
let _ = require("lodash"); // заменить lodash реджексом
class MenuHelper {
constructor(config) {
MenuHelper.config = config;
}
/**
* Checks if brand exists
*
* @returns {boolean}
*/
static hasBrand() {
return Boolean(this.config.brand && this.config.brand.link);
}
/**
* Get menu brand link
*
* @returns {string}
*/
static getBrandLink() {
if (!this.config.brand || !this.config.brand.link || typeof this.config.brand.link !== "object" ||
!this.config.brand.link.link) {
return '/admin';
}
return this.config.brand.link.link;
}
/**
* Get menu brand title
*
* @returns {string}
*/
getBrandTitle() {
if (!MenuHelper.config.brand || !MenuHelper.config.brand.link) {
return 'Sails-adminpanel';
},
/**
* Check if global actions buttons added to action
*
* @param {Object} instanceConfig
* @param {string=} [action] Defaults to `list`
* @returns {boolean}
*/
hasGlobalActions: function(instanceConfig, action) {
action = action || 'list';
if (!instanceConfig[action] || !instanceConfig[action].actions || !instanceConfig[action].actions.global) {
return false;
}
var actions = instanceConfig[action].actions.global;
if (actions.length > 0) {
return true;
}
}
if (typeof MenuHelper.config.brand.link === "string") {
return MenuHelper.config.brand.link;
}
if (typeof MenuHelper.config.brand.link === "object" && typeof MenuHelper.config.brand.link.title === "string") {
return MenuHelper.config.brand.link.title;
}
return 'Sails-adminpanel';
}
/**
* Check if global actions buttons added to action
*
* @param {Object} instanceConfig
* @param {string=} [action] Defaults to `list`
* @returns {boolean}
*/
hasGlobalActions(instanceConfig, action) {
action = action || 'list';
if (!instanceConfig[action] || !instanceConfig[action].actions || !instanceConfig[action].actions.global) {
return false;
},
/**
* Check if inline actions buttons added to action
*
* @param {Object} instanceConfig
* @param {string=} [action] Defaults to `list`
* @returns {boolean}
*/
hasInlineActions: function(instanceConfig, action) {
action = action || 'list';
if (!instanceConfig[action] || !instanceConfig[action].actions || !instanceConfig[action].actions.inline) {
return false;
}
var actions = instanceConfig[action].actions.inline;
if (actions.length > 0) {
return true;
}
}
let actions = instanceConfig[action].actions.global;
return actions.length > 0;
}
/**
* Check if inline actions buttons added to action
*
* @param {Object} instanceConfig
* @param {string=} [action] Defaults to `list`
* @returns {boolean}
*/
hasInlineActions(instanceConfig, action) {
action = action || 'list';
if (!instanceConfig[action] || !instanceConfig[action].actions || !instanceConfig[action].actions.inline) {
return false;
},
/**
* Get list of custom global buttons for action
*
* @param {Object} instanceConfig
* @param {string=} [action]
* @returns {Array}
*/
getGlobalActions: function(instanceConfig, action) {
action = action || 'list';
if (!this.hasGlobalActions(instanceConfig, action)) {
return [];
}
let actions = instanceConfig[action].actions.inline;
return actions.length > 0;
}
/**
* Get list of custom global buttons for action
*
* @param {Object} instanceConfig
* @param {string=} [action]
* @returns {Array}
*/
getGlobalActions(instanceConfig, action) {
action = action || 'list';
if (!this.hasGlobalActions(instanceConfig, action)) {
return [];
}
return instanceConfig[action].actions.global;
}
/**
* Get list of custom inline buttons for action
*
* @param {Object} instanceConfig
* @param {string=} [action]
* @returns {Array}
*/
getInlineActions(instanceConfig, action) {
action = action || 'list';
if (!this.hasInlineActions(instanceConfig, action)) {
return [];
}
return instanceConfig[action].actions.inline;
}
/**
* Replace fields in given URL and binds to model fields.
*
* URL can contain different properties from given model in such notation `:propertyName`.
* If model wouldn't have such property it will be left as `:varName`
*
* @param {string} url URL with list of variables to replace '/admin/test/:id/:title/'
* @param {Object} model
* @returns {string}
*/
static replaceModelFields(url, model) {
let words = (str, pat) => {
pat = pat || /\w+/g;
str = str.toLowerCase();
return str.match(pat);
};
// Check for model existence
if (!model) {
return url;
}
let split = words(url, /\:+[a-z\-_]*/gi);
// Replacing props
split.forEach(function (word) {
let variable = word.replace(':', '');
if (model && model[variable]) {
url = url.replace(word, model[variable]);
}
return instanceConfig[action].actions.global;
},
/**
* Get list of custom inline buttons for action
*
* @param {Object} instanceConfig
* @param {string=} [action]
* @returns {Array}
*/
getInlineActions: function(instanceConfig, action) {
action = action || 'list';
if (!this.hasInlineActions(instanceConfig, action)) {
return [];
});
return url;
}
/**
* Get list of instance menus that was not bound to groups
*
* @returns {Array}
*/
getMenuItems() {
let menus = [];
Object.entries(MenuHelper.config.instances).forEach(function ([key, val]) {
if (val.tools && val.tools.length > 0 && val.tools[0].id !== "overview") {
val.tools.unshift({
id: "overview",
link: MenuHelper.config.routePrefix + '/' + key,
title: 'Overview',
icon: ""
});
}
return instanceConfig[action].actions.inline;
},
/**
* Replace fields in given URL and binds to model fields.
*
* URL can contain different properties from given model in such notation `:propertyName`.
* If model wouldn't have such property it will be left as `:varName`
*
* @param {string} url URL with list of variables to replace '/admin/test/:id/:title/'
* @param {Object} model
* @returns {string}
*/
replaceModelFields: function(url, model) {
// Check for model existance
if (!model) {
return url;
}
var words = _.words(url, /\:+[a-z\-_]*/gi);
// Replacing props
_.forEach(words, function(word) {
var variable = word.replace(':', '');
if (model && model[variable]) {
url = url.replace(word, model[variable]);
}
menus.push({
link: MenuHelper.config.routePrefix + '/' + key,
title: val.title,
icon: val.icon || null,
actions: val.tools || null,
id: val.title.replace(" ", "_"),
instanceName: key
});
return url;
},
/**
* Will create a list of groups to show
*
* @returns {Array}
*/
getGroups: function () {
var groups = config.menu.groups || [];
_.forEach(groups, function(group, idx) {
if (!group.key) return;
// Clear menues to avoid data duplication
groups[idx].menues = [];
_.forEach(config.instances, function(val, key) {
if (val.menuGroup && val.menuGroup == group.key) {
groups[idx].menues.push({
link: config.routePrefix + '/' + key,
title: val.title,
icon: val.icon || null
});
}
});
if (config.menu.actions && config.menu.actions.length > 0) {
_.forEach(config.menu.actions, function(menu) {
if (!menu.link || !menu.title || !menu.menuGroup || menu.menuGroup != group.key) {
return;
}
groups[idx].menues.push({
link: menu.link,
title: menu.title,
icon: menu.icon || null
});
});
}
});
return groups;
},
/**
* Get list of instance menues that was not binded to groups
*
* @returns {Array}
*/
getMenuItems: function() {
var menues = [];
_.forEach(config.instances, function(val, key) {
if (val.menuGroup) {
});
if (MenuHelper.config.navbar.additionalLinks && MenuHelper.config.navbar.additionalLinks.length > 0) {
MenuHelper.config.navbar.additionalLinks.forEach(function (additionalLink) {
if (!additionalLink.link || !additionalLink.title || additionalLink.disabled) {
return;
}
if (val.actions && val.actions.length > 0 && val.actions[0].title !== "Overview") {
val.actions.unshift({
link: config.routePrefix + '/' + key,
title: "Overview",
icon: ""
})
}
menues.push({
link: config.routePrefix + '/' + key,
title: val.title,
icon: val.icon || null,
actions: val.actions || null,
id: val.id || val.title.replace(" ","_"),
instanceName: key
menus.push({
link: additionalLink.link,
title: additionalLink.title,
id: additionalLink.id || additionalLink.title.replace(" ", "_"),
icon: additionalLink.icon || null
});
});
if (config.menu.actions && config.menu.actions.length > 0) {
_.forEach(config.menu.actions, function(menu) {
if (!menu.link || !menu.title || menu.menuGroup || menu.disabled) {
return;
}
menues.push({
link: menu.link,
title: menu.title,
id: menu.id || menu.title.replace(" ","_"),
icon: menu.icon || null
});
});
}
return menues;
}
};
return module;
};
return menus;
}
}
exports.MenuHelper = MenuHelper;

@@ -1,15 +0,7 @@

'use strict';
var path = require('path');
var _ = require('lodash');
module.exports = {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewsHelper = void 0;
const path = require("path");
class ViewsHelper {
/**
* Base path for all views.
*/
BASE_VIEWS_PATH: path.join(__dirname, '../views/'),
/**
* Generate path to views files for given view engine

@@ -20,6 +12,5 @@ *

*/
getPathToEngine: function (engine) {
return path.join(this.BASE_VIEWS_PATH, engine, '/')
},
static getPathToEngine(engine) {
return path.join(this.BASE_VIEWS_PATH, engine, '/');
}
/**

@@ -31,6 +22,5 @@ * Will generate path to view file

*/
getViewPath: function getViewPath(view) {
static getViewPath(view) {
return path.resolve(sails.config.adminpanel.pathToViews, view);
},
}
/**

@@ -41,6 +31,5 @@ *

*/
hasFlash: function (req, type) {
static hasFlash(req, type) {
return (req.session.flash && req.session.flash[type]);
},
}
/**

@@ -53,9 +42,9 @@ * Get needed field value from dat provided.

*/
getFieldValue: function (key, field, data) {
var value = data[key];
if (_.isObject(value) && field.config.type == 'association') {
static getFieldValue(key, field, data) {
let value = data[key];
if (typeof value === "object" && field.config.type == 'association') {
value = value[field.config.identifierField];
}
if (_.isArray(value) && field.config.type == 'association-many') {
var result = [];
if (Array.isArray(value) && field.config.type == 'association-many') {
let result = [];
value.forEach(function (val) {

@@ -67,4 +56,3 @@ result.push(val[field.config.identifierField]);

return value;
},
}
/**

@@ -77,10 +65,10 @@ * Check if given option equals value or is in array

*/
isOptionSelected: function (option, value) {
if (_.isArray(value)) {
return _.includes(value, option);
} else {
static isOptionSelected(option, value) {
if (Array.isArray(value)) {
return value.includes(option);
}
else {
return (option == value);
}
},
}
/**

@@ -92,9 +80,9 @@ * Get's field value for view screen

*/
getAssociationValue: function (value, field) {
static getAssociationValue(value, field) {
if (!value) {
return '-----------';
}
var displayField = field.config.displayField || 'id';
if (_.isArray(value)) {
var result = '';
let displayField = field.config.displayField || 'id';
if (Array.isArray(value)) {
let result = '';
value.forEach(function (val) {

@@ -105,3 +93,3 @@ result += val[displayField] + '<br/>';

}
if (_.isObject(value)) {
if (typeof value === "object") {
return value[displayField];

@@ -111,2 +99,7 @@ }

}
};
}
exports.ViewsHelper = ViewsHelper;
/**
* Base path for all views.
*/
ViewsHelper.BASE_VIEWS_PATH = path.join(__dirname, '../views/');
'use strict';
let { MenuHelper } = require('./helper/menuHelper');
let { AccessRightsHelper } = require('./helper/accessRightsHelper');
module.exports = function (sails) {
let libInitialize = require("./lib/initialize");
return {

@@ -10,11 +14,13 @@

*/
defaults: require('./lib/defaults'),
defaults: require('./lib/defaults').content,
configure: require('./lib/configure')(sails),
configure: require('./lib/configure').default(),
initialize: require('./lib/initialize').default(sails),
initialize: async function initialize(cb) {
await libInitialize.default(sails, cb);
},
addMenuItem: function (link, label, icon, group) {
if (!link)
throw 'first argumant is required';
throw 'first argument is required';

@@ -30,3 +36,3 @@ sails.config.adminpanel.menu = sails.config.adminpanel.menu || {};

sails.config.views.locals.adminpanel.menuHelper = require('./helper/menuHelper')(sails.config.adminpanel);
sails.config.views.locals.adminpanel.menuHelper = new MenuHelper(sails.config.adminpanel);
},

@@ -36,3 +42,3 @@

if (!key)
throw 'first argumant is required';
throw 'first argument is required';

@@ -45,5 +51,11 @@ sails.config.adminpanel.menu = sails.config.adminpanel.menu || {};

});
}
},
registerAccessToken: AccessRightsHelper.registerToken,
getAllAccessTokens: AccessRightsHelper.getTokens,
havePermission: AccessRightsHelper.havePermission
};
};

@@ -1,47 +0,6 @@

'use strict';
var _ = require('lodash');
var path = require('path');
var AdminUtil = {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminUtil = void 0;
class AdminUtil {
/**
* Default configuration for instance
*
* @see AdminUtil.findConfig
*/
_defaultInstanceConfig: {
list: true,
add: true,
edit: true,
remove: true,
view: true
},
/**
* Default configs that will be returned for action. If nothing exists in config file.
*
* @see AdminUtil.findActionConfig
*/
_defaultActionConfig: {
fields: {}
},
/**
* Will fetch list of model attributes
*
* @param {Model} model
* @returns {Object}
*/
_getModelAttributes: function(model) {
if (!model || !model.attributes) {
return {};
}
return _.pick(model.attributes, function(val, key) {
// return (_.isPlainObject(val) && !val.collection);
return _.isPlainObject(val);
});
},
/**
* Check if given instance config has all required properties

@@ -53,12 +12,9 @@ *

*/
_isValidInstanceConfig: function(config) {
if (!_.isObject(config) || !_.isString(config.model)) {
return false;
}
return true;
},
static _isValidInstanceConfig(config) {
return (typeof config === "object" && typeof config.model === "string");
}
;
/**
* Normalizing instance config.
* Will retirn fulfilled configuration object.
* Will return fulfilled configuration object.
*

@@ -70,10 +26,10 @@ * @see AdminUtil._isValidInstanceConfig

*/
_normalizeInstanceConfig: function(config) {
static _normalizeInstanceConfig(config) {
if (!this._isValidInstanceConfig(config)) {
req._sails.log.error('Wrong instance configuration, using default');
sails.log.error('Wrong instance configuration, using default');
config = {};
}
_.defaults(config, this._defaultInstanceConfig);
config = Object.assign(this._defaultInstanceConfig, config);
//Check limits
if (_.isBoolean(config.list)) {
if (typeof config.list === "boolean") {
config.list = {

@@ -83,8 +39,8 @@ limit: 15

}
if (!_.isNumber(config.list.limit)) {
if (typeof config.list.limit !== "number") {
config.list.limit = 15;
}
return config;
},
}
;
/**

@@ -97,13 +53,8 @@ * Normalize action config object

*/
_normalizeActionConfig: function(config) {
static _normalizeActionConfig(config) {
//Adding fields
config.fields = config.fields || {};
_.defaults(config, this._defaultActionConfig);
//check just to be sure that we will have object
if (!_.isPlainObject(config.fields)) {
config.fields = {};
}
return config;
},
return Object.assign(this._defaultActionConfig, config);
}
;
/**

@@ -114,6 +65,5 @@ * Get admin panel config

*/
config: function() {
static config() {
return sails.config.adminpanel || {};
},
}
/**

@@ -125,10 +75,11 @@ * Get model from system

*/
getModel: function(name) {
static getModel(name) {
//Getting model
// console.log('admin > model > ', sails.models);
var Model = sails.models[name.toLowerCase()];
let Model = sails.models[name.toLowerCase()];
if (!Model) {
if (!sails) {
console.log('No model found in sails.');
} else {
}
else {
sails.log.error('No model found in sails.');

@@ -139,4 +90,3 @@ }

return Model;
},
}
/**

@@ -148,9 +98,15 @@ * Get instance name

*/
findInstanceName: function(req) {
if (!req.param('instance')) {
return null;
}
return req.param('instance');
},
static findInstanceName(req) {
if (!req.param('instance')) {
let instanceName = req.originalUrl.split('/')[2];
if (!this.config().instances || !this.config().instances[instanceName]) {
return null;
}
else {
return instanceName;
}
}
return req.param('instance');
}
;
/**

@@ -163,3 +119,3 @@ * Searches for config from admin panel

*/
findInstanceConfig: function findConfig(req, instanceName) {
static findInstanceConfig(req, instanceName) {
if (!this.config().instances || !this.config().instances[instanceName]) {

@@ -170,4 +126,3 @@ req._sails.log.error('No such route exists');

return this._normalizeInstanceConfig(this.config().instances[instanceName] || {});
},
}
/**

@@ -196,7 +151,7 @@ * Will get action config from configuration file depending to given action

*/
findActionConfig: function(instance, actionType) {
static findActionConfig(instance, actionType) {
if (!instance || !actionType) {
throw new Error('No `instance` or `actionType` passed !');
}
var result = _.defaults({}, this._defaultActionConfig);
let result = Object.assign({}, this._defaultActionConfig);
if (!instance.config || !instance.config[actionType]) {

@@ -209,8 +164,7 @@ return result;

*/
if (_.isBoolean(instance.config[actionType])) {
if (typeof instance.config[actionType] === "boolean") {
return result;
}
return this._normalizeActionConfig(instance.config[actionType]);
},
}
/**

@@ -224,3 +178,3 @@ * Trying to find model by request

*/
findModel: function findModel(req, instanceConfig) {
static findModel(req, instanceConfig) {
if (!this._isValidInstanceConfig(instanceConfig)) {

@@ -230,4 +184,3 @@ return null;

return this.getModel(instanceConfig.model);
},
}
/**

@@ -251,12 +204,35 @@ * Will create instance object from request.

*/
findInstanceObject: function(req) {
var instance = {};
instance.name = this.findInstanceName(req);
instance.config = this.findInstanceConfig(req, instance.name);
instance.model = this.findModel(req, instance.config);
instance.uri = this.config().routePrefix + '/' + instance.name;
return instance;
static findInstanceObject(req) {
let instanceName = this.findInstanceName(req);
let instanceConfig = this.findInstanceConfig(req, instanceName);
let instanceModel = this.findModel(req, instanceConfig);
let instanceUri = this.config().routePrefix + '/' + instanceName;
return {
name: instanceName,
config: instanceConfig,
model: instanceModel,
uri: instanceUri
};
}
}
exports.AdminUtil = AdminUtil;
/**
* Default configuration for instance
*
* @see AdminUtil.findConfig
*/
AdminUtil._defaultInstanceConfig = {
list: true,
add: true,
edit: true,
remove: true,
view: true
};
module.exports = AdminUtil;
/**
* Default configs that will be returned for action. If nothing exists in config file.
*
* @see AdminUtil.findActionConfig
*/
AdminUtil._defaultActionConfig = {
fields: {}
};

@@ -1,1 +0,1 @@

export default function (sails: any): Promise<void>;
export default function (): void;

@@ -5,8 +5,6 @@ "use strict";

const path = require("path");
async function default_1(sails) {
function default_1() {
sails.hooks.http.app.use('/admin/assets', serveStatic(path.join(__dirname, '../assets')));
//let layer = sails.hooks.http.app._router.stack.slice(-1)[0]
//sails.hooks.http.app._router.stack.splice(1, 0, layer)
}
exports.default = default_1;
;

@@ -1,7 +0,6 @@

import * as serveStatic from 'serve-static'
import * as path from "path"
export default async function(sails: any) {
import * as serveStatic from 'serve-static';
import * as path from "path";
export default function() {
sails.hooks.http.app.use('/admin/assets', serveStatic(path.join(__dirname, '../assets')));
//let layer = sails.hooks.http.app._router.stack.slice(-1)[0]
//sails.hooks.http.app._router.stack.splice(1, 0, layer)
};
};

@@ -1,3 +0,1 @@

declare var path: any;
declare var _login: any;
declare var superAdmin: string;
export default function bindAuthorization(): Promise<void>;

@@ -1,111 +0,46 @@

'use strict';
var path = require('path');
var _login = require('../actions/login');
var superAdmin = 'isAdminpanelSuperAdmin';
module.exports = function bindAuthorization(sails) {
/**
* Router
*/
var _bindPolicies = require('../lib/bindPolicies')(sails);
var policies = sails.config.adminpanel.policies || '';
var baseRoute = sails.config.adminpanel.routePrefix + '/:instance';
sails.router.bind(baseRoute + '/login', _bindPolicies(policies, _login));
sails.router.bind(baseRoute + '/logout', _bindPolicies(policies, _login));
var apConfName = ['list', 'add', 'edit', 'remove', 'view'];
var apConf = {
title: 'Users',
model: 'UserAP',
icon: 'user',
permission: superAdmin
};
for (var i in apConfName) {
var conf = {
permission: superAdmin
"use strict";
// 1. Если не установлена auth = true, то все пользователи superadmin и модель создавать не нужно
// 2. 2 модели (пользователя и группы), в оперативке список всех флагов разрешений, настройка внутри модели группы
// что может делать группы по этому флагу, суперадмину проверка не нужна
// 3. Как только найдено первый флаг разрешений у группы этого пользователя, то разрешено
// 4. Сделать извне методы добавляющие флаги разрешений с возможностью их группировать
Object.defineProperty(exports, "__esModule", { value: true });
const login_1 = require("../controllers/login");
let superAdmin = 'isAdminpanelSuperAdmin';
async function bindAuthorization() {
let config = sails.config.adminpanel;
let adminData;
if (config.administrator) {
adminData = config.administrator;
}
else {
let password = getRandomInt(1000000000000, 9999999999999);
adminData = {
login: "admin",
password: `${password}`
};
if (apConfName[i] !== 'remove') {
conf.fields = {
id: true,
passwordHashed: false,
createdAt: false,
updatedAt: false,
permission: {
widget: 'JsonEditor',
JsonEditor: {
height: 100,
mode: 'tree',
modes: ['code', 'form', 'text', 'tree', 'view']
}
},
password: false
};
}
if (apConfName[i] === 'add') {
conf.fields.password = true;
}
apConf[apConfName[i]] = conf;
}
sails.config.adminpanel.instances['userap'] = apConf;
};
/**
* Add method to check permission from controller
*/
sails.adminpanel = {};
sails.adminpanel.havePermission = (req, obj, action) => {
action = path.basename(action).split('.')[0];
if (!sails.config.adminpanel.auth)
return true;
if (action === '') {
if (req.session.UserAP) {
if (req.session.UserAP.permission) {
if (!obj.permission) {
return true;
}
}
}
// This code is only for single administrator, should be rewritten for multiple
try {
await UserAP.destroy({ isAdministrator: true });
await UserAP.create({ login: adminData.login, password: adminData.password, fullName: "Administrator",
isActive: true, isAdministrator: true });
}
if (req.session.UserAP) {
if (req.session.UserAP.permission) {
if (!Array.isArray(req.session.UserAP.permission)) {
req.session.UserAP.permission = [req.session.UserAP.permission];
}
if (req.session.UserAP.permission.indexOf(superAdmin) >= 0) {
return true;
}
else if (obj[action]) {
if (typeof obj[action] === 'boolean')
return true;
if (obj[action].permission) {
if (!Array.isArray(obj[action].permission)) {
obj[action].permission = [obj[action].permission];
}
for (var i in req.session.UserAP.permission) {
for (var j in obj[action].permission) {
if (req.session.UserAP.permission[i] === obj[action].permission[j]) {
return true;
}
}
}
}
else {
return true;
}
}
else if (action === '') {
if (obj.permission) {
if (!Array.isArray(obj.permission)) {
obj.permission = [obj.permission];
}
for (var i in req.session.UserAP.permission) {
for (var j in obj.permission) {
if (req.session.UserAP.permission[i] === obj.permission[j]) {
return true;
}
}
}
}
}
}
catch (e) {
sails.log.error("Could not create administrator profile", e);
return;
}
return false;
};
console.log(`\n### Administrator:\n###\tlogin: ${adminData.login}\n###\tpassword: ${adminData.password}\n`);
// ------------------------------------------------------------------------------------------------
/**
* Router
*/
let _bindPolicies = require('../lib/bindPolicies').default();
let policies = sails.config.adminpanel.policies || '';
let baseRoute = sails.config.adminpanel.routePrefix + '/:instance';
sails.router.bind(baseRoute + '/login', _bindPolicies(policies, login_1.default));
sails.router.bind(baseRoute + '/logout', _bindPolicies(policies, login_1.default));
}
exports.default = bindAuthorization;
;
sails.on('lifted', async function () {

@@ -119,26 +54,7 @@ /**

return;
if (sails.config.adminpanel.admin) {
conf = sails.config.adminpanel.admin;
}
else {
var conf = {
username: 'engineer',
password: 'engineer'
};
}
try {
let user = await UserAP.findOne({ username: conf.username });
if (!user) {
user = await UserAP.create({
username: conf.username,
password: conf.password,
permission: [superAdmin]
}).fetch();
if (!user)
sails.log.error("Can't create user!");
}
}
catch (e) {
sails.log.error(e);
}
});
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //Максимум не включается, минимум включается
}

@@ -1,114 +0,69 @@

'use strict';
// 1. Если не установлена auth = true, то все пользователи superadmin и модель создавать не нужно
// 2. 2 модели (пользователя и группы), в оперативке список всех флагов разрешений, настройка внутри модели группы
// что может делать группы по этому флагу, суперадмину проверка не нужна
// 3. Как только найдено первый флаг разрешений у группы этого пользователя, то разрешено
// 4. Сделать извне методы добавляющие флаги разрешений с возможностью их группировать
var path = require('path');
var _login = require('../actions/login');
// 1. У пользователя должен быть перманентный выбор локали
// 2. Нужно дать возможность разработчику переводить строки в своем проекте которые пойдут в админпанель
// 3. Если перевод не найден, то показывается оригинальный
// 4. Файлы переводов должны быть в config/locales/adminpanel [en, es, ru, fr]
// 5. Внутри конфига админпанели можно указать путь к этим переводам, и какие будут локали доступны
// 6. У админки будут свои переводы лежать и они будут мерджится с вышеуказанными переводами
// 7. Если пользователя нет, то выбирается дефолтная локаль из конфига. Если ее нет, то первая из списка локалей
// 8. Локаль достается из req.user
// 9. bindInternationalization на все /routePrefix/* будет навешивать этот middleware
// 10. In i18n-2/i18n.js should be appendLocale function
// 11. Проверять активность i18n и наличие метода appendLocale
var superAdmin = 'isAdminpanelSuperAdmin';
module.exports = function bindAuthorization(sails) {
// По умолчанию создается "administrator"
// Заменить вью группы и пользователя на создание и редактирование через конфиг
import * as path from "path";
import _login from "../controllers/login";
import {AdminpanelConfig} from "../interfaces/adminpanelConfig";
import {AccessRightsHelper} from "../helper/accessRightsHelper";
import UserAP from "../models/UserAP";
let superAdmin = 'isAdminpanelSuperAdmin';
export default async function bindAuthorization() {
let config: AdminpanelConfig = sails.config.adminpanel;
let adminData;
if (config.administrator) {
adminData = config.administrator;
} else {
let password = getRandomInt(1000000000000, 9999999999999)
adminData = {
login: "admin",
password: `${password}`
}
}
// This code is only for single administrator, should be rewritten for multiple
try {
await UserAP.destroy({isAdministrator: true});
await UserAP.create({login: adminData.login, password: adminData.password, fullName: "Administrator",
isActive: true, isAdministrator: true});
} catch (e) {
sails.log.error("Could not create administrator profile", e)
return;
}
console.log(`\n### Administrator:\n###\tlogin: ${adminData.login}\n###\tpassword: ${adminData.password}\n`)
// ------------------------------------------------------------------------------------------------
/**
* Router
*/
var _bindPolicies = require('../lib/bindPolicies')(sails);
var policies = sails.config.adminpanel.policies || '';
var baseRoute = sails.config.adminpanel.routePrefix + '/:instance';
let _bindPolicies = require('../lib/bindPolicies').default();
let policies = sails.config.adminpanel.policies || '';
let baseRoute = sails.config.adminpanel.routePrefix + '/:instance';
sails.router.bind(baseRoute + '/login', _bindPolicies(policies, _login));
sails.router.bind(baseRoute + '/logout', _bindPolicies(policies, _login));
var apConfName = ['list', 'add', 'edit', 'remove', 'view'];
var apConf = {
title: 'Users',
model: 'UserAP',
icon: 'user',
permission: superAdmin
};
for (var i in apConfName) {
var conf = {
permission: superAdmin
};
if (apConfName[i] !== 'remove') {
conf.fields = {
id: true,
passwordHashed: false,
createdAt: false,
updatedAt: false,
permission: {
widget: 'JsonEditor',
JsonEditor: {
height: 100,
mode: 'tree',
modes: ['code', 'form', 'text', 'tree', 'view']
}
},
password: false
}
}
if (apConfName[i] === 'add') {
conf.fields.password = true;
}
apConf[apConfName[i]] = conf;
}
sails.config.adminpanel.instances['userap'] = apConf;
};
/**
* Add method to check permission from controller
*/
sails.adminpanel = {};
sails.adminpanel.havePermission = (req, obj, action) => {
action = path.basename(action).split('.')[0];
if (!sails.config.adminpanel.auth)
return true;
if (action === '') {
if (req.session.UserAP) {
if (req.session.UserAP.permission) {
if (!obj.permission) {
return true;
}
}
}
}
if (req.session.UserAP) {
if (req.session.UserAP.permission) {
if (!Array.isArray(req.session.UserAP.permission)) {
req.session.UserAP.permission = [req.session.UserAP.permission];
}
if (req.session.UserAP.permission.indexOf(superAdmin) >= 0) {
return true;
} else if (obj[action]) {
if (typeof obj[action] === 'boolean')
return true;
if (obj[action].permission) {
if (!Array.isArray(obj[action].permission)) {
obj[action].permission = [obj[action].permission];
}
for (var i in req.session.UserAP.permission) {
for (var j in obj[action].permission) {
if (req.session.UserAP.permission[i] === obj[action].permission[j]) {
return true;
}
}
}
} else {
return true;
}
} else if (action === '') {
if (obj.permission) {
if (!Array.isArray(obj.permission)) {
obj.permission = [obj.permission];
}
for (var i in req.session.UserAP.permission) {
for (var j in obj.permission) {
if (req.session.UserAP.permission[i] === obj.permission[j]) {
return true;
}
}
}
}
}
}
}
return false;
}
sails.on('lifted', async function () {

@@ -123,26 +78,8 @@ /**

});
if (sails.config.adminpanel.admin) {
conf = sails.config.adminpanel.admin;
} else {
var conf = {
username: 'engineer',
password: 'engineer'
}
}
try {
let user = await UserAP.findOne({username: conf.username})
if (!user) {
user = await UserAP.create({
username: conf.username,
password: conf.password,
permission: [superAdmin]
}).fetch()
if (!user) sails.log.error("Can't create user!");
}
} catch (e) {
sails.log.error(e);
}
});
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //Максимум не включается, минимум включается
}

@@ -1,7 +0,8 @@

'use strict';
var viewsHelper = require('../helper/viewsHelper');
module.exports = function(sails) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const viewsHelper_1 = require("../helper/viewsHelper");
const fieldsHelper_1 = require("../helper/fieldsHelper");
const menuHelper_1 = require("../helper/menuHelper");
const configHelper_1 = require("../helper/configHelper");
function bindConfig() {
/**

@@ -11,3 +12,3 @@ * Bind adminpanel config to views

if (!sails.config.adminpanel.pathToViews) {
sails.config.adminpanel.pathToViews = viewsHelper.getPathToEngine(sails.config.views.extension);
sails.config.adminpanel.pathToViews = viewsHelper_1.ViewsHelper.getPathToEngine(sails.config.views.extension);
}

@@ -23,6 +24,8 @@ // binding locals

sails.config.views.locals.adminpanel.config = sails.config.adminpanel;
sails.config.views.locals.adminpanel.viewHelper = viewsHelper;
sails.config.views.locals.adminpanel.fieldsHelper = require('../helper/fieldsHelper');
sails.config.views.locals.adminpanel.menuHelper = require('../helper/menuHelper')(sails.config.adminpanel);
sails.config.views.locals.adminpanel.configHelper = require('../helper/configHelper')(sails);
};
sails.config.views.locals.adminpanel.viewHelper = viewsHelper_1.ViewsHelper;
sails.config.views.locals.adminpanel.fieldsHelper = fieldsHelper_1.FieldsHelper;
sails.config.views.locals.adminpanel.menuHelper = new menuHelper_1.MenuHelper(sails.config.adminpanel);
sails.config.views.locals.adminpanel.configHelper = configHelper_1.ConfigHelper;
}
exports.default = bindConfig;
;

@@ -1,9 +0,26 @@

'use strict';
var _ = require('lodash');
module.exports = function (sails) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
function bindPolicies() {
//write out policies to config
try {
let policiesDir = fs.readdirSync(__dirname + "/../policies");
for (let policy of policiesDir) {
if (path.extname(policy).toLowerCase() === ".js") {
let policyFile = require(__dirname + "/../policies/" + policy);
if (typeof policyFile === "function") {
sails.config.adminpanel.policies.push(policyFile);
}
else {
sails.log.error(`Adminpanel > Policy ${policyFile} is not a function`);
}
}
}
}
catch (e) {
sails.log.error("Adminpanel > Could not load policies", e);
}
return function (policies, action) {
if (_.isFunction(policies) && !action) {
if (typeof policies === "function" && !action) {
action = policies;

@@ -15,4 +32,3 @@ policies = '';

}
var result = [];
let result = [];
/**

@@ -23,17 +39,19 @@ * Bind policy to action

*/
var bindPolicy = function (policy) {
if (_.isFunction(policy)) {
let bindPolicy = function (policy) {
if (typeof policy === "function") {
result.push(policy);
return;
}
//Check for policy existance
//Check for policy existence
if (!sails.hooks.policies.middleware[policy.toLowerCase()]) {
sails.log.error('AdminPanel: No policy exist: ' + policy);
} else {
}
else {
result.push(sails.hooks.policies.middleware[policy.toLowerCase()]);
}
};
if (_.isArray(policies)) {
_.forEach(policies, bindPolicy);
} else {
if (Array.isArray(policies)) {
policies.forEach(bindPolicy);
}
else {
bindPolicy(policies);

@@ -47,2 +65,4 @@ }

};
};
}
exports.default = bindPolicies;
;

@@ -1,11 +0,7 @@

'use strict';
var _ = require('lodash');
var path = require('path');
var viewHelper = require('../helper/viewsHelper');
module.exports = function (sails) {
var bindResFunctions = function (req, res, next) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const viewsHelper_1 = require("../helper/viewsHelper");
const accessRightsHelper_1 = require("../helper/accessRightsHelper");
function bindResView() {
let bindResFunctions = function (req, res, next) {
/**

@@ -17,8 +13,8 @@ * Guess view name by request

*/
var guessViewName = function (req) {
if (!req || !req.route || !req.route.path || !_.isString(req.route.path)) {
let guessViewName = function (req) {
if (!req || !req.route || !req.route.path || typeof req.route.path !== "string") {
return '';
}
var routeSplited = req.route.path.split('/');
var viewName = routeSplited.pop();
let routeSplit = req.route.path.split('/');
let viewName = routeSplit.pop();
// :instance = list

@@ -30,22 +26,21 @@ if (viewName === ':instance') {

if (viewName === ':id') {
viewName = routeSplited.pop();
viewName = routeSplit.pop();
}
return viewName;
};
/**
* Show admin panel view.
*/
res.viewAdmin = function (/* specifiedPath, locals, cb_view */) {
var specifiedPath = arguments[0];
var locals = arguments[1];
var cb_view = arguments[2];
if (_.isObject(arguments[0])) {
// !TODO rewrite arguments to strong queue, without universal redefined arguments
res.viewAdmin = function ( /* specifiedPath, locals, cb_view */) {
let specifiedPath = arguments[0];
let locals = arguments[1];
let cb_view = arguments[2];
if (typeof arguments[0] === "object") {
locals = arguments[0];
}
if (_.isFunction(arguments[1])) {
if (typeof arguments[1] === "function") {
cb_view = arguments[1];
}
if (!specifiedPath || !_.isString(specifiedPath)) {
if (!specifiedPath || typeof specifiedPath !== "string") {
specifiedPath = guessViewName(res.req);

@@ -56,8 +51,6 @@ }

if (!locals) {
locals = {};
_.merge(locals, sails.config.views.locals);
locals = { ...sails.config.views.locals };
}
locals.layout = false;
}
// set theme for admin panel

@@ -69,9 +62,9 @@ if (!locals) {

locals.button = sails.config.adminpanel.button || 'solid';
return res.view(viewHelper.getViewPath(specifiedPath), locals, cb_view);
locals.havePermission = accessRightsHelper_1.AccessRightsHelper.havePermission;
if (locals.section === undefined)
locals.section = 'adminpanel';
return res.view(viewsHelper_1.ViewsHelper.getViewPath(specifiedPath), locals, cb_view);
};
next();
};
// Bind to /admin

@@ -82,3 +75,5 @@ console.log('admin > route bind > ', sails.config.adminpanel.routePrefix);

sails.router.bind(sails.config.adminpanel.routePrefix + '\/*', bindResFunctions);
sails.emit("adminpanel:viewadmin:loaded")
};
sails.emit("adminpanel:viewadmin:loaded");
}
exports.default = bindResView;
;

@@ -1,54 +0,61 @@

'use strict';
var path = require('path');
var _dashboard = require('../actions/dashboard');
var _welcome = require('../actions/welcome');
var _list = require('../actions/list');
var _list_json = require('../actions/list_json');
var _edit = require('../actions/edit');
var _add = require('../actions/add');
var _view = require('../actions/view');
var _remove = require('../actions/remove');
var _upload = require('../actions/upload');
module.exports = function bindRoutes(sails) {
var _bindPolicies = require('../lib/bindPolicies')(sails);
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dashboard_1 = require("../controllers/dashboard");
const welcome_1 = require("../controllers/welcome");
const list_1 = require("../controllers/list");
const listJson_1 = require("../controllers/listJson");
const edit_1 = require("../controllers/edit");
const add_1 = require("../controllers/add");
const view_1 = require("../controllers/view");
const remove_1 = require("../controllers/remove");
const upload_1 = require("../controllers/upload");
function bindRoutes() {
let _bindPolicies = require('../lib/bindPolicies').default();
/**
* List or one policy that should be binded to actions
* List or one policy that should be bound to actions
* @type {string|Array}
*/
var policies = sails.config.adminpanel.policies || '';
let config = sails.config.adminpanel;
let policies = config.policies || '';
//Create a base instance route
var baseRoute = sails.config.adminpanel.routePrefix + '/:instance';
let baseRoute = config.routePrefix + '/:instance';
/**
* List of records
*/
sails.router.bind(baseRoute, _bindPolicies(policies, _list));
sails.router.bind(baseRoute+"/json", _bindPolicies(policies, _list_json));
sails.router.bind(baseRoute, _bindPolicies(policies, list_1.default));
for (let instance of Object.keys(config.instances)) {
/**
* Create new record
*/
if (config.instances[instance].add && config.instances[instance].add.controller) {
let controller = require(config.instances[instance].add.controller);
sails.router.bind(`${config.routePrefix}/${instance}/add`, _bindPolicies(policies, controller.default));
}
else {
sails.router.bind(`${config.routePrefix}/${instance}/add`, _bindPolicies(policies, add_1.default));
}
/**
* Edit existing record
*/
if (config.instances[instance].edit && config.instances[instance].edit.controller) {
let controller = require(config.instances[instance].edit.controller);
sails.router.bind(`${config.routePrefix}/${instance}/edit/:id`, _bindPolicies(policies, controller.default));
}
else {
sails.router.bind(`${config.routePrefix}/${instance}/edit/:id`, _bindPolicies(policies, edit_1.default));
}
}
/**
* Create new record
*/
sails.router.bind(baseRoute + '/add', _bindPolicies(policies, _add));
/**
* View record details
*/
sails.router.bind(baseRoute + '/view/:id', _bindPolicies(policies, _view));
sails.router.bind(baseRoute + '/view/:id', _bindPolicies(policies, view_1.default));
sails.router.bind(baseRoute + "/json", _bindPolicies(policies, listJson_1.default));
/**
* Edit existing record
*/
sails.router.bind(baseRoute + '/edit/:id', _bindPolicies(policies, _edit));
/**
* Remove record
*/
sails.router.bind(baseRoute + '/remove/:id', _bindPolicies(policies, _remove));
sails.router.bind(baseRoute + '/remove/:id', _bindPolicies(policies, remove_1.default));
/**
* Upload files
*/
sails.router.bind(baseRoute + '/upload', _bindPolicies(policies, _upload));
sails.router.bind(baseRoute + '/upload', _bindPolicies(policies, upload_1.default));
/**

@@ -58,9 +65,10 @@ * Create a default dashboard

*/
if (Boolean(sails.config.adminpanel.dashboard)) {
sails.router.bind(sails.config.adminpanel.routePrefix, _bindPolicies(policies, _dashboard));
} else {
sails.router.bind(sails.config.adminpanel.routePrefix, _bindPolicies(policies, _welcome));
if (Boolean(config.dashboard)) {
sails.router.bind(config.routePrefix, _bindPolicies(policies, dashboard_1.default));
}
};
else {
sails.router.bind(config.routePrefix, _bindPolicies(policies, welcome_1.default));
}
}
exports.default = bindRoutes;
;

@@ -1,5 +0,4 @@

'use strict';
module.exports = function ToConfigure(sails) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function ToConfigure() {
return function configure() {

@@ -10,10 +9,10 @@ // Check for disable admin panel

}
// Add hooks here
sails.config.adminpanel.hooks=[];
sails.config.adminpanel.styles=[];
sails.config.adminpanel.script={};
sails.config.adminpanel.script.header=[];
sails.config.adminpanel.script.footer=[];
//recheck reoute prefix
// !TODO add styles render in ejs
sails.config.adminpanel.styles = [];
// !TODO add scripts
sails.config.adminpanel.script = {};
sails.config.adminpanel.script.header = [];
sails.config.adminpanel.script.footer = [];
sails.config.adminpanel.policies = [];
//recheck route prefix
sails.config.adminpanel.routePrefix = sails.config.adminpanel.routePrefix || '/admin';

@@ -25,2 +24,4 @@ //check and adding base slash

};
};
}
exports.default = ToConfigure;
;

@@ -1,63 +0,81 @@

'use strict';
module.exports = {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.content = void 0;
const packageJson = require('../package.json');
const timezones = require('./timezones.json');
/**
* Default admin config
*/
const adminpanelConfig = {
/**
* Default admin config
* Default url prefix for admin panel
*/
adminpanel: {
/**
* Default url prefix for admin panel
*/
routePrefix: '\/admin',
/**
* Will set method how assets will be placed into project
*/
assets: 'copy',
/**
* Default path to views
*
* @type {?string}
*/
pathToViews: null,
/**
* Name of model identifier field
*/
identifierField: 'id',
/**
* Default policy that will be used to check access
*/
policy: '',
/**
* Base menu configuration
*/
menu: {
// Should admin panel brand be visible ?
brand: true,
// Menu groups
groups: [],
// List of additional actions
actions: []
routePrefix: '\/admin',
/**
* Default path to views
*
* @type {?string}
*/
pathToViews: null,
/**
* Name of model identifier field
*/
identifierField: 'id',
/**
* Policies
*/
policies: null,
/**
* Base navbar configuration
*/
navbar: {
// List of additional actions
additionalLinks: []
},
brand: {
link: null
},
/**
* List of admin pages
*/
instances: {
users: {
title: "Users",
model: "userap",
icon: "user",
add: {
controller: "../controllers/addUser"
},
edit: {
controller: "../controllers/editUser"
}
},
/**
* List of admin pages
*/
instances: {},
/**
* List of custom actions
*/
actions: [],
/**
* List of sections in head
*/
sections: []
}
groups: {
title: "Groups",
model: "groupap",
icon: "group",
add: {
controller: "../controllers/addGroup"
},
edit: {
controller: "../controllers/editGroup"
}
}
},
translation: {
locales: ['en', 'ru'],
path: `${process.cwd()}/config/locales/adminpanel`,
defaultLocale: 'en'
},
/**
* List of sections in head
*/
sections: [],
package: packageJson,
showVersion: true,
timezones: timezones
};
exports.content = {
adminpanel: adminpanelConfig
};
console.log(adminpanelConfig);

@@ -1,1 +0,1 @@

export default function ToInitialize(sails: any): (cb: any) => Promise<any>;
export default function (sails: any, cb: any): Promise<any>;

@@ -1,15 +0,16 @@

'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require('lodash');
var fs = require('fs');
var viewsHelper = require('../helper/viewsHelper');
const fs = require("fs");
const viewsHelper_1 = require("../helper/viewsHelper");
const path = require("path");
const bindAssets_1 = require("./bindAssets");
function ToInitialize(sails) {
const hookTools_1 = require("./hookTools");
const path_1 = require("path");
const afterHook_1 = require("./afterHook");
async function default_1(sails, cb) {
/**
* List of hooks that required for adminpanel to work
*/
var requiredHooks = [
let requiredHooks = [
'blueprints',
'controllers',
'http',

@@ -20,46 +21,23 @@ 'orm',

];
return async function initialize(cb) {
// If disabled. Do not load anything
if (!sails.config.adminpanel) {
return cb();
}
// Set up listener to bind shadow routes when the time is right.
//
// Always wait until after router has bound static routes.
// If policies hook is enabled, also wait until policies are bound.
// If orm hook is enabled, also wait until models are known.
// If controllers hook is enabled, also wait until controllers are known.
var eventsToWaitFor = [];
eventsToWaitFor.push('router:after');
try {
/**
* Check hooks availability
*/
_.forEach(requiredHooks, function (hook) {
// if (!sails.hooks[hook]) {
// throw new Error('Cannot use `adminpanel` hook without the `' + hook + '` hook.');
// }
// eventsToWaitFor.push('hook:' + hook + ':loaded');
});
}
catch (err) {
if (err) {
return cb(err);
}
}
//Check views engine and check if folder with templates exist
if (!fs.existsSync(viewsHelper.getPathToEngine(sails.config.views.extension))) {
return cb(new Error('For now adminpanel hook could work only with Pug template engine.'));
}
var initAuth = require('./initializeAuthorization')(sails, cb);
// sails.after(eventsToWaitFor, require('../lib/afterHooksLoaded')(sails));
sails.on("lifted", require('../lib/afterHooksLoaded')(sails));
sails.config.adminpanel.templateRootPath = viewsHelper.BASE_VIEWS_PATH;
sails.config.adminpanel.rootPath = path.resolve(__dirname + "/..");
// Bind assets
await (0, bindAssets_1.default)(sails);
cb();
};
// If disabled. Do not load anything
if (!sails.config.adminpanel) {
return cb();
}
//Check views engine and check if folder with templates exist
if (!fs.existsSync(viewsHelper_1.ViewsHelper.getPathToEngine(sails.config.views.extension))) {
return cb(new Error('For now adminpanel hook could work only with Pug template engine.'));
}
require('./initializeAuthorization').default(cb);
sails.config.adminpanel.templateRootPath = viewsHelper_1.ViewsHelper.BASE_VIEWS_PATH;
sails.config.adminpanel.rootPath = path.resolve(__dirname + "/..");
hookTools_1.default.waitForHooks("adminpanel", requiredHooks, afterHook_1.default);
await hookTools_1.default.bindModels((0, path_1.resolve)(__dirname, "../models"));
// if (!sails.hooks.i18n.locales) sails.hooks.i18n.locales = []
// sails.hooks.i18n.locales = [...sails.hooks.i18n.locales, ...sails.config.adminpanel.translation.locales]
// .filter(function(item, pos, self) { return self.indexOf(item) == pos })
// Bind assets
await (0, bindAssets_1.default)();
cb();
}
exports.default = ToInitialize;
exports.default = default_1;
;

@@ -1,18 +0,16 @@

'use strict';
var _ = require('lodash');
var fs = require('fs');
var viewsHelper = require('../helper/viewsHelper');
import * as fs from 'fs';
import { ViewsHelper } from "../helper/viewsHelper";
import * as path from "path";
import bindAssets from "./bindAssets"
import HookTools from "./hookTools";
import {resolve} from "path";
import afterHook from "./afterHook";
export default async function(sails: any, cb) {
export default function ToInitialize(sails) {
/**
* List of hooks that required for adminpanel to work
*/
var requiredHooks = [
let requiredHooks: string[] = [
'blueprints',
'controllers',
'http',

@@ -24,51 +22,27 @@ 'orm',

return async function initialize(cb) {
// If disabled. Do not load anything
if (!sails.config.adminpanel) {
return cb();
}
// If disabled. Do not load anything
if (!sails.config.adminpanel) {
return cb();
}
//Check views engine and check if folder with templates exist
if (!fs.existsSync(ViewsHelper.getPathToEngine(sails.config.views.extension))) {
return cb(new Error('For now adminpanel hook could work only with Pug template engine.'));
}
// Set up listener to bind shadow routes when the time is right.
//
// Always wait until after router has bound static routes.
// If policies hook is enabled, also wait until policies are bound.
// If orm hook is enabled, also wait until models are known.
// If controllers hook is enabled, also wait until controllers are known.
var eventsToWaitFor = [];
eventsToWaitFor.push('router:after');
require('./initializeAuthorization').default(cb);
try {
/**
* Check hooks availability
*/
_.forEach(requiredHooks, function (hook) {
// if (!sails.hooks[hook]) {
// throw new Error('Cannot use `adminpanel` hook without the `' + hook + '` hook.');
// }
// eventsToWaitFor.push('hook:' + hook + ':loaded');
});
} catch(err) {
if (err) {
return cb(err);
}
}
sails.config.adminpanel.templateRootPath = ViewsHelper.BASE_VIEWS_PATH;
sails.config.adminpanel.rootPath = path.resolve(__dirname + "/..")
//Check views engine and check if folder with templates exist
if (!fs.existsSync(viewsHelper.getPathToEngine(sails.config.views.extension))) {
return cb(new Error('For now adminpanel hook could work only with Pug template engine.'));
}
HookTools.waitForHooks("adminpanel", requiredHooks, afterHook);
await HookTools.bindModels(resolve(__dirname, "../models"));
var initAuth = require('./initializeAuthorization')(sails, cb);
// if (!sails.hooks.i18n.locales) sails.hooks.i18n.locales = []
// sails.hooks.i18n.locales = [...sails.hooks.i18n.locales, ...sails.config.adminpanel.translation.locales]
// .filter(function(item, pos, self) { return self.indexOf(item) == pos })
// sails.after(eventsToWaitFor, require('../lib/afterHooksLoaded')(sails));
sails.on("lifted", require('../lib/afterHooksLoaded')(sails));
sails.config.adminpanel.templateRootPath = viewsHelper.BASE_VIEWS_PATH;
sails.config.adminpanel.rootPath = path.resolve(__dirname+"/..")
// Bind assets
await bindAssets(sails);
cb();
}
// Bind assets
await bindAssets();
cb();
};

@@ -1,6 +0,6 @@

var path = require('path');
var buildDictionary = require('sails-build-dictionary');
module.exports = function initializeAuthorization(sails, cb) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
let buildDictionary = require('sails-build-dictionary');
function initializeAuthorization(cb) {
/**

@@ -10,3 +10,3 @@ * Model

buildDictionary.optional({
dirname: path.resolve(__dirname, '../api/models'),
dirname: path.resolve(__dirname, '../models'),
filter: /^([^.]+)\.(js|coffee|litcoffee)$/,

@@ -21,3 +21,3 @@ replaceExpr: /^.*\//,

buildDictionary.optional({
dirname: path.resolve(__dirname, '../api/models'),
dirname: path.resolve(__dirname, '../models'),
filter: /(.+)\.attributes.json$/,

@@ -29,3 +29,2 @@ replaceExpr: /^.*\//,

return cb(err);
// console.log('admin > init > ', models, supplements, sails.models, sails.hooks.orm.models);

@@ -35,13 +34,11 @@ // var finalModels = {...models, ...supplements};

// sails.models = {...sails.models, ...models, ...supplements};
let finalModels = _.merge(models, supplements);
sails.hooks.orm.models = _.merge(finalModels || {}, sails.hooks.orm.models || {});
sails.models = _.merge(finalModels || {}, sails.models || {});
let finalModels = { ...models, ...supplements };
sails.hooks.orm.models = Object.assign(finalModels || {}, sails.hooks.orm.models || {});
sails.models = Object.assign(finalModels || {}, sails.models || {});
// console.log('sails.hooks.orm.models > ', sails.hooks.orm.models);
// console.log('sails.models > ', sails.models);
});
});
};
}
exports.default = initializeAuthorization;
;

@@ -1,7 +0,5 @@

'use strict';
var _ = require('lodash');
var async = require('async');
var queryString = require('querystring');
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestProcessor = void 0;
let queryString = require('querystring');
/**

@@ -13,4 +11,3 @@ * Default helper that will contain all methods

*/
module.exports = {
class RequestProcessor {
/**

@@ -23,11 +20,11 @@ * Will add new HTTP GET params to current params and will return a new string of GET query params.

*/
addGetParams: function(req, params) {
if (!req || !_.isPlainObject(req.query)) throw new Error('Wrong request given !');
if (!_.isPlainObject(params)) {
static addGetParams(req, params) {
if (!req || typeof req.query !== "object")
throw new Error('Wrong request given !');
if (typeof params !== "object") {
params = {};
}
var query = _.merge(_.clone(req.query), _.clone(params));
let query = { ...req.query, ...params };
return queryString.stringify(query);
},
}
/**

@@ -42,16 +39,19 @@ * upload file to server

*/
uploadFile: function(key, val, field, cb) {
if (!key || !val || !field) {
return null;
}
if (!req.file || !_.isFunction(req.file)) {
return null;
}
var options = {};
if (field.config.uploadPath) {
options.dirname = field.config.uploadPath;
}
req.file(key).upload(options, cb);
},
static uploadFile(key, val, field, cb) {
// !TODO fix this, there is no req in parameters
// if (!key || !val || !field) {
// return null;
// }
//
// if (!req.file || typeof req.file !== "function") {
// return null;
// }
//
// let options = {};
// if (field.config.uploadPath) {
// options.dirname = field.config.uploadPath;
// }
//
// req.file(key).upload(options, cb);
}
/**

@@ -64,26 +64,29 @@ * Will fetch all files from request. That should be stored

*/
processFiles: function(req, fields, cb) {
var fileFieldKeys = [];
_.forIn(fields, function(field, key) {
if (field.config && field.config.file) {
static async processFiles(req, fields) {
let fileFieldKeys = [];
for (let key in fields) {
if (fields[key].config && fields[key].config.file) {
fileFieldKeys.push(key);
}
});
}
if (fileFieldKeys.length == 0) {
return cb();
return;
}
var files = {};
async.eachLimit(fileFieldKeys, 10, function(key, done) {
req.file(key).upload(function(err, file) {
if (err) {
return done(err);
}
files[key] = file;
done();
});
}, function(err, result) {
cb(err, files);
});
},
let files = {};
for await (let elem of fileFieldKeys) {
try {
await req.file(elem).upload(function (err, file) {
if (err) {
return err;
}
files[elem] = file;
});
}
catch (e) {
sails.log.error(e);
return e;
}
}
return files;
}
/**

@@ -98,26 +101,23 @@ * Will try to find all fields that should be used in model

*/
processRequest: function(req, fields, cb) {
var that = this;
//that.processFiles(req, fields, function(err, result) {
// console.log(result);
//});
var booleanFields = _.pick(fields, function(field, key) {
return (field.config.type === 'boolean');
});
console.log("booleanFields",booleanFields);
var data = req.allParams();
var postParams = _.pick(data, function(value, key) {
return Boolean(fields[key]);
});
console.log("postParams",postParams);
_.forIn(postParams, function(val, key) {
var field = fields[key];
static processRequest(req, fields) {
let booleanFields = {};
for (let key of Object.keys(fields)) {
if (fields[key].config.type === 'boolean') {
booleanFields[key] = fields[key];
}
}
let data = req.allParams();
let postParams = {};
for (let key of Object.keys(data)) {
if (Boolean(fields[key])) {
postParams[key] = data[key];
}
}
for (let key in postParams) {
let field = fields[key];
if (field.model.type == 'boolean') {
if (val === '0') {
val = false;
if (postParams[key] === '0') {
postParams[key] = false;
}
postParams[key] = Boolean(val);
postParams[key] = Boolean(postParams[key]);
}

@@ -128,13 +128,15 @@ //if (field.model.type == 'integer') {

if (field.model.type == 'number') {
postParams[key] = parseFloat(val);
postParams[key] = parseFloat(postParams[key]);
}
if (field.model.type == 'json') {
try {
postParams[key] = JSON.parse(val);
} catch (error) {
sails.log.error("Adminpanel > processRequest: json parse error", error)
postParams[key] = JSON.parse(postParams[key]);
}
catch (error) {
// show error only when string and when string is not empty
if (typeof postParams[key] === "string" && postParams[key].replace(/(\r\n|\n|\r|\s{2,})/gm, "")) {
sails.log.error(`Adminpanel > processRequest: json parse error when parsing ${postParams[key]}`, error);
}
}
}
//remove empty field from list

@@ -144,30 +146,13 @@ if (field.model.type == 'association' && !postParams[key]) {

}
//if (field.config.file) {
// //need to upload file
// that.uploadFile(key, val, field, function(err, file) {
// console.log(file);
// });
//}
// if (field.model.type === 'date') {
// if (!postParams[key]) {
// delete postParams[key];
// }
// }
});
// Hook for seting boolean vars to false.
}
// Hook for setting boolean vars to false.
// HTTP wouldn't send data here
_.forEach(booleanFields, function(field, key) {
for (let key in booleanFields) {
if (!postParams[key]) {
postParams[key] = false;
}
});
//Check for fields that was not passed
//_.forIn(fields, function(field, key) {
// if (!postParams[key] && field.model.type == 'boolean') {
// postParams[key] = false;
// }
//});
//return cb(null, postParams);
}
return postParams;
}
};
}
exports.RequestProcessor = RequestProcessor;

@@ -7,7 +7,3 @@ {

"dependencies": {
"async": "^0.9.0",
"gulp-less": "^3.0.3",
"jimp": "^0.2.28",
"lodash": "^3.0.0",
"ncp": "^2.0.0",
"password-hash": "^1.2.2",

@@ -19,7 +15,16 @@ "sails-build-dictionary": "^0.10.1"

"devDependencies": {
"chai": "^1.10.0",
"@42pub/typed-sails": "^1.0.0",
"@types/waterline": "^0.13.4",
"@types/chai": "^4.3.0",
"@types/express": "^4.17.7",
"@types/mocha": "^9.0.0",
"dotenv": "^16.0.0",
"ts-node": "^10.7.0",
"typescript": "^3.9.7",
"chai": "^4.1.0",
"chance": "^0.7.3",
"mocha": "^2.0.1",
"mocha": "^8.1.1",
"node-sass": "^7.0.1",
"sails": "^1.4.0"
"sails": "^1.4.0",
"chai-http": "^4.3.0"
},

@@ -48,6 +53,9 @@ "homepage": "https://github.com/sails-adminpanel/sails-adminpanel#readme",

"scripts": {
"test": "node ./node_modules/mocha/bin/mocha -b --timeout 5000",
"test": "mocha -r ts-node/register test/bootstrap.ts './test/{,!(fixture)/**}/*.test.ts' --exit",
"test:js": "mocha test/bootstrap.js './test/{,!(fixture)/**}/*.test.js' --exit",
"test:init": "npm i && cd ./test/fixture && npm i --no-package-lock --prefix ./ && cd -",
"test:init-win": "npm i && cd test\\fixture && rmdir /s/Q node_modules && npm i --no-package-lock",
"build:css": "sass assets/styles:clarity/src"
},
"version": "1.2.13"
"version": "2.0.0-pre.9"
}
## Roadmap
add icons by package https://github.com/dmhendricks/file-icon-vectors

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc