mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-04-29 20:16:08 +08:00
model enum pluggable
This commit is contained in:
42
app.coffee
42
app.coffee
@@ -7,6 +7,7 @@ app.libs =
|
|||||||
async: require 'async'
|
async: require 'async'
|
||||||
bodyParser: require 'body-parser'
|
bodyParser: require 'body-parser'
|
||||||
cookieParser: require 'cookie-parser'
|
cookieParser: require 'cookie-parser'
|
||||||
|
copy: require 'copy-to'
|
||||||
crypto: require 'crypto'
|
crypto: require 'crypto'
|
||||||
depd: require 'depd'
|
depd: require 'depd'
|
||||||
express: require 'express'
|
express: require 'express'
|
||||||
@@ -23,7 +24,7 @@ app.libs =
|
|||||||
|
|
||||||
ObjectId: (require 'mongoose').Schema.Types.ObjectId
|
ObjectId: (require 'mongoose').Schema.Types.ObjectId
|
||||||
|
|
||||||
{cookieParser, crypto, bodyParser, depd, express, fs, harp, middlewareInjector, mongoose} = exports.libs
|
{cookieParser, copy, crypto, bodyParser, depd, express, fs, harp, middlewareInjector, mongoose} = exports.libs
|
||||||
{morgan, nodemailer, path, redis, redisStore, expressSession} = exports.libs
|
{morgan, nodemailer, path, redis, redisStore, expressSession} = exports.libs
|
||||||
|
|
||||||
RedisStore = redisStore expressSession
|
RedisStore = redisStore expressSession
|
||||||
@@ -66,14 +67,35 @@ do ->
|
|||||||
|
|
||||||
app.config = config
|
app.config = config
|
||||||
app.db = require './core/db'
|
app.db = require './core/db'
|
||||||
|
app.pluggable = require './core/pluggable'
|
||||||
|
|
||||||
|
app.schemas = {}
|
||||||
|
|
||||||
|
app.models =
|
||||||
|
Account: {}
|
||||||
|
BalanceLog: {}
|
||||||
|
CouponCode: {}
|
||||||
|
Notification: {}
|
||||||
|
SecurityLog: {}
|
||||||
|
Ticket: {}
|
||||||
|
|
||||||
|
app.pluggable.initializePlugins()
|
||||||
|
|
||||||
|
require './core/model/account'
|
||||||
|
require './core/model/balance_log'
|
||||||
|
require './core/model/coupon_code'
|
||||||
|
require './core/model/notification'
|
||||||
|
require './core/model/security_log'
|
||||||
|
require './core/model/ticket'
|
||||||
|
|
||||||
|
for name, schema of app.schemas
|
||||||
|
copy(mongoose.model(name, schema)).to app.models[name]
|
||||||
|
|
||||||
app.templates = require './core/templates'
|
app.templates = require './core/templates'
|
||||||
app.i18n = require './core/i18n'
|
app.i18n = require './core/i18n'
|
||||||
app.utils = require './core/utils'
|
app.utils = require './core/utils'
|
||||||
app.cache = require './core/cache'
|
app.cache = require './core/cache'
|
||||||
app.config = require './config'
|
|
||||||
app.package = require './package.json'
|
|
||||||
app.billing = require './core/billing'
|
app.billing = require './core/billing'
|
||||||
app.pluggable = require './core/pluggable'
|
|
||||||
app.middleware = require './core/middleware'
|
app.middleware = require './core/middleware'
|
||||||
app.notification = require './core/notification'
|
app.notification = require './core/notification'
|
||||||
app.authenticator = require './core/authenticator'
|
app.authenticator = require './core/authenticator'
|
||||||
@@ -84,16 +106,6 @@ app.express = express()
|
|||||||
app.redis = redis.createClient 6379, '127.0.0.1',
|
app.redis = redis.createClient 6379, '127.0.0.1',
|
||||||
auth_pass: config.redis.password
|
auth_pass: config.redis.password
|
||||||
|
|
||||||
app.schemas = {}
|
|
||||||
|
|
||||||
app.models =
|
|
||||||
Account: require './core/model/account'
|
|
||||||
BalanceLog: require './core/model/balance_log'
|
|
||||||
CouponCode: require './core/model/coupon_code'
|
|
||||||
Notification: require './core/model/notification'
|
|
||||||
SecurityLog: require './core/model/security_log'
|
|
||||||
Ticket: require './core/model/ticket'
|
|
||||||
|
|
||||||
app.express.use bodyParser.json()
|
app.express.use bodyParser.json()
|
||||||
app.express.use morgan 'dev'
|
app.express.use morgan 'dev'
|
||||||
app.express.use cookieParser()
|
app.express.use cookieParser()
|
||||||
@@ -164,8 +176,6 @@ app.express.use '/ticket', require './core/router/ticket'
|
|||||||
app.express.use '/admin', require './core/router/admin'
|
app.express.use '/admin', require './core/router/admin'
|
||||||
app.express.use '/panel', require './core/router/panel'
|
app.express.use '/panel', require './core/router/panel'
|
||||||
|
|
||||||
app.pluggable.initializePlugins()
|
|
||||||
|
|
||||||
app.express.get '/', (req, res) ->
|
app.express.get '/', (req, res) ->
|
||||||
unless res.headerSent
|
unless res.headerSent
|
||||||
res.redirect '/panel/'
|
res.redirect '/panel/'
|
||||||
|
|||||||
@@ -11,3 +11,6 @@ else
|
|||||||
module.exports = mongoose.createConnection mongodb_uri
|
module.exports = mongoose.createConnection mongodb_uri
|
||||||
|
|
||||||
exports.mongodb_uri = mongodb_uri
|
exports.mongodb_uri = mongodb_uri
|
||||||
|
|
||||||
|
mongoose.connection.on 'error', (err) ->
|
||||||
|
console.error err if err
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
async = require 'async'
|
{pluggable} = app
|
||||||
_ = require 'underscore'
|
{selectModelEnum} = pluggable
|
||||||
|
|
||||||
{_, async, mongoose} = app.libs
|
{_, async, mongoose} = app.libs
|
||||||
|
|
||||||
Token = mongoose.Schema
|
Token = mongoose.Schema
|
||||||
type:
|
type:
|
||||||
required: true
|
required: true
|
||||||
type: String
|
type: String
|
||||||
enum: ['full_access']
|
enum: ['full_access'].concat selectModelEnum 'Token', 'type'
|
||||||
|
|
||||||
token:
|
token:
|
||||||
required: true
|
required: true
|
||||||
@@ -81,7 +80,9 @@ Account = mongoose.Schema
|
|||||||
type: Object
|
type: Object
|
||||||
default: {}
|
default: {}
|
||||||
|
|
||||||
module.exports = mongoose.model 'Account', Account
|
_.extend app.schemas,
|
||||||
|
Account: Account
|
||||||
|
Token: Token
|
||||||
|
|
||||||
# @param account: username, email, password
|
# @param account: username, email, password
|
||||||
# @param callback(account)
|
# @param callback(account)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
{pluggable} = app
|
||||||
|
{selectModelEnum} = pluggable
|
||||||
{_, ObjectId, mongoose} = app.libs
|
{_, ObjectId, mongoose} = app.libs
|
||||||
|
|
||||||
BalanceLog = mongoose.Schema
|
BalanceLog = mongoose.Schema
|
||||||
@@ -9,7 +11,7 @@ BalanceLog = mongoose.Schema
|
|||||||
type:
|
type:
|
||||||
required: true
|
required: true
|
||||||
type: String
|
type: String
|
||||||
enum: ['deposit']
|
enum: ['deposit'].concat selectModelEnum 'BalanceLog', 'type'
|
||||||
|
|
||||||
amount:
|
amount:
|
||||||
required: true
|
required: true
|
||||||
@@ -23,4 +25,5 @@ BalanceLog = mongoose.Schema
|
|||||||
type: Object
|
type: Object
|
||||||
default: {}
|
default: {}
|
||||||
|
|
||||||
module.exports = mongoose.model 'BalanceLog', BalanceLog
|
_.extend app.schemas,
|
||||||
|
BalanceLog: BalanceLog
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
{pluggable} = app
|
||||||
|
{selectModelEnum} = pluggable
|
||||||
{_, ObjectId, mongoose} = app.libs
|
{_, ObjectId, mongoose} = app.libs
|
||||||
|
|
||||||
CouponCode = mongoose.Schema
|
CouponCode = mongoose.Schema
|
||||||
@@ -16,7 +18,7 @@ CouponCode = mongoose.Schema
|
|||||||
type:
|
type:
|
||||||
required: true
|
required: true
|
||||||
type: String
|
type: String
|
||||||
enum: ['amount']
|
enum: ['amount'].concat selectModelEnum 'CouponCode', 'type'
|
||||||
|
|
||||||
meta:
|
meta:
|
||||||
type: Object
|
type: Object
|
||||||
@@ -33,7 +35,8 @@ CouponCode = mongoose.Schema
|
|||||||
default: Date.now
|
default: Date.now
|
||||||
]
|
]
|
||||||
|
|
||||||
module.exports = mongoose.model 'CouponCode', CouponCode
|
_.extend app.schemas,
|
||||||
|
CouponCode: CouponCode
|
||||||
|
|
||||||
exports.type_meta =
|
exports.type_meta =
|
||||||
amount:
|
amount:
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
{pluggable} = app
|
||||||
|
{selectModelEnum} = pluggable
|
||||||
{_, ObjectId, mongoose} = app.libs
|
{_, ObjectId, mongoose} = app.libs
|
||||||
|
|
||||||
module.exports = exports = app.db.collection 'notifications'
|
|
||||||
|
|
||||||
Notification = mongoose.Schema
|
Notification = mongoose.Schema
|
||||||
account_id:
|
account_id:
|
||||||
type: ObjectId
|
type: ObjectId
|
||||||
@@ -15,12 +15,12 @@ Notification = mongoose.Schema
|
|||||||
type:
|
type:
|
||||||
required: true
|
required: true
|
||||||
type: String
|
type: String
|
||||||
enum: ['payment_success']
|
enum: ['payment_success'].concat selectModelEnum 'Notification', 'type'
|
||||||
|
|
||||||
level:
|
level:
|
||||||
required: true
|
required: true
|
||||||
type: String
|
type: String
|
||||||
enum: ['notice', 'event', 'log']
|
enum: ['notice', 'event', 'log'].concat selectModelEnum 'Notification', 'type'
|
||||||
|
|
||||||
created_at:
|
created_at:
|
||||||
type: Date
|
type: Date
|
||||||
@@ -30,7 +30,8 @@ Notification = mongoose.Schema
|
|||||||
type: Object
|
type: Object
|
||||||
default: {}
|
default: {}
|
||||||
|
|
||||||
module.exports = mongoose.model 'Notification', Notification
|
_.extend app.schemas,
|
||||||
|
Notification: Notification
|
||||||
|
|
||||||
exports.createNotice = (account, group_name, type, level, meta, callback) ->
|
exports.createNotice = (account, group_name, type, level, meta, callback) ->
|
||||||
exports.insert
|
exports.insert
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
{pluggable} = app
|
||||||
|
{selectModelEnum} = pluggable
|
||||||
{_, ObjectId, mongoose} = app.libs
|
{_, ObjectId, mongoose} = app.libs
|
||||||
|
|
||||||
SecurityLog = mongoose.Schema
|
SecurityLog = mongoose.Schema
|
||||||
@@ -9,7 +11,7 @@ SecurityLog = mongoose.Schema
|
|||||||
type:
|
type:
|
||||||
required: true
|
required: true
|
||||||
type: String
|
type: String
|
||||||
enum: ['update_password', 'update_setting', 'update_email']
|
enum: ['update_password', 'update_setting', 'update_email'].concat selectModelEnum 'SecurityLog', 'type'
|
||||||
|
|
||||||
created_at:
|
created_at:
|
||||||
type: Date
|
type: Date
|
||||||
@@ -24,7 +26,8 @@ SecurityLog = mongoose.Schema
|
|||||||
type: ObjectId
|
type: ObjectId
|
||||||
ref: 'Token'
|
ref: 'Token'
|
||||||
|
|
||||||
module.exports = mongoose.model 'SecurityLog', SecurityLog
|
_.extend app.schemas,
|
||||||
|
SecurityLog: SecurityLog
|
||||||
|
|
||||||
exports.create = (account, type, token, payload, callback) ->
|
exports.create = (account, type, token, payload, callback) ->
|
||||||
matched_token = _.findWhere account.tokens,
|
matched_token = _.findWhere account.tokens,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
{pluggable} = app
|
||||||
|
{selectModelEnum} = pluggable
|
||||||
{_, ObjectId, mongoose} = app.libs
|
{_, ObjectId, mongoose} = app.libs
|
||||||
|
|
||||||
module.exports = exports = app.db.collection 'tickets'
|
|
||||||
|
|
||||||
Reply = mongoose.Schema
|
Reply = mongoose.Schema
|
||||||
_id:
|
_id:
|
||||||
type: ObjectId
|
type: ObjectId
|
||||||
@@ -50,7 +50,7 @@ Ticket = mongoose.Schema
|
|||||||
|
|
||||||
status:
|
status:
|
||||||
type: String
|
type: String
|
||||||
enum: ['open', 'pending', 'finish', 'closed']
|
enum: ['open', 'pending', 'finish', 'closed'].concat selectModelEnum 'Ticket', 'type'
|
||||||
|
|
||||||
option:
|
option:
|
||||||
type: Object
|
type: Object
|
||||||
@@ -64,7 +64,8 @@ Ticket = mongoose.Schema
|
|||||||
Reply
|
Reply
|
||||||
]
|
]
|
||||||
|
|
||||||
module.exports = mongoose.model 'Ticket', Ticket
|
_.extend app.schemas,
|
||||||
|
Ticket: Ticket
|
||||||
|
|
||||||
exports.createTicket = (account, title, content, members, status, options, callback) ->
|
exports.createTicket = (account, title, content, members, status, options, callback) ->
|
||||||
exports.insert
|
exports.insert
|
||||||
|
|||||||
@@ -14,13 +14,27 @@ hookHelper = (options) ->
|
|||||||
return _.extend [], options
|
return _.extend [], options
|
||||||
|
|
||||||
exports.hooks =
|
exports.hooks =
|
||||||
|
app:
|
||||||
|
# action: function(callback)
|
||||||
|
started: hookHelper
|
||||||
|
global_event: true
|
||||||
|
|
||||||
|
model:
|
||||||
|
# model: string, field: string, type: string
|
||||||
|
type_enum: hookHelper
|
||||||
|
global_event: true
|
||||||
|
|
||||||
|
# model: string, action(schema, callback)
|
||||||
|
middleware: hookHelper
|
||||||
|
global_event: true
|
||||||
|
|
||||||
account:
|
account:
|
||||||
# filter: function(username, callback(is_allow))
|
# filter: function(username, callback(is_allow))
|
||||||
username_filter: hookHelper
|
username_filter: hookHelper
|
||||||
always_notice: true
|
global_event: true
|
||||||
# filter: function(account, callback)
|
# filter: function(account, callback)
|
||||||
before_register: hookHelper
|
before_register: hookHelper
|
||||||
always_notice: true
|
global_event: true
|
||||||
# action: function(account, callback)
|
# action: function(account, callback)
|
||||||
resources_limit_changed: []
|
resources_limit_changed: []
|
||||||
|
|
||||||
@@ -105,7 +119,7 @@ exports.selectHook = (account, hook_name) ->
|
|||||||
return _.filter pointer, (hook) ->
|
return _.filter pointer, (hook) ->
|
||||||
if hook.plugin_info.type == 'extension'
|
if hook.plugin_info.type == 'extension'
|
||||||
return true
|
return true
|
||||||
else if pointer.always_notice or hook.always_notice
|
else if pointer.global_event or hook.always_notice
|
||||||
return true
|
return true
|
||||||
else if !account
|
else if !account
|
||||||
return false
|
return false
|
||||||
@@ -114,6 +128,15 @@ exports.selectHook = (account, hook_name) ->
|
|||||||
else
|
else
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
exports.selectModelEnum = (model, field) ->
|
||||||
|
result = []
|
||||||
|
|
||||||
|
for hook in exports.selectHook null, 'model.type_enum'
|
||||||
|
if hook.model == model and hook.field == field
|
||||||
|
result.push hook.type
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
exports.initializePlugins = (callback) ->
|
exports.initializePlugins = (callback) ->
|
||||||
checkDependencies = ->
|
checkDependencies = ->
|
||||||
all_plugins = _.union config.plugin.available_extensions, config.plugin.available_services
|
all_plugins = _.union config.plugin.available_extensions, config.plugin.available_services
|
||||||
|
|||||||
@@ -53,7 +53,8 @@
|
|||||||
"morgan": "^1.3.2",
|
"morgan": "^1.3.2",
|
||||||
"mongoose": "^3.8.17",
|
"mongoose": "^3.8.17",
|
||||||
"depd": "^1.0.0",
|
"depd": "^1.0.0",
|
||||||
"body-parser": "^1.9.0"
|
"body-parser": "^1.9.0",
|
||||||
|
"copy-to": "^1.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "^1.21.5",
|
"mocha": "^1.21.5",
|
||||||
|
|||||||
Reference in New Issue
Block a user