Browse Source

Normalize line endings.

master
Zed A. Shaw 2 weeks ago
parent
commit
6a4b274212
1 changed files with 163 additions and 163 deletions
  1. +163
    -163
      lib/models.js

+ 163
- 163
lib/models.js View File

@@ -1,163 +1,163 @@
const knexfile = require('../knexfile');
const uuid4 = require('uuid4');
const { log } = require('../lib/logging');
const assert = require('assert');
let knex = require('knex')(knexfile.development);
const bookshelf = require('bookshelf')(knex);
function fix_booleans(me, attr) {
for(let a of attr) {
// TODO: make sure this is the same with postgres or knex to figure it out
me.on('fetched', (model, response, options) => {
assert(a in model.attributes, `No attribute ${a} in ${model}`);
model.attributes[a] = model.attributes[a] == '1';
});
me.on('saved', (model, response, options) => {
assert(a in model.attributes, `No attribute ${a} in ${model}`);
model.attributes[a] = model.attributes[a] == '1';
});
}
}
exports.User = bookshelf.model('User',
{ // object methods
tableName: 'users',
initialize() {
this.constructor.__super__.initialize.apply(this, arguments);
fix_booleans(this, ['send_emails', 'tos_agree', 'verified', 'registered']);
},
},
{ // class methods
async find_and_validate(email, attr_only) {
assert(email, `Valid email required: ${email}`);
try {
let user = await new this({email}).fetch({require: false});
// TODO: why am I getting by email then checking the email? do more validation
let is_valid = user ? user.get("email") === email && user.get('verified') : false;
log.debug("User validity check for", email, "returned", user, "with is_valid", is_valid);
if(attr_only) {
return {user: user === null ? null : user.attributes, is_valid};
} else {
return {user, is_valid};
}
} catch(error) {
log.error(error, "User.find_and_validate");
return {user: undefined, is_valid: false};
}
},
async delete_by_email(email, require) {
assert(email);
try {
return new this().where({email}).destroy({require});
} catch (error) {
log.error(error, "User.delete_by_email");
return undefined;
}
},
async register(email, full_name, send_emails, tos_agree) {
try {
return await new this({
email: email,
verified: true,
full_name, send_emails, tos_agree, registered: true
}).save();
} catch (error) {
log.error(error);
throw error;
}
},
async update_by_email(email, full_name, send_emails) {
try {
log.debug("update received", {full_name, send_emails});
let user = await this.where({email}).save({
full_name, send_emails}, {method: "update"});
log.debug("User is after update", user)
return user;
} catch (error) {
log.error(error, "User.update_by_email");
}
},
}
);
exports.Auth = bookshelf.model('Auth', {
tableName: 'auths',
initialize() {
this.constructor.__super__.initialize.apply(this, arguments);
fix_booleans(this, ['registration']);
},
},
{
async create(email, ip_addr, registration) {
assert(email, `Valid email required: ${email}`);
assert(ip_addr, "ip_addr required");
let token = uuid4();
try {
return new this({ email, ip_addr, registration, token }).save();
} catch (error) {
log.error(error, "Auth.create");
throw error;
}
},
async find_by_email(email) {
assert(email, `Valid email required: ${email}`);
// TODO: make a smaller query that just finds the token
try {
return new this({email}).fetch({require: false});
} catch (error) {
log.error(error,"Auth.find_by_email");
throw error;
}
},
async token_by_email(email) {
try {
assert(email, `Valid email required: ${email}`);
let auth = await this.find_by_email(email);
log.debug("Auth returned by token", auth);
return auth ? auth.get('token') : undefined;
} catch (error) {
log.error(error, "Auth.token_by_email");
throw error;
}
},
async find_by_token(token) {
console.log("TOKEN", token);
assert(token);
try {
// TODO: validate the token format
return new this().where({token}).fetch({require: false});
} catch (error) {
log.error(error, 'find_by_token');
throw error;
}
},
async destroy_by_email(email, require) {
assert(email);
try {
return new this().where({email}).destroy({require: require});
} catch (error) {
log.error(error, "Auth.destroy_by_email");
throw error;
}
}
}
);
const knexfile = require('../knexfile');
const uuid4 = require('uuid4');
const { log } = require('../lib/logging');
const assert = require('assert');
let knex = require('knex')(knexfile.development);
const bookshelf = require('bookshelf')(knex);
function fix_booleans(me, attr) {
for(let a of attr) {
// TODO: make sure this is the same with postgres or knex to figure it out
me.on('fetched', (model, response, options) => {
assert(a in model.attributes, `No attribute ${a} in ${model}`);
model.attributes[a] = model.attributes[a] == '1';
});
me.on('saved', (model, response, options) => {
assert(a in model.attributes, `No attribute ${a} in ${model}`);
model.attributes[a] = model.attributes[a] == '1';
});
}
}
exports.User = bookshelf.model('User',
{ // object methods
tableName: 'users',
initialize() {
this.constructor.__super__.initialize.apply(this, arguments);
fix_booleans(this, ['send_emails', 'tos_agree', 'verified', 'registered']);
},
},
{ // class methods
async find_and_validate(email, attr_only) {
assert(email, `Valid email required: ${email}`);
try {
let user = await new this({email}).fetch({require: false});
// TODO: why am I getting by email then checking the email? do more validation
let is_valid = user ? user.get("email") === email && user.get('verified') : false;
log.debug("User validity check for", email, "returned", user, "with is_valid", is_valid);
if(attr_only) {
return {user: user === null ? null : user.attributes, is_valid};
} else {
return {user, is_valid};
}
} catch(error) {
log.error(error, "User.find_and_validate");
return {user: undefined, is_valid: false};
}
},
async delete_by_email(email, require) {
assert(email);
try {
return new this().where({email}).destroy({require});
} catch (error) {
log.error(error, "User.delete_by_email");
return undefined;
}
},
async register(email, full_name, send_emails, tos_agree) {
try {
return await new this({
email: email,
verified: true,
full_name, send_emails, tos_agree, registered: true
}).save();
} catch (error) {
log.error(error);
throw error;
}
},
async update_by_email(email, full_name, send_emails) {
try {
log.debug("update received", {full_name, send_emails});
let user = await this.where({email}).save({
full_name, send_emails}, {method: "update"});
log.debug("User is after update", user)
return user;
} catch (error) {
log.error(error, "User.update_by_email");
}
},
}
);
exports.Auth = bookshelf.model('Auth', {
tableName: 'auths',
initialize() {
this.constructor.__super__.initialize.apply(this, arguments);
fix_booleans(this, ['registration']);
},
},
{
async create(email, ip_addr, registration) {
assert(email, `Valid email required: ${email}`);
assert(ip_addr, "ip_addr required");
let token = uuid4();
try {
return new this({ email, ip_addr, registration, token }).save();
} catch (error) {
log.error(error, "Auth.create");
throw error;
}
},
async find_by_email(email) {
assert(email, `Valid email required: ${email}`);
// TODO: make a smaller query that just finds the token
try {
return new this({email}).fetch({require: false});
} catch (error) {
log.error(error,"Auth.find_by_email");
throw error;
}
},
async token_by_email(email) {
try {
assert(email, `Valid email required: ${email}`);
let auth = await this.find_by_email(email);
log.debug("Auth returned by token", auth);
return auth ? auth.get('token') : undefined;
} catch (error) {
log.error(error, "Auth.token_by_email");
throw error;
}
},
async find_by_token(token) {
console.log("TOKEN", token);
assert(token);
try {
// TODO: validate the token format
return new this().where({token}).fetch({require: false});
} catch (error) {
log.error(error, 'find_by_token');
throw error;
}
},
async destroy_by_email(email, require) {
assert(email);
try {
return new this().where({email}).destroy({require: require});
} catch (error) {
log.error(error, "Auth.destroy_by_email");
throw error;
}
}
}
);

Loading…
Cancel
Save