0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Added limit service initial commit

- This provides some basic functionality and error message generation for adding host-based limits in Ghost
- It is a first-pass, needs unit tests etc
This commit is contained in:
Hannah Wolfe 2021-03-03 12:18:51 +00:00
parent 4bd8dc90ed
commit 201e133386
13 changed files with 366 additions and 0 deletions

View file

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/node'
]
};

View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2013-2021 Ghost Foundation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,39 @@
# Limit Service
## Install
`npm install @tryghost/limit-service --save`
or
`yarn add @tryghost/limit-service`
## Usage
## Develop
This is a mono repository, managed with [lerna](https://lernajs.io/).
Follow the instructions for the top-level repo.
1. `git clone` this repo & `cd` into it as usual
2. Run `yarn` to install top-level dependencies.
## Run
- `yarn dev`
## Test
- `yarn lint` run just eslint
- `yarn test` run lint and tests
# Copyright & License
Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).

View file

View file

@ -0,0 +1,26 @@
module.exports = {
members: {
currentCountQuery: async (db) => {
let result = await db.knex('members').count('id', {as: 'count'}).first();
return result.count;
}
},
staff: {
currentCountQuery: async (db) => {
let result = await db.knex('users')
.count('users.id', {as: 'count'})
.leftJoin('roles_users', 'users.id', 'roles_users.user_id')
.leftJoin('roles', 'roles_users.role_id', 'roles.id')
.whereNot('roles.name', 'Contributor').andWhereNot('users.status', 'inactive').first();
return result.count;
}
},
custom_integrations: {
currentCountQuery: async (db) => {
let result = await db.knex('integrations').count('id', {as: 'count'}).whereNotIn('type', ['internal', 'builtin']).first();
return result.count;
}
},
custom_themes: {}
};

View file

@ -0,0 +1,76 @@
const errors = require('@tryghost/errors');
const {MaxLimit, FlagLimit} = require('./limit');
const config = require('./config');
const _ = require('lodash');
class LimitService {
constructor() {
this.limits = {};
}
loadLimits({limits, helpLink, db}) {
Object.keys(limits).forEach((name) => {
if (config[name]) {
let limitConfig = _.merge({}, limits[name], config[name]);
if (_.has(limitConfig, 'max')) {
this.limits[name] = new MaxLimit({name: name, config: limitConfig, helpLink, db});
} else {
this.limits[name] = new FlagLimit({name: name, config: limitConfig, helpLink});
}
}
});
}
isLimited(limitName) {
return !!this.limits[limitName];
}
async checkIsOverLimit(limitName) {
if (!this.isLimited(limitName)) {
return;
}
try {
await this.limits[limitName].errorIfIsOverLimit();
return false;
} catch (error) {
if (error instanceof errors.HostLimitError) {
return true;
}
}
}
async checkWouldGoOverLimit(limitName) {
if (!this.isLimited(limitName)) {
return;
}
try {
await this.limits[limitName].errorIfWouldGoOverLimit();
return false;
} catch (error) {
if (error instanceof errors.HostLimitError) {
return true;
}
}
}
async errorIfIsOverLimit(limitName) {
if (!this.isLimited(limitName)) {
return;
}
await this.limits[limitName].errorIfIsOverLimit();
}
async errorIfWouldGoOverLimit(limitName) {
if (!this.isLimited(limitName)) {
return;
}
await this.limits[limitName].errorIfWouldGoOverLimit();
}
}
module.exports = LimitService;

View file

@ -0,0 +1,120 @@
const errors = require('@tryghost/errors');
const _ = require('lodash');
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
class Limit {
constructor({name, error, helpLink, db}) {
this.name = name;
this.error = error;
this.helpLink = helpLink;
this.db = db;
}
generateError() {
let errorObj = {
errorDetails: {
name: this.name
}
};
if (this.helpLink) {
errorObj.help = this.helpLink;
}
return errorObj;
}
}
class MaxLimit extends Limit {
constructor({name, config, helpLink, db}) {
super({name, error: config.error || '', helpLink, db});
if (!config.currentCountQuery) {
throw new errors.IncorrectUsageError('Attempted to setup a max limit without a current count query');
}
this.currentCountQueryFn = config.currentCountQuery;
this.max = config.max;
this.fallbackMessage = `This action would exceed the ${_.lowerCase(this.name)} limit on your current plan.`;
}
generateError(count) {
let errorObj = super.generateError();
let max = this.max;
errorObj.message = this.fallbackMessage;
if (this.error) {
try {
errorObj.message = _.template(this.error)({max, count});
} catch (e) {
errorObj.message = this.fallbackMessage;
}
}
errorObj.errorDetails.limit = max;
errorObj.errorDetails.total = count;
return new errors.HostLimitError(errorObj);
}
async currentCountQuery() {
return await this.currentCountQueryFn(this.db);
}
async errorIfWouldGoOverLimit() {
let currentCount = await this.currentCountQuery(this.db);
if ((currentCount + 1) > this.max) {
throw this.generateError(currentCount);
}
}
async errorIfIsOverLimit() {
let currentCount = await this.currentCountQuery(this.db);
if (currentCount > this.max) {
throw this.generateError(currentCount);
}
}
}
class FlagLimit extends Limit {
constructor({name, config, helpLink, db}) {
super({name, error: config.error || '', helpLink, db});
this.disabled = config.disabled;
this.fallbackMessage = `Your plan does not support ${_.lowerCase(this.name)}. Please upgrade to enable ${_.lowerCase(this.name)}.`;
}
generateError() {
let errorObj = super.generateError();
if (this.error) {
errorObj.message = this.error;
} else {
errorObj.message = this.fallbackMessage;
}
return new errors.HostLimitError(errorObj);
}
/**
* Flag limits are on/off so using a feature is always over the limit
*/
async errorIfWouldGoOverLimit() {
if (this.disabled) {
throw this.generateError();
}
}
/**
* Flag limits are on/off so we can't be over the limit
*/
async errorIfIsOverLimit() {
return;
}
}
module.exports = {
MaxLimit,
FlagLimit
};

View file

@ -0,0 +1,30 @@
{
"name": "@tryghost/limit-service",
"version": "0.0.0",
"repository": "https://github.com/TryGhost/Utils/tree/master/packages/limit-service",
"author": "Ghost Foundation",
"license": "MIT",
"main": "./lib/limit-service.js",
"exports": "./lib/limit-service.js",
"scripts": {
"dev": "echo \"Implement me!\"",
"test": "NODE_ENV=testing mocha './test/**/*.test.js'",
"lint": "eslint . --ext .js --cache",
"posttest": "yarn lint"
},
"files": [
"index.js",
"lib"
],
"publishConfig": {
"access": "public"
},
"devDependencies": {
"mocha": "8.3.0",
"should": "13.2.3",
"sinon": "9.2.4"
},
"dependencies": {
"lodash": "^4.17.21"
}
}

View file

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/test'
]
};

View file

@ -0,0 +1,10 @@
// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
describe('Hello world', function () {
it('Runs a test', function () {
// TODO: Write me!
'hello'.should.eql('hello');
});
});

View file

@ -0,0 +1,11 @@
/**
* Custom Should Assertions
*
* Add any custom assertions to this file.
*/
// Example Assertion
// should.Assertion.add('ExampleAssertion', function () {
// this.params = {operator: 'to be a valid Example Assertion'};
// this.obj.should.be.an.Object;
// });

View file

@ -0,0 +1,11 @@
/**
* Test Utilities
*
* Shared utils for writing tests
*/
// Require overrides - these add globals for tests
require('./overrides');
// Require assertions - adds custom should assertions
require('./assertions');

View file

@ -0,0 +1,10 @@
// This file is required before any test is run
// Taken from the should wiki, this is how to make should global
// Should is a global in our eslint test config
global.should = require('should').noConflict();
should.extend();
// Sinon is a simple case
// Sinon is a global in our eslint test config
global.sinon = require('sinon');