-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUser.js
190 lines (152 loc) · 5.21 KB
/
User.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const scrypt = require('scrypt-kdf');
const crypto = require('crypto');
function forceUppercaseOnFirst(name) {
const trimName = name.trim();
return trimName.charAt(0).toUpperCase() + trimName.slice(1);
}
function getGravatarUrl(email) {
return 'https://www.gravatar.com/avatar/' + crypto.createHash('md5').update(email).digest('hex');
}
async function hashPassword(pass) {
const hash = await scrypt.kdf(pass, {logN: 15});
return hash.toString('base64');
}
async function updatePassword(password) {
// "binary to ASCII" aka base64
// btoa('scrypt') = c2NyeXB0
if (password && password.length > 1 && password.substring(0, 8) !== 'c2NyeXB0') {
password = await hashPassword(password);
}
return password;
}
module.exports = {
primaryKey: 'id',
attributes: {
id: {
type: 'string',
columnType: 'varchar(36)',
required: true
},
role: {
type: 'string',
isIn: [
'user',
'manager',
'admin'
],
required: true,
columnType: 'varchar(7)'
},
email: {
type: 'string',
isEmail: true,
required: true,
// unique: true, // can NOT be unique, if we are using soft-deleted users; controller must deal with uniqueness
columnType: 'varchar(191)' // 191 is the max length to safely use the utf8mb4 varchar.
},
firstName: {
type: 'string',
allowNull: true,
columnType: 'varchar(70)'
},
lastName: {
type: 'string',
allowNull: true,
columnType: 'varchar(70)'
},
password: {
type: 'string',
allowNull: true,
columnType: 'varchar(191)', // 191 is the max length to safely use the utf8mb4 varchar.
// see: https://sailsjs.com/documentation/reference/waterline-orm/queries/decrypt
// You will need to "decrypt" the user object before you can check if the password is valid.
// encrypt: true // currently, does not work as intended, as password is encrypted before we can hash it
},
verificationKey: { // placeholder for something like email verification
type: 'string',
allowNull: true,
columnType: 'varchar(191)' // 191 is the max length to safely use the utf8mb4 varchar.
},
avatar: {
type: 'string',
isURL: true,
columnType: 'varchar(191)' // 191 is the max length to safely use the utf8mb4 varchar.
},
isGravatar: {
type: 'boolean',
defaultsTo: true
},
deletedBy: {
model: 'user'
},
deletedAt: {
type: 'ref',
columnType: 'datetime'
},
createdBy: {
model: 'user'
},
createdAt: {
type: 'ref',
columnType: 'datetime',
autoCreatedAt: true
},
updatedAt: {
type: 'ref',
columnType: 'datetime',
autoUpdatedAt: true
}
},
customToJSON: function() { // DO NOT use => function
// This will be run by the "keep-models-safe" helper.
return _.omit(this, [
'password',
'verificationKey'
]);
},
doPasswordsMatch: (raw, hashed) => {
if (!raw || !hashed) {
throw new Error('Both "raw" and "hashed" passwords are required.');
}
const hashBuffer = Buffer.from(hashed, 'base64');
return scrypt.verify(hashBuffer, raw).then((isAMatch) => isAMatch);
},
fullName: (user) => {
/* istanbul ignore if */
if (!user.firstName) {
throw new Error('User has no firstName attribute.');
}
/* istanbul ignore if */
if (!user.lastName) {
throw new Error('User has no lastName attribute.');
}
return user.firstName + ' ' + user.lastName;
},
beforeCreate: async function(user, next) {
const email = user.email.toLowerCase().trim();
user.id = sails.helpers.generateUuid();
user.email = email;
user.avatar = getGravatarUrl(email);
user.firstName = forceUppercaseOnFirst(user.firstName);
user.lastName = forceUppercaseOnFirst(user.lastName);
user.password = await updatePassword(user.password);
return next();
},
beforeUpdate: async function(user, next) {
if (user.email && user.email.trim().length) {
const email = user.email.toLowerCase().trim();
user.email = email;
user.avatar = getGravatarUrl(email);
}
if (user.firstName && user.firstName.trim().length) {
user.firstName = forceUppercaseOnFirst(user.firstName.trim());
}
if (user.lastName && user.lastName.trim().length) {
user.lastName = forceUppercaseOnFirst(user.lastName.trim());
}
if (user.password && user.password !== '' && user.password.length > 7) {
user.password = await updatePassword(user.password.trim());
}
return next();
}
};