Advertisement
Guest User

User.model.js

a guest
May 27th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var App = require('../../app');
  2. var r = require("rethinkdb");
  3. const crypto = require('crypto');
  4. const moment = require('moment');
  5. var Security = require("../class/Security");
  6. var JWTHandler = require("../class/JWTHandler");
  7. var Status = require("../class/Status");
  8.  
  9. module.exports = (function () {
  10.     var id;
  11.     var username;
  12.     var email;
  13.     var rank;
  14.     var userhost;
  15.     var password;
  16.     var catalog;
  17.     var attachedContact;
  18.     class User {
  19.         static get ADMIN() {
  20.             return 0b101;
  21.         }
  22.  
  23.         static get ACHAT() {
  24.             return 0b100;
  25.         }
  26.  
  27.         static get REGULAR_EMPLOYEE() {
  28.             return 0b011;
  29.         }
  30.  
  31.         static get GROSSISTE() {
  32.             return 0b010;
  33.         }
  34.  
  35.         static get CLIENT_GROSSISTE() {
  36.             return 0b001;
  37.         }
  38.  
  39.         static getUserFromToken(token) {
  40.  
  41.         }
  42.  
  43.         static getById(userID, cb) {
  44.             r.table("users").get(userID).run(App.rdbConn, function (err, data) {
  45.                 if(data){
  46.                     cb(new Status(200, "User found"), new User(data));
  47.                 } else {
  48.                     cb(new Status(404, "User not found"));
  49.                 }
  50.             });
  51.         }
  52.  
  53.         static create(userDescriptor, invitationToken, cb) {
  54.             //create user in database
  55.  
  56.  
  57.             if (((userDescriptor.username != "") && (userDescriptor.email != "") && (userDescriptor.password != "") && ((userDescriptor.username != undefined) && (userDescriptor.email != undefined) && (userDescriptor.password != undefined) ))) {
  58.                 Invitation.getByToken(invitationToken, function (err, invitation) {
  59.                     if (invitation) {
  60.                         if (Security.emailRegex.exec(userDescriptor.email) !== null) {
  61.                             if (Security.passwordregex.exec(userDescriptor.password) !== null) {
  62.                                 //test if user exists
  63.                                 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) {
  64.                                     data.toArray(function (err, result) {
  65.                                         if (err) throw err;
  66.                                         if (result.length === 0) {
  67.  
  68.                                             var newUserDescriptor = {
  69.                                                 username: userDescriptor.username,
  70.                                                 email: userDescriptor.email,
  71.                                                 password: crypto.createHash('sha256').update(userDescriptor.password).digest('hex'),
  72.                                                 rank: invitation.targetRank,
  73.                                                 created: moment().unix()
  74.                                             };
  75.                                             if (invitation.targetRank == User.CLIENT_GROSSISTE && userDescriptor.invitedBy != "") {
  76.                                                 Object.assign(newUserDescriptor, {
  77.                                                     invitedBy: invitation.userHost
  78.                                                 });
  79.                                             }
  80.                                             if (invitation.targetRank == User.GROSSISTE && userDescriptor.catalogID != "" && userDescriptor.attachedContact != "") {
  81.                                                 Catalog.create()
  82.                                                 Object.assign(newUserDescriptor, {
  83.                                                     catalogID: "",
  84.                                                     attachedContact: invitation.userHost
  85.                                                 });
  86.                                             }
  87.  
  88.                                             r.table("users").insert(newUserDescriptor).run(App.rdbConn, function (err, res) {
  89.                                                 if (!err) {
  90.                                                     User.getById(res.generated_keys[0], function (status, user) {
  91.                                                         if (typeof user !== "undefined") {
  92.                                                             cb(new Status(200, "User created"), user);
  93.                                                         } else {
  94.                                                             cb(new Status(500, "Error : " + status.status + " " + status.message));
  95.                                                         }
  96.                                                     })
  97.                                                 } else {
  98.                                                     cb(new Status(500, "Database error").printError(err));
  99.                                                 }
  100.                                             });
  101.  
  102.                                         } else {
  103.                                             cb(new Status(409, "User already Exists"), false);
  104.                                         }
  105.                                     });
  106.                                 });
  107.                             } else {
  108.                                 cb(new Status(403, "Password too weak"), false);
  109.                             }
  110.                         } else {
  111.                             cb(new Status(403, "Bad Email Format"), false);
  112.                         }
  113.                     } else {
  114.                         cb(new Status(403, "Invitation token not valid "));
  115.                     }
  116.  
  117.                 });
  118.             } else {
  119.                 cb(new Status(400, "Missing parameters"), false);
  120.             }
  121.  
  122.         }
  123.  
  124.         static authentify(loginDescriptor, cb) {
  125.             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) {
  126.                 data.toArray(function (err, result) {
  127.                     if (err) throw err;
  128.                     if (result.length !== 0) {
  129.                         if (crypto.createHash('sha256').update(loginDescriptor.password).digest('hex') == result[0].password) {
  130.                             JWTHandler.sign(new User(result[0]).publicObject, function (token) {
  131.  
  132.                                 cb(new Status(200, "User Authentified"), token);
  133.                             });
  134.                         } else {
  135.                             cb(new Status(403, "Bad Password"));
  136.                         }
  137.                     } else {
  138.                         cb(new Status(404, "User not found"));
  139.                     }
  140.                 });
  141.             });
  142.         }
  143.  
  144.         constructor(dbObject) {
  145.             if (dbObject.id && dbObject.username && dbObject.email && dbObject.rank) {
  146.                 id = dbObject.id;
  147.                 username = dbObject.username;
  148.                 email = dbObject.email;
  149.                 rank = dbObject.rank;
  150.                 password = dbObject.password;
  151.                 if (dbObject.userhost) {
  152.                     userhost = dbObject.userhost;
  153.                 }
  154.                 if (dbObject.catalog) {
  155.                     catalog = dbObject.catalog;
  156.                     attachedContact = dbObject.attachedContact;
  157.                 }
  158.             } else {
  159.                 throw new Error("Incorrect user descriptor");
  160.                 return;
  161.             }
  162.         }
  163.  
  164.         set email(newEmail) {
  165.             r.table("users").get(id).update({
  166.                 email: newEmail
  167.             }).run(App.rdbConn);
  168.             email = newEmail;
  169.         }
  170.  
  171.         get email() {
  172.             return email;
  173.         }
  174.  
  175.         get id() {
  176.             return id;
  177.         }
  178.  
  179.         set username(newUsername) {
  180.             r.table("users").get(id).update({
  181.                 username: newUsername
  182.             }).run(App.rdbConn);
  183.             username = newUsername;
  184.         }
  185.  
  186.         get username() {
  187.             return username;
  188.         }
  189.  
  190.         set password(newPassword) {
  191.             r.table("users").get(id).update({
  192.                 username: crypto.createHash('sha256').update(newPassword).digest('hex')
  193.             }).run(App.rdbConn);
  194.             username = newUsername;
  195.         }
  196.  
  197.         get username() {
  198.             return password;
  199.         }
  200.  
  201.         get username() {
  202.             return username;
  203.         }
  204.  
  205.         promote(newRank) {
  206.             if ((newRank == this.ADMIN) || (newRank == this.ACHAT) || (newRank == this.REGULAR_EMPLOYEE) || (newRank == this.GROSSISTE) || (newRank == this.CLIENT_GROSSISTE)) {
  207.                 var rank = newRank;
  208.                 r.table("users").get(id).update({
  209.                     rank: newRank
  210.                 }).run(App.rdbConn);
  211.             }
  212.         }
  213.  
  214.         get rank() {
  215.             return rank;
  216.         }
  217.  
  218.         get catalog() {
  219.             return new Catalog();
  220.         }
  221.  
  222.         get publicObject() {
  223.             if (userhost) {
  224.                 return {
  225.                     id: id,
  226.                     username: username,
  227.                     email: email,
  228.                     rank: rank,
  229.                     userhost: userhost
  230.                 }
  231.             } else if (catalog && attachedContact) {
  232.                 return {
  233.                     id: id,
  234.                     username: username,
  235.                     email: email,
  236.                     rank: rank,
  237.                     catalog: catalog,
  238.                     attachedContact: attachedContact
  239.                 }
  240.             } else {
  241.                 return {
  242.                     id: id,
  243.                     username: username,
  244.                     email: email,
  245.                     rank: rank
  246.                 }
  247.             }
  248.         }
  249.     }
  250.     return User;
  251. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement