forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.js
More file actions
113 lines (100 loc) · 2.53 KB
/
Copy pathusers.js
File metadata and controls
113 lines (100 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const passhash = require('../lib/passhash');
const ensureJson = require('./ensure-json');
class Users {
/**
* @param {import('../sequelize-db')} sequelizeDb
* @param {import('../lib/config')} config
*/
constructor(sequelizeDb, config) {
this.sequelizeDb = sequelizeDb;
this.config = config;
}
async create(data) {
const { password, ...rest } = data;
if (password) {
rest.passhash = await passhash.getPasshash(password);
}
if (rest.email) {
rest.email = rest.email.toLowerCase();
}
if (rest.ldapId) {
rest.ldapId = rest.ldapId.toLowerCase();
}
const newUser = await this.sequelizeDb.Users.create(rest);
return this.findOneById(newUser.id);
}
async update(id, changes) {
const { password, ...rest } = changes;
if (password) {
rest.passhash = await passhash.getPasshash(password);
}
if (rest.email) {
rest.email = rest.email.toLowerCase();
}
if (rest.ldapId) {
rest.ldapId = rest.ldapId.toLowerCase();
}
await this.sequelizeDb.Users.update(rest, { where: { id } });
return this.findOneById(id);
}
/**
* @param {string} email
*/
async findOneByEmail(email) {
const user = await this.sequelizeDb.Users.findOne({
where: { email: email.toLowerCase() },
});
if (user) {
let final = user.toJSON();
final.data = ensureJson(final.data);
return final;
}
}
/**
* @param {string} ldapId
*/
async findOneByLdapId(ldapId) {
const user = await this.sequelizeDb.Users.findOne({
where: { ldapId: ldapId.toLowerCase() },
});
if (user) {
let final = user.toJSON();
final.data = ensureJson(final.data);
return final;
}
}
async findOneById(id) {
const user = await this.sequelizeDb.Users.findOne({ where: { id } });
if (user) {
let final = user.toJSON();
final.data = ensureJson(final.data);
return final;
}
}
findOneByPasswordResetId(passwordResetId) {
return this.sequelizeDb.Users.findOne({ where: { passwordResetId } });
}
async findAll() {
const users = await this.sequelizeDb.Users.findAll({
attributes: [
'id',
'name',
'email',
'ldapId',
'role',
'disabled',
'signupAt',
'createdAt',
'updatedAt',
],
order: [['email']],
});
return users.map((user) => {
return user.toJSON();
});
}
removeById(id) {
return this.sequelizeDb.Users.destroy({ where: { id } });
}
}
module.exports = Users;