Skip to content
Snippets Groups Projects
Commit 7a250eb7 authored by Lloyd Hilaiel's avatar Lloyd Hilaiel
Browse files

SCHEMA CHANGE: password is now nullable in schema. also, add...

SCHEMA CHANGE: password is now nullable in schema.  also, add .createUserWithPrimaryEmail and .emailType to db abstractoin
parent 3faadab7
No related branches found
No related tags found
No related merge requests found
......@@ -112,7 +112,8 @@ exports.onReady = function(f) {
'checkAuth',
'listEmails',
'lastStaged',
'ping'
'ping',
'emailType'
].forEach(function(fn) {
exports[fn] = function() {
checkReady();
......@@ -130,6 +131,7 @@ exports.onReady = function(f) {
'removeEmail',
'cancelAccount',
'updatePassword',
'createUserWithPrimaryEmail'
].forEach(function(fn) {
exports[fn] = function() {
if (!config.get('database').may_write) {
......
......@@ -127,6 +127,12 @@ exports.emailKnown = function(email, cb) {
setTimeout(function() { cb(m.length > 0) }, 0);
};
exports.emailType = function(email, cb) {
sync();
var m = jsel.match(".emails ." + ESC(email), db.users);
process.nextTick(function() { cb(m.length ? m.type : undefined); });
};
exports.isStaged = function(email, cb) {
if (cb) {
setTimeout(function() {
......@@ -201,11 +207,23 @@ exports.stageEmail = function(existing_email, new_email, cb) {
};
db.stagedEmails[new_email] = secret;
flush();
setTimeout(function() { cb(secret); }, 0);
});
};
exports.createUserWithPrimaryEmail = function(email, cb) {
var emailVal = { };
emailVal[email] = { type: 'primary' };
db.users.push({
password: null,
emails: emailVal
});
flush();
process.nextTick(function() {
cb(undefined);
});
};
exports.emailForVerificationSecret = function(secret, cb) {
setTimeout(function() {
......
......@@ -41,11 +41,12 @@
/*
* The Schema:
*
* +--- user ------+ +--- email ----+
* |*int id | <-\ |*int id |
* | string passwd | \- |*int user |
* +---------------+ |*string address
* +--------------+
* +--- user ------+ +--- email -----+
* |*int id | <-\ |*int id |
* | string passwd | \- |*int user |
* +---------------+ |*string address|
* | enum type |
* +---------------+
*
*
* +------ staged ----------+
......@@ -70,7 +71,7 @@ var client = undefined;
const schemas = [
"CREATE TABLE IF NOT EXISTS user (" +
"id BIGINT AUTO_INCREMENT PRIMARY KEY," +
"passwd CHAR(64) NOT NULL" +
"passwd CHAR(64)" +
") ENGINE=InnoDB;",
"CREATE TABLE IF NOT EXISTS email (" +
......@@ -233,6 +234,16 @@ exports.emailKnown = function(email, cb) {
);
}
exports.emailType = function(email, cb) {
client.query(
"SELECT type FROM email WHERE address = ?", [ email ],
function(err, rows) {
if (err) logUnexpectedError(err);
cb((rows && rows.length > 0) ? rows[0].type : null);
}
);
}
exports.isStaged = function(email, cb) {
client.query(
"SELECT COUNT(*) as N FROM staged WHERE email = ?", [ email ],
......@@ -352,6 +363,23 @@ exports.gotVerificationSecret = function(secret, hash, cb) {
);
}
exports.createUserWithPrimaryEmail = function(email, cb) {
// create a new user acct with no password
client.query(
"INSERT INTO user() VALUES()",
function(err, info) {
console.log(info.insertId);
if (err) { logUnexpectedError(err); cb(err); return; }
client.query(
"INSERT INTO email(user, address, type) VALUES(?, ?, ?)",
[ info.insertId, email, 'primary' ],
function(err, info) {
if (err) logUnexpectedError(err);
cb(err ? err : undefined);
});
});
};
exports.emailsBelongToSameAccount = function(lhs, rhs, cb) {
client.query(
'SELECT COUNT(*) AS n FROM email WHERE address = ? AND user = ( SELECT user FROM email WHERE address = ? );',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment