mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-04-24 11:45:40 +08:00
重构 Model
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
crypto = require 'crypto'
|
||||
|
||||
exports.sha256 = (data) ->
|
||||
if not data
|
||||
return null
|
||||
return crypto.createHash('sha256').update(data).digest('hex')
|
||||
|
||||
exports.randomSalt = ->
|
||||
return exports.sha256 crypto.randomBytes 256
|
||||
|
||||
exports.hashPasswd = (passwd, passwd_salt) ->
|
||||
return exports.sha256(exports.sha256(passwd) + passwd_salt)
|
||||
@@ -41,9 +41,4 @@ module.exports =
|
||||
transfer: 39
|
||||
memory: 27
|
||||
|
||||
db:
|
||||
type: 'mongo'
|
||||
server: 'localhost'
|
||||
name: 'RootPanel'
|
||||
user: 'rpadmin'
|
||||
passwd: ''
|
||||
mongodb: 'mongodb://localhost/RootPanel'
|
||||
|
||||
@@ -2,22 +2,27 @@ MongoClient = (require 'mongodb').MongoClient
|
||||
ObjectID = require('mongodb').ObjectID
|
||||
_ = require 'underscore'
|
||||
|
||||
config = (require './config').db
|
||||
config = require './config'
|
||||
|
||||
exports.connect = (callback = null)->
|
||||
if config.passwd
|
||||
url = "mongodb://#{config.user}:#{config.passwd}@#{config.server}/#{config.name}"
|
||||
else
|
||||
url = "mongodb://#{config.server}/#{config.name}"
|
||||
exports.db = {}
|
||||
|
||||
MongoClient.connect url, {}, (err, db) ->
|
||||
exports.connect = (callback) ->
|
||||
MongoClient.connect config.mongodb, {}, (err, db) ->
|
||||
throw err if err
|
||||
exports.mongo = db
|
||||
|
||||
callback(db) if callback
|
||||
callback db
|
||||
|
||||
exports.collection = (name) ->
|
||||
return exports.mongo.collection name
|
||||
exports.buildModel = (collection) ->
|
||||
model = exports.mongo.collection collection
|
||||
|
||||
model.findId = (id, callback) ->
|
||||
if _.isString id
|
||||
id = exports.ObjectID id
|
||||
|
||||
mongo.findOne
|
||||
_id: id
|
||||
, callback
|
||||
|
||||
exports.ObjectID = (id) ->
|
||||
try
|
||||
@@ -25,51 +30,9 @@ exports.ObjectID = (id) ->
|
||||
catch e
|
||||
return null
|
||||
|
||||
exports.buildModel = (that, mongo) ->
|
||||
that.find = (selector, options, callback) ->
|
||||
mongo.find selector, options, (err, cursor) ->
|
||||
throw err if err
|
||||
cursor.toArray (err, result) ->
|
||||
throw err if err
|
||||
callback result
|
||||
|
||||
that.findOne = (selector, options, callback) ->
|
||||
mongo.findOne selector, options, (err, result) ->
|
||||
throw err if err
|
||||
callback result
|
||||
|
||||
that.findId = (id, callback) ->
|
||||
if _.isString id
|
||||
id = exports.ObjectID id
|
||||
|
||||
mongo.findOne
|
||||
_id: id
|
||||
, (err, result) ->
|
||||
throw err if err
|
||||
callback result
|
||||
|
||||
that.update = (selector, documents, options, callback = null) ->
|
||||
mongo.update selector, documents, options, (err, result) ->
|
||||
throw err if err
|
||||
callback result if callback
|
||||
|
||||
that.insert = (data, options, callback = null) ->
|
||||
mongo.insert data, options, (err, result) ->
|
||||
if _.isArray data
|
||||
callback result
|
||||
else
|
||||
callback result[0]
|
||||
|
||||
that.remove = (selector, options, callback = null) ->
|
||||
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
|
||||
mongo.findOne selector, callback
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
_ = require 'underscore'
|
||||
crypto = require 'crypto'
|
||||
|
||||
auth = require '../auth'
|
||||
db = require '../db'
|
||||
billing = require '../billing'
|
||||
|
||||
cAccount = db.collection 'accounts'
|
||||
module.exports = exports = db.buildModel 'accounts'
|
||||
|
||||
db.buildModel module.exports, cAccount
|
||||
|
||||
exports.byUsername = db.buildByXXOO 'username', cAccount
|
||||
exports.byEmail = db.buildByXXOO 'email', cAccount
|
||||
exports.byUsername = db.buildByXXOO 'username', exports
|
||||
exports.byEmail = db.buildByXXOO 'email', exports
|
||||
|
||||
sample =
|
||||
username: 'jysperm'
|
||||
@@ -54,13 +51,24 @@ sample =
|
||||
ua: 'Mozilla/5.0 (Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102'
|
||||
]
|
||||
|
||||
exports.register = (username, email, passwd, callback = null) ->
|
||||
passwd_salt = auth.randomSalt()
|
||||
exports.sha256 = (data) ->
|
||||
if not data
|
||||
return null
|
||||
return crypto.createHash('sha256').update(data).digest('hex')
|
||||
|
||||
exports.randomSalt = ->
|
||||
return exports.sha256 crypto.randomBytes 256
|
||||
|
||||
exports.hashPasswd = (passwd, passwd_salt) ->
|
||||
return exports.sha256(exports.sha256(passwd) + passwd_salt)
|
||||
|
||||
exports.register = (username, email, passwd, callback) ->
|
||||
passwd_salt = exports.randomSalt()
|
||||
|
||||
exports.insert
|
||||
_id: db.ObjectID()
|
||||
username: username
|
||||
passwd: auth.hashPasswd(passwd, passwd_salt)
|
||||
passwd: exports.hashPasswd(passwd, passwd_salt)
|
||||
passwd_salt: passwd_salt
|
||||
email: email
|
||||
signup_at: new Date()
|
||||
@@ -75,32 +83,30 @@ exports.register = (username, email, passwd, callback = null) ->
|
||||
arrears_at: null
|
||||
resources_limit: []
|
||||
tokens: []
|
||||
, {}, callback
|
||||
, callback
|
||||
|
||||
exports.updatePasswd = (account, passwd, callback) ->
|
||||
passwd_salt = auth.randomSalt()
|
||||
passwd_salt = exports.randomSalt()
|
||||
|
||||
exports.update _id: account._id,
|
||||
$set:
|
||||
passwd: auth.hashPasswd(passwd, passwd_salt)
|
||||
passwd: exports.hashPasswd(passwd, passwd_salt)
|
||||
passwd_salt: passwd_salt
|
||||
, {}, callback
|
||||
, callback
|
||||
|
||||
# @param callback(token)
|
||||
exports.createToken = (account, attribute, callback) ->
|
||||
# @param callback(token)
|
||||
generateToken = (callback) ->
|
||||
token = auth.randomSalt()
|
||||
token = exports.randomSalt()
|
||||
|
||||
exports.findOne
|
||||
'tokens.token': token
|
||||
, {}, (result) ->
|
||||
, (err, result) ->
|
||||
if result
|
||||
generateToken callback
|
||||
else
|
||||
callback token
|
||||
callback null, token
|
||||
|
||||
generateToken (token) ->
|
||||
generateToken (err, token) ->
|
||||
exports.update _id: account._id,
|
||||
$push:
|
||||
tokens:
|
||||
@@ -109,15 +115,15 @@ exports.createToken = (account, attribute, callback) ->
|
||||
created_at: new Date()
|
||||
updated_at: new Date()
|
||||
attribute: attribute
|
||||
, {}, ->
|
||||
callback token
|
||||
, ->
|
||||
callback null, token
|
||||
|
||||
exports.removeToken = (token, callback) ->
|
||||
exports.update 'tokens.token': token,
|
||||
$pull:
|
||||
tokens:
|
||||
token: token
|
||||
, {}, callback
|
||||
, callback
|
||||
|
||||
exports.authenticate = (token, callback) ->
|
||||
unless token
|
||||
@@ -125,22 +131,22 @@ exports.authenticate = (token, callback) ->
|
||||
|
||||
exports.findOne
|
||||
'tokens.token': token
|
||||
, {}, callback
|
||||
, callback
|
||||
|
||||
exports.byUsernameOrEmailOrId = (username, callback) ->
|
||||
exports.byUsername username, (account) ->
|
||||
exports.byUsername username, (err, account) ->
|
||||
if account
|
||||
return callback account
|
||||
return callback null, account
|
||||
|
||||
exports.byEmail username, (account) ->
|
||||
exports.byEmail username, (err, account) ->
|
||||
if account
|
||||
return callback account
|
||||
return callback null, account
|
||||
|
||||
exports.findId username, callback
|
||||
exports.findId username, (err, account) ->
|
||||
callback null, account
|
||||
|
||||
# @return bool
|
||||
exports.matchPasswd = (account, passwd) ->
|
||||
return auth.hashPasswd(passwd, account.passwd_salt) == account.passwd
|
||||
return exports.hashPasswd(passwd, account.passwd_salt) == account.passwd
|
||||
|
||||
exports.inGroup = (account, group) ->
|
||||
return group in account.group
|
||||
@@ -152,7 +158,7 @@ exports.joinPlan = (account, plan, callback) ->
|
||||
'attribute.plans': plan
|
||||
$set:
|
||||
'attribute.resources_limit': billing.calcResourcesLimit account.attribute.plans
|
||||
, {}, callback
|
||||
, callback
|
||||
|
||||
exports.leavePlan = (account, plan, callback) ->
|
||||
account.attribute.plans = _.reject account.attribute.plans, (i) -> i == plan
|
||||
@@ -161,10 +167,10 @@ exports.leavePlan = (account, plan, callback) ->
|
||||
'attribute.plans': plan
|
||||
$set:
|
||||
'attribute.resources_limit': billing.calcResourcesLimit account.attribute.plans
|
||||
, {}, callback
|
||||
, callback
|
||||
|
||||
exports.incBalance = (account, amount, callback) ->
|
||||
exports.update _id: account._id,
|
||||
$inc:
|
||||
'attribute.balance': amount
|
||||
, {}, callback
|
||||
, callback
|
||||
|
||||
@@ -4,9 +4,7 @@ _ = require 'underscore'
|
||||
|
||||
db = require '../db'
|
||||
|
||||
cTicket = db.collection 'tickets'
|
||||
|
||||
db.buildModel module.exports, cTicket
|
||||
module.exports = exports = db.buildModel 'tickets'
|
||||
|
||||
sample =
|
||||
account_id: ObjectID()
|
||||
@@ -52,7 +50,7 @@ exports.createTicket = (account, title, content, type, members, status, attribut
|
||||
members: membersID
|
||||
attribute: attribute
|
||||
replys: []
|
||||
, {}, callback
|
||||
, callback
|
||||
|
||||
exports.createReply = (ticket, account, content, status, callback) ->
|
||||
data =
|
||||
@@ -69,12 +67,12 @@ exports.createReply = (ticket, account, content, status, callback) ->
|
||||
$set:
|
||||
status: status
|
||||
updated_at: new Date()
|
||||
, {}, ->
|
||||
, ->
|
||||
unless exports.getMember ticket, account
|
||||
exports.addMember ticket, account, ->
|
||||
callback data
|
||||
callback null, data
|
||||
else
|
||||
callback data
|
||||
callback null, data
|
||||
|
||||
exports.addMember = (ticket, account, callback) ->
|
||||
exports.update
|
||||
|
||||
@@ -27,16 +27,16 @@ exports.post '/signup', errorHandling, (req, res) ->
|
||||
if req.body.username in config.account.invalid_username
|
||||
return res.error 'username_exist'
|
||||
|
||||
mAccount.byUsername req.body.username, (account) ->
|
||||
mAccount.byUsername req.body.username, (err, account) ->
|
||||
if account
|
||||
return res.error 'username_exist'
|
||||
|
||||
mAccount.byEmail req.body.email, (account) ->
|
||||
mAccount.byEmail req.body.email, (err, account) ->
|
||||
if account
|
||||
return res.error 'email_exist'
|
||||
|
||||
mAccount.register req.body.username, req.body.email, req.body.passwd, (account) ->
|
||||
mAccount.createToken account, {}, (token)->
|
||||
mAccount.register req.body.username, req.body.email, req.body.passwd, (err, account) ->
|
||||
mAccount.createToken account, {}, (err, token)->
|
||||
res.cookie 'token', token,
|
||||
expires: new Date(Date.now() + config.account.cookie_time)
|
||||
|
||||
@@ -44,14 +44,14 @@ exports.post '/signup', errorHandling, (req, res) ->
|
||||
id: account._id
|
||||
|
||||
exports.post '/login', errorHandling, (req, res) ->
|
||||
mAccount.byUsernameOrEmailOrId req.body.username, (account) ->
|
||||
mAccount.byUsernameOrEmailOrId req.body.username, (err, account) ->
|
||||
unless account
|
||||
return res.error 'auth_failed'
|
||||
|
||||
unless mAccount.matchPasswd account, req.body.passwd
|
||||
return res.error 'auth_failed'
|
||||
|
||||
mAccount.createToken account, {}, (token) ->
|
||||
mAccount.createToken account, {}, (err, token) ->
|
||||
res.cookie 'token', token,
|
||||
expires: new Date(Date.now() + config.account.cookie_time)
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ mAccount = require '../model/account'
|
||||
module.exports = exports = express.Router()
|
||||
|
||||
exports.get '/', requestAdminAuthenticate, renderAccount, (req, res) ->
|
||||
mAccount.find {}, {}, (accounts) ->
|
||||
mAccount.find({}).toArray (err, accounts) ->
|
||||
res.render 'admin/index',
|
||||
accounts: accounts
|
||||
|
||||
exports.post '/create_payment', requestAdminAuthenticate, (req, res) ->
|
||||
|
||||
@@ -24,7 +24,7 @@ exports.errorHandling = (req, res, next) ->
|
||||
|
||||
exports.accountInfo = (req, res, next) ->
|
||||
req.inject [exports.parseToken], ->
|
||||
mAccount.authenticate req.token, (account) ->
|
||||
mAccount.authenticate req.token, (err, account) ->
|
||||
req.account = account
|
||||
next()
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ exports.post '/subscribe', requestAuthenticate, (req, res) ->
|
||||
mAccount.update _id: account._id,
|
||||
$addToSet:
|
||||
'attribute.services': serviceName
|
||||
, {}, ->
|
||||
, ->
|
||||
if config.debug.mock_test
|
||||
return callback()
|
||||
|
||||
@@ -59,7 +59,7 @@ exports.post '/unsubscribe', requestAuthenticate, (req, res) ->
|
||||
mAccount.update _id: account._id,
|
||||
$pull:
|
||||
'attribute.services': serviceName
|
||||
, {}, ->
|
||||
, ->
|
||||
if config.debug.mock_test
|
||||
return callback()
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ exports.get '/list', requestAuthenticate, renderAccount, (req, res) ->
|
||||
,
|
||||
sort:
|
||||
updated_at: -1
|
||||
, (tickets) ->
|
||||
.toArray (err, tickets) ->
|
||||
res.render 'ticket/list',
|
||||
tickets: tickets
|
||||
|
||||
@@ -30,7 +30,7 @@ exports.get '/create', requestAuthenticate, renderAccount, (req, res) ->
|
||||
ticketTypes: config.ticket.availableType
|
||||
|
||||
exports.get '/view', requestAuthenticate, renderAccount, getParam, (req, res) ->
|
||||
mTicket.findId req.body.id, (ticket) ->
|
||||
mTicket.findId req.body.id, (err, ticket) ->
|
||||
unless ticket
|
||||
return res.send 404
|
||||
|
||||
@@ -39,20 +39,20 @@ exports.get '/view', requestAuthenticate, renderAccount, getParam, (req, res) ->
|
||||
return res.send 403
|
||||
|
||||
async.map ticket.members, (member, callback) ->
|
||||
mAccount.findId member, (member_account) ->
|
||||
mAccount.findId member, (err, member_account) ->
|
||||
callback null, member_account
|
||||
, (err, result) ->
|
||||
ticket.members = result
|
||||
|
||||
async.map ticket.replys, (reply, callback) ->
|
||||
mAccount.findId reply.account_id, (reply_account) ->
|
||||
mAccount.findId reply.account_id, (err, reply_account) ->
|
||||
reply.account = reply_account
|
||||
callback null, reply
|
||||
|
||||
, (err, result) ->
|
||||
ticket.replys = result
|
||||
|
||||
mAccount.findId ticket.account_id, (ticket_account) ->
|
||||
mAccount.findId ticket.account_id, (err, ticket_account) ->
|
||||
ticket.account = ticket_account
|
||||
|
||||
res.render 'ticket/view',
|
||||
@@ -66,7 +66,7 @@ exports.post '/create', requestAuthenticate, (req, res) ->
|
||||
return res.error 'invalid_type'
|
||||
|
||||
createTicket = (members, status) ->
|
||||
mTicket.createTicket req.account, req.body.title, req.body.content, req.body.type, members, status, {}, (ticket) ->
|
||||
mTicket.createTicket req.account, req.body.title, req.body.content, req.body.type, members, status, {}, (err, ticket) ->
|
||||
return res.json
|
||||
id: ticket._id
|
||||
|
||||
@@ -77,7 +77,7 @@ exports.post '/create', requestAuthenticate, (req, res) ->
|
||||
for memberName in req.body.members
|
||||
do (memberName = _.clone(memberName)) ->
|
||||
tasks.push (callback) ->
|
||||
mAccount.byUsernameOrEmailOrId memberName, (member) ->
|
||||
mAccount.byUsernameOrEmailOrId memberName, (err, member) ->
|
||||
unless member
|
||||
res.error 'invalid_account', username: memberName
|
||||
callback true
|
||||
@@ -106,7 +106,7 @@ exports.post '/reply', requestAuthenticate, (req, res) ->
|
||||
return res.error 'forbidden'
|
||||
|
||||
status = if mAccount.inGroup(req.account, 'root') then 'open' else 'pending'
|
||||
mTicket.createReply ticket, req.account, req.body.content, status, (reply) ->
|
||||
mTicket.createReply ticket, req.account, req.body.content, status, (err, reply) ->
|
||||
return res.json
|
||||
id: reply._id
|
||||
|
||||
@@ -131,7 +131,7 @@ exports.post '/list', requestAuthenticate, (req, res) ->
|
||||
updated_at: -1
|
||||
limit: req.body.limit ? 30
|
||||
skip: req.body.skip ? 0
|
||||
, (tickets) ->
|
||||
.toArray (err, tickets) ->
|
||||
res.json _.map tickets, (item) ->
|
||||
return {
|
||||
id: item._id
|
||||
@@ -181,7 +181,7 @@ exports.post '/update', requestAuthenticate, (req, res) ->
|
||||
unless _.isEmpty modifier
|
||||
mTicket.update _id: ticket._id,
|
||||
$set: modifier
|
||||
, {}, callback
|
||||
, callback
|
||||
else
|
||||
callback()
|
||||
|
||||
@@ -191,7 +191,7 @@ exports.post '/update', requestAuthenticate, (req, res) ->
|
||||
$addToSet:
|
||||
members:
|
||||
$each: addToSetModifier
|
||||
, {}, callback
|
||||
, callback
|
||||
else
|
||||
callback()
|
||||
|
||||
@@ -200,7 +200,7 @@ exports.post '/update', requestAuthenticate, (req, res) ->
|
||||
mTicket.update _id: ticket._id,
|
||||
$pullAll:
|
||||
members: pullModifier
|
||||
, {}, callback
|
||||
, callback
|
||||
else
|
||||
callback()
|
||||
], ->
|
||||
|
||||
@@ -6,7 +6,7 @@ mAccount = require '../../core/model/account'
|
||||
module.exports = exports = express.Router()
|
||||
|
||||
exports.use (req, res, next) ->
|
||||
mAccount.authenticate req.token, (account) ->
|
||||
mAccount.authenticate req.token, (err, account) ->
|
||||
unless account
|
||||
return res.json 400, error: 'auth_failed'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user