egg-born-module-a-base-sync
Advanced tools
Comparing version 1.3.2 to 1.4.0
@@ -72,4 +72,4 @@ // eslint-disable-next-line | ||
}; | ||
// registered or rememberMe | ||
config.registered = { | ||
// authenticated or rememberMe | ||
config.authenticated = { | ||
maxAge: 30 * 24 * 3600 * 1000, // 30 天 | ||
@@ -79,5 +79,16 @@ }; | ||
config.checkUserName = true; | ||
// signupRoleName | ||
// default is 'activated', if need activating by mobile/email, then add to 'registered' first | ||
config.signupRoleName = 'activated'; | ||
// account | ||
config.account = { | ||
needActivation: true, | ||
activationWays: 'mobile,email', | ||
url: { | ||
emailConfirm: '/a/authsimple/emailConfirm', | ||
mobileVerify: '', | ||
passwordChange: '/a/authsimple/passwordChange', | ||
passwordForgot: '/a/authsimple/passwordForgot', | ||
passwordReset: '/a/authsimple/passwordReset', | ||
}, | ||
// default is 'activated', if need activating by mobile/email, then add to 'registered' first | ||
activatedRoles: 'activated', | ||
}; | ||
@@ -84,0 +95,0 @@ // public dir |
@@ -67,2 +67,7 @@ const path = require('path'); | ||
// alert | ||
getAlertUrl({ data }) { | ||
return this.getAbsoluteUrl(`/#!/a/base/base/alert?data=${encodeURIComponent(JSON.stringify(data))}`); | ||
} | ||
modules() { | ||
@@ -69,0 +74,0 @@ if (!_modulesLocales[ctx.locale]) { |
@@ -163,4 +163,3 @@ const modelFn = require('../../../model/role.js'); | ||
const res = await this.modelUserRole.insert({ | ||
userId, | ||
roleId, | ||
userId, roleId, | ||
}); | ||
@@ -170,3 +169,10 @@ return res.insertId; | ||
async deleteUserRole({ id }) { | ||
async deleteUserRole({ id, userId, roleId }) { | ||
if (!id) { | ||
const item = await this.modelUserRole.get({ | ||
userId, roleId, | ||
}); | ||
if (!item) return; | ||
id = item.id; | ||
} | ||
await this.modelUserRole.delete({ id }); | ||
@@ -173,0 +179,0 @@ } |
@@ -18,2 +18,3 @@ const modelFn = require('../../../model/user.js'); | ||
this._sequence = null; | ||
this._config = null; | ||
} | ||
@@ -46,2 +47,7 @@ | ||
get config() { | ||
if (!this._config) this._config = ctx.config.module(moduleInfo.relativeName); | ||
return this._config; | ||
} | ||
async anonymous() { | ||
@@ -57,3 +63,3 @@ // new | ||
async loginAsAnonymous() { | ||
const maxAge = ctx.config.module(moduleInfo.relativeName).anonymous.maxAge; | ||
const maxAge = this.config.anonymous.maxAge; | ||
let userId = ctx.cookies.get('anonymous', { encrypt: true }); | ||
@@ -109,12 +115,57 @@ let userOp; | ||
async signup(user, ops) { | ||
ops = ops || {}; | ||
const userId = await this.add(user); | ||
if (ops.addRole !== false) { | ||
const role = await ctx.meta.role.getSystemRole({ roleName: ctx.config.module(moduleInfo.relativeName).signupRoleName }); | ||
async setActivated({ user }) { | ||
// save | ||
if (user.activated !== undefined) delete user.activated; | ||
await this.save({ user }); | ||
// tryActivate | ||
const tryActivate = user.emailConfirmed || user.mobileVerified; | ||
if (tryActivate) { | ||
await this.userRoleStageActivate({ userId: user.id }); | ||
} | ||
} | ||
async userRoleStageAdd({ userId }) { | ||
// roleNames | ||
let roleNames = this.config.account.needActivation ? 'registered' : this.config.account.activatedRoles; | ||
roleNames = roleNames.split(','); | ||
for (const roleName of roleNames) { | ||
const role = await ctx.meta.role.get({ roleName }); | ||
await ctx.meta.role.addUserRole({ userId, roleId: role.id }); | ||
} | ||
return userId; | ||
} | ||
async userRoleStageActivate({ userId }) { | ||
// get | ||
const user = await this.get({ id: userId }); | ||
// only once | ||
if (user.activated) return; | ||
// adjust role | ||
if (this.config.account.needActivation) { | ||
// userRoles | ||
const userRoles = await ctx.meta.role.getUserRolesDirect({ userId }); | ||
// userRolesMap | ||
const map = {}; | ||
for (const role of userRoles) { | ||
map[role.roleName] = role; | ||
} | ||
// remove from registered | ||
if (map.registered) { | ||
const roleRegistered = await ctx.meta.role.getSystemRole({ roleName: 'registered' }); | ||
await ctx.meta.role.deleteUserRole({ userId, roleId: roleRegistered.id }); | ||
} | ||
// add to activated | ||
const roleNames = this.config.account.activatedRoles.split(','); | ||
for (const roleName of roleNames) { | ||
if (!map[roleName]) { | ||
const role = await ctx.meta.role.get({ roleName }); | ||
await ctx.meta.role.addUserRole({ userId, roleId: role.id }); | ||
} | ||
} | ||
} | ||
// set activated | ||
await this.save({ | ||
user: { id: userId, activated: 1 }, | ||
}); | ||
} | ||
async exists({ userName, email, mobile }) { | ||
@@ -124,3 +175,3 @@ userName = userName || ''; | ||
mobile = mobile || ''; | ||
if (ctx.config.module(moduleInfo.relativeName).checkUserName === true && userName) { | ||
if (this.config.checkUserName === true && userName) { | ||
return await this.model.queryOne( | ||
@@ -137,3 +188,5 @@ `select * from aUser | ||
async add({ disabled = 0, userName, realName, email, mobile, avatar, motto, locale, anonymous = 0 }) { | ||
async add({ | ||
disabled = 0, userName, realName, email, mobile, avatar, motto, locale, anonymous = 0, | ||
}) { | ||
// check if incomplete information | ||
@@ -143,3 +196,3 @@ let needCheck; | ||
needCheck = false; | ||
} else if (ctx.config.module(moduleInfo.relativeName).checkUserName === true) { | ||
} else if (this.config.checkUserName === true) { | ||
needCheck = userName || email || mobile; | ||
@@ -174,3 +227,5 @@ } else { | ||
async save({ user }) { | ||
await this.model.update(user); | ||
if (Object.keys(user).length > 1) { | ||
await this.model.update(user); | ||
} | ||
} | ||
@@ -267,7 +322,4 @@ | ||
async verify(profileUser) { | ||
// state | ||
// login/associate | ||
const state = ctx.request.query.state || 'login'; | ||
// state: login/associate | ||
async verify({ state = 'login', profileUser }) { | ||
// verifyUser | ||
@@ -281,3 +333,3 @@ const verifyUser = {}; | ||
}); | ||
const config = JSON.parse(providerItem.config); | ||
// const config = JSON.parse(providerItem.config); | ||
@@ -314,3 +366,5 @@ // check if auth exists | ||
// columns | ||
const columns = [ 'userName', 'realName', 'email', 'mobile', 'avatar', 'motto', 'locale' ]; | ||
const columns = [ | ||
'userName', 'realName', 'email', 'mobile', 'avatar', 'motto', 'locale', | ||
]; | ||
@@ -328,2 +382,8 @@ // | ||
if (authUserId !== userId) { | ||
// delete old records | ||
await this.modelAuth.delete({ | ||
providerId: providerItem.id, | ||
userId, | ||
}); | ||
// update | ||
await this.modelAuth.update({ | ||
@@ -350,6 +410,4 @@ id: authId, | ||
} else { | ||
// check if addUser | ||
if (config.addUser === false) return false; | ||
// add user | ||
userId = await this._addUserInfo(profileUser.profile, columns, config.addRole !== false); | ||
userId = await this._addUserInfo(profileUser.profile, columns); | ||
user = await this.model.get({ id: userId }); | ||
@@ -371,3 +429,3 @@ // update auth's userId | ||
} else { | ||
ctx.session.maxAge = profileUser.maxAge || ctx.config.module(moduleInfo.relativeName).registered.maxAge; | ||
ctx.session.maxAge = profileUser.maxAge || this.config.authenticated.maxAge; | ||
} | ||
@@ -377,8 +435,26 @@ return verifyUser; | ||
async _addUserInfo(profile, columns, addRole) { | ||
async _addUserInfo(profile, columns) { | ||
const user = {}; | ||
for (const column of columns) { | ||
// others | ||
await this._setUserInfoColumn(user, column, profile[column]); | ||
} | ||
return await this.signup(user, { addRole }); | ||
// add user | ||
const userId = await this.add(user); | ||
// add role | ||
await this.userRoleStageAdd({ userId }); | ||
// try setActivated | ||
const data = { id: userId }; | ||
// emailConfirmed | ||
if (profile.emailConfirmed && profile.email) { | ||
data.emailConfirmed = 1; | ||
} | ||
// mobileVerified | ||
if (profile.mobileVerified && profile.mobile) { | ||
data.mobileVerified = 1; | ||
} | ||
// setActivated | ||
await this.setActivated({ user: data }); | ||
// ok | ||
return userId; | ||
} | ||
@@ -395,6 +471,7 @@ async _updateUserInfo(userId, profile, columns) { | ||
user.id = userId; | ||
await this.model.update(user); | ||
await this.save({ user }); | ||
} | ||
async _setUserInfoColumn(user, column, value) { | ||
// only set when empty | ||
if (user[column] || !value) return; | ||
@@ -412,3 +489,3 @@ // userName | ||
if (res) { | ||
value = ''; | ||
value = null; | ||
} | ||
@@ -415,0 +492,0 @@ } |
@@ -53,2 +53,12 @@ const require3 = require('require3'); | ||
replaceTemplate(content, scope) { | ||
if (!content) return null; | ||
return content.toString().replace(/(\\)?{{ *(\w+) *}}/g, (block, skip, key) => { | ||
if (skip) { | ||
return block.substring(skip.length); | ||
} | ||
return scope[key] !== undefined ? scope[key] : ''; | ||
}); | ||
} | ||
} | ||
@@ -55,0 +65,0 @@ |
@@ -13,2 +13,3 @@ module.exports = app => { | ||
'x-exists': true, | ||
ebReadOnly: true, | ||
}, | ||
@@ -25,5 +26,6 @@ realName: { | ||
ebTitle: 'Email', | ||
notEmpty: true, | ||
format: 'email', | ||
// notEmpty: true, | ||
// format: 'email', | ||
'x-exists': true, | ||
ebReadOnly: true, | ||
}, | ||
@@ -34,4 +36,5 @@ mobile: { | ||
ebTitle: 'Mobile', | ||
notEmpty: true, | ||
// notEmpty: true, | ||
'x-exists': true, | ||
ebReadOnly: true, | ||
}, | ||
@@ -38,0 +41,0 @@ motto: { |
@@ -0,1 +1,4 @@ | ||
const require3 = require('require3'); | ||
const extend = require3('extend2'); | ||
module.exports = app => { | ||
@@ -42,4 +45,6 @@ | ||
this.app.passport.verify(async function(ctx, profileUser) { | ||
// state: login/associate | ||
const state = ctx.request.query.state || 'login'; | ||
// user verify | ||
const verifyUser = await ctx.meta.user.verify(profileUser); | ||
const verifyUser = await ctx.meta.user.verify({ state, profileUser }); | ||
// user verify event | ||
@@ -67,2 +72,3 @@ await ctx.meta.event.invoke({ | ||
instance: this.getInstance(), | ||
config: this.getConfig(), | ||
}; | ||
@@ -83,2 +89,17 @@ // login info event | ||
getConfig() { | ||
// account | ||
const account = extend(true, {}, this.ctx.config.account); | ||
account.activatedRoles = undefined; | ||
// config | ||
const config = { | ||
modules: { | ||
'a-base': { | ||
account, | ||
}, | ||
}, | ||
}; | ||
return config; | ||
} | ||
} | ||
@@ -85,0 +106,0 @@ |
@@ -35,8 +35,14 @@ const require3 = require('require3'); | ||
const authenticate = createAuthenticate(moduleRelativeName, providerName, config); | ||
// middlewares | ||
const middlewaresPost = []; | ||
const middlewaresGet = []; | ||
if (!this.ctx.app.meta.isTest) middlewaresPost.push('inner'); | ||
middlewaresPost.push(authenticate); | ||
middlewaresGet.push(authenticate); | ||
// mount routes | ||
const routes = [ | ||
{ name: `get:${config.loginURL}`, method: 'get', path: '/' + config.loginURL, middlewares: [ authenticate ], meta: { auth: { enable: false } } }, | ||
{ name: `post:${config.loginURL}`, method: 'post', path: '/' + config.loginURL, middlewares: [ authenticate ], meta: { auth: { enable: false } } }, | ||
{ name: `get:${config.callbackURL}`, method: 'get', path: '/' + config.callbackURL, middlewares: [ authenticate ], meta: { auth: { enable: false } } }, | ||
{ name: `post:${config.callbackURL}`, method: 'post', path: '/' + config.callbackURL, middlewares: [ authenticate ], meta: { auth: { enable: false } } }, | ||
{ name: `get:${config.loginURL}`, method: 'get', path: '/' + config.loginURL, middlewares: middlewaresGet, meta: { auth: { enable: false } } }, | ||
{ name: `post:${config.loginURL}`, method: 'post', path: '/' + config.loginURL, middlewares: middlewaresPost, meta: { auth: { enable: false } } }, | ||
{ name: `get:${config.callbackURL}`, method: 'get', path: '/' + config.callbackURL, middlewares: middlewaresGet, meta: { auth: { enable: false } } }, | ||
// { name: `post:${config.callbackURL}`, method: 'post', path: '/' + config.callbackURL, middlewares, meta: { auth: { enable: false } } }, | ||
]; | ||
@@ -43,0 +49,0 @@ for (const route of routes) { |
@@ -5,2 +5,3 @@ const VersionUpdate1Fn = require('./version/update1.js'); | ||
const VersionUpdate4Fn = require('./version/update4.js'); | ||
const VersionUpdate6Fn = require('./version/update6.js'); | ||
const VersionInit2Fn = require('./version/init2.js'); | ||
@@ -16,2 +17,7 @@ const VersionInit4Fn = require('./version/init4.js'); | ||
if (options.version === 6) { | ||
const versionUpdate6 = new (VersionUpdate6Fn(this.ctx))(); | ||
await versionUpdate6.run(); | ||
} | ||
if (options.version === 4) { | ||
@@ -18,0 +24,0 @@ const versionUpdate4 = new (VersionUpdate4Fn(this.ctx))(); |
@@ -47,2 +47,6 @@ const require3 = require('require3'); | ||
const userId = await ctx.meta.user.add(userRoot.item); | ||
// activated | ||
await ctx.meta.user.save({ | ||
user: { id: userId, activated: 1 }, | ||
}); | ||
// user->role | ||
@@ -49,0 +53,0 @@ await ctx.meta.role.addUserRole({ |
@@ -35,2 +35,3 @@ export default { | ||
Search: '搜索', | ||
Link: '链接', | ||
'Atom Name': '原子名称', | ||
@@ -51,2 +52,3 @@ 'Modification time': '修改时间', | ||
'Flag Not Found': 'Flag未发现', | ||
'Friendly Tips': '友情提示', | ||
}; |
@@ -20,2 +20,3 @@ function load(name) { | ||
{ path: 'attachment/list', component: load('attachment/list') }, | ||
{ path: 'base/alert', component: load('base/alert') }, | ||
]; |
{ | ||
"name": "egg-born-module-a-base-sync", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"title": "Base", | ||
"eggBornModule": { | ||
"fileVersion": 5, | ||
"fileVersion": 6, | ||
"dependencies": { | ||
@@ -8,0 +8,0 @@ "a-instance": "1.0.0", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
1028691
134
15410