Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
db_gpt
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
linyangyang
db_gpt
Commits
2a74dc05
Commit
2a74dc05
authored
Aug 14, 2024
by
林洋洋
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改密码 添加根据部门查询用户
parent
f8655823
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
97 additions
and
36 deletions
+97
-36
crud.py
dbgpt/app/apps/vadmin/auth/crud.py
+18
-0
user.py
dbgpt/app/apps/vadmin/auth/models/user.py
+1
-1
user.py
dbgpt/app/apps/vadmin/auth/params/user.py
+1
-1
__init__.py
dbgpt/app/apps/vadmin/auth/schemas/__init__.py
+1
-1
user.py
dbgpt/app/apps/vadmin/auth/schemas/user.py
+12
-0
current.py
dbgpt/app/apps/vadmin/auth/utils/current.py
+1
-1
login.py
dbgpt/app/apps/vadmin/auth/utils/login.py
+38
-26
views.py
dbgpt/app/apps/vadmin/auth/views.py
+25
-6
No files found.
dbgpt/app/apps/vadmin/auth/crud.py
View file @
2a74dc05
...
@@ -177,6 +177,24 @@ class UserDal(DalBase):
...
@@ -177,6 +177,24 @@ class UserDal(DalBase):
user
.
is_reset_password
=
True
user
.
is_reset_password
=
True
await
self
.
flush
(
user
)
await
self
.
flush
(
user
)
async
def
update_password_id
(
self
,
data
:
schemas
.
UpdatePwd
)
->
str
:
"""
修改密码
:param data:
:return:
"""
if
data
.
password
!=
data
.
password_two
:
raise
CustomException
(
msg
=
"两次密码不一致"
,
code
=
400
)
result
=
test_password
(
data
.
password
)
if
isinstance
(
result
,
str
):
raise
CustomException
(
msg
=
result
,
code
=
400
)
user
=
await
self
.
get_data
(
data
.
user_id
,
v_options
=
[
joinedload
(
self
.
model
.
roles
),
joinedload
(
self
.
model
.
depts
)])
user
.
password
=
self
.
model
.
get_password_hash
(
data
.
password
)
user
.
is_reset_password
=
True
await
self
.
flush
(
user
)
return
"修改密码成功"
async
def
update_current_info
(
self
,
user
:
models
.
VadminUser
,
data
:
schemas
.
UserUpdateBaseInfo
)
->
Any
:
async
def
update_current_info
(
self
,
user
:
models
.
VadminUser
,
data
:
schemas
.
UserUpdateBaseInfo
)
->
Any
:
"""
"""
更新当前用户基本信息
更新当前用户基本信息
...
...
dbgpt/app/apps/vadmin/auth/models/user.py
View file @
2a74dc05
...
@@ -40,9 +40,9 @@ class VadminUser(BaseModel):
...
@@ -40,9 +40,9 @@ class VadminUser(BaseModel):
is_staff
:
Mapped
[
bool
]
=
mapped_column
(
Boolean
,
default
=
False
,
comment
=
"是否为工作人员"
)
is_staff
:
Mapped
[
bool
]
=
mapped_column
(
Boolean
,
default
=
False
,
comment
=
"是否为工作人员"
)
wx_server_openid
:
Mapped
[
str
|
None
]
=
mapped_column
(
String
(
255
),
comment
=
"服务端微信平台openid"
)
wx_server_openid
:
Mapped
[
str
|
None
]
=
mapped_column
(
String
(
255
),
comment
=
"服务端微信平台openid"
)
is_wx_server_openid
:
Mapped
[
bool
]
=
mapped_column
(
Boolean
,
default
=
False
,
comment
=
"是否已有服务端微信平台openid"
)
is_wx_server_openid
:
Mapped
[
bool
]
=
mapped_column
(
Boolean
,
default
=
False
,
comment
=
"是否已有服务端微信平台openid"
)
roles
:
Mapped
[
set
[
VadminRole
]]
=
relationship
(
secondary
=
vadmin_auth_user_roles
)
roles
:
Mapped
[
set
[
VadminRole
]]
=
relationship
(
secondary
=
vadmin_auth_user_roles
)
depts
:
Mapped
[
set
[
VadminDept
]]
=
relationship
(
secondary
=
vadmin_auth_user_depts
)
depts
:
Mapped
[
set
[
VadminDept
]]
=
relationship
(
secondary
=
vadmin_auth_user_depts
)
# dept_id: VadminDept.id
@
staticmethod
@
staticmethod
def
get_password_hash
(
password
:
str
)
->
str
:
def
get_password_hash
(
password
:
str
)
->
str
:
"""
"""
...
...
dbgpt/app/apps/vadmin/auth/params/user.py
View file @
2a74dc05
...
@@ -34,6 +34,6 @@ class UserParams(QueryParams):
...
@@ -34,6 +34,6 @@ class UserParams(QueryParams):
self
.
email
=
(
"like"
,
email
)
self
.
email
=
(
"like"
,
email
)
self
.
is_active
=
is_active
self
.
is_active
=
is_active
self
.
is_staff
=
is_staff
self
.
is_staff
=
is_staff
self
.
dept_id
=
dept_id
# self.dept_id=
dept_id
dbgpt/app/apps/vadmin/auth/schemas/__init__.py
View file @
2a74dc05
from
.user
import
UserOut
,
UserUpdate
,
User
,
UserIn
,
UserSimpleOut
,
ResetPwd
,
UserUpdateBaseInfo
,
UserPasswordOut
from
.user
import
UserOut
,
UserUpdate
,
User
,
UserIn
,
UserSimpleOut
,
ResetPwd
,
UserUpdateBaseInfo
,
UserPasswordOut
,
UpdatePwd
from
.role
import
Role
,
RoleOut
,
RoleIn
,
RoleOptionsOut
,
RoleSimpleOut
from
.role
import
Role
,
RoleOut
,
RoleIn
,
RoleOptionsOut
,
RoleSimpleOut
from
.menu
import
Menu
,
MenuSimpleOut
,
RouterOut
,
Meta
,
MenuTreeListOut
from
.menu
import
Menu
,
MenuSimpleOut
,
RouterOut
,
Meta
,
MenuTreeListOut
from
.dept
import
Dept
,
DeptSimpleOut
,
DeptTreeListOut
from
.dept
import
Dept
,
DeptSimpleOut
,
DeptTreeListOut
dbgpt/app/apps/vadmin/auth/schemas/user.py
View file @
2a74dc05
...
@@ -96,3 +96,15 @@ class ResetPwd(BaseModel):
...
@@ -96,3 +96,15 @@ class ResetPwd(BaseModel):
if
'password'
in
info
.
data
and
v
!=
info
.
data
[
'password'
]:
if
'password'
in
info
.
data
and
v
!=
info
.
data
[
'password'
]:
raise
ValueError
(
'两次密码不一致!'
)
raise
ValueError
(
'两次密码不一致!'
)
return
v
return
v
class
UpdatePwd
(
BaseModel
):
password
:
str
password_two
:
str
user_id
:
int
@
field_validator
(
'password_two'
)
def
check_passwords_match
(
cls
,
v
,
info
:
FieldValidationInfo
):
if
'password'
in
info
.
data
and
v
!=
info
.
data
[
'password'
]:
raise
ValueError
(
'两次密码不一致!'
)
return
v
dbgpt/app/apps/vadmin/auth/utils/current.py
View file @
2a74dc05
...
@@ -85,7 +85,7 @@ class FullAdminAuth(AuthValidation):
...
@@ -85,7 +85,7 @@ class FullAdminAuth(AuthValidation):
async
def
__call__
(
async
def
__call__
(
self
,
self
,
request
:
Request
,
request
:
Request
,
token
:
str
=
Cookie
(
None
),
token
:
str
=
Depends
(
settings
.
oauth2_scheme
),
#
Cookie(None),
db
:
AsyncSession
=
Depends
(
db_getter
)
db
:
AsyncSession
=
Depends
(
db_getter
)
)
->
Auth
:
)
->
Auth
:
"""
"""
...
...
dbgpt/app/apps/vadmin/auth/utils/login.py
View file @
2a74dc05
...
@@ -42,28 +42,28 @@ import jwt
...
@@ -42,28 +42,28 @@ import jwt
router
=
APIRouter
()
router
=
APIRouter
()
@
router
.
post
(
"/api/login"
,
summary
=
"API 手机号密码登录"
,
description
=
"Swagger API 文档登录认证"
)
#
@router.post("/api/login", summary="API 手机号密码登录", description="Swagger API 文档登录认证")
async
def
api_login_for_access_token
(
#
async def api_login_for_access_token(
request
:
Request
,
#
request: Request,
data
:
OAuth2PasswordRequestForm
=
Depends
(),
#
data: OAuth2PasswordRequestForm = Depends(),
db
:
AsyncSession
=
Depends
(
db_getter
)
#
db: AsyncSession = Depends(db_getter)
):
#
):
user
=
await
UserDal
(
db
)
.
get_data
(
telephone
=
data
.
username
,
v_return_none
=
True
)
#
user = await UserDal(db).get_data(telephone=data.username, v_return_none=True)
error_code
=
status
.
HTTP_401_UNAUTHORIZED
#
error_code = status.HTTP_401_UNAUTHORIZED
if
not
user
:
#
if not user:
raise
CustomException
(
status_code
=
error_code
,
code
=
error_code
,
msg
=
"该手机号不存在"
)
#
raise CustomException(status_code=error_code, code=error_code, msg="该手机号不存在")
result
=
VadminUser
.
verify_password
(
data
.
password
,
user
.
password
)
#
result = VadminUser.verify_password(data.password, user.password)
if
not
result
:
#
if not result:
raise
CustomException
(
status_code
=
error_code
,
code
=
error_code
,
msg
=
"手机号或密码错误"
)
#
raise CustomException(status_code=error_code, code=error_code, msg="手机号或密码错误")
if
not
user
.
is_active
:
#
if not user.is_active:
raise
CustomException
(
status_code
=
error_code
,
code
=
error_code
,
msg
=
"此手机号已被冻结"
)
#
raise CustomException(status_code=error_code, code=error_code, msg="此手机号已被冻结")
elif
not
user
.
is_staff
:
#
elif not user.is_staff:
raise
CustomException
(
status_code
=
error_code
,
code
=
error_code
,
msg
=
"此手机号无权限"
)
#
raise CustomException(status_code=error_code, code=error_code, msg="此手机号无权限")
access_token
=
LoginManage
.
create_token
({
"sub"
:
user
.
telephone
,
"password"
:
user
.
password
})
#
access_token = LoginManage.create_token({"sub": user.telephone, "password": user.password})
record
=
LoginForm
(
platform
=
'2'
,
method
=
'0'
,
telephone
=
data
.
username
,
password
=
data
.
password
)
#
record = LoginForm(platform='2', method='0', telephone=data.username, password=data.password)
resp
=
{
"access_token"
:
access_token
,
"token_type"
:
"bearer"
}
#
resp = {"access_token": access_token, "token_type": "bearer"}
# await VadminLoginRecord.create_login_record(db, record, True, request, resp)
#
# await VadminLoginRecord.create_login_record(db, record, True, request, resp)
return
resp
#
return resp
@
router
.
post
(
"/login"
,
summary
=
"手机号密码登录"
,
description
=
"员工登录通道,限制最多输错次数,达到最大值后将is_active=False"
)
@
router
.
post
(
"/login"
,
summary
=
"手机号密码登录"
,
description
=
"员工登录通道,限制最多输错次数,达到最大值后将is_active=False"
)
async
def
login_for_access_token
(
async
def
login_for_access_token
(
...
@@ -99,8 +99,13 @@ async def login_for_access_token(
...
@@ -99,8 +99,13 @@ async def login_for_access_token(
"is_reset_password"
:
result
.
user
.
is_reset_password
,
"is_reset_password"
:
result
.
user
.
is_reset_password
,
"is_wx_server_openid"
:
result
.
user
.
is_wx_server_openid
"is_wx_server_openid"
:
result
.
user
.
is_wx_server_openid
}
}
response
=
JSONResponse
(
resp
)
result
=
{
response
.
set_cookie
(
key
=
"token"
,
value
=
access_token
,
domain
=
""
)
"code"
:
200
,
"message"
:
"登录成功"
,
"data"
:
resp
}
response
=
JSONResponse
(
result
)
response
.
set_cookie
(
key
=
"token"
,
value
=
access_token
)
# await VadminLoginRecord.create_login_record(db, data, True, request, resp)
# await VadminLoginRecord.create_login_record(db, data, True, request, resp)
return
response
return
response
except
ValueError
as
e
:
except
ValueError
as
e
:
...
@@ -139,6 +144,13 @@ async def token_refresh(refresh: str = Body(..., title="刷新Token")):
...
@@ -139,6 +144,13 @@ async def token_refresh(refresh: str = Body(..., title="刷新Token")):
"refresh_token"
:
refresh_token
,
"refresh_token"
:
refresh_token
,
"token_type"
:
"bearer"
"token_type"
:
"bearer"
}
}
response
=
JSONResponse
(
resp
)
result
=
{
response
.
set_cookie
(
key
=
"jwt"
,
value
=
access_token
,
httponly
=
True
)
"code"
:
200
,
"message"
:
"刷新成功"
,
"data"
:
resp
}
response
=
JSONResponse
(
result
)
response
.
set_cookie
(
key
=
"token"
,
value
=
access_token
)
return
response
return
response
dbgpt/app/apps/vadmin/auth/views.py
View file @
2a74dc05
...
@@ -8,7 +8,6 @@
...
@@ -8,7 +8,6 @@
from
redis.asyncio
import
Redis
from
redis.asyncio
import
Redis
from
fastapi
import
APIRouter
,
Depends
,
Body
,
UploadFile
,
Request
from
fastapi
import
APIRouter
,
Depends
,
Body
,
UploadFile
,
Request
from
sqlalchemy.orm
import
joinedload
from
dbgpt.app.apps.core.database
import
redis_getter
from
dbgpt.app.apps.core.database
import
redis_getter
from
dbgpt.app.apps.utils.response
import
SuccessResponse
,
ErrorResponse
from
dbgpt.app.apps.utils.response
import
SuccessResponse
,
ErrorResponse
from
.
import
schemas
,
crud
,
models
from
.
import
schemas
,
crud
,
models
...
@@ -16,6 +15,11 @@ from dbgpt.app.apps.core.dependencies import IdList
...
@@ -16,6 +15,11 @@ from dbgpt.app.apps.core.dependencies import IdList
from
dbgpt.app.apps.vadmin.auth.utils.current
import
AllUserAuth
,
FullAdminAuth
,
OpenAuth
from
dbgpt.app.apps.vadmin.auth.utils.current
import
AllUserAuth
,
FullAdminAuth
,
OpenAuth
from
dbgpt.app.apps.vadmin.auth.utils.validation.auth
import
Auth
from
dbgpt.app.apps.vadmin.auth.utils.validation.auth
import
Auth
from
.params
import
UserParams
,
RoleParams
,
DeptParams
from
.params
import
UserParams
,
RoleParams
,
DeptParams
from
sqlalchemy
import
and_
from
sqlalchemy.orm
import
joinedload
,
aliased
from
sqlalchemy.orm.strategy_options
import
_AbstractLoad
,
contains_eager
from
typing
import
Optional
from
dbgpt.app.apps.vadmin.auth.models
import
VadminUser
router
=
APIRouter
()
router
=
APIRouter
()
...
@@ -33,20 +37,29 @@ async def test(auth: Auth = Depends(OpenAuth())):
...
@@ -33,20 +37,29 @@ async def test(auth: Auth = Depends(OpenAuth())):
###########################################################
###########################################################
@
router
.
get
(
"/users"
,
summary
=
"获取用户列表"
)
@
router
.
get
(
"/users"
,
summary
=
"获取用户列表"
)
async
def
get_users
(
async
def
get_users
(
dept_id
:
Optional
[
int
]
=
None
,
params
:
UserParams
=
Depends
(),
params
:
UserParams
=
Depends
(),
auth
:
Auth
=
Depends
(
FullAdminAuth
(
permissions
=
[
"auth.user.list"
]))
auth
:
Auth
=
Depends
(
FullAdminAuth
(
permissions
=
[
"auth.user.list"
]))
):
):
model
=
models
.
VadminUser
model
=
models
.
VadminUser
options
=
[
joinedload
(
model
.
roles
),
joinedload
(
model
.
depts
)]
dept_alias
=
aliased
(
models
.
VadminDept
)
options
=
[
joinedload
(
model
.
roles
),
contains_eager
(
model
.
depts
,
alias
=
dept_alias
)]
outer_join
=
[
[
models
.
vadmin_auth_user_depts
,
models
.
vadmin_auth_user_depts
.
c
.
user_id
==
model
.
id
],
[
dept_alias
,
and_
(
dept_alias
.
id
==
models
.
vadmin_auth_user_depts
.
c
.
dept_id
,
dept_alias
.
id
==
dept_id
)]
]
if
not
dept_id
:
options
=
[
joinedload
(
model
.
roles
),
joinedload
(
model
.
depts
)]
outer_join
=
[]
schema
=
schemas
.
UserOut
schema
=
schemas
.
UserOut
datas
,
count
=
await
crud
.
UserDal
(
auth
.
db
)
.
get_datas
(
datas
,
count
=
await
crud
.
UserDal
(
auth
.
db
)
.
get_datas
(
**
params
.
dict
(),
**
params
.
dict
(),
v_options
=
options
,
v_options
=
options
,
v_schema
=
schema
,
v_schema
=
schema
,
v_outer_join
=
[
v_return_count
=
True
,
[
models
.
vadmin_auth_user_depts
,
params
.
dept_id
==
models
.
vadmin_auth_user_depts
.
c
.
dept_id
],
v_join
=
outer_join
],
v_return_count
=
True
)
)
return
SuccessResponse
(
datas
,
count
=
count
)
return
SuccessResponse
(
datas
,
count
=
count
)
...
@@ -91,6 +104,12 @@ async def user_current_reset_password(data: schemas.ResetPwd, auth: Auth = Depen
...
@@ -91,6 +104,12 @@ async def user_current_reset_password(data: schemas.ResetPwd, auth: Auth = Depen
return
SuccessResponse
(
await
crud
.
UserDal
(
auth
.
db
)
.
reset_current_password
(
auth
.
user
,
data
))
return
SuccessResponse
(
await
crud
.
UserDal
(
auth
.
db
)
.
reset_current_password
(
auth
.
user
,
data
))
@
router
.
post
(
"/user/current/update/password"
,
summary
=
"修改用户密码"
)
async
def
user_current_update_password
(
data
:
schemas
.
UpdatePwd
,
auth
:
Auth
=
Depends
(
AllUserAuth
())):
return
SuccessResponse
(
await
crud
.
UserDal
(
auth
.
db
)
.
update_password_id
(
data
))
@
router
.
post
(
"/user/current/update/info"
,
summary
=
"更新当前用户基本信息"
)
@
router
.
post
(
"/user/current/update/info"
,
summary
=
"更新当前用户基本信息"
)
async
def
post_user_current_update_info
(
data
:
schemas
.
UserUpdateBaseInfo
,
auth
:
Auth
=
Depends
(
AllUserAuth
())):
async
def
post_user_current_update_info
(
data
:
schemas
.
UserUpdateBaseInfo
,
auth
:
Auth
=
Depends
(
AllUserAuth
())):
return
SuccessResponse
(
await
crud
.
UserDal
(
auth
.
db
)
.
update_current_info
(
auth
.
user
,
data
))
return
SuccessResponse
(
await
crud
.
UserDal
(
auth
.
db
)
.
update_current_info
(
auth
.
user
,
data
))
...
...
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