2017-09-17 14:36:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const knex = require('../lib/knex');
|
|
|
|
const dtHelpers = require('../lib/dt-helpers');
|
|
|
|
const shares = require('./shares');
|
2018-04-29 16:13:40 +00:00
|
|
|
const tools = require('../lib/tools');
|
2018-11-22 10:31:16 +00:00
|
|
|
const { enforce } = require('../lib/helpers');
|
2017-09-17 14:36:23 +00:00
|
|
|
|
|
|
|
async function listDTAjax(context, params) {
|
|
|
|
shares.enforceGlobalPermission(context, 'manageBlacklist');
|
|
|
|
|
|
|
|
return await dtHelpers.ajaxList(
|
|
|
|
params,
|
|
|
|
builder => builder
|
|
|
|
.from('blacklist'),
|
|
|
|
['blacklist.email']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-28 22:59:05 +00:00
|
|
|
async function search(context, offset, limit, search) {
|
2017-12-10 20:44:35 +00:00
|
|
|
return await knex.transaction(async tx => {
|
|
|
|
shares.enforceGlobalPermission(context, 'manageBlacklist');
|
|
|
|
|
|
|
|
search = '%' + search + '%';
|
|
|
|
|
2018-01-28 22:59:05 +00:00
|
|
|
const count = await tx('blacklist').where('email', 'like', search).count('* as count').first().count;
|
2017-12-10 20:44:35 +00:00
|
|
|
|
2018-01-28 22:59:05 +00:00
|
|
|
const rows = await tx('blacklist').where('email', 'like', search).offset(offset).limit(limit);
|
2017-12-10 20:44:35 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
emails: rows.map(row => row.email),
|
|
|
|
total: count
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-17 14:36:23 +00:00
|
|
|
async function add(context, email) {
|
2018-11-21 21:02:14 +00:00
|
|
|
enforce(email, 'Email has to be set');
|
|
|
|
|
2017-09-17 14:36:23 +00:00
|
|
|
return await knex.transaction(async tx => {
|
|
|
|
shares.enforceGlobalPermission(context, 'manageBlacklist');
|
|
|
|
|
|
|
|
const existing = await tx('blacklist').where('email', email).first();
|
|
|
|
if (!existing) {
|
|
|
|
await tx('blacklist').insert({email});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function remove(context, email) {
|
2018-11-21 21:02:14 +00:00
|
|
|
enforce(email, 'Email has to be set');
|
|
|
|
|
2017-09-17 14:36:23 +00:00
|
|
|
shares.enforceGlobalPermission(context, 'manageBlacklist');
|
|
|
|
await knex('blacklist').where('email', email).del();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function isBlacklisted(email) {
|
2018-11-21 21:02:14 +00:00
|
|
|
enforce(email, 'Email has to be set');
|
|
|
|
|
2017-09-17 14:36:23 +00:00
|
|
|
const existing = await knex('blacklist').where('email', email).first();
|
|
|
|
return !!existing;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function serverValidate(context, data) {
|
|
|
|
shares.enforceGlobalPermission(context, 'manageBlacklist');
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
if (data.email) {
|
|
|
|
const user = await knex('blacklist').where('email', data.email).first();
|
|
|
|
|
|
|
|
result.email = {};
|
|
|
|
result.email.invalid = await tools.validateEmail(data.email) !== 0;
|
|
|
|
result.email.exists = !!user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-09 22:55:44 +00:00
|
|
|
module.exports.listDTAjax = listDTAjax;
|
|
|
|
module.exports.add = add;
|
|
|
|
module.exports.remove = remove;
|
|
|
|
module.exports.search = search;
|
|
|
|
module.exports.isBlacklisted = isBlacklisted;
|
|
|
|
module.exports.serverValidate = serverValidate;
|