Commit 518c491e by 蒋勇

d

parent 0fe740a4
......@@ -30,6 +30,8 @@ class CtlBase {
return system.getResult(rs);
}
async refQuery(pobj, qobj, req) {
pobj.refwhere.app_id=pobj.app_id;
pobj.refwhere.company_id=pobj.company_id;
return this.service.refQuery(pobj);
}
async setContextParams(pobj, qobj, req) {
......@@ -43,6 +45,7 @@ class CtlBase {
credid: req.headers["x-credential-identifier"],
companykey:req.headers["xcompanykey"],//专用于自由用户注册,自由用户用于一定属于某个存在的公司
regrole:req.headers["xregrole"],
bizpath:req.headers["xbizpath"],
}
if(!req.xctx.appkey){
return [-200,"请求头缺少应用x-app-key"]
......@@ -63,6 +66,7 @@ class CtlBase {
if(req.xctx.companyid){//在请求传递数据对象注入公司id
pobj.company_id=req.xctx.companyid;
}
pobj.bizpath=req.xctx.bizpath;
}
async doexec(methodname, pobj, query, req) {
try {
......
......@@ -9,6 +9,10 @@ class UserCtl extends CtlBase {
super("auth", CtlBase.getServiceName(UserCtl));
}
async allowOrNot(pobj, qobj, req){
await this.service.updateByWhere({isEnabled:!pobj.isEnabled},{company_id:pobj.company_id})
return system.getResult({});
}
async initNewInstance(queryobj, req) {
var rtn = {};
rtn.roles = [];
......
......@@ -2,6 +2,7 @@ const system = require("../system")
const settings = require("../../config/settings.js");
class CacheBase {
constructor() {
this.db = system.getObject("db.common.connection").getCon();
this.redisClient = system.getObject("util.redisClient");
this.desc = this.desc();
this.prefix = this.prefix();
......
const CacheBase = require("../cache.base");
const system = require("../../system");
const settings = require("../../../config/settings");
class UserCache extends CacheBase{
constructor(){
super();
this.userDao=system.getObject("db.auth.userDao");
}
isdebug(){
return settings.env=="dev";
}
desc(){
return "缓存本地应用对象";
}
prefix(){
return "g_userlocal_"
}
async buildCacheVal(cachekey,inputkey, val, ex, ...items) {
const configValue = await this.userDao.model.findOne({
where: { userName: inputkey, app_id: settings.pmappid },
attributes: ['userName', 'nickName','headUrl','jwtkey','jwtsecret','created_at','isSuper','isAdmin'],
include: [{ model: this.db.models.company, raw: true, attributes: ["companykey"] }],
raw:true
});
if (configValue) {
return JSON.stringify(configValue);
}
return null;
}
}
module.exports=UserCache;
\ No newline at end of file
......@@ -199,14 +199,18 @@ class Dao {
}
async updateByWhere(setObj, whereObj, t) {
let inWhereObj={}
if (t && t != 'undefined') {
if (whereObj && whereObj != 'undefined') {
whereObj.transaction = t;
inWhereObj["where"]=whereObj;
inWhereObj["transaction"] = t;
} else {
whereObj = { transaction: t };
inWhereObj["transaction"] = t;
}
}else{
inWhereObj["where"]=whereObj;
}
return this.model.update(setObj, whereObj);
return this.model.update(setObj, inWhereObj);
}
async customExecAddOrPutSql(sql, paras = null) {
return this.db.query(sql, paras);
......
......@@ -67,10 +67,12 @@ class UserDao extends Dao{
return {"key":"include","value":[{model:this.db.models.app,},{model:this.db.models.role,as:"Roles",attributes:["id","name"]}]};
}
extraWhere(obj,w,qc,linkAttrs){
if(obj.codepath && obj.codepath!=""){
if(obj.codepath.indexOf("appuser")>0 || obj.codepath.indexOf("organization")>0){//说明是应用管理员的查询
w["app_id"]=obj.appid;
w["owner_id"]=obj.tocompanyid;
if(obj.bizpath && obj.bizpath!=""){
if(obj.bizpath.indexOf("tanents_info")>0){//说明是超级管理员的查询
w["isAdmin"]=true;
}else{
w["isAdmin"]=false;
w["company_id"]=obj.company_id;
}
}
if(linkAttrs.length>0){
......
......@@ -48,6 +48,9 @@ class DbFactory{
this.db.models.org.belongsTo(this.db.models.app,{constraints: false,});
this.db.models.auth.belongsTo(this.db.models.app,{constraints: false,});
this.db.models.app.belongsTo(this.db.models.user,{as:"creator",constraints: false,});
this.db.models.user.belongsTo(this.db.models.company,{constraints: false,});
this.db.models.role.belongsTo(this.db.models.company, {constraints: false,});
this.db.models.org.belongsTo(this.db.models.company,{constraints: false,});
......
......@@ -22,7 +22,8 @@ db.sync({force:true}).then(async ()=>{
await Role.create({code:"pr",name:"个人",isSystem:true,app_id:appnew.id,company_id:settings.pmcompanyid})
let usuper=await usS.pmregister({userName:"sm",password:"951753",isSuper:true,isAdmin:true,isSystem:true,isEnabled:true,nickName:"superman",app_id:appnew.id,company_id:settings.id})
appnew.creator_id=usuper.user.id
await appnew.save()
......
......@@ -15,7 +15,7 @@ class UserService extends ServiceBase {
//按照用户名和密码进行注册
//控制器端检查用户名和密码非空
async registerByTantent(p,q){
p.rolecodes=[settings.pmroleid["pr"]];
p.rolecodes=(p.roles && p.roles.length>0)?p.roles:[settings.pmroleid["pr"]];
let rtn= await this.pmregister(p,q)
return rtn;
}
......@@ -46,15 +46,20 @@ class UserService extends ServiceBase {
let cmpkey = self.getUUID();
let rolecodes = p.rolecodes ? p.rolecodes : [settings.pmroleid["ta"]];
if (rolecodes[0] == settings.pmroleid["ta"]) {//如果注册时,角色是租户,那么需要创建默认公司
p.isAdmin=true;//租户默认就是管理员的权限
let cmp = await self.companyDao.create({ name: p.userName + "的公司", companykey: cmpkey }, t);
p.company_id = cmp.id;
}
//p.company_id----已经在控制器基类中设置
//如果是用户注册,平台用户应该只属于平台应用
let roleappid=p.app_id;//先取出当前应用的id,给后续的取角色用,角色是按照应用和公司区分
p.app_id=settings.pmappid
let u = await self.dao.create(p, t)
//设置默认角色,租户
//设置默认普通角色,由于有了租户概念,所以注册时,需要知道当前租户和应用的id 才可以设置默认角色 todo
//如果是非租户,那么按照当前应用ID是找不到指定的角色,所以是空的
var roles = await self.roleDao.model.findAll({ where: { id: { [self.db.Op.in]: rolecodes },app_id:p.app_id,company_id:p.company_id}, transaction: t });
var roles = await self.roleDao.model.findAll({ where: { id: { [self.db.Op.in]: rolecodes },app_id:roleappid,company_id:p.company_id}, transaction: t });
if(roles && roles.length>0){
await u.setRoles(roles, { transaction: t });
}
......@@ -100,13 +105,13 @@ class UserService extends ServiceBase {
}
var rtn = {}
return this.db.transaction(async function (t) {
//
let userfind = await self.dao.model.findOne({
where: { userName: p.userName, app_id: settings.pmappid },
attributes: ['userName', 'nickName','headUrl','jwtkey','jwtsecret','created_at','isSuper','isAdmin'],
include: [{ model: self.db.models.company, raw: true, attributes: ["companykey"] }]
});
//从缓存中取得
// let userfind = await self.dao.model.findOne({
// where: { userName: p.userName, app_id: settings.pmappid },
// attributes: ['userName', 'nickName','headUrl','jwtkey','jwtsecret','created_at','isSuper','isAdmin'],
// include: [{ model: self.db.models.company, raw: true, attributes: ["companykey"] }]
// });
let userfind = await self.cacheManager["UserCache"].cache(p.userName)
if (userfind) {
let token = await self.cmakejwt(userfind.jwtkey, userfind.jwtsecret, null);
rtn.token = token;
......@@ -120,11 +125,14 @@ class UserService extends ServiceBase {
})
}
async getUserInfo(uname){
let userfind = await this.dao.model.findOne({
where: { userName: uname, app_id: settings.pmappid },
attributes: ['userName', 'nickName',"headUrl",'isSuper','isAdmin'],
include: [{ model: this.db.models.company, raw: true, attributes: ["companykey"] }]
});
// let userfind = await this.dao.model.findOne({
// where: { userName: uname, app_id: settings.pmappid },
// attributes: ['userName', 'nickName',"headUrl",'isSuper','isAdmin'],
// include: [{ model: this.db.models.company, raw: true, attributes: ["companykey"] }]
// });
let userfind = await this.cacheManager["UserCache"].cache(uname)
delete userfind["jwtkey"]
delete userfind["jwtsecret"]
return userfind;
}
......@@ -168,7 +176,7 @@ class UserService extends ServiceBase {
// rtn.token = token;
// rtn.user = u;
regrtn.token=token
return rtn;
return regrtn;
}
}
//不一致那么就
......@@ -252,11 +260,14 @@ class UserService extends ServiceBase {
var self=this;
return this.db.transaction(async function (t) {
let up=await self.dao.update(qobj, t);
//令缓存失效
await this.cacheManager["UserCache"].invalidate(qobj.userName);
let roles = await self.db.models.role.findAll({ where: { id: { [self.db.Op.in]: qobj.roles } } });
if(roles && roles.length>0){
await up.setRoles(roles, { transaction: t });
}
return up;
let cacheUser=await this.cacheManager["UserCache"].cache(up.userName);
return cacheUser;
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment