-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_user_service.py
203 lines (167 loc) · 7.95 KB
/
sys_user_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import bcrypt
import pandas as pd
from flask import request, g, send_file
from sqlalchemy import func
from db import db
from model import SysUserModel, SysRoleModel, SysUserRoleModel
from service import *
from enums import SysDataScopeEnum, SysLoginStatusEnum, SysLoginOperationEnum, SysUserStatusEnum
class SysUserService(BaseService):
def get_by_id(self, user_id):
res = db.session.query(SysUserModel).filter(SysUserModel.id == user_id, SysUserModel.deleted == 0).one()
return res
def page(self):
query = db.session.query(SysUserModel)
username = request.args.get('username')
if username:
query = query.filter(SysUserModel.username == username)
mobile = request.args.get('mobile')
if mobile:
query = query.filter(SysUserModel.mobile == mobile)
gender = request.args.get('gender')
if gender:
query = query.filter(SysUserModel.gender == gender)
query = query.filter(SysUserModel.deleted == 0)
return self.query_page(self.get_query_by_data_scope(query))
def login(self, username, password):
res = None
try:
res: SysUserModel = db.session.query(SysUserModel).filter(
SysUserModel.username == username).one()
except:
SysLogLoginService().save(username=username, status=SysLoginStatusEnum.FAIL.value,
operation=SysLoginOperationEnum.ACCOUNT_FAIL.value)
# 用户不存在
raise Exception("用户不存在")
password = str(password).encode('utf-8')
hashed_password = str(res.password).encode('utf-8')
if bcrypt.checkpw(password, hashed_password):
if res.status == SysUserStatusEnum.DISABLE.value:
raise Exception("账号已被停用")
return res
else:
SysLogLoginService().save(username=username, status=SysLoginStatusEnum.FAIL.value,
operation=SysLoginOperationEnum.ACCOUNT_FAIL.value)
raise Exception("用户名或密码错误")
def update_password(self, vo):
# 旧密码
old_password = vo['password'].encode('utf-8')
# 新密码
new_password = vo['new_password'].encode('utf-8')
db_model = db.session.query(SysUserModel).filter(SysUserModel.id == g.user['id']).one()
# 数据库存的密码
hashed_password = db_model.password.encode('utf-8')
# 比对以前的密码
if bcrypt.checkpw(old_password, hashed_password):
salt = bcrypt.gensalt()
new_password = bcrypt.hashpw(password=new_password, salt=salt).decode("utf-8")
db_model.password = new_password
db.session.commit()
return "修改成功"
else:
raise Exception("原密码错误")
# 删除在线用户,在线用户需要重新登录
def get_data_scope(self, user):
data_scope = db.session.query(
func.min(SysRoleModel.data_scope)). \
join(SysUserRoleModel, SysRoleModel.id == SysUserRoleModel.role_id). \
filter(SysUserRoleModel.user_id == user.id, SysRoleModel.deleted == 0, SysUserRoleModel.deleted == 0). \
scalar()
if data_scope == None:
return []
else:
# 全部数据权限,则返回null
if data_scope == SysDataScopeEnum.ALL.value:
return None
# 本机构及子机构数据
# 自定义数据权限范围
elif data_scope == SysDataScopeEnum.ORG_AND_CHILD.value:
res = []
res += SysOrgService().get_sub_org_id_list(user.org_id)
res += SysRoleDataScopeService().get_data_scope_list(user.id)
return res
# 本机构数据
# 自定义数据权限范围
elif data_scope == SysDataScopeEnum.ORG_ONLY.value:
res = []
res += [user.org_id]
res += SysRoleDataScopeService().get_data_scope_list(user.id)
return res
# 自定义数据权限范围
elif data_scope == SysDataScopeEnum.CUSTOM.value:
res = SysRoleDataScopeService().get_data_scope_list(user.id)
return res
else:
return []
def get_by_username(self, username):
return db.session.query(SysUserModel).filter(SysUserModel.username == username,
SysUserModel.deleted == 0).one_or_none()
def get_by_mobile(self, mobile):
return db.session.query(SysUserModel).filter(SysUserModel.mobile == mobile,
SysUserModel.deleted == 0).one_or_none()
def save(self, vo):
user = SysUserModel(**vo)
res = self.get_by_username(user.username)
if res != None:
raise Exception("用户已存在")
res = self.get_by_mobile(user.mobile)
if res != None:
raise Exception("手机号已存在")
user.password = bcrypt.hashpw(password=user.password.encode('utf-8'), salt=bcrypt.gensalt()).decode("utf-8")
# 保存用户
db.session.add(user)
db.session.commit()
# 保存用户角色关系
SysUserRoleService().save_or_update(user.id, user.role_id_list)
SysUserPostService().save_or_update(user.id, user.post_id_list)
return True
def update(self, vo):
user = db.session.query(SysUserModel).filter(SysUserModel.id == vo['id'], SysUserModel.deleted == 0).one()
res = self.get_by_username(vo['username'])
if res != None and res.id != user.id:
raise Exception("用户已存在")
res = self.get_by_mobile(vo['mobile'])
if res != None and res.id != user.id:
raise Exception("手机号已存在")
# 保存用户
for key, value in vo.items():
setattr(user, key, value)
if user.password != None:
user.password = bcrypt.hashpw(user.password.encode('utf-8'), salt=bcrypt.gensalt()).decode("utf-8")
db.session.add(user)
db.session.commit()
# 保存用户角色关系
SysUserRoleService().save_or_update(user.id, vo['role_id_list'])
SysUserPostService().save_or_update(user.id, vo['post_id_list'])
# 修改用户的缓存信息
SysUserTokenService().update_cache_auth_by_user_id(user.id)
return True
def delete(self, curr_user_id, id_list):
if curr_user_id in id_list:
raise Exception("不能删除当前用户")
users = db.session.query(SysUserModel).filter(SysUserModel.id.in_(id_list), SysUserModel.deleted == 0).all()
for user in users:
user.deleted = 1
db.session.commit()
# 批量删除角色关系
SysUserPostService().delete_by_user_id_list(id_list)
# 批量删除岗位关系
SysUserRoleService().delete_by_user_id_list(id_list)
return True
def role_user_page(self):
# 非必传参数
username = request.args.get('username')
mobile = request.args.get('mobile')
gender = request.args.get('gender')
role_id = request.args.get('role_id')
query = db.session.query(SysUserModel).join(SysUserRoleModel, SysUserModel.id == SysUserRoleModel.user_id,
isouter=True).filter(SysUserModel.deleted == 0,
SysUserRoleModel.deleted == 0,
SysUserRoleModel.role_id == role_id)
if username:
query = query.filter(SysUserModel.username.like(f"%{username}%"))
if mobile:
query = query.filter(SysUserModel.mobile.like(f"%{mobile}%"))
if gender:
query = query.filter(SysUserModel.gender == gender)
return self.query_page(query)