Merge branch 'refactor/moved_repo' into develop

This commit is contained in:
Alexis
2021-07-18 15:53:05 +02:00
43 changed files with 7368 additions and 0 deletions

View File

@@ -0,0 +1,230 @@
DROP DATABASE IF EXISTS auracle;
CREATE DATABASE auracle CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE auracle;
/* =========== PRIMARY TABLES =========== */
-- ROLES
CREATE TABLE IF NOT EXISTS `role` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`name` VARCHAR(255) NOT NULL,
`description` VARCHAR(255) NOT NULL,
PRIMARY KEY(`id`)
);
-- PERMISSIONS
CREATE TABLE IF NOT EXISTS `permission` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`slug` VARCHAR(255) NOT NULL,
PRIMARY KEY(`id`)
);
-- USERS
CREATE TABLE IF NOT EXISTS `user` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`uuid` VARCHAR(36) NOT NULL UNIQUE,
`name` VARCHAR(255) NOT NULL DEFAULT "Disciple",
`mail` VARCHAR(255) NOT NULL,
`avatar` VARCHAR(255),
`gender` VARCHAR(255),
`register_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`password` VARCHAR(255) NOT NULL,
`role_id` INT UNSIGNED NOT NULL DEFAULT 1,
`verified` BOOLEAN DEFAULT false,
`verification_token` VARCHAR(255),
`banned` BOOLEAN DEFAULT false,
PRIMARY KEY(`id`),
FOREIGN KEY(`role_id`) REFERENCES role(`id`)
);
-- API_TOKENS
CREATE TABLE IF NOT EXISTS `api_token` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`value` VARCHAR(255) NOT NULL,
`user_uuid` VARCHAR(36) NOT NULL UNIQUE,
PRIMARY KEY(`id`),
FOREIGN KEY(`user_uuid`) REFERENCES user(`uuid`)
);
-- SPELLS
CREATE TABLE IF NOT EXISTS `spell` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`name` VARCHAR(255) NOT NULL DEFAULT "Nom du sort",
`description` VARCHAR(1000) NOT NULL DEFAULT "Description du sort",
`level` INT UNSIGNED DEFAULT 0,
`charge` INT UNSIGNED DEFAULT 0,
`cost` VARCHAR(255) DEFAULT 0,
`is_ritual` BOOLEAN DEFAULT false,
`published` BOOLEAN DEFAULT false,
`public` BOOLEAN DEFAULT true,
`author_id` INT UNSIGNED NOT NULL DEFAULT 1,
PRIMARY KEY (`id`),
FOREIGN KEY(`author_id`) REFERENCES user(`id`)
);
-- META SCHOOLS
CREATE TABLE IF NOT EXISTS `meta_school` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`name` VARCHAR(255) NOT NULL DEFAULT "Nom de l'école mère",
`description` VARCHAR(255) DEFAULT "Description de l'école mère",
PRIMARY KEY (`id`)
);
-- SCHOOLS
CREATE TABLE IF NOT EXISTS `school` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`name` VARCHAR(255) NOT NULL DEFAULT "Nom de l'école",
`description` VARCHAR(255) DEFAULT "Description de l'école",
`published` BOOLEAN DEFAULT false,
`meta_school_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY(`meta_school_id`) REFERENCES meta_school(`id`)
);
-- COMMON INGREDIENTS
CREATE TABLE IF NOT EXISTS `ingredient` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`name` VARCHAR(255) NOT NULL DEFAULT "Langue de salamandre",
`description` VARCHAR(255) NOT NULL DEFAULT "Une langue de salamandre de feu encore chaude.",
`published` BOOLEAN DEFAULT false,
PRIMARY KEY (`id`)
);
-- COMMON VARIABLES
CREATE TABLE IF NOT EXISTS `variable` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`description` VARCHAR(255) NOT NULL DEFAULT "Nombre de créatures affectées",
`published` BOOLEAN DEFAULT false,
PRIMARY KEY (`id`)
);
/* ==== ASSOCIATION TABLES ==== */
-- SPELLS' SCHOOLS
-- One spell can have multiple (up to 3) schools
CREATE TABLE IF NOT EXISTS `spell_school` (
`spell_id` INT UNSIGNED NOT NULL,
`school_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`spell_id`, `school_id`),
FOREIGN KEY(`spell_id`) REFERENCES spell(`id`),
FOREIGN KEY(`school_id`) REFERENCES school(`id`)
);
-- SPELLS' VARIABLES
-- One spell can have multiple (up to 2) variables of cost
CREATE TABLE IF NOT EXISTS `spell_variable` (
`spell_id` INT UNSIGNED NOT NULL,
`variable_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`spell_id`, `variable_id`),
FOREIGN KEY(`spell_id`) REFERENCES spell(`id`),
FOREIGN KEY(`variable_id`) REFERENCES variable(`id`)
);
-- SPELLS' VARIABLES
-- One spell can have multiple ingredients
CREATE TABLE IF NOT EXISTS `spell_ingredient` (
`spell_id` INT UNSIGNED NOT NULL,
`ingredient_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`spell_id`, `ingredient_id`),
FOREIGN KEY(`spell_id`) REFERENCES spell(`id`),
FOREIGN KEY(`ingredient_id`) REFERENCES ingredient(`id`)
);
-- ROLES' PERMISSIONS
-- One role can have any number of permissions, or none at all
CREATE TABLE IF NOT EXISTS `role_permission` (
`role_id` INT UNSIGNED NOT NULL,
`permission_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`role_id`, `permission_id`),
FOREIGN KEY(`role_id`) REFERENCES role(`id`),
FOREIGN KEY(`permission_id`) REFERENCES permission(`id`)
);
-- Ajout d`une nouvelle ligne avant l`insert de description
DELIMITER $$
CREATE TRIGGER `multiLine` BEFORE INSERT ON `spell` FOR EACH ROW
BEGIN
SET NEW.description = replace(NEW.description, "<l>", "\n");
END$$
DELIMITER ;
/* =========== PRIMARY INSERTS =========== */
SET NAMES utf8;
-- CSV DATA
LOAD DATA INFILE 'C:/temp/auracle_data/permission.csv'
INTO TABLE `permission`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/role.csv'
INTO TABLE `role`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/role_permission.csv'
INTO TABLE `role_permission`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/user.csv'
INTO TABLE `user`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/api_token.csv'
INTO TABLE `api_token`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/spell.csv'
INTO TABLE `spell`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/meta_school.csv'
INTO TABLE `meta_school`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/school.csv'
INTO TABLE `school`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/ingredient.csv'
INTO TABLE `ingredient`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/variable.csv'
INTO TABLE `variable`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
LOAD DATA INFILE 'C:/temp/auracle_data/spell_school.csv'
INTO TABLE `spell_school`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

14
database/bookshelf.js Normal file
View File

@@ -0,0 +1,14 @@
// Setting up the database connection
const knex = require('knex')({
client: "mysql",
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
charset: "utf8"
},
});
const bookshelf = require('bookshelf')(knex);
module.exports = { bookshelf };

Binary file not shown.

BIN
database/data/school.ods Normal file

Binary file not shown.

BIN
database/data/spell.ods Normal file

Binary file not shown.

BIN
database/data/variable.ods Normal file

Binary file not shown.

35
functions.js Normal file
View File

@@ -0,0 +1,35 @@
const regexInt = RegExp(/^[1-9]\d*$/);
const regexXSS = RegExp(/<[^>]*script/);
// Check if int for param validation
const paramIntCheck = (req, res, next, input) => {
try {
if (regexInt.test(input)) {
next();
} else {
throw new Error;
}
} catch (err) {
res.status(err.code).send(JSON.stringify({
"message": "Le paramètre doit être un entier non-nul.",
"code": 403,
})
);
}
};
// Check if script injection attempt
const isXSSAttempt = (string) => {
return regexXSS.test(string);
};
// Check if object is null
const isEmptyObject = (obj) => {
return (Object.keys(obj).length === 0 && obj.constructor === Object);
};
module.exports = {
paramIntCheck,
isXSSAttempt,
isEmptyObject
};

41
index.js Normal file
View File

@@ -0,0 +1,41 @@
'use strict';
// MODULES
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const morgan = require('morgan');
const cors = require('cors'); // module to format the json response
require('dotenv').config();
// CONSTANTS
const port = 2814;
// Import routes
const routes = require('./routes');
// Builds app w/ express
let app = express();
app.use(bodyParser.json({ limit: '10kb' }));
app.use(cors({
origin: [
"http://localhost:8080",
],
credentials: true,
}));
app.use(morgan('dev'));
app.use(helmet());
// Server
app.listen(port, () => console.log(`App listening on port ${port}`));
// Entry route
app.use('/api/v1/', routes.auth);
// Routing
app.use('/api/v1/spells', routes.spells);
app.use('/api/v1/schools', routes.schools);
app.use('/api/v1/meta_schools', routes.meta_schools);
app.use('/api/v1/variables', routes.variables);
app.use('/api/v1/ingredients', routes.ingredients);
app.use('/api/v1/users', routes.users);

13
models/api-token-model.js Normal file
View File

@@ -0,0 +1,13 @@
const bookshelf = require('../database/bookshelf').bookshelf;
require('./user-model');
let APIToken = bookshelf.Model.extend({
tableName: 'api_token',
hidden: ['id'],
user() {
return this.belongsTo('User', 'user_uuid', 'uuid');
}
});
module.exports = bookshelf.model('APIToken', APIToken);

View File

@@ -0,0 +1,12 @@
const bookshelf = require('../database/bookshelf').bookshelf;
require('./spell-model');
let Ingredient = bookshelf.Model.extend({
tableName: 'ingredient',
spells() {
return this.belongsToMany('Spell', 'spell_ingredient');
}
});
module.exports = bookshelf.model('Ingredient', Ingredient);

View File

@@ -0,0 +1,12 @@
const bookshelf = require('../database/bookshelf').bookshelf;
require('./school-model');
let MetaSchool = bookshelf.Model.extend({
tableName: 'meta_school',
schools() {
return this.hasMany('School');
}
});
module.exports = bookshelf.model('MetaSchool', MetaSchool);

View File

@@ -0,0 +1,13 @@
const bookshelf = require('../database/bookshelf').bookshelf;
require('./role-model');
let Permission = bookshelf.Model.extend({
tableName: 'permission',
hidden: ['id'],
role() {
return this.belongsToMany('Role', 'role_permission');
}
});
module.exports = bookshelf.model('Permission', Permission);

12
models/role-model.js Normal file
View File

@@ -0,0 +1,12 @@
const bookshelf = require('../database/bookshelf').bookshelf;
require('./permission-model');
let Role = bookshelf.Model.extend({
tableName: 'role',
permissions() {
return this.belongsToMany('Permission', 'role_permission');
}
});
module.exports = bookshelf.model('Role', Role);

16
models/school-model.js Normal file
View File

@@ -0,0 +1,16 @@
const bookshelf = require('../database/bookshelf').bookshelf;
require('./spell-model');
require('./meta-school-model');
let School = bookshelf.Model.extend({
tableName: 'school',
spells() {
return this.belongsToMany('Spell', 'spell_school');
},
meta_schools() {
return this.belongsTo('MetaSchool', 'meta_school_id');
}
});
module.exports = bookshelf.model('School', School);

24
models/spell-model.js Normal file
View File

@@ -0,0 +1,24 @@
const bookshelf = require('../database/bookshelf').bookshelf;
require('./school-model');
require('./variable-model');
require('./ingredient-model');
let Spell = bookshelf.Model.extend({
tableName: 'spell',
hidden: [ 'author_id' ],
author() {
return this.belongsTo( 'User', 'author_id' );
},
schools() {
return this.belongsToMany( 'School', 'spell_school' );
},
variables() {
return this.belongsToMany( 'Variable', 'spell_variable' );
},
ingredients() {
return this.belongsToMany( 'Ingredient', 'spell_ingredient' );
}
});
module.exports = bookshelf.model('Spell', Spell);

17
models/user-model.js Normal file
View File

@@ -0,0 +1,17 @@
const bookshelf = require('../database/bookshelf').bookshelf;
require('./role-model');
require('./spell-model');
let User = bookshelf.Model.extend({
tableName: 'user',
hidden: ['password', 'role_id'],
role() {
return this.belongsTo('Role');
},
spells() {
return this.hasMany('Spell', 'author_id');
}
});
module.exports = bookshelf.model('User', User);

12
models/variable-model.js Normal file
View File

@@ -0,0 +1,12 @@
const bookshelf = require('../database/bookshelf').bookshelf;
require('./spell-model');
let Variable = bookshelf.Model.extend({
tableName: 'variable',
spells() {
return this.belongsToMany('Spell', 'spell_variable');
}
});
module.exports = bookshelf.model('Variable', Variable);

3907
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

73
package.json Normal file
View File

@@ -0,0 +1,73 @@
{
"name": "auracle-api",
"version": "1.0.0",
"description": "API for Auracle database",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/AlexisNP/spellsaurus.git"
},
"keywords": [
"api",
"rpg",
"spells",
"magic",
"database",
"ttrpg",
"dices",
"worldbuilding"
],
"author": "AlexisNP",
"license": "ISC",
"bugs": {
"url": "https://github.com/AlexisNP/spellsaurus/issues"
},
"homepage": "https://github.com/AlexisNP/spellsaurus#readme",
"dependencies": {
"bcrypt": "^5.0.0",
"bookshelf": "^1.1.1",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"handlebars": "^4.7.6",
"helmet": "^3.22.0",
"jsonschema": "^1.2.6",
"knex": "^0.21.1",
"morgan": "^1.10.0",
"mysql": "^2.18.1",
"nodemailer": "^6.4.17",
"uuid": "^8.2.0"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^7.18.0",
"eslint-plugin-import": "^2.22.1"
},
"eslintConfig": {
"env": {
"es6": true,
"browser": true
},
"parser": "babel-eslint",
"rules": {
"accessor-pairs": "warn",
"array-callback-return": "error",
"brace-style": "warn",
"consistent-return": "error",
"default-case": "error",
"eqeqeq": "warn",
"no-case-declarations": "error",
"no-empty-pattern": "warn",
"no-fallthrough": "error",
"no-unused-vars": "error",
"no-useless-catch": "error",
"no-var": "error",
"semi": "warn",
"no-extra-semi": "error",
"yoda": "warn"
}
}
}

View File

@@ -0,0 +1,206 @@
// Bookshelf
const bookshelf = require('../database/bookshelf').bookshelf;
const model = require('../models/ingredient-model');
// Model validation
const Validator = require('jsonschema').Validator;
const validator = new Validator();
const IngredientValidation = require("../validations/IngredientValidation");
validator.addSchema(IngredientValidation, "/IngredientValidation");
// Validations
const isXSSAttempt = require('../functions').isXSSAttempt;
const isEmptyObject = require('../functions').isEmptyObject;
class IngredientRepository {
constructor() {
}
getAll() {
return new Promise((resolve, reject) => {
new model()
.fetchAll({ withRelated: ['spells'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "Il n'existe aucun ingrédient disponible.",
"code": 404,
});
});
});
}
getOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ withRelated: ['spells'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "L'ingrédient en question n'a pas pu être trouvé.",
"code": 404,
});
});
});
}
getSpellsFromOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ withRelated: ['spells', 'spells.schools', 'spells.variables', 'spells.ingredients', 'spells.schools.meta_schools'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "Les sortilèges liés à cet ingrédient n'ont pas pu être récupérés.",
"code": 404,
});
});
});
}
addOne(igr) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (isEmptyObject(igr)) {
reject({
"message": "Le corps de la requête ne peut pas être vide.",
"code": 403,
});
} else if (!validator.validate(igr, IngredientValidation).valid) {
reject({
"message": `Le modèle d'ingrédient n'est pas respecté : ${validator.validate(s, IngredientValidation).errors}`,
"code": 403,
});
} else if (isXSSAttempt(igr.description)) {
reject({
"message": "Tentative d'injection XSS détectée, requête refusée.",
"code": 403,
});
} else {
bookshelf.transaction(t => {
return new model({
'name': igr.name,
'description': igr.description,
}).save(null, {
transacting: t
})
.catch(err => {
throw err;
});
})
.then(v => {
return v.load(['spells']);
})
.then(v => {
resolve(this.getOne(v.id));
})
.catch(err => {
console.log(err);
reject({
"message": "Une erreur d'insertion s'est produite.",
"code": 500,
});
});
}
});
}
updateOne(id, igr) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (isEmptyObject(igr)) {
reject({
"message": "Le corps de la requête ne peut pas être vide.",
"code": 403,
});
} else if (!validator.validate(igr, IngredientValidation).valid) {
reject({
"message": `Le modèle d'ingrédient n'est pas respecté : ${validator.validate(s, IngredientValidation).errors}`,
"code": 403,
});
} else if (isXSSAttempt(igr.description)) {
reject({
"message": "Tentative d'injection XSS détectée, requête refusée.",
"code": 403,
});
} else {
new model({ id: id })
.fetch({ require: true, withRelated: ['spells'] })
.then(v => {
bookshelf.transaction(t => {
return v.save({
'name': igr.name,
'description': igr.description,
}, {
method: 'update',
transacting: t
})
.catch(err => {
console.log(err);
throw err;
});
})
.then(v => {
return v.load(['spells']);
})
.then(v => {
resolve(this.getOne(v.id));
})
.catch(err => {
console.log(err);
reject({
"message": "Une erreur d'insertion s'est produite.",
"code": 500,
});
});
})
.catch(err => {
console.log(err);
reject({
"message": "L'ingrédient en question n'a pas été trouvé.",
"code": 404,
});
});
}
});
}
deleteOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ require: true, withRelated: ['spells'] })
.then(v => {
v.spells().detach();
v.destroy();
})
.then(() => {
resolve({
'message': 'Ingredient with ID ' + id + ' successfully deleted !'
});
})
.catch(err => {
console.log(err);
reject({
"message": "L'ingrédient en question n'a pas été trouvé.",
"code": 404,
});
});
});
}
}
module.exports = IngredientRepository;

View File

@@ -0,0 +1,51 @@
// Bookshelf
const model = require('../models/meta-school-model')
// Model validation
const Validator = require('jsonschema').Validator
const validator = new Validator()
const MetaSchoolValidation = require("../validations/MetaSchoolValidation")
validator.addSchema(MetaSchoolValidation, "/MetaSchoolValidation")
class MetaSchoolRepository {
constructor() {
}
getAll() {
return new Promise((resolve, reject) => {
new model()
.fetchAll({ withRelated: ['schools'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject({
"message": "Il n'existe aucune grande école disponible.",
"code": 404
});
})
})
}
getOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ withRelated: ['schools'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject({
"message": "La grande école en question n'a pas pu être trouvée.",
"code": 404
});
})
})
}
}
module.exports = MetaSchoolRepository

View File

@@ -0,0 +1,207 @@
// Bookshelf
const bookshelf = require('../database/bookshelf').bookshelf;
const model = require('../models/school-model');
// Model validation
const Validator = require('jsonschema').Validator;
const validator = new Validator();
const SchoolValidation = require("../validations/SchoolValidation");
validator.addSchema(SchoolValidation, "/SchoolValidation");
// Validations
const isXSSAttempt = require('../functions').isXSSAttempt;
const isEmptyObject = require('../functions').isEmptyObject;
class SchoolRepository {
constructor() {
}
getAll() {
return new Promise((resolve, reject) => {
new model()
.fetchAll({ withRelated: ['meta_schools'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "Il n'existe aucune école disponible.",
"code": 404,
});
});
});
}
getOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ withRelated: ['meta_schools'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "L'école en question n'a pas pu être trouvée.",
"code": 404,
});
});
});
}
getSpellsFromOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ withRelated: ['spells', 'spells.schools', 'spells.variables', 'spells.ingredients', 'spells.schools.meta_schools'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "Les sortilèges de cette école n'ont pas pu être récupérés.",
"code": 404,
});
});
});
}
addOne(s) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (isEmptyObject(s)) {
reject({
"message": "Le corps de la requête ne peut pas être vide.",
"code": 403,
});
} else if (!validator.validate(s, SchoolValidation).valid) {
reject({
"message": `Le modèle d'école n'est pas respecté : ${validator.validate(s, SchoolValidation).errors}`,
"code": 403,
});
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) {
reject({
"message": "Tentative d'injection XSS détectée, requête refusée.",
"code": 403,
});
} else {
bookshelf.transaction(t => {
return new model({
'name': s.name,
'description': s.description,
'meta_school_id': s.meta_school_id,
}).save(null, {
transacting: t
})
.catch(err => {
throw err;
});
})
.then(v => {
return v.load(['meta_schools']);
})
.then(v => {
resolve(this.getOne(v.id));
})
.catch(err => {
console.log(err);
reject({
"message": "Une erreur d'insertion s'est produite.",
"code": 500,
});
});
}
});
}
updateOne(id, s) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (isEmptyObject(s)) {
reject({
"message": "Le corps de la requête ne peut pas être vide.",
"code": 403,
});
} else if (!validator.validate(s, SchoolValidation).valid) {
reject({
"message": `Le modèle d'école n'est pas respecté : ${validator.validate(s, SchoolValidation).errors}`,
"code": 403,
});
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) {
reject({
"message": "Tentative d'injection XSS détectée, requête refusée.",
"code": 403,
});
} else {
new model({ id: id })
.fetch({ require: true, withRelated: ['meta_schools'] })
.then(v => {
bookshelf.transaction(t => {
return v.save({
'name': s.name,
'description': s.description,
'meta_school_id': s.meta_school_id
}, {
method: 'update',
transacting: t
})
.catch(err => {
console.log(err);
throw err;
});
})
.then(v => {
return v.load(['meta_schools']);
})
.then(v => {
resolve(this.getOne(v.id));
})
.catch(err => {
console.log(err);
reject({
"message": "Une erreur d'insertion s'est produite.",
"code": 500,
});
});
})
.catch(err => {
console.log(err);
reject({
"message": "L'école en question n'a pas été trouvée.",
"code": 404,
});
});
}
});
}
deleteOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ require: true, withRelated: ['spells', 'meta_schools'] })
.then(v => {
v.spells().detach();
v.destroy();
})
.then(() => {
resolve({
'message': 'School with ID ' + id + ' successfully deleted !'
});
})
.catch(err => {
console.log(err);
reject({
"message": "L'école en question n'a pas été trouvée.",
"code": 404,
});
});
});
}
}
module.exports = SchoolRepository;

View File

@@ -0,0 +1,351 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/bookshelf').bookshelf
const model = require('../models/spell-model')
// Model validation
const Validator = require('jsonschema').Validator
const validator = new Validator()
const SpellValidation = require("../validations/SpellValidation")
validator.addSchema(SpellValidation, "/SpellValidation")
// Validations
const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
class SpellRepository {
constructor() {
}
getAll(name, description, level, charge, cost, ritual) {
return new Promise((resolve, reject) => {
let query = new model();
if (name) {
query.where('name', 'like', `%${name}%`)
}
if (description) {
query.where('description', 'like', `%${description}%`)
}
if (level) {
query.where({ 'level': level })
}
if (charge) {
query.where({ 'charge': charge })
}
if (cost) {
query.where({ 'cost': cost })
}
if (ritual) {
query.where({ 'is_ritual': ritual })
}
query.fetchAll({ withRelated: ['schools.meta_schools', 'variables', 'ingredients', 'author'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err);
reject({
"message": "Il n'existe aucun sortilège disponible.",
"code": 404,
});
})
})
}
getAllPublic(name, description, level, charge, cost, ritual) {
return new Promise((resolve, reject) => {
let query = new model().where({ 'public': 1 })
if (name) {
query.where('name', 'like', `%${name}%`)
}
if (description) {
query.where('description', 'like', `%${description}%`)
}
if (level) {
query.where({ 'level': level })
}
if (charge) {
query.where({ 'charge': charge })
}
if (cost) {
query.where({ 'cost': cost })
}
if (ritual) {
query.where({ 'is_ritual': ritual })
}
query.fetchAll({ withRelated: ['schools.meta_schools', 'variables', 'ingredients', 'author'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "Il n'existe aucun sortilège disponible.",
"code": 404,
});
})
})
}
getPage(page) {
return new Promise((resolve, reject) => {
new model()
.where({ 'public': 1 })
.fetchPage({
pageSize: 20,
page: page,
withRelated: ['schools.meta_schools', 'variables', 'ingredients', 'author'],
})
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err);
reject({
"message": "La page de sortilèges n'a pas pu être chargée",
"code": 404,
});
})
})
}
getOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ withRelated: ['schools.meta_schools', 'variables', 'ingredients', 'author'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err);
reject({
"message": "Le sortilège en question n'a pas été trouvé.",
"code": 404,
});
})
})
}
addOne(s) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (isEmptyObject(s)) {
reject({
"message": "Le corps de la requête ne peut pas être vide.",
"code": 403,
});
} else if (!validator.validate(s, SpellValidation).valid) {
reject({
"message": `Le modèle de sortilège n'est pas respecté : ${validator.validate(s, SpellValidation).errors}`,
"code": 403,
});
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) {
reject({
"message": "Tentative d'injection XSS détectée, requête refusée.",
"code": 403,
});
} else {
bookshelf.transaction(t => {
return new model({
'name': s.name,
'description': s.description,
'level': s.level,
'charge': s.charge,
'cost': s.cost,
'is_ritual': s.is_ritual
}).save(null, {
transacting: t
})
.tap(spell => {
return spell
.schools()
.attach(s.schools, {
transacting: t
});
})
.tap(spell => {
return spell
.variables()
.attach(s.variables, {
transacting: t
});
})
.tap(spell => {
return spell
.ingredients()
.attach(s.ingredients, {
transacting: t
});
})
.catch(err => {
console.log(err);
reject({
"message": "Un attributs du sortilège a provoqué une erreur d'insertion.",
"code": 500,
});
})
})
.then(v => {
return v.load(['schools.meta_schools', 'variables', 'ingredients', 'author'])
})
.then(v => {
resolve(this.getOne(v.id))
})
.catch(err => {
console.log(err);
reject({
"message": "Le sortilège n'a pas pu être ajouté.",
"code": 500,
});
})
}
})
}
updateOne(id, s) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (isEmptyObject(s)) {
reject({
"message": "Le corps de la requête ne peut pas être vide.",
"code": 403,
});
} else if (!validator.validate(s, SpellValidation).valid) {
reject({
"message": `Le modèle de sortilège n'est pas respecté : ${validator.validate(s, SpellValidation).errors}`,
"code": 403,
});
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) {
reject({
"message": "Tentative d'injection XSS détectée, requête refusée.",
"code": 403,
});
} else {
new model({ id: id })
.fetch({ require: true, withRelated: ['schools.meta_schools', 'variables', 'ingredients', 'author'] })
.then(v => {
bookshelf.transaction(t => {
return v.save({
'name': s.name,
'description': s.description,
'level': s.level,
'charge': s.charge,
'cost': s.cost,
'is_ritual': s.is_ritual
}, {
method: 'update',
transacting: t
})
// Detaches AND attaches pivot tables, dw about it
.tap(spell => {
if (s.schools) {
let schools = spell.related('school');
return spell.schools().detach(schools, { transacting: t });
}
return spell
})
.tap(spell => {
if (s.variables) {
let variables = spell.related('variable');
return spell.variables().detach(variables, { transacting: t });
}
return spell;
})
.tap(spell => {
if (s.ingredients) {
let ingredients = spell.related('ingredient');
return spell.ingredients().detach(ingredients, { transacting: t });
}
return spell;
})
.tap(spell => {
return spell
.schools()
.attach(s.schools, {
transacting: t
});
})
.tap(spell => {
return spell
.variables()
.attach(s.variables, {
transacting: t
});
})
.tap(spell => {
return spell
.ingredients()
.attach(s.ingredients, {
transacting: t
});
})
.catch(err => {
console.log(err);
reject({
"message": "Un attributs du sortilège a provoqué une erreur d'insertion.",
"code": 500,
})
})
})
.then(v => {
return v.load(['schools.meta_schools', 'variables', 'ingredients', 'author']);
})
.then(v => {
resolve(this.getOne(v.id));
})
.catch(err => {
console.log(err);
reject({
"message": "Une erreur de chargement est survenue.",
"code": 500,
});
})
})
.catch(err => {
console.log(err);
reject({
"message": "Le sortilège en question n'a pas été trouvé.",
"code": 404,
});
})
}
})
}
deleteOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ require: true, withRelated: ['schools.meta_schools', 'variables', 'ingredients', 'author'] })
.then(v => {
v.schools().detach();
v.variables().detach();
v.ingredients().detach();
v.destroy();
})
.then(() => {
resolve({
"message": `Le sortilège #${id} a été supprimé avec succès.`,
"code": 200,
});
})
.catch(err => {
console.log(err);
reject({
"message": "Le sortilège en question n'a pas été trouvé.",
"code": 404,
});
})
})
}
}
module.exports = SpellRepository

View File

