Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var App = require('../../app');
- var r = require("rethinkdb");
- const crypto = require('crypto');
- const moment = require('moment');
- var Security = require("../class/Security");
- var JWTHandler = require("../class/JWTHandler");
- var Status = require("../class/Status");
- module.exports = (function () {
- var id;
- var username;
- var email;
- var rank;
- var userhost;
- var password;
- var catalog;
- var attachedContact;
- class User {
- static get ADMIN() {
- return 0b101;
- }
- static get ACHAT() {
- return 0b100;
- }
- static get REGULAR_EMPLOYEE() {
- return 0b011;
- }
- static get GROSSISTE() {
- return 0b010;
- }
- static get CLIENT_GROSSISTE() {
- return 0b001;
- }
- static getUserFromToken(token) {
- }
- static getById(userID, cb) {
- r.table("users").get(userID).run(App.rdbConn, function (err, data) {
- if(data){
- cb(new Status(200, "User found"), new User(data));
- } else {
- cb(new Status(404, "User not found"));
- }
- });
- }
- static create(userDescriptor, invitationToken, cb) {
- //create user in database
- if (((userDescriptor.username != "") && (userDescriptor.email != "") && (userDescriptor.password != "") && ((userDescriptor.username != undefined) && (userDescriptor.email != undefined) && (userDescriptor.password != undefined) ))) {
- Invitation.getByToken(invitationToken, function (err, invitation) {
- if (invitation) {
- if (Security.emailRegex.exec(userDescriptor.email) !== null) {
- if (Security.passwordregex.exec(userDescriptor.password) !== null) {
- //test if user exists
- r.table("users").filter(r.row("username").downcase().eq(userDescriptor.username.toLowerCase()) || r.row("email").downcase().eq(userDescriptor.email.toLowerCase())).run(App.rdbConn, function (err, data) {
- data.toArray(function (err, result) {
- if (err) throw err;
- if (result.length === 0) {
- var newUserDescriptor = {
- username: userDescriptor.username,
- email: userDescriptor.email,
- password: crypto.createHash('sha256').update(userDescriptor.password).digest('hex'),
- rank: invitation.targetRank,
- created: moment().unix()
- };
- if (invitation.targetRank == User.CLIENT_GROSSISTE && userDescriptor.invitedBy != "") {
- Object.assign(newUserDescriptor, {
- invitedBy: invitation.userHost
- });
- }
- if (invitation.targetRank == User.GROSSISTE && userDescriptor.catalogID != "" && userDescriptor.attachedContact != "") {
- Catalog.create()
- Object.assign(newUserDescriptor, {
- catalogID: "",
- attachedContact: invitation.userHost
- });
- }
- r.table("users").insert(newUserDescriptor).run(App.rdbConn, function (err, res) {
- if (!err) {
- User.getById(res.generated_keys[0], function (status, user) {
- if (typeof user !== "undefined") {
- cb(new Status(200, "User created"), user);
- } else {
- cb(new Status(500, "Error : " + status.status + " " + status.message));
- }
- })
- } else {
- cb(new Status(500, "Database error").printError(err));
- }
- });
- } else {
- cb(new Status(409, "User already Exists"), false);
- }
- });
- });
- } else {
- cb(new Status(403, "Password too weak"), false);
- }
- } else {
- cb(new Status(403, "Bad Email Format"), false);
- }
- } else {
- cb(new Status(403, "Invitation token not valid "));
- }
- });
- } else {
- cb(new Status(400, "Missing parameters"), false);
- }
- }
- static authentify(loginDescriptor, cb) {
- r.table("users").filter(r.row("username").downcase().eq(loginDescriptor.login.toLowerCase()) || r.row("email").downcase().eq(loginDescriptor.login.toLowerCase())).run(App.rdbConn, function (err, data) {
- data.toArray(function (err, result) {
- if (err) throw err;
- if (result.length !== 0) {
- if (crypto.createHash('sha256').update(loginDescriptor.password).digest('hex') == result[0].password) {
- JWTHandler.sign(new User(result[0]).publicObject, function (token) {
- cb(new Status(200, "User Authentified"), token);
- });
- } else {
- cb(new Status(403, "Bad Password"));
- }
- } else {
- cb(new Status(404, "User not found"));
- }
- });
- });
- }
- constructor(dbObject) {
- if (dbObject.id && dbObject.username && dbObject.email && dbObject.rank) {
- id = dbObject.id;
- username = dbObject.username;
- email = dbObject.email;
- rank = dbObject.rank;
- password = dbObject.password;
- if (dbObject.userhost) {
- userhost = dbObject.userhost;
- }
- if (dbObject.catalog) {
- catalog = dbObject.catalog;
- attachedContact = dbObject.attachedContact;
- }
- } else {
- throw new Error("Incorrect user descriptor");
- return;
- }
- }
- set email(newEmail) {
- r.table("users").get(id).update({
- email: newEmail
- }).run(App.rdbConn);
- email = newEmail;
- }
- get email() {
- return email;
- }
- get id() {
- return id;
- }
- set username(newUsername) {
- r.table("users").get(id).update({
- username: newUsername
- }).run(App.rdbConn);
- username = newUsername;
- }
- get username() {
- return username;
- }
- set password(newPassword) {
- r.table("users").get(id).update({
- username: crypto.createHash('sha256').update(newPassword).digest('hex')
- }).run(App.rdbConn);
- username = newUsername;
- }
- get username() {
- return password;
- }
- get username() {
- return username;
- }
- promote(newRank) {
- if ((newRank == this.ADMIN) || (newRank == this.ACHAT) || (newRank == this.REGULAR_EMPLOYEE) || (newRank == this.GROSSISTE) || (newRank == this.CLIENT_GROSSISTE)) {
- var rank = newRank;
- r.table("users").get(id).update({
- rank: newRank
- }).run(App.rdbConn);
- }
- }
- get rank() {
- return rank;
- }
- get catalog() {
- return new Catalog();
- }
- get publicObject() {
- if (userhost) {
- return {
- id: id,
- username: username,
- email: email,
- rank: rank,
- userhost: userhost
- }
- } else if (catalog && attachedContact) {
- return {
- id: id,
- username: username,
- email: email,
- rank: rank,
- catalog: catalog,
- attachedContact: attachedContact
- }
- } else {
- return {
- id: id,
- username: username,
- email: email,
- rank: rank
- }
- }
- }
- }
- return User;
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement