Update
This commit is contained in:
parent
80b0ea401b
commit
c620e19449
23 changed files with 919 additions and 0 deletions
8
app-backend/config/host.config.js
Normal file
8
app-backend/config/host.config.js
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
module.exports = {
|
||||||
|
youtube: {
|
||||||
|
baseAPIUrl: 'https://www.youtube.com/'
|
||||||
|
},
|
||||||
|
dailymotion: {
|
||||||
|
baseAPIUrl: 'https://api.dailymotion.com/'
|
||||||
|
}
|
||||||
|
};
|
||||||
4
app-backend/config/mongodb.config.js
Normal file
4
app-backend/config/mongodb.config.js
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
module.exports = {
|
||||||
|
prodUrl: "mongodb://mongodb:27017/polynotfound",
|
||||||
|
devUrl: "mongodb://127.0.0.1:27017/polynotfound"
|
||||||
|
};
|
||||||
9
app-backend/config/response.config.js
Normal file
9
app-backend/config/response.config.js
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
function sendMessage (res, successCode, data, token=null) {
|
||||||
|
res.status(200).json({ status: 'success', successCode: successCode, token: token, data: data });
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendError (res, statusCode, errorCode, reason, token=null) {
|
||||||
|
res.status(statusCode).json({ status: 'error', errorCode: errorCode, data: { token: token, reason: reason }});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { sendMessage, sendError };
|
||||||
18
app-backend/config/role.config.js
Normal file
18
app-backend/config/role.config.js
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
module.exports = {
|
||||||
|
User: {
|
||||||
|
name: "user",
|
||||||
|
permission: 0
|
||||||
|
},
|
||||||
|
Advertiser: {
|
||||||
|
name: "advertiser",
|
||||||
|
permission: 5
|
||||||
|
},
|
||||||
|
Admin: {
|
||||||
|
name: "admin",
|
||||||
|
permission: 10
|
||||||
|
},
|
||||||
|
SuperAdmin: {
|
||||||
|
name: "superAdmin",
|
||||||
|
permission: 1000
|
||||||
|
}
|
||||||
|
};
|
||||||
101
app-backend/config/sessionJWT.config.js
Normal file
101
app-backend/config/sessionJWT.config.js
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
const sessionJWTConfig = require ('jsonwebtoken');
|
||||||
|
require('dotenv').config({ path: './app/.env' });
|
||||||
|
const {sendError} = require ("./response.config");
|
||||||
|
|
||||||
|
if(process.env.JWTRS256_PRIVATE_KEY === undefined || process.env.JWTRS256_PUBLIC_KEY === undefined){
|
||||||
|
console.log('Error Env Variables');
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Env variables received');
|
||||||
|
const JWTRS256_PRIVATE_KEY = Buffer.from(process.env.JWTRS256_PRIVATE_KEY, 'base64').toString('utf-8');
|
||||||
|
const JWTRS256_PUBLIC_KEY = Buffer.from(process.env.JWTRS256_PUBLIC_KEY, 'base64').toString('utf-8');
|
||||||
|
|
||||||
|
|
||||||
|
function createSessionJWT (id, login, role) {
|
||||||
|
return sessionJWTConfig.sign(
|
||||||
|
{
|
||||||
|
id: id,
|
||||||
|
login: login,
|
||||||
|
role: role,
|
||||||
|
midExp: Math.floor(Date.now() / 1000) + 1800
|
||||||
|
},
|
||||||
|
JWTRS256_PRIVATE_KEY,
|
||||||
|
{
|
||||||
|
algorithm: 'RS256',
|
||||||
|
expiresIn: '1h'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createSessionCookie(req, res, payload) {
|
||||||
|
let jwtToken;
|
||||||
|
if (typeof payload.id !== 'undefined' &&
|
||||||
|
typeof payload.login !== 'undefined' &&
|
||||||
|
typeof payload.role !== 'undefined' &&
|
||||||
|
typeof payload.midExp !== 'undefined' &&
|
||||||
|
(Math.floor(Date.now() / 1000) <= payload.midExp)) {
|
||||||
|
jwtToken = req.headers.cookie;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
jwtToken = createSessionJWT(payload.id, payload.login, payload.role);
|
||||||
|
}
|
||||||
|
res.cookie('SESSIONID', jwtToken, {httpOnly:true, secure:false});
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeSessionCookie(sessionid) {
|
||||||
|
if (typeof sessionid === 'undefined') {
|
||||||
|
return {id: -1, login: -1, role: -1};
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const token = sessionJWTConfig.verify(
|
||||||
|
sessionid,
|
||||||
|
JWTRS256_PUBLIC_KEY,
|
||||||
|
{algorithms: ['RS256']});
|
||||||
|
return {token: token};
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
return {id: -1, login: -1, role: -1};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSession(sessionid) {
|
||||||
|
return decodeSessionCookie(sessionid);
|
||||||
|
}
|
||||||
|
module.exports.getSession = getSession
|
||||||
|
|
||||||
|
function setSessionCookie (req, res, session) {
|
||||||
|
createSessionCookie(req, res, session);
|
||||||
|
}
|
||||||
|
module.exports.setSessionCookie = setSessionCookie;
|
||||||
|
|
||||||
|
function getToken(session) {
|
||||||
|
if (typeof session === 'undefined' || typeof session.token === 'undefined') return -1;
|
||||||
|
return session.token;
|
||||||
|
}
|
||||||
|
module.exports.getToken = getToken;
|
||||||
|
|
||||||
|
function checkLogin(req, res, role=null){
|
||||||
|
if(typeof req.cookies !== 'undefined'){
|
||||||
|
const session = getSession(req.cookies.SESSIONID);
|
||||||
|
const token = getToken(session);
|
||||||
|
if(token.login === 'undefined' || token.login === -1){
|
||||||
|
return sendError(res, 500, -1, "User not authenticated.");
|
||||||
|
} else {
|
||||||
|
if(role === null){
|
||||||
|
return token;
|
||||||
|
} else {
|
||||||
|
if(token.role !== 'undefined' &&
|
||||||
|
((Array.isArray(role) && role.includes(token.role)) ||
|
||||||
|
( typeof role === 'object' && token.role.permission >= role.permission))){
|
||||||
|
return token;
|
||||||
|
} else {
|
||||||
|
return sendError(res, 500, -1, "User doesn't have permission.", token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return sendError(res, 500, -1, "Cookies don't exist.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module.exports.checkLogin = checkLogin;
|
||||||
53
app-backend/controllers/ad.controller.js
Normal file
53
app-backend/controllers/ad.controller.js
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
const db = require("../models/mongodb.model");
|
||||||
|
const {sendError, sendMessage} = require ("../config/response.config");
|
||||||
|
const {checkLogin} = require("../config/sessionJWT.config");
|
||||||
|
const roles = require("../config/role.config");
|
||||||
|
const Ads = db.ads;
|
||||||
|
|
||||||
|
// Create a new Ad
|
||||||
|
exports.create = (req, res) => {
|
||||||
|
const token = checkLogin(req, res, [roles.Admin, roles.Advertiser]);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Ads.create not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Retrieve all Ads
|
||||||
|
exports.findAll = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Ads.findAll not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Retrieve a single Ad with id
|
||||||
|
exports.findOne = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Ads.findOne not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update a Ad with id
|
||||||
|
exports.update = (req, res) => {
|
||||||
|
const token = checkLogin(req, res, [roles.Admin, roles.Advertiser]);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Ads.update not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delete a Ad with id
|
||||||
|
exports.delete = (req, res) => {
|
||||||
|
const token = checkLogin(req, res, [roles.Admin, roles.Advertiser]);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Ads.delete not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delete all Ads
|
||||||
|
exports.deleteAll = (req, res) => {
|
||||||
|
const token = checkLogin(req, res, [roles.Admin, roles.Advertiser]);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Ads.deleteAll not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
52
app-backend/controllers/playlist.controller.js
Normal file
52
app-backend/controllers/playlist.controller.js
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
const db = require("../models/mongodb.model");
|
||||||
|
const {sendError, sendMessage} = require ("../config/response.config");
|
||||||
|
const {checkLogin} = require("../config/sessionJWT.config");
|
||||||
|
const Playlist = db.playlists;
|
||||||
|
|
||||||
|
// Create a new Playlist
|
||||||
|
exports.create = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Playlist.create not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Retrieve all Playlists
|
||||||
|
exports.findAll = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Playlist.findAll not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Retrieve a single Playlist with id
|
||||||
|
exports.findOne = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Playlist.findOne not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update a Playlist with id
|
||||||
|
exports.update = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Playlist.update not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delete a Playlist with id
|
||||||
|
exports.delete = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Playlist.delete not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delete all Playlists
|
||||||
|
exports.deleteAll = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Playlist.deleteAll not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
325
app-backend/controllers/user.controller.js
Normal file
325
app-backend/controllers/user.controller.js
Normal file
|
|
@ -0,0 +1,325 @@
|
||||||
|
const db = require("../models/mongodb.model");
|
||||||
|
const {sendError, sendMessage} = require ("../config/response.config");
|
||||||
|
const {checkLogin, setSessionCookie, getSession, getToken} = require("../config/sessionJWT.config");
|
||||||
|
const ObjectId = require('mongoose').Types.ObjectId;
|
||||||
|
const roles = require("../config/role.config");
|
||||||
|
const User = db.users;
|
||||||
|
const History = db.histories;
|
||||||
|
|
||||||
|
// Authenticate a User
|
||||||
|
exports.auth = (req, res) => {
|
||||||
|
// Validate request
|
||||||
|
if (!req.body.login || !req.body.hashPass) {
|
||||||
|
sendError(res, 400,-1,"Content can not be empty . (login and hashPass needed)");
|
||||||
|
} else{
|
||||||
|
// Check User in the database
|
||||||
|
User
|
||||||
|
.findOne({login: req.body.login, hashPass: req.body.hashPass, active: true}, {role: true})
|
||||||
|
.then(data => {
|
||||||
|
if (data !== null){
|
||||||
|
setSessionCookie(req, res, {id: data._id, login: req.body.login, role: data.role});
|
||||||
|
return sendMessage(res, 1, {id: data._id, login: req.body.login, role: data.role});
|
||||||
|
} else {
|
||||||
|
setSessionCookie(req, res, {id: -1, login: -1, role: -1 });
|
||||||
|
return sendError(res, 500, -1, "Invalid login or password.");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res, 500,-1,err.message || "Some error occurred while authenticating the User.");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Logout a User
|
||||||
|
exports.logout = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
setSessionCookie(req, res, {id: -1, login: -1, role: -1});
|
||||||
|
return sendMessage(res, 1, {message: "User disconnected"}, token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create and Save a new User
|
||||||
|
exports.create = (req, res) => {
|
||||||
|
// Validate request
|
||||||
|
if (!req.body.login || !req.body.hashPass || !req.body.mail) {
|
||||||
|
sendError(res, 400,-1,"Content can not be empty . (login, hashPass and mail needed");
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
User.exists({login: req.body.login}, function (err, docs){
|
||||||
|
if(err){
|
||||||
|
sendError(res, 500,-1,err.message || "Some error occurred while checking if the User already exists.");
|
||||||
|
} else{
|
||||||
|
if(docs === null) {
|
||||||
|
let user;
|
||||||
|
const session = getSession(req.cookies.SESSIONID);
|
||||||
|
const token = getToken(session);
|
||||||
|
if((typeof token.login === 'undefined' || token.login === -1) && typeof req.body.role === 'undefined'){
|
||||||
|
user = new User({
|
||||||
|
login: req.body.login,
|
||||||
|
hashPass: req.body.hashPass,
|
||||||
|
mail: req.body.mail,
|
||||||
|
profilePictureUrl: req.body.profilePictureUrl ? req.body.profilePictureUrl : null,
|
||||||
|
dateOfBirth: req.body.dateOfBirth ? req.body.dateOfBirth : null,
|
||||||
|
gender: req.body.gender ? req.body.gender : null,
|
||||||
|
interests: req.body.interests ? req.body.interests : null
|
||||||
|
});
|
||||||
|
} else if(typeof token.role !== 'undefined' &&
|
||||||
|
typeof req.body.role !== 'undefined' &&
|
||||||
|
typeof req.body.role.permission !== 'undefined' &&
|
||||||
|
token.role.permission > req.body.role.permission) {
|
||||||
|
user = new User({
|
||||||
|
login: req.body.login,
|
||||||
|
hashPass: req.body.hashPass,
|
||||||
|
mail: req.body.mail,
|
||||||
|
role: req.body.role,
|
||||||
|
profilePictureUrl: req.body.profilePictureUrl ? req.body.profilePictureUrl : null,
|
||||||
|
dateOfBirth: req.body.dateOfBirth ? req.body.dateOfBirth : null,
|
||||||
|
gender: req.body.gender ? req.body.gender : null,
|
||||||
|
interests: req.body.interests ? req.body.interests : null
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
user = new User({
|
||||||
|
login: req.body.login,
|
||||||
|
hashPass: req.body.hashPass,
|
||||||
|
mail: req.body.mail,
|
||||||
|
profilePictureUrl: req.body.profilePictureUrl ? req.body.profilePictureUrl : null,
|
||||||
|
dateOfBirth: req.body.dateOfBirth ? req.body.dateOfBirth : null,
|
||||||
|
gender: req.body.gender ? req.body.gender : null,
|
||||||
|
interests: req.body.interests ? req.body.interests : null
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Save User in the database
|
||||||
|
user
|
||||||
|
.save(user)
|
||||||
|
.then(data => {
|
||||||
|
data.active = undefined;
|
||||||
|
data.hashPass = undefined; // Hiding hashPass on return
|
||||||
|
sendMessage(res, 1, data)
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res, 500,-1,err.message || "Some error occurred while creating the User.");
|
||||||
|
});
|
||||||
|
} else{
|
||||||
|
sendError(res, 500, -1, err || `User ${req.body.login} already exists.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Retrieve all Users from the database if at least admin.
|
||||||
|
exports.findAll = (req, res) => {
|
||||||
|
const token = checkLogin(req, res, roles.Admin);
|
||||||
|
if(token){
|
||||||
|
let query = {};
|
||||||
|
|
||||||
|
const ids = req.query.ids;
|
||||||
|
let condition = ids ? {$in: ids} : {};
|
||||||
|
query._id = condition;
|
||||||
|
|
||||||
|
const login = req.query.login;
|
||||||
|
condition = login ? { $regex: new RegExp(login), $options: "i" } : undefined;
|
||||||
|
query.login = condition;
|
||||||
|
|
||||||
|
const mail = req.query.mail;
|
||||||
|
condition = mail ? { $regex: new RegExp(mail), $options: "i" } : undefined;
|
||||||
|
query.mail = condition;
|
||||||
|
|
||||||
|
const role = req.query.role;
|
||||||
|
condition = role ? { $regex: new RegExp(role), $options: "i" } : undefined;
|
||||||
|
query.role = condition;
|
||||||
|
|
||||||
|
const active = req.query.active;
|
||||||
|
condition = active ? active : undefined;
|
||||||
|
query.active = condition;
|
||||||
|
|
||||||
|
User.find(condition, {hashPass: false})
|
||||||
|
.then(data => {
|
||||||
|
sendMessage(res, 1, data, token)
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res,500,-1,err.message || "Some error occurred while retrieving users.", token);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Find a single User by session id
|
||||||
|
exports.findOne = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token && typeof req.params.id !== 'undefined') {
|
||||||
|
let id = null;
|
||||||
|
if(typeof token.id !== 'undefined' && req.params.id === token.id){
|
||||||
|
id = req.params.id;
|
||||||
|
} else {
|
||||||
|
if (typeof token.role !== 'undefined' &&
|
||||||
|
typeof token.role.permission !== 'undefined' &&
|
||||||
|
token.role.permission >= roles.Admin.permission) {
|
||||||
|
id = req.params.id;
|
||||||
|
} else {
|
||||||
|
sendError(res, 500, -1, `Cannot find User with id=${id}. User do not have the permission`, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(id){
|
||||||
|
User.findById(id, {hashPass: false})
|
||||||
|
.then(data => {
|
||||||
|
if(data){
|
||||||
|
sendMessage(res, 1, data, token);
|
||||||
|
} else {
|
||||||
|
sendError(res,404,-1,"User not found with id " + id, token);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res,500,-1,err.message || "Error retrieving User with id=" + id, token);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sendError(res, 500, -1, `No id given`, token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update a User by the id in the request
|
||||||
|
exports.update = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token && typeof req.params.id !== 'undefined') {
|
||||||
|
let id = null;
|
||||||
|
if(typeof token.id !== 'undefined' && req.params.id === token.id){
|
||||||
|
id = req.params.id;
|
||||||
|
} else {
|
||||||
|
if (typeof token.role !== 'undefined' &&
|
||||||
|
typeof token.role.permission !== 'undefined' &&
|
||||||
|
token.role.permission >= roles.Admin.permission) {
|
||||||
|
id = req.params.id;
|
||||||
|
} else {
|
||||||
|
sendError(res, 500, -1, `Cannot update User with id=${id}. User do not have the permission`, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(id){
|
||||||
|
User.findById(id, {hashPass: false})
|
||||||
|
.then(user => {
|
||||||
|
if(user){
|
||||||
|
const history = new History({update: user});
|
||||||
|
history
|
||||||
|
.save(history)
|
||||||
|
.then(data => {
|
||||||
|
if(data) {
|
||||||
|
User.findByIdAndUpdate(id, req.body, {useFindAndModify: false})
|
||||||
|
.then(data => {
|
||||||
|
data.hashPass = undefined;
|
||||||
|
console.log(data);
|
||||||
|
if (data) {
|
||||||
|
sendMessage(res, 1, {message: "User was updated successfully."}, token);
|
||||||
|
} else {
|
||||||
|
sendError(res, 404, -1, `Cannot update User with id=${id}. Maybe User was not found.`, token);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res, 500, -1, err.message || "Error updating User with id=" + id, token);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res, 500,-1,err.message || "Some error occurred while creating the User.");
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
sendError(res,404,-1,"User not found with id " + id, token);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res,500,-1,err.message || "Error retrieving User with id=" + id, token);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sendError(res, 500, -1, `No id given`, token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delete a User with the specified id in the request
|
||||||
|
exports.delete = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token && typeof req.params.id !== 'undefined') {
|
||||||
|
let id = null;
|
||||||
|
if(typeof token.id !== 'undefined' && req.params.id === token.id){
|
||||||
|
id = req.params.id;
|
||||||
|
} else {
|
||||||
|
if (typeof token.role !== 'undefined' &&
|
||||||
|
typeof token.role.permission !== 'undefined' &&
|
||||||
|
token.role.permission >= roles.Admin.permission) {
|
||||||
|
id = req.params.id;
|
||||||
|
} else {
|
||||||
|
sendError(res, 500, -1, `Cannot delete User with id=${id}. User do not have the permission`, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(id && ObjectId.isValid(id)){
|
||||||
|
User.findById(id, {hashPass: false})
|
||||||
|
.then(user => {
|
||||||
|
if(user){
|
||||||
|
const history = new History({delete: user});
|
||||||
|
history
|
||||||
|
.save(history)
|
||||||
|
.then(data => {
|
||||||
|
if(data) {
|
||||||
|
User.findByIdAndRemove(id)
|
||||||
|
.then(data => {
|
||||||
|
if (data) {
|
||||||
|
sendMessage(res, 1, {message: `User ${id} was deleted successfully.`}, token);
|
||||||
|
} else {
|
||||||
|
sendError(res, 404, -1, `Cannot delete User with id=${id}. Maybe User was not found.`, token);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res, 500, -1, err.message || "Could not delete User with id=" + id, token);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res, 500,-1,err.message || "Some error occurred while creating the User.");
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
sendError(res,404,-1,"User not found with id " + id, token);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res,500,-1,err.message || "Error retrieving User with id=" + id, token);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
sendError(res, 500, -1, `Error id is not valid`, token);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sendError(res, 500, -1, `No id given`, token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delete all Users from the database except superAdmin
|
||||||
|
exports.deleteAll = (req, res) => {
|
||||||
|
const token = checkLogin(req, res, roles.SuperAdmin);
|
||||||
|
if(token) {
|
||||||
|
User.deleteMany({login: {$ne: "superAdmin"}})
|
||||||
|
.then(data => {
|
||||||
|
sendMessage(res, 1, {
|
||||||
|
message: `${data.deletedCount} Users were deleted successfully.`
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
sendError(res, 500, -1, err.message || "Some error occurred while removing all Users.");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get all Roles depending on the role of the User
|
||||||
|
exports.roles = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
let rolesP = [];
|
||||||
|
for(const [roleName, role] of Object.entries(roles)){
|
||||||
|
if(role.permission < token.role.permission){
|
||||||
|
rolesP.push(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(Object.entries(rolesP).length === 0){
|
||||||
|
sendError(res, 500, -1, "User do not have permission to see & create user with roles.", token);
|
||||||
|
} else{
|
||||||
|
sendMessage(res, 1, rolesP);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
68
app-backend/controllers/video.controller.js
Normal file
68
app-backend/controllers/video.controller.js
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
const db = require("../models/mongodb.model");
|
||||||
|
const {sendError, sendMessage} = require ("../config/response.config");
|
||||||
|
const {checkLogin} = require("../config/sessionJWT.config");
|
||||||
|
const Video = db.video;
|
||||||
|
|
||||||
|
// Search Video
|
||||||
|
exports.search = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Video.search not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// History
|
||||||
|
exports.history = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Video.search not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create a new Video
|
||||||
|
exports.create = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Video.create not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Retrieve all Videos
|
||||||
|
exports.findAll = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Video.findAll not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Retrieve a single Video with id
|
||||||
|
exports.findOne = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Video.findOne not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update a Video with id
|
||||||
|
exports.update = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Video.update not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delete a Video with id
|
||||||
|
exports.delete = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Video.delete not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delete all Videos
|
||||||
|
exports.deleteAll = (req, res) => {
|
||||||
|
const token = checkLogin(req, res);
|
||||||
|
if(token){
|
||||||
|
return sendError(res, 501, -1, "Video.deleteAll not Implemented", token);
|
||||||
|
}
|
||||||
|
};
|
||||||
14
app-backend/jwtRS256.key.pub
Normal file
14
app-backend/jwtRS256.key.pub
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAyTaN1skc89wdcz8SLY9c
|
||||||
|
lkcARENbO40DncmcUZwQEq+EYR9BzUhjIzKJ6JetU+qGt4SJQkPAczQbw8+LaF6P
|
||||||
|
NT0QTF6E6BUgTZg1p98E/208AiFDnoqEjmlLdQN7ekttJXGDrVOTds9WMbn8lVpa
|
||||||
|
4EpVc+8CPDmrSTIC2YVSZmmektmFTSUA6411+5FGlq5oUdyKkToWYdn/ViJbYst8
|
||||||
|
N48E2Vuh1ghY5t7oPWGzPibMc/6A+uDAF7+VVD8x5UydMZ9id+RxC7lhtDDvZeRM
|
||||||
|
BllHcnWfw0UMhVk8PC6/BenJ4I8HiOgyl4cypTvlevfbZjSoNJ4g/u/lDKpdqbBg
|
||||||
|
T76OksaYqvwvTrcvPdgF1f8l/7M9ESYZTMpxvqK6YvYC/MG2355fmZ1SeuqKfDt8
|
||||||
|
rQXfXzesGSNmFNkm8mORHYiXBqyuNAwnSqRtP8qfoB4yXZ2W1HjUf24TvkvMrqwT
|
||||||
|
7PFg55c/f4LVdPjx52z30QzBJmcyVZgzXNOCG1KafwBibhriQmhdfiWogs824mwI
|
||||||
|
9w0vG2pPqSHRAa6N1y9JHSP1rIfu1jzRNFWTUuqyKgLYBE47HqxxJ21BwBryTVUz
|
||||||
|
8Ei+o05lJFkQX2/ISFYP2RunfUBccqmv0nEcGr+RSLTeqz5+WUTWs8tQxUItf2p6
|
||||||
|
9Y30htlmCJlSnHn2JlaJWQUCAwEAAQ==
|
||||||
|
-----END PUBLIC KEY-----
|
||||||
8
app-backend/jwtRS256.sh
Normal file
8
app-backend/jwtRS256.sh
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key -q -N ""
|
||||||
|
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub
|
||||||
|
rm .env
|
||||||
|
echo "JWTRS256_PRIVATE_KEY='`cat ./jwtRS256.key | base64 -w 0`'" >> .env
|
||||||
|
echo "JWTRS256_PUBLIC_KEY='`cat ./jwtRS256.key.pub | base64 -w 0`'" >> .env
|
||||||
|
source .env
|
||||||
|
rm jwtRS256.key
|
||||||
18
app-backend/models/ad.model.js
Normal file
18
app-backend/models/ad.model.js
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
module.exports = mongoose => {
|
||||||
|
let schema = mongoose.Schema({
|
||||||
|
images: [],
|
||||||
|
text: String,
|
||||||
|
subjectTarget: [],
|
||||||
|
seen: Number
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
schema.method("toJSON", function() {
|
||||||
|
const { __v, _id, ...object } = this.toObject();
|
||||||
|
object.id = _id;
|
||||||
|
return object;
|
||||||
|
});
|
||||||
|
|
||||||
|
return mongoose.model("ad", schema);
|
||||||
|
};
|
||||||
15
app-backend/models/history.model.js
Normal file
15
app-backend/models/history.model.js
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
module.exports = mongoose => {
|
||||||
|
let schema = mongoose.Schema({
|
||||||
|
user: Object
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
schema.method("toJSON", function() {
|
||||||
|
const { __v, _id, ...object } = this.toObject();
|
||||||
|
object.id = _id;
|
||||||
|
return object;
|
||||||
|
});
|
||||||
|
|
||||||
|
return mongoose.model("history", schema);
|
||||||
|
};
|
||||||
18
app-backend/models/image.model.js
Normal file
18
app-backend/models/image.model.js
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
module.exports = mongoose => {
|
||||||
|
let schema = mongoose.Schema({
|
||||||
|
base64: String,
|
||||||
|
fromUrl: String,
|
||||||
|
description: String,
|
||||||
|
type: Number
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
schema.method("toJSON", function() {
|
||||||
|
const { __v, _id, ...object } = this.toObject();
|
||||||
|
object.id = _id;
|
||||||
|
return object;
|
||||||
|
});
|
||||||
|
|
||||||
|
return mongoose.model("image", schema);
|
||||||
|
};
|
||||||
20
app-backend/models/mongodb.model.js
Normal file
20
app-backend/models/mongodb.model.js
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
const dbConfig = require("../config/mongodb.config");
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
mongoose.Promise = global.Promise;
|
||||||
|
|
||||||
|
const db = {};
|
||||||
|
db.mongoose = mongoose;
|
||||||
|
|
||||||
|
if(typeof process.env.NODE_ENV !== 'undefined' && process.env.NODE_ENV === 'production'){
|
||||||
|
db.url = dbConfig.prodUrl;
|
||||||
|
} else {
|
||||||
|
db.url = dbConfig.devUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
db.users = require("./user.model")(mongoose);
|
||||||
|
db.playlists = require("./playlist.model")(mongoose);
|
||||||
|
db.ads = require("./ad.model")(mongoose);
|
||||||
|
db.histories = require("./history.model")(mongoose);
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = db;
|
||||||
17
app-backend/models/playlist.model.js
Normal file
17
app-backend/models/playlist.model.js
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
module.exports = mongoose => {
|
||||||
|
let schema = mongoose.Schema({
|
||||||
|
userId: String,
|
||||||
|
name: String,
|
||||||
|
videos: Array
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
schema.method("toJSON", function() {
|
||||||
|
const { __v, _id, ...object } = this.toObject();
|
||||||
|
object.id = _id;
|
||||||
|
return object;
|
||||||
|
});
|
||||||
|
|
||||||
|
return mongoose.model("playlist", schema);
|
||||||
|
};
|
||||||
16
app-backend/models/subjectTarget.model.js
Normal file
16
app-backend/models/subjectTarget.model.js
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
module.exports = mongoose => {
|
||||||
|
let schema = mongoose.Schema({
|
||||||
|
name: String,
|
||||||
|
keywords: []
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
schema.method("toJSON", function() {
|
||||||
|
const { __v, _id, ...object } = this.toObject();
|
||||||
|
object.id = _id;
|
||||||
|
return object;
|
||||||
|
});
|
||||||
|
|
||||||
|
return mongoose.model("subjectTarget", schema);
|
||||||
|
};
|
||||||
43
app-backend/models/user.model.js
Normal file
43
app-backend/models/user.model.js
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
const roles = require("../config/role.config");
|
||||||
|
|
||||||
|
module.exports = mongoose => {
|
||||||
|
let schema = mongoose.Schema({
|
||||||
|
login: String,
|
||||||
|
hashPass: String, // WARNING: We don't want to send back the hashPass
|
||||||
|
mail: String,
|
||||||
|
role: {
|
||||||
|
type: Object,
|
||||||
|
default: roles.User
|
||||||
|
},
|
||||||
|
profilePictureUrl: {
|
||||||
|
type: String,
|
||||||
|
default: null
|
||||||
|
},
|
||||||
|
dateOfBirth: {
|
||||||
|
type: Date,
|
||||||
|
default: null
|
||||||
|
},
|
||||||
|
gender: {
|
||||||
|
type: String,
|
||||||
|
default: null
|
||||||
|
},
|
||||||
|
interests: {
|
||||||
|
type: Array,
|
||||||
|
default: null
|
||||||
|
},
|
||||||
|
active: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
schema.method("toJSON", function() {
|
||||||
|
const { __v, _id, ...object } = this.toObject();
|
||||||
|
object.id = _id;
|
||||||
|
return object;
|
||||||
|
});
|
||||||
|
|
||||||
|
return mongoose.model("user", schema);
|
||||||
|
};
|
||||||
18
app-backend/models/video.model.js
Normal file
18
app-backend/models/video.model.js
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
module.exports = mongoose => {
|
||||||
|
let schema = mongoose.Schema({
|
||||||
|
url: String,
|
||||||
|
title: String,
|
||||||
|
description: String,
|
||||||
|
views: Number
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
schema.method("toJSON", function() {
|
||||||
|
const { __v, _id, ...object } = this.toObject();
|
||||||
|
object.id = _id;
|
||||||
|
return object;
|
||||||
|
});
|
||||||
|
|
||||||
|
return mongoose.model("video", schema);
|
||||||
|
};
|
||||||
24
app-backend/routes/ad.routes.js
Normal file
24
app-backend/routes/ad.routes.js
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
const ads = require("../controllers/ad.controller");
|
||||||
|
module.exports = app => {
|
||||||
|
let router = require("express").Router();
|
||||||
|
|
||||||
|
// Create a new Ad
|
||||||
|
router.post("/ad/create", ads.create);
|
||||||
|
|
||||||
|
// Retrieve all Ads
|
||||||
|
router.get("/ad/findAll", ads.findAll);
|
||||||
|
|
||||||
|
// Retrieve a single Ad with id
|
||||||
|
router.get("/ad/findOne/:id", ads.findOne);
|
||||||
|
|
||||||
|
// Update an Ad with id
|
||||||
|
router.put("/ad/update/:id", ads.update);
|
||||||
|
|
||||||
|
// Delete an Ad with id
|
||||||
|
router.delete("/ad/delete/:id", ads.delete);
|
||||||
|
|
||||||
|
// Delete all Ads
|
||||||
|
router.delete("/ad/deleteAll", ads.deleteAll);
|
||||||
|
|
||||||
|
app.use('/api', router);
|
||||||
|
};
|
||||||
24
app-backend/routes/playlist.routes.js
Normal file
24
app-backend/routes/playlist.routes.js
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
const playlists = require("../controllers/playlist.controller");
|
||||||
|
module.exports = app => {
|
||||||
|
let router = require("express").Router();
|
||||||
|
|
||||||
|
// Create a new Playlist
|
||||||
|
router.post("/playlist/create", playlists.create);
|
||||||
|
|
||||||
|
// Retrieve all Playlists
|
||||||
|
router.get("/playlist/findAll", playlists.findAll);
|
||||||
|
|
||||||
|
// Retrieve a single Playlist with id
|
||||||
|
router.get("/playlist/findOne/:id", playlists.findOne);
|
||||||
|
|
||||||
|
// Update a Playlist with id
|
||||||
|
router.put("/playlist/update/:id", playlists.update);
|
||||||
|
|
||||||
|
// Delete a Playlist with id
|
||||||
|
router.delete("/playlist/delete/:id", playlists.delete);
|
||||||
|
|
||||||
|
// Delete all Playlists
|
||||||
|
router.delete("/playlist/deleteAll", playlists.deleteAll);
|
||||||
|
|
||||||
|
app.use('/api', router);
|
||||||
|
};
|
||||||
24
app-backend/routes/user.routes.js
Normal file
24
app-backend/routes/user.routes.js
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
const users = require("../controllers/user.controller");
|
||||||
|
module.exports = app => {
|
||||||
|
let router = require("express").Router();
|
||||||
|
|
||||||
|
router.post("/user/create", users.create);
|
||||||
|
|
||||||
|
router.get("/user/findAll", users.findAll);
|
||||||
|
|
||||||
|
router.get("/user/findOne/:id", users.findOne);
|
||||||
|
|
||||||
|
router.put("/user/update/:id", users.update);
|
||||||
|
|
||||||
|
router.delete("/user/delete/:id", users.delete);
|
||||||
|
|
||||||
|
router.delete("/user/deleteAll", users.deleteAll);
|
||||||
|
|
||||||
|
router.post("/user/auth", users.auth);
|
||||||
|
|
||||||
|
router.delete("/user/logout", users.logout);
|
||||||
|
|
||||||
|
router.get("/user/roles", users.roles);
|
||||||
|
|
||||||
|
app.use('/api', router);
|
||||||
|
};
|
||||||
22
app-backend/routes/video.routes.js
Normal file
22
app-backend/routes/video.routes.js
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
const videos = require("../controllers/video.controller");
|
||||||
|
module.exports = app => {
|
||||||
|
let router = require("express").Router();
|
||||||
|
|
||||||
|
router.post("/video/search", videos.search);
|
||||||
|
|
||||||
|
router.post("/video/history", videos.history);
|
||||||
|
|
||||||
|
router.post("/video/create", videos.create);
|
||||||
|
|
||||||
|
router.get("/video/findAll", videos.findAll);
|
||||||
|
|
||||||
|
router.get("/video/findOne/:id", videos.findOne);
|
||||||
|
|
||||||
|
router.put("/video/update/:id", videos.update);
|
||||||
|
|
||||||
|
router.delete("/video/delete/:id", videos.delete);
|
||||||
|
|
||||||
|
router.delete("/video/deleteAll", videos.deleteAll);
|
||||||
|
|
||||||
|
app.use('/api', router);
|
||||||
|
};
|
||||||
Reference in a new issue