@@ -0,0 +1,562 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/bookshelf').bookshelf;
const model = require('../models/user-model');
const token_model = require('../models/api-token-model');
// Hashing and passwords
const bcrypt = require('bcrypt');
const { v4: uuidv4 } = require('uuid');
// Mailing methods
const mails = require('../smtp/mails');
// Model validation
const Validator = require('jsonschema').Validator;
const validator = new Validator();
const UserValidation = require("../validations/UserValidation");
validator.addSchema(UserValidation, "/UserValidation");
// Validations
const isXSSAttempt = require('../functions').isXSSAttempt;
const isEmptyObject = require('../functions').isEmptyObject;
class UserRepository {
constructor() {
}
/**
* Fetches all users in the dabatase.
*
* @returns { Promise }
* Fulfilled data: Array of user objects.
*/
getAll() {
return new Promise((resolve, reject) => {
new model()
.fetchAll({ withRelated: ['role.permissions'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err)
reject({
"message": "Erreur de base de données, les utilisateurs n'ont pas pu être récupérés.",
"code": 500,
});
})
})
}
/**
* Fetches a user object associated with the uuid.
*
* @param { string } uuid
* @param { boolean } full
* Whether the password should also be fetched. (should never be true unless you want to log the user)
*
* @returns { Promise }
* Fulfilled data: Queried user object.
*/
getOneByUUID(uuid, full) {
return new Promise((resolve, reject) => {
if (!(uuid)) {
reject({
"message": "La requête doit renseigner un uuid.",
"code": 400,
})
}
new model()
.where({ 'uuid': uuid })
.fetch({ withRelated: ['role.permissions'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true, visibility: !full }));
})
.catch(() => {
reject({
"message": "L'utilisateur avec cet UUID n'a pas été trouvé.",
"code": 404,
});
})
})
}
/**
* Fetches a user object associated with the mail address.
*
* @param { string } mail
* @param { boolean } full
* Whether the password should also be fetched. (should never be true unless you want to log the user)
*
* @returns { Promise }
* Fulfilled data: Queried user object.
*/
getOneByEmail(mail, full) {
return new Promise((resolve, reject) => {
if (!(mail)) {
reject({
"message": "La requête doit renseigner un email.",
"code": 400,
})
}
new model()
.where({ 'mail': mail })
.fetch({ withRelated: ['role'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true, visibility: !full }));
})
.catch(() => {
reject({
"message": "L'utilisateur avec cet email n'a pas été trouvé.",
"code": 404,
});
});
})
}
/**
* Fetches all spells linked to a user's uuid
*
* @param { string } uuid
*
* @returns
*/
getSpellsFromOne(uuid) {
return new Promise((resolve, reject) => {
if (!(uuid)) {
reject({
"message": "La requête doit renseigner un uuid.",
"code": 400,
})
}
new model()
.where({ 'uuid': uuid })
.fetch({ withRelated: ['role', 'spells.schools.meta_schools', 'spells.variables', 'spells.ingredients'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(() => {
reject({
"message": "Les sorts liés à cet utilisateur n'ont pas été trouvés.",
"code": 404,
});
});
})
}
/**
* Registers a user based on the model at ./models/user-model.js.
*
* @param { object} u
* User object
*
* @returns { Promise }
* Fulfilled data: Queried user object.
*/
addOne(u) {
return new Promise(async (resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (isEmptyObject(u)) {
reject({
"message": "Le corps de requête ne peut être vide.",
"code": 403,
})
} else if (!validator.validate(u, UserValidation).valid) {
reject({
"message": "Structure de la requête invalide - " + validator.validate(u, UserValidation).errors,
"code": 403,
})
} else if (isXSSAttempt(u.name) || isXSSAttempt(u.password) || isXSSAttempt(u.mail)) {
reject({
"message": "Essai d'injection détecté, avortement de la requête.",
"code": 403,
})
} else {
let hash = await bcrypt.hash(u.password, 10);
let uuid = uuidv4();
let verification_token = uuidv4();
this.checkIfEmailAvailable(u.mail)
.then(() => {
bookshelf.transaction(t => {
return new model({
'uuid': uuid,
'name': u.name,
'mail': u.mail,
'password': hash,
'verification_token': verification_token,
})
.save(null, {
transacting: t
})
.catch(err => {
console.log(err);
reject({
"message": "Un attribut de l'utilisateur a provoqué une erreur d'insertion.",
"code": 500,
});
})
})
.then(() => {
return this.getOneByUUID(uuid, false)
})
.then(newUser => {
// Send a mail to the new user's email with a link to verification url
mails.sendRegistrationMail({
user: {
name: newUser.name,
mail: newUser.mail,
token: verification_token,
}
});
// Then resolves the api call
resolve({
"message": `Compte utilisateur #${newUser.id} créé avec succès.`,
"code": 201,
"user": newUser,
});
})
.catch(err => {
console.log(err);
resolve({
"message": "Une erreur s'est produite en créant votre compte. Veuillez réessayer ultérieurement ou contactez l'administrateur.",
"code": 500,
})
})
})
.catch(err => {
reject(err)
})
}
})
}
/**
* Verifies an account based on a private UUID token
*
* @param { string } token
* A UUID v4 identification token provided at registration and on special demands
*
* @returns Redirects to login page
*/
verifyUser(token) {
return new Promise((resolve, reject) => {
new model()
.where({ 'verification_token': token })
.fetch()
.then(v => {
bookshelf.transaction(t => {
return v.save({
'verification_token': null,
'verified': 1,
}, {
method: 'update',
transacting: t
})
})
.then(() => {
resolve({
"message": "Insérez ici une future redirection vers le client.",
"code": 202,
})
})
})
.catch(() => {
reject({
"message": "Le lien de vérification ne semble pas correct.",
"code": 404,
})
})
});
}
/**
* Logs a user by comparing the dual mail/password inputs
*
* @param { string } mail
* @param { string } password
*
* @return { Promise }
* Fulfilled data: Queried user object.
*/
logUser(mail, password) {
return new Promise((resolve, reject) => {
this.getOneByEmail(mail, true)
.then(async fetchedUser => {
let match = await bcrypt.compare(password, fetchedUser.password);
// Makes sure no hash gets out
delete fetchedUser.password;
// If you found a user...
if (match) {
// If they're banned...
if (fetchedUser.banned) {
reject({
"message": `L'utilisateur #${fetchedUser.name} a été banni, la connexion est impossible.`,
"code": 403,
});
// If they're not verified...
} else if (!fetchedUser.verified) {
reject({
"message": `L'utilisateur #${fetchedUser.name} n'as pas été vérifié, le compte doit être activé avant la connexion.`,
"code": 401,
});
} else {
resolve({
"message": `L'utilisateur #${fetchedUser.name} s'est connecté.`,
"code": 200,
"user": fetchedUser,
});
}
} else {
reject({
"message": "Les informations de connexions sont erronées.",
"code": 400,
});
}
})
.catch(err => {
reject(err);
})
})
}
/**
* Generate a token for the user to use the API
* Requires mail and password for verifying the user
*
* @param { string } mail
* @param { string } password
*
* @returns { Promise }
* Fulfilled data: A unique UUID token string.
*/
genAPIToken(mail, password) {
return new Promise((resolve, reject) => {
this.logUser(mail, password)
.then(v => {
let user = v.user;
let new_token = uuidv4();
bookshelf.transaction(t => {
return new token_model({
'value': new_token,
'user_uuid': user.uuid,
})
.save(null, {
transacting: t
})
.catch(err => {
// If the account already has an API key linked...
if (err.errno === 1062) {
this.fetchAPIKey(user.uuid)
.then(old_api_key => {
reject({
"message": "Votre compte a déjà généré une clé d'API.",
"code": 409,
"API_key": old_api_key.value,
});
});
// Default errors
} else {
throw err
}
});
})
.then(api_key => {
resolve({
"message": "La clé d'API a été généré.",
"code": 201,
"API_key": api_key,
})
})
.catch(err => {
console.log(err);
reject({
"message": "La génération de jeton d'API n'a pas pu être conclue.",
"code": 500,
});
})
})
.catch(err => {
reject(err);
})
})
}
checkAPITokenPerms(token, permissions) {
return new Promise(async (resolve, reject) => {
// If the request doesn't have a token, reject it.
if (!token) {
reject({
"message": "Vous devez utiliser un jeton d'API dans l'en-tête de votre requête.",
"code": 401,
})
}
// Fetches user from token
new token_model()
.where({ 'value': token })
.fetch({ withRelated: 'user.role.permissions' })
// Catches not found errors
.catch(err => {
if (err instanceof token_model.NotFoundError) {
reject({
"message": "Ce jeton n'est affilié à aucun compte.",
"code": 404,
});
} else {
reject({
"message": "Le jeton ou l'utilisateur n'a pas pu être récupéré.",
"code": 500,
});
}
})
.then(fullToken => {
token = fullToken.toJSON({ omitPivot: true });
// If the token is associated with a banned user...
if (token.user.banned) {
reject({
"message": "Le jeton est lié à un utilisateur banni, il n'est donc plus utilisable.",
"code": 401,
});
}
// If the token is associated with a non-verified user...
if (!token.user.verified) {
reject({
"message": "Le jeton est lié à un utilisateur non-vérifié, il n'est donc pas utilisable.",
"code": 401,
});
}
let user_permissions = token.user.role.permissions;
// Convert user_perm to array
user_permissions.forEach((user_perm, i) => {
user_permissions[i] = user_perm.slug;
});
// Loops to check if the person has all the permissions required...
permissions.forEach(perm => {
// If not, reject...
if (!user_permissions.includes(perm)) {
reject({
"message": "Permissions insuffisantes.",
"code": 401
})
}
});
// If everything went well, resolve the promise.
resolve({
"message": "Credentials accepted.",
"code": 200
});
})
// Unhandled errors
.catch(() => {
reject({
"message": "Une erreur inconnue est survenue.",
"code": 500,
});
});
})
}
/**
* Check if the email that was input is available for account creation.
*
* @param {string} mail
*
* @returns { Promise }
* Fulfilled: HTTP 200 if email is available.
* Rejected: HTTP 400-409 if email is already used.
*/
checkIfEmailAvailable(mail) {
return new Promise((resolve, reject) => {
if (!(mail)) {
reject({
"message": "La requête doit renseigner un email.",
"code": 400,
})
}
if (!this.validateMail(mail)) {
reject({
"message": "La requête n'est pas un email valide.",
"code": 400,
})
}
this.getOneByEmail(mail, false)
.then(() => {
reject({
"message": "Cet email est déjà lié à un compte.",
"code": 409,
})
})
.catch(() => {
resolve({
"message": "Cet email est disponible.",
"code": 200,
})
})
})
}
/**
* Fetches the associated api_token from a user uuid.
*
* @param { string } uuid
*
* @returns { Promise }
* Fulfilled data: Queried API token object
*/
fetchAPIKey(uuid) {
return new Promise((resolve, reject) => {
new token_model()
.where({ 'user_uuid': uuid })
.fetch()
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
reject(err);
})
})
}
/**
* Whether a mail is correctly formed and ripe for receiving, ie: xxx@yyy.zzz.
*
* @param { string } mail
*
* @returns { boolean }
*/
validateMail(mail) {
const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regex.test(String(mail).toLowerCase());
}
}
module.exports = UserRepository

View File

@@ -0,0 +1,204 @@
// Bookshelf
const bookshelf = require('../database/bookshelf').bookshelf;
const model = require('../models/variable-model');
// Model validation
const Validator = require('jsonschema').Validator;
const validator = new Validator();
const VariableValidation = require("../validations/VariableValidation");
validator.addSchema(VariableValidation, "/VariableValidation");
// Validations
const isXSSAttempt = require('../functions').isXSSAttempt;
const isEmptyObject = require('../functions').isEmptyObject;
class VariableRepository {
constructor() {
}
getAll() {
return new Promise((resolve, reject) => {
new model()
.fetchAll({ withRelated: ['spells'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "Il n'existe aucune variable disponible.",
"code": 404,
});
});
});
}
getOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ withRelated: ['spells'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "La variable en question n'a pas pu être trouvée.",
"code": 404,
});
});
});
}
getSpellsFromOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ withRelated: ['spells', 'spells.schools', 'spells.variables', 'spells.ingredients', 'spells.schools.meta_schools'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }));
})
.catch(err => {
console.log(err);
reject({
"message": "Les sortilèges liés à cette variable n'ont pas pu être récupérés.",
"code": 404,
});
});
});
}
addOne(vr) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (isEmptyObject(vr)) {
reject({
"message": "Le corps de la requête ne peut pas être vide.",
"code": 403,
});
} else if (!validator.validate(vr, VariableValidation).valid) {
reject({
"message": `Le modèle de variable n'est pas respecté : ${validator.validate(s, VariableValidation).errors}`,
"code": 403,
});
} else if (isXSSAttempt(vr.description)) {
reject({
"message": "Tentative d'injection XSS détectée, requête refusée.",
"code": 403,
});
} else {
bookshelf.transaction(t => {
return new model({
'description': vr.description,
}).save(null, {
transacting: t
})
.catch(err => {
throw err;
});
})
.then(v => {
return v.load(['spells']);
})
.then(v => {
resolve(this.getOne(v.id));
})
.catch(err => {
console.log(err);
reject({
"message": "Une erreur d'insertion s'est produite.",
"code": 500,
});
});
}
});
}
updateOne(id, vr) {
return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't
if (isEmptyObject(vr)) {
reject({
"message": "Le corps de la requête ne peut pas être vide.",
"code": 403,
});
} else if (!validator.validate(vr, VariableValidation).valid) {
reject({
"message": `Le modèle de variable n'est pas respecté : ${validator.validate(s, VariableValidation).errors}`,
"code": 403,
});
} else if (isXSSAttempt(vr.description)) {
reject({
"message": "Tentative d'injection XSS détectée, requête refusée.",
"code": 403,
});
} else {
new model({ id: id })
.fetch({ require: true, withRelated: ['spells'] })
.then(v => {
bookshelf.transaction(t => {
return v.save({
'description': vr.description,
}, {
method: 'update',
transacting: t
})
.catch(err => {
console.log(err);
throw err;
});
})
.then(v => {
return v.load(['spells']);
})
.then(v => {
resolve(this.getOne(v.id));
})
.catch(err => {
console.log(err);
reject({
"message": "Une erreur d'insertion s'est produite.",
"code": 500,
});
});
})
.catch(err => {
console.log(err);
reject({
"message": "La variable en question n'a pas été trouvée.",
"code": 404,
});
});
}
});
}
deleteOne(id) {
return new Promise((resolve, reject) => {
new model()
.where({ 'id': id })
.fetch({ require: true, withRelated: ['spells'] })
.then(v => {
v.spells().detach();
v.destroy();
})
.then(() => {
resolve({
'message': 'Variable with ID ' + id + ' successfully deleted !'
});
})
.catch(err => {
console.log(err);
reject({
"message": "La variable en question n'a pas été trouvée.",
"code": 404,
});
});
});
}
}
module.exports = VariableRepository;

28
routes/auth.js Normal file
View File

@@ -0,0 +1,28 @@
// Router
const express = require('express');
let router = express.Router();
// Repository
const UserRepository = require('../repositories/user-repository');
const Users = new UserRepository();
// ROUTES
// GEN API TOKEN
const generateAPIToken = (mail, password) => {
return Users.genAPIToken(mail, password)
.catch(err => {
throw err;
});
};
router.get('/genToken', async (req, res) => {
generateAPIToken(req.body.mail, req.body.password)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(err));
});
});
module.exports = router;

18
routes/index.js Normal file
View File

@@ -0,0 +1,18 @@
const spells = require('./spells');
const schools = require('./schools');
const meta_schools = require('./meta_schools');
const variables = require('./variables');
const ingredients = require('./ingredients');
const users = require('./users');
const auth = require('./auth');
module.exports = {
auth,
spells,
schools,
meta_schools,
ingredients,
variables,
users,
};

183
routes/ingredients.js Normal file
View File

@@ -0,0 +1,183 @@
// Router
const express = require('express');
let router = express.Router();
// AuthGuard
const authGuard = require('./middleware/authGuard');
// Repository
const IngredientRepository = require('../repositories/ingredient-repository');
const Ingredients = new IngredientRepository();
// Functions
const functions = require('../functions');
// ROUTES
// GET ALL ------------------
const getIngredients = () => {
return Ingredients.getAll()
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/',
async (req, res) => {
getIngredients()
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET ONE ------------------
const getIngredient = (id) => {
return Ingredients.getOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/:id/',
async (req, res) => {
getIngredient(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET SPELLS FROM ONE ------------------
const getSpellsFromOne = (id) => {
return Ingredients.getSpellsFromOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/:id/spells',
async (req, res) => {
getSpellsFromOne(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// CREATE ONE ------------------
const addIngredient = (igr) => {
return Ingredients.addOne(igr)
.catch(err => {
console.log(err);
throw err;
});
};
router.post(
'/',
authGuard(['SUBMIT_INGREDIENTS']),
async (req, res) => {
addIngredient(req.body)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// UPDATE ONE ------------------
const updateIngredient = (id, igr) => {
return Ingredients.updateOne(id, igr)
.catch(err => {
console.log(err);
throw err;
});
};
router.put(
'/:id/',
authGuard(['SUBMIT_INGREDIENTS', 'MODIFY_INGREDIENTS']),
async (req, res) => {
updateIngredient(req.params.id, req.body)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// DELETE ONE ------------------
const deleteIngredient = (id) => {
return Ingredients.deleteOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.delete(
'/:id/',
authGuard(['SUBMIT_INGREDIENTS', 'MODIFY_INGREDIENTS', 'DELETE_INGREDIENTS']),
async (req, res) => {
deleteIngredient(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// Param validations
router.param('id', functions.paramIntCheck);
module.exports = router;

65
routes/meta_schools.js Normal file
View File

@@ -0,0 +1,65 @@
// Router
const express = require('express');
let router = express.Router();
// Functions
const functions = require('../functions');
// Repository
const MetaSchoolRepository = require('../repositories/meta-school-repository');
const MetaSchools = new MetaSchoolRepository();
// ROUTES
// GET ALL ------------------
const getMetaSchools = () => {
return MetaSchools.getAll()
.catch(err => {
console.log(err);
throw err;
});
};
router.get('/', async (req, res) => {
getMetaSchools()
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET ONE ------------------
const getMetaSchool = (id) => {
return MetaSchools.getOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.get('/:id/', async (req, res) => {
getMetaSchool(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// Param validations
router.param('id', functions.paramIntCheck);
module.exports = router;

View File

@@ -0,0 +1,23 @@
// Repository
const UserRepository = require('../../repositories/user-repository');
const Users = new UserRepository();
// AUTHGUARD
const authGuard = (permissions) => {
return async (req, res, next) => {
// Get token from headers
let api_token = req.headers['auracle_key'];
// Uses repo to validate the associated perms with the token
Users.checkAPITokenPerms(api_token, permissions)
.then(() => {
next();
})
.catch(err => {
res.status(err.code).send(JSON.stringify(err));
});
};
};
module.exports = authGuard;

182
routes/schools.js Normal file
View File

@@ -0,0 +1,182 @@
// Router
const express = require('express');
let router = express.Router();
// AuthGuard
const authGuard = require('./middleware/authGuard');
// Repository
const SchoolRepository = require('../repositories/school-repository');
const Schools = new SchoolRepository();
// Functions
const functions = require('../functions');
// ROUTES
// GET ALL ------------------
const getSchools = () => {
return Schools.getAll()
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/',
async (req, res) => {
getSchools()
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET ONE ------------------
const getSchool = (id) => {
return Schools.getOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/:id/',
async (req, res) => {
getSchool(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET SPELLS FROM ONE ------------------
const getSpellsFromOne = (id) => {
return Schools.getSpellsFromOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/:id/spells',
async (req, res) => {
getSpellsFromOne(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// CREATE ONE ------------------
const addSchool = (s) => {
return Schools.addOne(s)
.catch(err => {
console.log(err);
throw err;
});
};
router.post(
'/',
authGuard(['SUBMIT_SCHOOL']),
async (req, res) => {
addSchool(req.body)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// UPDATE ONE ------------------
const updateSchool = (id, s) => {
return Schools.updateOne(id, s)
.catch(err => {
console.log(err);
throw err;
});
};
router.put(
'/:id/',
authGuard(['SUBMIT_SCHOOLS', 'MODIFY_SCHOOLS']),
async (req, res) => {
updateSchool(req.params.id, req.body)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// DELETE ONE ------------------
const deleteSchool = (id) => {
return Schools.deleteOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.delete(
'/:id/',
authGuard(['SUBMIT_SCHOOLS', 'MODIFY_SCHOOLS', 'DELETE_SCHOOLS']),
async (req, res) => {
deleteSchool(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// Param validations
router.param('id', functions.paramIntCheck);
module.exports = router;

209
routes/spells.js Normal file
View File

@@ -0,0 +1,209 @@
// Router
const express = require('express');
let router = express.Router();
// AuthGuard
const authGuard = require('./middleware/authGuard');
// Repository
const SpellReposity = require('../repositories/spell-repository');
const Spells = new SpellReposity();
// Functions
const functions = require('../functions');
// ROUTES
// GET ALL PUBLIC ------------------
const getPublicSpells = (name, description, level, charge, cost, ritual) => {
return Spells.getAllPublic(name, description, level, charge, cost, ritual)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'//:name?/:description?/:level?/:charge?/:cost?/:ritual?/',
async (req, res) => {
getPublicSpells(req.query.name, req.query.description, req.query.level, req.query.charge, req.query.cost, req.query.ritual)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET ALL ------------------
const getSpells = (name, description, level, charge, cost, ritual) => {
return Spells.getAll(name, description, level, charge, cost, ritual)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/private/:name?/:description?/:level?/:charge?/:cost?/:ritual?/',
authGuard(['SECRET_SPELLS']),
async (req, res) => {
getSpells(req.query.name, req.query.description, req.query.level, req.query.charge, req.query.cost, req.query.ritual)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET SOME ------------------
const getSomeSpells = (page) => {
return Spells.getPage(page)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/page/:page',
async (req, res) => {
getSomeSpells(req.params.page)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET ONE ------------------
const getSpell = (id) => {
return Spells.getOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/:id/',
async (req, res) => {
getSpell(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// CREATE ONE ------------------
const addSpell = (s) => {
return Spells.addOne(s)
.catch(err => {
console.log(err);
throw err;
});
};
router.post(
'/',
authGuard(['SUBMIT_SPELLS']),
async (req, res) => {
addSpell(req.body)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// UPDATE ONE ------------------
const updateSpell = (id, s) => {
return Spells.updateOne(id, s)
.catch(err => {
console.log(err);
throw err;
});
};
router.put(
'/:id/',
authGuard(['SUBMIT_SPELLS', 'MODIFY_SPELLS']),
async (req, res) => {
updateSpell(req.params.id, req.body)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// DELETE ONE ------------------
const deleteSpell = (id) => {
return Spells.deleteOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.delete(
'/:id/',
authGuard(['SUBMIT_SPELLS', 'MODIFY_SPELLS', 'DELETE_SPELLS']),
async (req, res) => {
deleteSpell(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// Param validations
router.param('id', functions.paramIntCheck);
router.param('page', functions.paramIntCheck);
module.exports = router;

178
routes/users.js Normal file
View File

@@ -0,0 +1,178 @@
// Router
const express = require('express');
let router = express.Router();
// AuthGuard
// const authGuard = require('./middleware/authGuard');
// Repository
const UserRepository = require('../repositories/user-repository');
const Users = new UserRepository();
// ROUTES
// GET ALL ------------------
const getUsers = () => {
return Users.getAll()
.catch(err => {
throw err;
});
};
router.get('/', async (req, res) => {
getUsers()
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET ONE FROM UUID ------------------
const getUserByUUID = (uuid) => {
return Users.getOneByUUID(uuid)
.catch(err => {
throw err;
});
};
router.get('/:uuid/', async (req, res) => {
getUserByUUID(req.params.uuid)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET SPELLS FROM ONE ------------------
const getSpellsFromUser = (uuid) => {
return Users.getSpellsFromOne(uuid)
.catch(err => {
throw err;
});
};
router.get('/:uuid/spells', async (req, res) => {
getSpellsFromUser(req.params.uuid)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// CHECK IF MAIL IS AVAILABLE ------------------
const checkIfEmailAvailable = (mail) => {
return Users.checkIfEmailAvailable(mail)
.catch(err => {
throw err;
});
};
router.get('/available/:mail/', async (req, res) => {
checkIfEmailAvailable(req.params.mail)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// CREATE ONE ------------------
const addUser = (u) => {
return Users.addOne(u)
.catch(err => {
throw err;
});
};
router.post('/', async (req, res) => {
addUser(req.body)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// VERIFY A USER
const verifyUser = (token) => {
return Users.verifyUser(token)
.catch(err => {
throw err;
});
};
router.get('/verification/:token', async (req, res) => {
verifyUser(req.params.token)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// LOG A USER ------------------
const logUser = (mail, password) => {
return Users.logUser(mail, password)
.catch(err => {
throw err;
});
};
router.post('/login', async (req, res) => {
logUser(req.body.mail, req.body.password)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
module.exports = router;

185
routes/variables.js Normal file
View File

@@ -0,0 +1,185 @@
'use strict';
// Router
const express = require('express');
let router = express.Router();
// AuthGuard
const authGuard = require('./middleware/authGuard');
// Repository
const VariableRepository = require('../repositories/variable-repository');
const Variables = new VariableRepository();
// Functions
const functions = require('../functions');
// ROUTES
// GET ALL ------------------
const getvariables = () => {
return Variables.getAll()
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/',
async (req, res) => {
getvariables()
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET ONE ------------------
const getVariable = (id) => {
return Variables.getOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/:id/',
async (req, res) => {
getVariable(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// GET SPELLS FROM ONE ------------------
const getSpellsFromOne = (id) => {
return Variables.getSpellsFromOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.get(
'/:id/spells',
async (req, res) => {
getSpellsFromOne(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// CREATE ONE ------------------
const addVariable = (vr) => {
return Variables.addOne(vr)
.catch(err => {
console.log(err);
throw err;
});
};
router.post(
'/',
authGuard(['SUBMIT_VARIABLES']),
async (req, res) => {
addVariable(req.body)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// UPDATE ONE ------------------
const updateVariable = (id, vr) => {
return Variables.updateOne(id, vr)
.catch(err => {
console.log(err);
throw err;
});
};
router.put(
'/:id/',
authGuard(['SUBMIT_VARIABLES', 'MODIFY_VARIABLES']),
async (req, res) => {
updateVariable(req.params.id, req.body)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// DELETE ONE ------------------
const deleteVariable = (id) => {
return Variables.deleteOne(id)
.catch(err => {
console.log(err);
throw err;
});
};
router.delete(
'/:id/',
authGuard(['SUBMIT_VARIABLES', 'MODIFY_VARIABLES', 'DELETE_VARIABLES']),
async (req, res) => {
deleteVariable(req.params.id)
.then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.send(JSON.stringify(v));
})
.catch(err => {
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
);
});
});
// Param validations
router.param('id', functions.paramIntCheck);
module.exports = router;

14
smtp/config.js Normal file
View File

@@ -0,0 +1,14 @@
const nodemailer = require('nodemailer');
const transport = nodemailer.createTransport({
host: "smtp.mailtrap.io",
port: 2525,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
}
});
module.exports = {
transport,
};

61
smtp/mails.js Normal file
View File

@@ -0,0 +1,61 @@
const smtp = require('./config');
const fs = require('fs');
const handlebars = require('handlebars');
// Sender address for mail service
const sender = 'tymos@auracle.io';
// Fetches a HTML template file for parsing
const getTemplateFile = (path) => {
return new Promise((resolve, reject) => {
fs.readFile(path, { encoding: 'utf-8' }, (err, html) => {
if (err) {
reject(err);
} else {
resolve(html);
}
});
});
};
/**
* SEND REGISTRATION MAIL FUNCTION
* @param {Object} data
* - user
* - name
* - mail
* - token
*/
const sendRegistrationMail = (data) => {
getTemplateFile(__dirname + '/templates/template-sign-up.html')
.then(html => {
let template = handlebars.compile(html);
let template_parsed = template(data);
let message = {
from: sender,
to: data.user.mail,
subject: 'Inscription Auracle.io',
html: template_parsed,
};
smtp.transport.sendMail(message, (err, info) => {
if (err) {
throw err;
} else {
console.log(info);
}
});
})
.catch(err => {
console.log(err);
});
};
// const sendBanEmail = (date) => {
// return null;
// };
module.exports = {
sendRegistrationMail,
};

View File

@@ -0,0 +1,114 @@
<!DOCTYPE html>
<html>
<head>
<title>Template Email Auracle Inscription</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700&family=Playfair+Display:wght@700;800;900&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--black: 52, 58, 64;
--white: #FFF;
--primary: 89, 158, 244;
}
body {
color: rgb(var(--black));
font-family: 'Lato', sans-serif;
max-width: 100%;
}
.display {
font-family: 'Playfair Display', serif;
}
h1 {
font-size: 32px;
}
p:not(:last-child) {
margin-bottom: 15px;
}
.italics {
font-style: italic;
}
.wrapper {
position: relative;
padding: 10vh 5vw;
background: url('https://i.imgur.com/IxEKKEY.png') center center fixed repeat;
}
.wrapper:before {
display: block;
content: '';
position: absolute;
top: 0;
height: 120px;
left: 0;
right: 0;
background-color: rgb(var(--primary));
z-index: 0;
}
.container {
position: relative;
background-color: var(--white);
z-index: 5;
}
header,
footer {
text-align: center;
color: var(--white);
background-color: rgb(var(--black));
}
header {
padding: 15px;
}
main {
padding: 30px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container">
<header>
<h1 class="display">Bienvenue sur Auracle, {{ user.name }}!</h1>
</header>
<main>
<p>Votre compte Auracle a bien été enregistré et est rattaché à cette adresse mail.</p>
<p>Cependant, afin de garantir la sécurité de vos données, votre compte doit être vérifié avant votre
première connexion. Vous pouvez
<a href="http://localhost:2814/api/v1/users/verification/{{ user.token }}">
suivre ce lien afin de confirmer votre inscription.
</a>
</p>
<p>Si vous rencontrez des problèmes, n'hésitez pas à envoyer un mail à
<a href="mailto:support@auracle.io">
support@auracle.io
</a>
</p>
<p><strong>Merci de votre soutien !</strong></p>
<p class="italics">Bien sincèrement, Izàc Tymos.</p>
</main>
<footer>
<div class="icon">
</div>
</footer>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,11 @@
const Ingredient = {
"id": "/IngredientValidation",
"type": Object,
"properties": {
"name": { "type": "string" },
"description": { "type": "string" }
},
"required": ["name", "description"]
};
module.exports = Ingredient;

View File

@@ -0,0 +1,12 @@
const MetaSchool = {
"id": "/MetaSchoolValidation",
"type": Object,
"properties": {
"name": { "type": "string" },
"description": { "type": "string" },
"schools": { "type": "array" }
},
"required": ["name", "description"]
};
module.exports = MetaSchool;

View File

@@ -0,0 +1,12 @@
const School = {
"id": "/SchoolValidation",
"type": Object,
"properties": {
"name": { "type": "string" },
"description": { "type": "string" },
"meta_school_id": { "type": "number" },
},
"required": ["name", "description", "meta_school_id"]
};
module.exports = School;

View File

@@ -0,0 +1,39 @@
const Spell = {
"id": "/SpellValidation",
"type": Object,
"properties": {
"name": { "type": "string" },
"description": { "type": "string" },
"level": { "type": "number" },
"charge": { "type": "number" },
"cost": { "type": "string" },
"is_ritual": { "type": "boolean" },
"schools": {
"type": { "type": "array" },
"items": {
"properties": {
"id": { "type": "number" },
}
}
},
"variables": {
"type": { "type": "array" },
"items": {
"properties": {
"id": { "type": "number" },
}
}
},
"ingredients": {
"type": { "type": "array" },
"items": {
"properties": {
"id": { "type": "number" },
}
}
}
},
"required": ["name", "description"]
};
module.exports = Spell;

View File

@@ -0,0 +1,12 @@
const User = {
"id": "/UserValidation",
"type": Object,
"properties": {
"name": { "type": "string" },
"mail": { "type": "string" },
"password": { "type": "string" },
},
"required": ["name", "password", "mail"]
};
module.exports = User;

View File

@@ -0,0 +1,10 @@
const Variable = {
"id": "/VariableValidation",
"type": Object,
"properties": {
"description": { "type": "string" },
},
"required": ["description"]
};
module.exports = Variable;