Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Z
zhichan
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
蒋勇
zhichan
Commits
518c491e
Commit
518c491e
authored
May 02, 2020
by
蒋勇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
d
parent
0fe740a4
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
88 additions
and
25 deletions
+88
-25
center-manage/app/base/controller/ctl.base.js
+4
-0
center-manage/app/base/controller/impl/auth/userCtl.js
+4
-0
center-manage/app/base/db/cache.base.js
+1
-0
center-manage/app/base/db/cache/userCache.js
+33
-0
center-manage/app/base/db/dao.base.js
+7
-3
center-manage/app/base/db/impl/auth/userDao.js
+6
-4
center-manage/app/base/db/impl/common/connection.js
+3
-0
center-manage/app/base/db/initData.js
+2
-1
center-manage/app/base/service/impl/auth/userSve.js
+28
-17
No files found.
center-manage/app/base/controller/ctl.base.js
View file @
518c491e
...
...
@@ -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
{
...
...
center-manage/app/base/controller/impl/auth/userCtl.js
View file @
518c491e
...
...
@@ -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
=
[];
...
...
center-manage/app/base/db/cache.base.js
View file @
518c491e
...
...
@@ -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
();
...
...
center-manage/app/base/db/cache/userCache.js
0 → 100644
View file @
518c491e
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
center-manage/app/base/db/dao.base.js
View file @
518c491e
...
...
@@ -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
,
w
hereObj
);
return
this
.
model
.
update
(
setObj
,
inW
hereObj
);
}
async
customExecAddOrPutSql
(
sql
,
paras
=
null
)
{
return
this
.
db
.
query
(
sql
,
paras
);
...
...
center-manage/app/base/db/impl/auth/userDao.js
View file @
518c491e
...
...
@@ -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
){
...
...
center-manage/app/base/db/impl/common/connection.js
View file @
518c491e
...
...
@@ -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
,});
...
...
center-manage/app/base/db/initData.js
View file @
518c491e
...
...
@@ -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
()
...
...
center-manage/app/base/service/impl/auth/userSve.js
View file @
518c491e
...
...
@@ -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
:
roleapp
id
,
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
r
egr
tn
;
}
}
//不一致那么就
...
...
@@ -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
;
});
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment