From bc003a31e23ce80f8f20905ac935555c5dfdbf0f Mon Sep 17 00:00:00 2001 From: jysperm Date: Fri, 11 Apr 2014 13:30:08 +0800 Subject: [PATCH] refactor account model --- core/auth.coffee | 2 +- core/db.coffee | 9 ++ core/model/Account.coffee | 103 ------------------- core/model/aAccount.coffee | 89 ++++++++++++++++ core/model/{Ticket.coffee => tTicket.coffee} | 0 core/router/account.coffee | 16 +-- core/router/panel.coffee | 4 +- core/router/ticket.coffee | 26 ++--- 8 files changed, 122 insertions(+), 127 deletions(-) delete mode 100644 core/model/Account.coffee create mode 100644 core/model/aAccount.coffee rename core/model/{Ticket.coffee => tTicket.coffee} (100%) diff --git a/core/auth.coffee b/core/auth.coffee index cb9f5c1..4a229ad 100644 --- a/core/auth.coffee +++ b/core/auth.coffee @@ -1,6 +1,6 @@ crypto = require 'crypto' assert = require 'assert' -User = require './model/Account' +User = require './model/aAccount' exports.sha256 = (data) -> if not data diff --git a/core/db.coffee b/core/db.coffee index e76282e..2d7c3c6 100644 --- a/core/db.coffee +++ b/core/db.coffee @@ -59,3 +59,12 @@ exports.buildModel = (that, mongo) -> mongo.remove selector, options, (err, result) -> throw err if err callback result if callback + +exports.buildByXXOO = (xxoo, mongo) -> + return (value, callback) -> + selector = {} + selector[xxoo] = value + + mongo.findOne selector, (err, result) -> + throw err if err + callback result diff --git a/core/model/Account.coffee b/core/model/Account.coffee deleted file mode 100644 index 553cbf5..0000000 --- a/core/model/Account.coffee +++ /dev/null @@ -1,103 +0,0 @@ -_ = require 'underscore' - -auth = require '../auth' -db = require '../db' - -mAccount = db.collection 'accounts' - -db.buildModel module.exports, mAccount - - - -module.exports = class Account extends Model - @create: (data) -> - new Account data - - @register: (username, email, passwd, callback = null) -> - passwd_salt = auth.randomSalt() - - @insert - username: username - passwd: auth.hashPasswd(passwd, passwd_salt) - passwd_salt: passwd_salt - email: email - signup: new Date() - group: [] - setting: {} - attribure: {} - tokens: [] - , callback - - # @param callback(token) - createToken: (attribute, callback) -> - # @param callback(token) - generateToken = (callback) -> - token = auth.randomSalt() - - Account.findOne - 'tokens.token': token - , (result) -> - if result - generateToken callback - else - callback token - - generateToken (token) => - @update - $push: - tokens: - token: token - available: true - created_at: new Date() - updated_at: new Date() - attribute: attribute - , -> - callback token - - removeToken: (token, callback = null) -> - @update - $pull: - tokens: - token: token - , -> - callback() if callback - - # @param callback(User) - @authenticate: (token, callback) -> - unless token - callback null - - Account.findOne - 'tokens.token': token - , (result) -> - if result - callback result - else - callback null - - # @return bool - matchPasswd: (passwd) -> - auth.hashPasswd(passwd, @data.passwd_salt) is @data.passwd - - inGroup: (group) -> - return group in @data.group - - @byUsername: (username, callback) -> - @findOne - username: username - , (result) -> - callback result - - @byEmail: (email, callback) -> - @findOne - email: email - , (result) -> - callback result - - @byUsernameOrEmail: (username, callback) -> - Account.byUsername username, (account) -> - if account - return callback account - - Account.byEmail username, (account) -> - return callback account diff --git a/core/model/aAccount.coffee b/core/model/aAccount.coffee new file mode 100644 index 0000000..fc4eab2 --- /dev/null +++ b/core/model/aAccount.coffee @@ -0,0 +1,89 @@ +_ = require 'underscore' + +auth = require '../auth' +db = require '../db' + +cAccount = db.collection 'accounts' + +db.buildModel module.exports, cAccount + +exports.register = (username, email, passwd, callback = null) -> + passwd_salt = auth.randomSalt() + + exports.insert + username: username + passwd: auth.hashPasswd(passwd, passwd_salt) + passwd_salt: passwd_salt + email: email + signup: new Date() + group: [] + setting: {} + attribure: {} + tokens: [] + , {}, (result) -> + callback(result) if callback + +# @param callback(token) +exports.createToken = (attribute, callback) -> + # @param callback(token) + generateToken = (callback) -> + token = auth.randomSalt() + + exports.findOne + 'tokens.token': token + , {}, (result) -> + if result + generateToken callback + else + callback token + + generateToken (token) -> + exports.update + $push: + tokens: + token: token + available: true + created_at: new Date() + updated_at: new Date() + attribute: attribute + , {}, -> + callback token + +exports.removeToken = (token, callback = null) -> + exports.update + $pull: + tokens: + token: token + , {}, -> + callback() if callback + +exports.authenticate = (token, callback) -> + unless token + callback null + + exports.findOne + 'tokens.token': token + , {}, (result) -> + if result + callback result + else + callback null + +exports.byUsername = db.buildByXXOO 'username', cAccount + +exports.byEmail = db.buildByXXOO 'email', cAccount + +exports.byUsernameOrEmail = (username, callback) -> + exports.byUsername username, (account) -> + if account + return callback account + + exports.byEmail username, (account) -> + return callback account + +# @return bool +exports.matchPasswd = (account, passwd) -> + return auth.hashPasswd(passwd, account.passwd_salt) == account.passwd + +exports.inGroup = (account, group) -> + return group in account.group diff --git a/core/model/Ticket.coffee b/core/model/tTicket.coffee similarity index 100% rename from core/model/Ticket.coffee rename to core/model/tTicket.coffee diff --git a/core/router/account.coffee b/core/router/account.coffee index a0a9fe5..b7ab838 100644 --- a/core/router/account.coffee +++ b/core/router/account.coffee @@ -1,16 +1,16 @@ config = require '../config' -Account = require '../model/Account' +Account = require '../model/aAccount' module.exports = get: signup: (req, res) -> - Account.authenticate req.token, (account) -> + account.authenticate req.token, (account) -> res.render 'signup', account: account login: (req, res) -> - Account.authenticate req.token, (account) -> + account.authenticate req.token, (account) -> res.render 'login', account: account @@ -27,15 +27,15 @@ module.exports = unless data.passwd or not /^.+$/.test data.passwd return res.json 400, error: 'invalid_passwd' - Account.byUsername data.username, (account) -> + account.byUsername data.username, (account) -> if account return res.json 400, error: 'username_exist' - Account.byEmail data.email, (account) -> + account.byEmail data.email, (account) -> if account return res.json 400, error: 'email_exist' - Account.register data.username, data.email, data.passwd, (account) -> + account.register data.username, data.email, data.passwd, (account) -> account.createToken {}, (token)-> res.cookie 'token', token, expires: new Date(Date.now() + config.account.cookieTime) @@ -46,7 +46,7 @@ module.exports = login: (req, res) -> data = req.body - Account.byUsernameOrEmail data.username, (account) -> + account.byUsernameOrEmail data.username, (account) -> unless account return res.json 400, error: 'auth_failed' @@ -62,7 +62,7 @@ module.exports = token: token logout: (req, res) -> - Account.authenticate req.token, (account) -> + account.authenticate req.token, (account) -> unless account return res.json 400, error: 'auth_failed' diff --git a/core/router/panel.coffee b/core/router/panel.coffee index 2d99518..5624d04 100644 --- a/core/router/panel.coffee +++ b/core/router/panel.coffee @@ -1,4 +1,4 @@ -Account = require '../model/Account' +Account = require '../model/aAccount' module.exports = get: @@ -6,7 +6,7 @@ module.exports = res.redirect '/panel/' '/panel/': (req, res) -> - Account.authenticate req.token, (account) -> + account.authenticate req.token, (account) -> unless account return res.redirect '/account/login/' diff --git a/core/router/ticket.coffee b/core/router/ticket.coffee index 461cff9..f3a8fd0 100644 --- a/core/router/ticket.coffee +++ b/core/router/ticket.coffee @@ -4,17 +4,17 @@ ObjectID = require('mongodb').ObjectID config = require '../config' -Account = require '../model/Account' -Ticket = require '../model/Ticket' +Account = require '../model/aAccount' +Ticket = require '../model/tTicket' module.exports = get: list: (req, res) -> - Account.authenticate req.token, (account) -> + account.authenticate req.token, (account) -> unless account return res.redirect '/account/login/' - Ticket.find + tTicket.find account_id: account.id() , (tickets) -> res.render 'ticket/list', @@ -22,7 +22,7 @@ module.exports = tickets: tickets create: (req, res) -> - Account.authenticate req.token, (account) -> + account.authenticate req.token, (account) -> unless account return res.redirect '/account/login/' @@ -32,7 +32,7 @@ module.exports = post: create: (req, res) -> - Account.authenticate req.token, (account) -> + account.authenticate req.token, (account) -> unless account return res.json 400, error: 'auth_failed' @@ -45,7 +45,7 @@ module.exports = return res.json 400, error: 'invalid_type' createTicket = (members) -> - Ticket.createTicket account, data.title, data.content, data.type, members, {}, (ticket) -> + tTicket.createTicket account, data.title, data.content, data.type, members, {}, (ticket) -> return res.json id: ticket.id() @@ -56,7 +56,7 @@ module.exports = for memberName in data.members do (memberName = clone(memberName)) -> tasks.push (callback) -> - Account.byUsernameOrEmail memberName, (member) -> + account.byUsernameOrEmail memberName, (member) -> unless member res.json 400, error: 'invalid_account', username: memberName callback true @@ -76,16 +76,16 @@ module.exports = createTicket [account] reply: (req, res) -> - Account.authenticate req.token, (account) -> + account.authenticate req.token, (account) -> unless account return res.json 400, error: 'auth_failed' data = req.body - Ticket.findById data.id, (ticket) -> + tTicket.findById data.id, (ticket) -> checkReplyTo = (callback) -> if data.reply_to - Ticket.findOne + tTicket.findOne 'replys._id': data.reply_to , (result) -> if result @@ -108,7 +108,7 @@ module.exports = id: reply._id update: (req, res) -> - Account.authenticate req.token, (account) -> + account.authenticate req.token, (account) -> unless account return res.json 400, error: 'auth_failed' @@ -118,7 +118,7 @@ module.exports = addToSetModifier = [] pullModifier = [] - Ticket.findById data.id, (ticket) -> + tTicket.findById data.id, (ticket) -> if data.type if data.type in config.ticket.availableType modifier['type'] = data.type