|
|
@ -2,7 +2,8 @@ |
|
|
|
|
|
|
|
|
|
|
|
const Mongo = require(__dirname+'/db.js') |
|
|
|
const Mongo = require(__dirname+'/db.js') |
|
|
|
, db = Mongo.client.db('jschan').collection('accounts') |
|
|
|
, db = Mongo.client.db('jschan').collection('accounts') |
|
|
|
, bcrypt = require('bcrypt'); |
|
|
|
, bcrypt = require('bcrypt') |
|
|
|
|
|
|
|
, cache = require(__dirname+'/../redis.js'); |
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
module.exports = { |
|
|
|
|
|
|
|
|
|
|
@ -32,7 +33,7 @@ module.exports = { |
|
|
|
// hash the password
|
|
|
|
// hash the password
|
|
|
|
const passwordHash = await bcrypt.hash(password, 12); |
|
|
|
const passwordHash = await bcrypt.hash(password, 12); |
|
|
|
//add to db
|
|
|
|
//add to db
|
|
|
|
return db.insertOne({ |
|
|
|
const res = await db.insertOne({ |
|
|
|
'_id': username, |
|
|
|
'_id': username, |
|
|
|
original, |
|
|
|
original, |
|
|
|
authLevel, |
|
|
|
authLevel, |
|
|
@ -40,17 +41,21 @@ module.exports = { |
|
|
|
'ownedBoards': [], |
|
|
|
'ownedBoards': [], |
|
|
|
'modBoards': [] |
|
|
|
'modBoards': [] |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
cache.del(`users:${username}`); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
changePassword: async (username, newPassword) => { |
|
|
|
changePassword: async (username, newPassword) => { |
|
|
|
const passwordHash = await bcrypt.hash(newPassword, 12); |
|
|
|
const passwordHash = await bcrypt.hash(newPassword, 12); |
|
|
|
return db.updateOne({ |
|
|
|
const res = await db.updateOne({ |
|
|
|
'_id': username |
|
|
|
'_id': username |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'$set': { |
|
|
|
'$set': { |
|
|
|
'passwordHash': passwordHash |
|
|
|
'passwordHash': passwordHash |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
cache.del(`users:${username}`); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
find: (filter, skip=0, limit=0) => { |
|
|
|
find: (filter, skip=0, limit=0) => { |
|
|
@ -63,36 +68,42 @@ module.exports = { |
|
|
|
}).skip(skip).limit(limit).toArray(); |
|
|
|
}).skip(skip).limit(limit).toArray(); |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
deleteMany: (usernames) => { |
|
|
|
deleteMany: async (usernames) => { |
|
|
|
return db.deleteMany({ |
|
|
|
const res = await db.deleteMany({ |
|
|
|
'_id': { |
|
|
|
'_id': { |
|
|
|
'$in': usernames |
|
|
|
'$in': usernames |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
cache.del(usernames.map(n => `users:${n}`)); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
addOwnedBoard: (username, board) => { |
|
|
|
addOwnedBoard: async (username, board) => { |
|
|
|
return db.updateOne({ |
|
|
|
const res = await db.updateOne({ |
|
|
|
'_id': username |
|
|
|
'_id': username |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'$addToSet': { |
|
|
|
'$addToSet': { |
|
|
|
'ownedBoards': board |
|
|
|
'ownedBoards': board |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
cache.del(`users:${username}`); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
removeOwnedBoard: (username, board) => { |
|
|
|
removeOwnedBoard: async (username, board) => { |
|
|
|
return db.updateOne({ |
|
|
|
const res = await db.updateOne({ |
|
|
|
'_id': username |
|
|
|
'_id': username |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
'$pull': { |
|
|
|
'$pull': { |
|
|
|
'ownedBoards': board |
|
|
|
'ownedBoards': board |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
cache.del(`users:${username}`); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
addModBoard: (usernames, board) => { |
|
|
|
addModBoard: async (usernames, board) => { |
|
|
|
return db.updateMany({ |
|
|
|
const res = await db.updateMany({ |
|
|
|
'_id': { |
|
|
|
'_id': { |
|
|
|
'$in': usernames |
|
|
|
'$in': usernames |
|
|
|
} |
|
|
|
} |
|
|
@ -101,10 +112,12 @@ module.exports = { |
|
|
|
'modBoards': board |
|
|
|
'modBoards': board |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
cache.del(`users:${username}`); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
removeModBoard: (usernames, board) => { |
|
|
|
removeModBoard: async (usernames, board) => { |
|
|
|
return db.updateMany({ |
|
|
|
const res = await db.updateMany({ |
|
|
|
'_id': { |
|
|
|
'_id': { |
|
|
|
'$in': usernames |
|
|
|
'$in': usernames |
|
|
|
} |
|
|
|
} |
|
|
@ -113,6 +126,8 @@ module.exports = { |
|
|
|
'modBoards': board |
|
|
|
'modBoards': board |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
cache.del(`users:${username}`); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
getOwnedOrModBoards: (usernames) => { |
|
|
|
getOwnedOrModBoards: (usernames) => { |
|
|
@ -140,9 +155,9 @@ module.exports = { |
|
|
|
}).toArray(); |
|
|
|
}).toArray(); |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
setLevel: (usernames, level) => { |
|
|
|
setLevel: async (usernames, level) => { |
|
|
|
//increase users auth level
|
|
|
|
//increase users auth level
|
|
|
|
return db.updateMany({ |
|
|
|
const res = await db.updateMany({ |
|
|
|
'_id': { |
|
|
|
'_id': { |
|
|
|
'$in': usernames |
|
|
|
'$in': usernames |
|
|
|
} |
|
|
|
} |
|
|
@ -151,6 +166,8 @@ module.exports = { |
|
|
|
'authLevel': level |
|
|
|
'authLevel': level |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
cache.del(`users:${username}`); |
|
|
|
|
|
|
|
return res; |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
deleteAll: () => { |
|
|
|
deleteAll: () => { |
|
|
|