changed to findById and improved db.loadJson
This commit is contained in:
		@@ -88,6 +88,12 @@ export default class db {
 | 
			
		||||
 | 
			
		||||
    let loadCounter = 0;  // count number of loaded collections to know when to return done()
 | 
			
		||||
    Object.keys(json.collections).forEach(collectionName => {  // create each collection
 | 
			
		||||
      for(let i in json.collections[collectionName]) {  // convert $oid fields to actual ObjectIds
 | 
			
		||||
        console.log(json.collections[collectionName][i]);
 | 
			
		||||
        Object.keys(json.collections[collectionName][i]).forEach(key => {
 | 
			
		||||
          json.collections[collectionName][i][key] = json.collections[collectionName][i][key].hasOwnProperty('$oid') ? mongoose.Types.ObjectId(json.collections[collectionName][i][key].$oid) : json.collections[collectionName][i][key];
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
      this.state.db.createCollection(collectionName, (err, collection) => {
 | 
			
		||||
        collection.insertMany(json.collections[collectionName], () => {  // insert JSON data
 | 
			
		||||
          if (++ loadCounter >= Object.keys(json.collections).length) {  // all collections loaded
 | 
			
		||||
 
 | 
			
		||||
@@ -56,7 +56,7 @@ function basic (req, next): any {  // checks basic auth and returns changed user
 | 
			
		||||
  return new Promise(resolve => {
 | 
			
		||||
    const auth = basicAuth(req);
 | 
			
		||||
    if (auth !== undefined) {  // basic auth available
 | 
			
		||||
      UserModel.find({name: auth.name}).lean().exec( 'find', (err, data) => {  // find user
 | 
			
		||||
      UserModel.find({name: auth.name}).lean().exec( (err, data: any) => {  // find user
 | 
			
		||||
        if (err) next(err);
 | 
			
		||||
        if (data.length === 1) {  // one user found
 | 
			
		||||
          bcrypt.compare(auth.pass, data[0].pass, (err, res) => {  // check password
 | 
			
		||||
@@ -83,7 +83,7 @@ function basic (req, next): any {  // checks basic auth and returns changed user
 | 
			
		||||
function key (req, next): any {  // checks API key and returns changed user object
 | 
			
		||||
  return new Promise(resolve => {
 | 
			
		||||
    if (req.query.key !== undefined) {
 | 
			
		||||
      UserModel.find({key: req.query.key}).lean().exec( 'find', (err, data) => {  // find user
 | 
			
		||||
      UserModel.find({key: req.query.key}).lean().exec( (err, data: any) => {  // find user
 | 
			
		||||
        if (err) next(err);
 | 
			
		||||
        if (data.length === 1) {  // one user found
 | 
			
		||||
          resolve({level: data[0].level, name: data[0].name});
 | 
			
		||||
 
 | 
			
		||||
@@ -175,7 +175,7 @@ describe('/user/passreset', () => {
 | 
			
		||||
      });
 | 
			
		||||
  });
 | 
			
		||||
  it('changes the user password', done => {
 | 
			
		||||
    UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => {
 | 
			
		||||
    UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data: any) => {
 | 
			
		||||
      if (err) return done(err);
 | 
			
		||||
      const oldpass = data[0].pass;
 | 
			
		||||
      supertest(server)
 | 
			
		||||
@@ -189,8 +189,9 @@ describe('/user/passreset', () => {
 | 
			
		||||
        .end((err, res) => {
 | 
			
		||||
          if (err) done(err);
 | 
			
		||||
          should(res.body).be.eql({status: 'OK'});
 | 
			
		||||
          UserModel.find({name: 'janedoe'}).lean().exec( 'find', (err, data) => {
 | 
			
		||||
          UserModel.find({name: 'janedoe'}).lean().exec(  (err, data: any) => {
 | 
			
		||||
            if (err) return done(err);
 | 
			
		||||
            console.log(data);
 | 
			
		||||
            should(data[0].pass).not.eql(oldpass);
 | 
			
		||||
            done();
 | 
			
		||||
          });
 | 
			
		||||
 
 | 
			
		||||
@@ -42,13 +42,13 @@ router.post('/user/new', (req, res, next) => {
 | 
			
		||||
 | 
			
		||||
router.post('/user/passreset', (req, res, next) => {
 | 
			
		||||
  // check if user/email combo exists
 | 
			
		||||
  UserModel.find({name: req.body.name, email: req.body.email}).lean().exec( 'find', (err, data) => {
 | 
			
		||||
  UserModel.find({name: req.body.name, email: req.body.email}).lean().exec( (err, data: any) => {
 | 
			
		||||
    if (err) next(err);
 | 
			
		||||
    if (data.length === 1) {  // it exists
 | 
			
		||||
      const newPass = Math.random().toString(36).substring(2);
 | 
			
		||||
      bcrypt.hash(newPass, 10, (err, hash) => {  // password hashing
 | 
			
		||||
        if (err) next(err);
 | 
			
		||||
        UserModel.findOneAndUpdate({name: req.body.name, email: req.body.email}, {pass: hash}, err => {
 | 
			
		||||
        UserModel.findByIdAndUpdate(data[0]._id, {pass: hash}, err => {  // write new password
 | 
			
		||||
          if (err) next(err);
 | 
			
		||||
          mail(data[0].email, 'Your new password for the DFOP database', 'Hi, <br><br> You requested to reset your password.<br>Your new password is:<br><br>' + newPass + '<br><br>If you did not request a password reset, talk to the sysadmin quickly!<br><br>Have a nice day.<br><br>The DFOP team', err => {
 | 
			
		||||
            if (err) next(err);
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
  "collections": {
 | 
			
		||||
    "users": [
 | 
			
		||||
      {
 | 
			
		||||
        "_id": "5ea0450ed851c30a90e70894",
 | 
			
		||||
        "_id": {"$oid":"5ea0450ed851c30a90e70894"},
 | 
			
		||||
        "email": "jane.doe@bosch.com",
 | 
			
		||||
        "name": "janedoe",
 | 
			
		||||
        "pass": "$2a$10$i872o3qR5V3JnbDArD8Z.eDo.BNPDBaR7dUX9KSEtl9pUjLyucy2K",
 | 
			
		||||
@@ -13,7 +13,7 @@
 | 
			
		||||
        "__v": 0
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        "_id": "5ea131671feb9c2ee0aafc9b",
 | 
			
		||||
        "_id": {"$oid":"5ea131671feb9c2ee0aafc9b"},
 | 
			
		||||
        "email": "a.d.m.i.n@bosch.com",
 | 
			
		||||
        "name": "admin",
 | 
			
		||||
        "pass": "$2a$10$i872o3qR5V3JnbDArD8Z.eDo.BNPDBaR7dUX9KSEtl9pUjLyucy2K",
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user