diff --git a/app.coffee b/app.coffee index 5fe84a7..8489085 100644 --- a/app.coffee +++ b/app.coffee @@ -41,7 +41,7 @@ exports.run = -> app.package = require './package.json' app.pluggable = require './core/pluggable' app.middleware = require './core/middleware' - app.token_manager = require './core/token_manager' + app.authenticator = require './core/authenticator' app.models = mAccount: require './model/account' diff --git a/bin/rp-migration.coffee b/bin/rp-migration.coffee index b13948e..74e7ebe 100644 --- a/bin/rp-migration.coffee +++ b/bin/rp-migration.coffee @@ -38,7 +38,7 @@ service supervisor restart bitcoin_secret = crypto.createHash('sha256').update(crypto.randomBytes(256)).digest('hex') bitcoin.genAddress bitcoin_secret, (address) -> - mAccount.update _id: account._id, + mAccount.update {_id: account._id}, $set: 'attribute.bitcoin_deposit_address': address 'bitcoin_secret': bitcoin_secret diff --git a/core/token_manager.coffee b/core/authenticator.coffee similarity index 97% rename from core/token_manager.coffee rename to core/authenticator.coffee index a8701f5..cfed23f 100644 --- a/core/token_manager.coffee +++ b/core/authenticator.coffee @@ -15,7 +15,7 @@ exports.generateAvailableToken = (callback) -> # @param callback(token) exports.createToken = (account, type, payload, callback) -> exports.generateAvailableToken (token) -> - exports.update _id: account._id, + exports.update {_id: account._id}, $push: tokens: type: type diff --git a/core/model/account.coffee b/core/model/account.coffee index b393ee0..a11f2ea 100644 --- a/core/model/account.coffee +++ b/core/model/account.coffee @@ -25,9 +25,11 @@ sample = services: ['shadowsocks'] plans: ['all'] + last_billing_at: + all: new Date() + balance: 100 - last_billing_at: Date() - arrears_at: Date() + arrears_at: new Date() pluggable: bitcoin: @@ -83,7 +85,7 @@ exports.register = (account, callback) -> plans: [] balance: 0 - last_billing_at: Date() + last_billing_at: {} arrears_at: null pluggable: {} @@ -101,7 +103,7 @@ exports.register = (account, callback) -> exports.updatePassword = (account, password, callback) -> password_salt = utils.randomSalt() - exports.update _id: account._id, + exports.update {_id: account._id}, $set: password: utils.hashPassword password, password_salt password_salt: password_salt @@ -127,7 +129,7 @@ exports.inGroup = (account, group) -> exports.joinPlan = (account, plan, callback) -> account.attribute.plans.push plan - exports.update _id: account._id, + exports.update {_id: account._id}, $addToSet: 'attribute.plans': plan $set: @@ -136,7 +138,7 @@ exports.joinPlan = (account, plan, callback) -> exports.leavePlan = (account, plan, callback) -> account.attribute.plans = _.reject account.attribute.plans, (i) -> i == plan - exports.update _id: account._id, + exports.update {_id: account._id}, $pull: 'attribute.plans': plan $set: @@ -144,7 +146,7 @@ exports.leavePlan = (account, plan, callback) -> , callback exports.incBalance = (account, type, amount, attribute, callback) -> - exports.update _id: account._id, + exports.update {_id: account._id}, $inc: 'attribute.balance': amount , -> diff --git a/core/model/coupon_code.coffee b/core/model/coupon_code.coffee index 52feffc..054666d 100644 --- a/core/model/coupon_code.coffee +++ b/core/model/coupon_code.coffee @@ -30,7 +30,7 @@ exports.codeMessage = (coupon_code) -> return exports.type_meta[coupon_code.type].message coupon_code exports.applyCode = (account, coupon_code, callback) -> - exports.update _id: coupon_code._id, + exports.update {_id: coupon_code._id}, $inc: available_times: -1 $push: diff --git a/core/model/ticket.coffee b/core/model/ticket.coffee index 62feba2..8ed1ec0 100644 --- a/core/model/ticket.coffee +++ b/core/model/ticket.coffee @@ -56,7 +56,7 @@ exports.createReply = (ticket, account, content, status, callback) -> content_html: markdown.toHTML content payload: {} - exports.update _id: ticket._id, + exports.update {_id: ticket._id}, $push: replies: data $set: @@ -70,7 +70,7 @@ exports.createReply = (ticket, account, content, status, callback) -> callback null, data exports.addMember = (ticket, account, callback) -> - exports.update _id: ticket._id, + exports.update {_id: ticket._id}, $push: members: account._id $set: diff --git a/core/router/account.coffee b/core/router/account.coffee index a171f16..ea94958 100644 --- a/core/router/account.coffee +++ b/core/router/account.coffee @@ -1,6 +1,6 @@ {renderAccount, errorHandling, requireAuthenticate} = app.middleware {mAccount, mSecurityLog, mCouponCode} = app.models -{pluggable, config, utils, token_manager} = app +{pluggable, config, utils, authenticator} = app module.exports = exports = express.Router() @@ -57,7 +57,7 @@ exports.post '/register', errorHandling, (req, res) -> return if err mAccount.register _.pick(req.body, 'username', 'email', 'password'), (err, account) -> - token_manager.createToken account, + authenticator.createToken account, ip: req.headers['x-real-ip'] ua: req.headers['user-agent'] , (token)-> @@ -75,7 +75,7 @@ exports.post '/login', errorHandling, (req, res) -> unless mAccount.matchPassword account, req.body.password return res.error 'wrong_password' - token_manager.createToken account, + authenticator.createToken account, ip: req.headers['x-real-ip'] ua: req.headers['user-agent'] , (err, token) -> @@ -87,7 +87,7 @@ exports.post '/login', errorHandling, (req, res) -> token: token exports.post '/logout', requireAuthenticate, (req, res) -> - token_manager.remokeToken req.token, + authenticator.remokeToken req.token, revoke_at: new Date() revoke_ip: req.headers['x-real-ip'] revoke_ua: req.headers['user-agent'] @@ -118,7 +118,7 @@ exports.post '/update_email', requireAuthenticate, (req, res) -> unless utils.rx.email.test req.body.email return res.error 'invalid_email' - mAccount.update _id: req.account._id, + mAccount.update {_id: req.account._id}, $set: email: req.body.email , -> diff --git a/core/router/ticket.coffee b/core/router/ticket.coffee index 69568b5..954d600 100644 --- a/core/router/ticket.coffee +++ b/core/router/ticket.coffee @@ -175,7 +175,7 @@ exports.post '/update', requireAuthenticate, (req, res) -> async.parallel [ (callback) -> unless _.isEmpty modifier - mTicket.update _id: ticket._id, + mTicket.update {_id: ticket._id}, $set: modifier , callback else @@ -183,7 +183,7 @@ exports.post '/update', requireAuthenticate, (req, res) -> (callback) -> unless _.isEmpty addToSetModifier - mTicket.update _id: ticket._id, + mTicket.update {_id: ticket._id}, $addToSet: members: $each: addToSetModifier @@ -193,7 +193,7 @@ exports.post '/update', requireAuthenticate, (req, res) -> (callback) -> unless _.isEmpty pullModifier - mTicket.update _id: ticket._id, + mTicket.update {_id: ticket._id}, $pullAll: members: pullModifier , callback diff --git a/sample/core.config.coffee b/sample/core.config.coffee index db71ed5..900b166 100644 --- a/sample/core.config.coffee +++ b/sample/core.config.coffee @@ -24,8 +24,7 @@ module.exports = when_balance_below: 0 when_arrears_above: 0 - cyclical_billing: 3600 * 1000 - daily_billing_cycle: 24 * 3600 * 1000 + billing_cycle: 10 * 60 * 1000 plans: sample: @@ -34,6 +33,8 @@ module.exports = billing_by_time: unit: 3600 * 1000 + min_billing_unit: 24 + price: 10 / (30 * 24) services: []