/
update-accounts-to-uuid.js
216 lines (193 loc) · 7.53 KB
/
update-accounts-to-uuid.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var each = require('each-async')
var uuid = require('uuid').v1;
var path = require('path');
var dotenv = require('dotenv');
var fs = require('fs');
var through = require('through2').obj;
var UUID_REGEX = /^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})$/;
/*
* Be sure to have the variable PASSWORD set in your .env file
* (probably shouldn't check in the literal 'unguessable" password into version control)
*/
var config = JSON.parse(fs.readFileSync('config.js', 'utf8'));
var flatsheet = require('../lib/index.js')(config, ready);
function ready () {
var envFilePath = path.join(process.cwd(), '.env');
var envFile = fs.readFileSync(envFilePath);
this.secrets = dotenv.parse(envFile);
var self = this;
deleteSessionsResetsAndIndexes();
updateAccountStream.bind(self)(flatsheet.accountdown.list(), function () {
console.log("\nBegin sheets updates...");
updateSheetsStream.bind(self)(flatsheet.sheets.list());
});
}
function updateAccountStream (accountStream, cb) {
var self = this;
accountStream.pipe(through(function (account, _, next) {
migrateAccount.bind(self)(account, sendPasswordResetEmail.bind(self), next);
}))
.on('data', function (data) {
console.log("reading account stream:", data);
})
.on('err', function (err) {
console.log("Error reading account stream:", err);
})
.on('end', function () {
console.log("Finished reading account stream");
cb();
})
}
function updateSheetsStream (sheetStream) {
var self = this;
sheetStream
.on('data', function (sheet) {
migrateSheet.bind(self)(sheet);
})
.on('error', function (err) {
return console.log(err);
})
.on('end', function () {
console.log("Finished updating sheet stream");
});
}
var migrateSheet = function (sheet) {
usernameToUuidPermissionsHelper(sheet.owners, function (err, ownerUuids) {
if (err) return console.log("update.usernameToUuid: owner eachAsync loop err:", err);
console.log("updated sheet.owners:", ownerUuids);
sheet.owners = ownerUuids;
usernameToUuidPermissionsHelper(sheet.editors, function(err, editorUuids) {
if (err) return console.log("update.usernameToUuid: editor eachAsync loop err:", err);
console.log("updated sheet.editors:", editorUuids);
sheet.editors = editorUuids;
flatsheet.sheets.put(sheet, function (err, newSheet) {
if (err) return console.log("Update.migrateSheet: could not put new sheet, err:", err)
console.log("newSheet.owners:", newSheet.owners);
console.log("newSheet.editors:", newSheet.editors);
});
});
});
}
var usernameToUuidPermissionsHelper = function (usernameDict, cb) {
var uuidDict = {};
var usernameList = Object.keys(usernameDict);
each(usernameList, function(username, _, next) {
// check that the array value is not already a uuid:
if (!UUID_REGEX.test(username)) {
console.log("update.migrateSheet: getting key from username:", username);
// Get uuid from username
flatsheet.accountsIndexes.getKeyFromUsername(username, function (err, account) {
if (err) return console.log("update.usernameToUuid: cannot get key from " +
"username:", err);
uuidDict[account.key] = true;
next();
});
}
}, function (err) {
if (err) return cb(err);
console.log("update.usernameToUuid: new uuid dict:", uuidDict);
return cb(null, uuidDict);
});
}
var migrateAccount = function (oldAccount, sendPasswordResetEmail, next) {
var self = this;
if (!UUID_REGEX.test(oldAccount.key) || !oldAccount.value.key) {
console.log("Migrating account:", oldAccount.key);
flatsheet.accountdown.get(oldAccount.key, function (err, accountValue) {
if (err) return console.log("error retrieving test account:", err);
var newAccountKey = uuid();
accountValue['key'] = newAccountKey;
var updatedAccount = {
login: {
basic: {
uuid: accountValue.key,
password: self.secrets.PASSWORD
}
},
value: accountValue
};
flatsheet.accountdown.remove(oldAccount.key, function (err) {
if (err) return console.log("err while deleting old account:", err);
console.log("updates:migrateAccount: account removed:", oldAccount.key);
flatsheet.accountdown.create(newAccountKey, updatedAccount, function (err) {
if (err) return console.log("err while putting in new account:", err);
flatsheet.accountsIndexes.addIndexes(accountValue);
sendPasswordResetEmail.bind(self)(updatedAccount, next);
});
});
});
} else {
console.log("Account not migrated (already has a valid uuid):", oldAccount.key);
}
}
var sendPasswordResetEmail = function (account, next) {
var self = this;
var token = require('crypto').randomBytes(32).toString('hex');
var opts = { email: account.value.email, accepted: false };
flatsheet.resets.put(token, opts, function (err) {
if (err) return console.log(err);
var data = {
url: flatsheet.site.url + '/accounts/acceptReset?username=' + account.value.username +
'&key=' + account.value.key + '&token=' + token,
from: flatsheet.site.email,
fromname: flatsheet.site.contact
};
var message = {
to: account.value.email,
from: flatsheet.site.email,
fromname: flatsheet.site.contact,
// TODO: reset this for production
subject: 'Password reset needed for your Flatsheet account',
//subject: 'Password reset needed for your Flatsheet account:' + account.value.email,
text: flatsheet.render('password-reset-email-for-account-updates', data),
html: flatsheet.render('password-reset-email-for-account-updates', data)
};
//// uncomment this for testing (sends emails to a email, not to members)
//// Safety check to mask emails when testing the migration
//if (!self.secrets.ACTIVATE_EMAIL_MIGRATION) {
// message.to = 'luke.swart@gmail.com'
//}
//console.log("Update.sendResetEmail: email that will receive account change info:", message.to);
flatsheet.email.sendMail(message, function (err, info) {
if (err) return console.log(err);
console.log("Update.sendResetEmail: Email sent to account email:", account.value.email);
// info is `{ message: 'success'}`
console.log("Update.sendresetEmail: Email delivery status:", info.message);
return next();
});
});
}
var deleteSessionsResetsAndIndexes = function () {
var sessionRegex = /^!sessions!/;
var resetsRegex = /^!resets!/;
var accountIndexesRegex = /^!account-indexes!/;
flatsheet.db.createReadStream()
.on('data', function (data) {
// Delete all session rows
if (sessionRegex.test(data.key)) {
console.log("deleting session:", data);
// delete row
flatsheet.db.del(data.key, function (err) {
if (err) return console.log(err);
});
// Delete all reset rows
} else if (resetsRegex.test(data.key)) {
console.log("deleting account reset row:", data);
flatsheet.db.del(data.key, function (err) {
if (err) return console.log(err);
});
// Delete all account-index rows
} else if (accountIndexesRegex.test(data.key)) {
console.log("deleting account-index row:", data);
flatsheet.db.del(data.key, function (err) {
if (err) return console.log(err);
});
}
})
.on('error', function(err) {
console.log(err);
})
.on('done', function () {
console.log("done iterating over stream");
})
}