changed to findById and improved db.loadJson
This commit is contained in:
parent
4e68267bfd
commit
8bf408138f
31
package-lock.json
generated
31
package-lock.json
generated
@ -93,6 +93,19 @@
|
||||
"defer-to-connect": "^1.0.1"
|
||||
}
|
||||
},
|
||||
"@types/bcrypt": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/bcrypt/-/bcrypt-3.0.0.tgz",
|
||||
"integrity": "sha512-nohgNyv+1ViVcubKBh0+XiNJ3dO8nYu///9aJ4cgSqv70gBL+94SNy/iC2NLzKPT2Zt/QavrOkBVbZRLZmw6NQ=="
|
||||
},
|
||||
"@types/bson": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/bson/-/bson-4.0.2.tgz",
|
||||
"integrity": "sha512-+uWmsejEHfmSjyyM/LkrP0orfE2m5Mx9Xel4tXNeqi1ldK5XMQcDsFkBmLDtuyKUbxj2jGDo0H240fbCRJZo7Q==",
|
||||
"requires": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/color-name": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz",
|
||||
@ -103,6 +116,24 @@
|
||||
"resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-5.2.7.tgz",
|
||||
"integrity": "sha512-NYrtPht0wGzhwe9+/idPaBB+TqkY9AhTvOLMkThm0IoEfLaiVQZwBwyJ5puCkO3AUCWrmcoePjp2mbFocKy4SQ=="
|
||||
},
|
||||
"@types/mongodb": {
|
||||
"version": "3.5.10",
|
||||
"resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.5.10.tgz",
|
||||
"integrity": "sha512-6NkJNfFdFa/njBvN/9eAfq78bWUnapkdR3JbWGGpd7U71PjgKweA4Tlag8psi2mqm973vBYVTD1oc1u0lzRcig==",
|
||||
"requires": {
|
||||
"@types/bson": "*",
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/mongoose": {
|
||||
"version": "5.7.12",
|
||||
"resolved": "https://registry.npmjs.org/@types/mongoose/-/mongoose-5.7.12.tgz",
|
||||
"integrity": "sha512-yzLJk3cdSwuMXaIacUCWUb8m960YcgnID7S4ZPOOgzT39aSC46670TuunN+ajDio7OUcGG4mGg8eOGs2Z6VmrA==",
|
||||
"requires": {
|
||||
"@types/mongodb": "*",
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "13.1.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-13.1.6.tgz",
|
||||
|
@ -15,7 +15,9 @@
|
||||
"dependencies": {
|
||||
"@apidevtools/json-schema-ref-parser": "^8.0.0",
|
||||
"@hapi/joi": "^17.1.1",
|
||||
"@types/bcrypt": "^3.0.0",
|
||||
"@types/mocha": "^5.2.7",
|
||||
"@types/mongoose": "^5.7.12",
|
||||
"@types/node": "^13.1.6",
|
||||
"axios": "^0.19.2",
|
||||
"basic-auth": "^2.0.1",
|
||||
|
@ -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