groups and reset

This commit is contained in:
2020-05-15 00:40:15 -04:00
parent e71fccd27c
commit 0889832efc
22 changed files with 1463 additions and 128 deletions

View File

@ -22,7 +22,7 @@ Auth.login = async function(data){
return {user, token}
}catch(error){
throw error;
throw this.errors.login();
}
};

View File

@ -54,7 +54,10 @@ async function addMember(client, group, user){
}),
]);
}catch(error){
if(error = "TypeOrValueExistsError")return ;
// if(error = "TypeOrValueExistsError"){
// console.error('addMember error skipped', error)
// return ;
// }
throw error;
}
}
@ -100,6 +103,7 @@ Group.listDetail = async function(){
await client.unbind();
return groups;
}catch(error){
throw error;
@ -121,14 +125,24 @@ Group.get = async function(data){
scope: 'sub',
filter: `(&(objectClass=groupOfNames)(cn=${data.name}))`,
attributes: ['*', 'createTimestamp', 'modifyTimestamp'],
})).searchEntries;
})).searchEntries[0];
await client.unbind();
if(!Array.isArray(group.member)) group.member = [group.member];
return group;
if(group){
let obj = Object.create(this);
Object.assign(obj, group);
return obj;
}else{
let error = new Error('GroupNotFound');
error.name = 'GroupNotFound';
error.message = `LDAP:${data.cn} does not exists`;
error.status = 404;
throw error;
}
}catch(error){
throw error;
}
@ -145,8 +159,55 @@ Group.add = async function(data){
return this.get(data);
}catch(error){
throw error;
}
}
Group.addMember = async function(user){
try{
await client.bind(conf.bindDN, conf.bindPassword);
await addMember(client, this, user);
await client.unbind();
return this;
}catch(error){
throw error;
}
};
Group.removeMember = async function(user){
try{
await client.bind(conf.bindDN, conf.bindPassword);
await removeMember(client, this, user);
await client.unbind();
return this;
}catch(error){
throw error;
}
};
Group.remove = async function(){
try{
await client.bind(conf.bindDN, conf.bindPassword);
await client.del(this.dn);
await client.unbind();
return true;
}catch(error){
throw error;
}
}
module.exports = {Group};

View File

@ -59,4 +59,13 @@ AuthToken.add = async function(data){
return AuthToken.__proto__.add(data);
};
module.exports = {Token, InviteToken, AuthToken}
var PasswordResetToken = Object.create(Token({
name: 'auth',
}));
PasswordResetToken.add = async function(data){
data.created_by = data.uid;
return PasswordResetToken.__proto__.add(data);
};
module.exports = {Token, InviteToken, AuthToken, PasswordResetToken};

View File

@ -4,7 +4,7 @@ const { Client, Attribute, Change } = require('ldapts');
const crypto = require('crypto');
const {Mail} = require('./email');
const {Token, InviteToken} = require('./token');
const {Token, InviteToken, PasswordResetToken} = require('./token');
const conf = require('../app').conf.ldap;
const client = new Client({
@ -93,21 +93,6 @@ async function deleteLdapUser(client, data){
}
}
async function changeLdapPassword(client, data){
try{
await client.modify(`cn=${data.uid},${conf.userBase}`, [
new Change({
operation: 'replace',
modification: new Attribute({
type: 'userPassword',
values: ['{MD5}'+crypto.createHash('md5').update(data.userPassword, "binary").digest('base64')]
})}),
]);
}catch(error){
throw error;
}
}
const user_parse = function(data){
if(data[conf.userNameAttribute]){
data.username = data[conf.userNameAttribute]
@ -167,7 +152,7 @@ User.listDetail = async function(){
}
};
User.get = async function(data){
User.get = async function(data, value){
try{
if(typeof data !== 'object'){
let uid = data;
@ -177,7 +162,12 @@ User.get = async function(data){
await client.bind(conf.bindDN, conf.bindPassword);
let filter = `(&${conf.userFilter}(${conf.userNameAttribute}=${data.uid}))`;
data.searchKey = data.searchKey || conf.userNameAttribute;
data.searchValue = data.searchValue || data.uid;
let filter = `(&${conf.userFilter}(${data.searchKey}=${data.searchValue}))`;
console.log('get filter', filter)
const res = await client.search(conf.userBase, {
scope: 'sub',
@ -197,7 +187,7 @@ User.get = async function(data){
}else{
let error = new Error('UserNotFound');
error.name = 'UserNotFound';
error.message = `LDAP:${data.uid} does not exists`;
error.message = `LDAP:${data.searchValue} does not exists`;
error.status = 404;
throw error;
}
@ -289,11 +279,42 @@ User.verifyEmail = async function(data){
link:`${data.url}/login/invite/${token.token}/${token.mail_token}`
}
)
return this;
}catch(error){
throw error;
}
};
User.passwordReset = async function(url, mail){
try{
let user = await User.get({
searchKey: 'mail',
searchValue: mail
});
console.log('user', user)
let token = await PasswordResetToken.add(user);
await Mail.sendTemplate(
user.mail,
'reset_link',
{
user: user,
link:`${url}/login/resetpassword/${token.token}`
}
)
return true;
}catch(error){
// if(error.name === 'UserNotFound') return false;
throw error;
}
};
User.remove = async function(data){
try{
@ -303,22 +324,34 @@ User.remove = async function(data){
await client.unbind();
return true
return true;
}catch(error){
throw error;
}
};
// User.setPassword = async function(data){
// try{
// await linuxUser.setPassword(this.username, data.password);
User.setPassword = async function(data){
try{
// return this;
// }catch(error){
// throw error;
// }
// };
await client.bind(conf.bindDN, conf.bindPassword);
await client.modify(this.dn, [
new Change({
operation: 'replace',
modification: new Attribute({
type: 'userPassword',
values: ['{MD5}'+crypto.createHash('md5').update(data.userPassword, "binary").digest('base64')]
})}),
]);
await client.unbind();
return this;
}catch(error){
throw error;
}
};
User.invite = async function(){
try{