Skip to content

Commit cb545be

Browse files
committed
feat: 初始密码支持自定义修改策略
1 parent fb56adb commit cb545be

File tree

8 files changed

+53
-13
lines changed

8 files changed

+53
-13
lines changed

ruoyi-fastapi-backend/module_admin/controller/user_controller.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ async def reset_system_user_pwd(
165165
edit_user = EditUserModel(
166166
userId=reset_user.user_id,
167167
password=PwdUtil.get_password_hash(reset_user.password),
168+
pwdUpdateDate=datetime.now(),
168169
updateBy=current_user.user.user_name,
169170
updateTime=datetime.now(),
170171
type='pwd',
@@ -304,6 +305,7 @@ async def reset_system_user_password(
304305
userId=current_user.user.user_id,
305306
oldPassword=reset_password.old_password,
306307
password=reset_password.new_password,
308+
pwdUpdateDate=datetime.now(),
307309
updateBy=current_user.user.user_name,
308310
updateTime=datetime.now(),
309311
)

ruoyi-fastapi-backend/module_admin/entity/do/user_do.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class SysUser(Base):
3232
del_flag = Column(CHAR(1), nullable=True, server_default='0', comment='删除标志(0代表存在 2代表删除)')
3333
login_ip = Column(String(128), nullable=True, server_default="''", comment='最后登录IP')
3434
login_date = Column(DateTime, nullable=True, comment='最后登录时间')
35+
pwd_update_date = Column(DateTime, nullable=True, comment='密码最后更新时间')
3536
create_by = Column(String(64), nullable=True, server_default="''", comment='创建者')
3637
create_time = Column(DateTime, nullable=True, comment='创建时间', default=datetime.now())
3738
update_by = Column(String(64), nullable=True, server_default="''", comment='更新者')

ruoyi-fastapi-backend/module_admin/entity/vo/user_vo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class UserModel(BaseModel):
4040
del_flag: Optional[Literal['0', '2']] = Field(default=None, description='删除标志(0代表存在 2代表删除)')
4141
login_ip: Optional[str] = Field(default=None, description='最后登录IP')
4242
login_date: Optional[datetime] = Field(default=None, description='最后登录时间')
43+
pwd_update_date: Optional[datetime] = Field(default=None, description='密码最后更新时间')
4344
create_by: Optional[str] = Field(default=None, description='创建者')
4445
create_time: Optional[datetime] = Field(default=None, description='创建时间')
4546
update_by: Optional[str] = Field(default=None, description='更新者')
@@ -125,6 +126,7 @@ class CurrentUserModel(BaseModel):
125126
permissions: List = Field(description='权限信息')
126127
roles: List = Field(description='角色信息')
127128
user: Union[UserInfoModel, None] = Field(description='用户信息')
129+
is_default_modify_pwd: bool = Field(default=False, description='是否初始密码修改提醒')
128130

129131

130132
class UserDetailModel(BaseModel):

ruoyi-fastapi-backend/module_admin/service/login_service.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ async def get_current_user(
218218
else:
219219
# 此方法可实现同一账号同一时间只能登录一次
220220
redis_token = await request.app.state.redis.get(
221-
f"{RedisInitKeyConfig.ACCESS_TOKEN.key}:{query_user.get('user_basic_info').user_id}"
221+
f'{RedisInitKeyConfig.ACCESS_TOKEN.key}:{query_user.get("user_basic_info").user_id}'
222222
)
223223
if token == redis_token:
224224
if AppConfig.app_same_time_login:
@@ -229,7 +229,7 @@ async def get_current_user(
229229
)
230230
else:
231231
await request.app.state.redis.set(
232-
f"{RedisInitKeyConfig.ACCESS_TOKEN.key}:{query_user.get('user_basic_info').user_id}",
232+
f'{RedisInitKeyConfig.ACCESS_TOKEN.key}:{query_user.get("user_basic_info").user_id}',
233233
redis_token,
234234
ex=timedelta(minutes=JwtConfig.jwt_redis_expire_minutes),
235235
)
@@ -242,6 +242,9 @@ async def get_current_user(
242242
post_ids = ','.join([str(row.post_id) for row in query_user.get('user_post_info')])
243243
role_ids = ','.join([str(row.role_id) for row in query_user.get('user_role_info')])
244244
roles = [row.role_key for row in query_user.get('user_role_info')]
245+
is_default_modify_pwd = await cls.__init_password_is_modify(
246+
request, query_user.get('user_basic_info').pwd_update_date
247+
)
245248

246249
current_user = CurrentUserModel(
247250
permissions=permissions,
@@ -253,12 +256,27 @@ async def get_current_user(
253256
dept=CamelCaseUtil.transform_result(query_user.get('user_dept_info')),
254257
role=CamelCaseUtil.transform_result(query_user.get('user_role_info')),
255258
),
259+
isDefaultModifyPwd=is_default_modify_pwd,
256260
)
257261
return current_user
258262
else:
259263
logger.warning('用户token已失效,请重新登录')
260264
raise AuthException(data='', message='用户token已失效,请重新登录')
261265

266+
@classmethod
267+
async def __init_password_is_modify(cls, request: Request, pwd_update_date: datetime):
268+
"""
269+
判断当前用户是否初始密码登录
270+
271+
:param request: Request对象
272+
:param pwd_update_date: 密码最后更新时间
273+
:return: 是否初始密码登录
274+
"""
275+
init_password_is_modify = await request.app.state.redis.get(
276+
f'{RedisInitKeyConfig.SYS_CONFIG.key}:sys.account.initPasswordModify'
277+
)
278+
return init_password_is_modify == '1' and pwd_update_date is None
279+
262280
@classmethod
263281
async def get_current_user_routers(cls, user_id: int, query_db: AsyncSession):
264282
"""

ruoyi-fastapi-backend/sql/ruoyi-fastapi-pg.sql

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ create table sys_user (
6969
del_flag char(1) default '0',
7070
login_ip varchar(128) default '',
7171
login_date timestamp(0),
72+
pwd_update_date timestamp(0),
7273
create_by varchar(64) default '',
7374
create_time timestamp(0),
7475
update_by varchar(64) default '',
@@ -91,13 +92,20 @@ comment on column sys_user.status is '帐号状态(0正常 1停用)';
9192
comment on column sys_user.del_flag is '删除标志(0代表存在 2代表删除)';
9293
comment on column sys_user.login_ip is '最后登录IP';
9394
comment on column sys_user.login_date is '最后登录时间';
95+
comment on column sys_user.pwd_update_date is '密码最后更新时间';
9496
comment on column sys_user.create_by is '创建者';
9597
comment on column sys_user.create_time is '创建时间';
9698
comment on column sys_user.update_by is '更新者';
9799
comment on column sys_user.update_time is '更新时间';
98100
comment on column sys_user.remark is '备注';
99101
comment on table sys_user is '用户信息表';
100102

103+
-- ----------------------------
104+
-- 初始化-用户信息表数据
105+
-- ----------------------------
106+
insert into sys_user values(1, 103, 'admin', '超级管理员', '00', 'niangao@163.com', '15888888888', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', current_timestamp, current_timestamp, 'admin', current_timestamp, '', null, '管理员');
107+
insert into sys_user values(2, 105, 'niangao', '年糕', '00', 'niangao@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', current_timestamp, current_timestamp, 'admin', current_timestamp, '', null, '测试员');
108+
101109
-- ----------------------------
102110
-- 3、岗位信息表
103111
-- ----------------------------
@@ -136,12 +144,6 @@ insert into sys_post values(2, 'se', '项目经理', 2, '0', 'admin', current
136144
insert into sys_post values(3, 'hr', '人力资源', 3, '0', 'admin', current_timestamp, '', null, '');
137145
insert into sys_post values(4, 'user', '普通员工', 4, '0', 'admin', current_timestamp, '', null, '');
138146

139-
-- ----------------------------
140-
-- 初始化-用户信息表数据
141-
-- ----------------------------
142-
insert into sys_user values(1, 103, 'admin', '超级管理员', '00', 'niangao@163.com', '15888888888', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', current_timestamp, 'admin', current_timestamp, '', null, '管理员');
143-
insert into sys_user values(2, 105, 'niangao', '年糕', '00', 'niangao@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', current_timestamp, 'admin', current_timestamp, '', null, '测试员');
144-
145147
-- ----------------------------
146148
-- 4、角色信息表
147149
-- ----------------------------
@@ -704,6 +706,7 @@ insert into sys_config values(3, '主框架页-侧边栏主题', 'sys.
704706
insert into sys_config values(4, '账号自助-验证码开关', 'sys.account.captchaEnabled', 'true', 'Y', 'admin', current_timestamp, '', null, '是否开启验证码功能(true开启,false关闭)');
705707
insert into sys_config values(5, '账号自助-是否开启用户注册功能', 'sys.account.registerUser', 'false', 'Y', 'admin', current_timestamp, '', null, '是否开启注册用户功能(true开启,false关闭)');
706708
insert into sys_config values(6, '用户登录-黑名单列表', 'sys.login.blackIPList', '', 'Y', 'admin', current_timestamp, '', null, '设置登录IP黑名单限制,多个匹配项以;分隔,支持匹配(*通配、网段)');
709+
insert into sys_config values(7, '用户管理-初始密码修改策略', 'sys.account.initPasswordModify', '1', 'Y', 'admin', current_timestamp, '', null, '0:初始密码修改策略关闭,没有任何提示,1:提醒用户,如果未修改初始密码,则在登录时就会提醒修改密码对话框');
707710

708711
-- ----------------------------
709712
-- 14、系统访问记录

ruoyi-fastapi-backend/sql/ruoyi-fastapi.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ create table sys_user (
5454
del_flag char(1) default '0' comment '删除标志(0代表存在 2代表删除)',
5555
login_ip varchar(128) default '' comment '最后登录IP',
5656
login_date datetime comment '最后登录时间',
57+
pwd_update_date datetime comment '密码最后更新时间',
5758
create_by varchar(64) default '' comment '创建者',
5859
create_time datetime comment '创建时间',
5960
update_by varchar(64) default '' comment '更新者',
@@ -65,8 +66,8 @@ create table sys_user (
6566
-- ----------------------------
6667
-- 初始化-用户信息表数据
6768
-- ----------------------------
68-
insert into sys_user values(1, 103, 'admin', '超级管理员', '00', 'niangao@163.com', '15888888888', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', sysdate(), 'admin', sysdate(), '', null, '管理员');
69-
insert into sys_user values(2, 105, 'niangao', '年糕', '00', 'niangao@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', sysdate(), 'admin', sysdate(), '', null, '测试员');
69+
insert into sys_user values(1, 103, 'admin', '超级管理员', '00', 'niangao@163.com', '15888888888', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', sysdate(), sysdate(), 'admin', sysdate(), '', null, '管理员');
70+
insert into sys_user values(2, 105, 'niangao', '年糕', '00', 'niangao@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '127.0.0.1', sysdate(), sysdate(), 'admin', sysdate(), '', null, '测试员');
7071

7172

7273
-- ----------------------------
@@ -554,6 +555,7 @@ insert into sys_config values(3, '主框架页-侧边栏主题', 'sys.
554555
insert into sys_config values(4, '账号自助-验证码开关', 'sys.account.captchaEnabled', 'true', 'Y', 'admin', sysdate(), '', null, '是否开启验证码功能(true开启,false关闭)');
555556
insert into sys_config values(5, '账号自助-是否开启用户注册功能', 'sys.account.registerUser', 'false', 'Y', 'admin', sysdate(), '', null, '是否开启注册用户功能(true开启,false关闭)');
556557
insert into sys_config values(6, '用户登录-黑名单列表', 'sys.login.blackIPList', '', 'Y', 'admin', sysdate(), '', null, '设置登录IP黑名单限制,多个匹配项以;分隔,支持匹配(*通配、网段)');
558+
insert into sys_config values(7, '用户管理-初始密码修改策略', 'sys.account.initPasswordModify', '1', 'Y', 'admin', sysdate(), '', null, '0:初始密码修改策略关闭,没有任何提示,1:提醒用户,如果未修改初始密码,则在登录时就会提醒修改密码对话框');
557559

558560

559561
-- ----------------------------

ruoyi-fastapi-frontend/src/store/modules/user.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import router from '@/router'
2+
import { MessageBox, } from 'element-ui'
13
import { login, logout, getInfo } from '@/api/login'
24
import { getToken, setToken, removeToken } from '@/utils/auth'
35
import { isHttp, isEmpty } from "@/utils/validate"
@@ -24,7 +26,7 @@ const user = {
2426
SET_NAME: (state, name) => {
2527
state.name = name
2628
},
27-
SET_NICK_NAME: (state, nickName) =>{
29+
SET_NICK_NAME: (state, nickName) => {
2830
state.nickName = nickName
2931
},
3032
SET_AVATAR: (state, avatar) => {
@@ -75,6 +77,12 @@ const user = {
7577
commit('SET_NAME', user.userName)
7678
commit('SET_NICK_NAME', user.nickName)
7779
commit('SET_AVATAR', avatar)
80+
/* 初始密码提示 */
81+
if(res.isDefaultModifyPwd) {
82+
MessageBox.confirm('您的密码还是初始密码,请修改密码!', '安全提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
83+
router.push({ name: 'Profile', params: { activeTab: 'resetPwd' } })
84+
}).catch(() => {})
85+
}
7886
resolve(res)
7987
}).catch(error => {
8088
reject(error)

ruoyi-fastapi-frontend/src/views/system/user/profile/index.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<div slot="header" class="clearfix">
4545
<span>基本资料</span>
4646
</div>
47-
<el-tabs v-model="activeTab">
47+
<el-tabs v-model="selectedTab">
4848
<el-tab-pane label="基本资料" name="userinfo">
4949
<userInfo :user="user" />
5050
</el-tab-pane>
@@ -72,10 +72,14 @@ export default {
7272
user: {},
7373
roleGroup: {},
7474
postGroup: {},
75-
activeTab: "userinfo"
75+
selectedTab: "userinfo"
7676
};
7777
},
7878
created() {
79+
const activeTab = this.$route.params && this.$route.params.activeTab
80+
if (activeTab) {
81+
this.selectedTab = activeTab
82+
};
7983
this.getUser();
8084
},
8185
methods: {

0 commit comments

Comments
 (0)