refactor(系统设置): 优化权限体验,刷新页面就可获得最新权限

--bug=1014129 --user=刘瑞斌 【系统设置】用户只有工作空间权限时,添加项目权限需要重新登录后才生效 https://www.tapd.cn/55049933/s/1185866
This commit is contained in:
CaptainB 2022-06-17 18:31:22 +08:00 committed by f2c-ci-robot[bot]
parent 102a981b7b
commit 9e638688b8
3 changed files with 29 additions and 11 deletions

View File

@ -44,6 +44,7 @@ public class LoginController {
if (StringUtils.isBlank(user.getLanguage())) { if (StringUtils.isBlank(user.getLanguage())) {
user.setLanguage(LocaleContextHolder.getLocale().toString()); user.setLanguage(LocaleContextHolder.getLocale().toString());
} }
userService.autoSwitch(user);
SessionUser sessionUser = SessionUser.fromUser(user); SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser); SessionUtils.putUser(sessionUser);
return ResultHolder.success(sessionUser); return ResultHolder.success(sessionUser);

View File

@ -612,7 +612,10 @@ public class UserService {
if (subject.isAuthenticated()) { if (subject.isAuthenticated()) {
UserDTO user = (UserDTO) subject.getSession().getAttribute(ATTR_USER); UserDTO user = (UserDTO) subject.getSession().getAttribute(ATTR_USER);
autoSwitch(user); autoSwitch(user);
return ResultHolder.success(subject.getSession().getAttribute("user")); // 放入session中
SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser);
return ResultHolder.success(sessionUser);
} else { } else {
return ResultHolder.error(Translator.get("login_fail")); return ResultHolder.error(Translator.get("login_fail"));
} }
@ -631,7 +634,7 @@ public class UserService {
} }
} }
private void autoSwitch(UserDTO user) { public void autoSwitch(UserDTO user) {
// 用户有 last_project_id 权限 // 用户有 last_project_id 权限
if (hasLastProjectPermission(user)) { if (hasLastProjectPermission(user)) {
return; return;
@ -668,7 +671,6 @@ public class UserService {
user.setLastWorkspaceId(""); user.setLastWorkspaceId("");
user.setLastProjectId(""); user.setLastProjectId("");
updateUser(user); updateUser(user);
SessionUtils.putUser(SessionUser.fromUser(user));
} }
} else { } else {
UserGroup userGroup = project.stream().filter(p -> StringUtils.isNotBlank(p.getSourceId())) UserGroup userGroup = project.stream().filter(p -> StringUtils.isNotBlank(p.getSourceId()))
@ -680,7 +682,6 @@ public class UserService {
user.setLastProjectId(projectId); user.setLastProjectId(projectId);
user.setLastWorkspaceId(wsId); user.setLastWorkspaceId(wsId);
updateUser(user); updateUser(user);
SessionUtils.putUser(SessionUser.fromUser(user));
} }
} }
@ -697,7 +698,6 @@ public class UserService {
// last_project_id last_workspace_id 对应不上了 // last_project_id last_workspace_id 对应不上了
user.setLastWorkspaceId(project.getWorkspaceId()); user.setLastWorkspaceId(project.getWorkspaceId());
updateUser(user); updateUser(user);
SessionUtils.putUser(SessionUser.fromUser(user));
return true; return true;
} }
} }
@ -744,7 +744,6 @@ public class UserService {
user.setLastProjectId(project.getId()); user.setLastProjectId(project.getId());
user.setLastWorkspaceId(wsId); user.setLastWorkspaceId(wsId);
updateUser(user); updateUser(user);
SessionUtils.putUser(SessionUser.fromUser(user));
return true; return true;
} }
} }

View File

@ -122,12 +122,30 @@ export function enableModules(...modules) {
export function saveLocalStorage(response) { export function saveLocalStorage(response) {
// 登录信息保存 cookie // 登录信息保存 cookie
localStorage.setItem(TokenKey, JSON.stringify(response.data)); let user = response.data;
if (!sessionStorage.getItem(PROJECT_ID)) { localStorage.setItem(TokenKey, JSON.stringify(user));
sessionStorage.setItem(PROJECT_ID, response.data.lastProjectId); // 校验权限
user.userGroups.forEach(ug => {
user.groupPermissions.forEach(gp => {
if (gp.group.id === ug.groupId) {
ug.userGroupPermissions = gp.userGroupPermissions;
ug.group = gp.group;
}
});
});
// 检查当前项目有没有权限
let currentProjectId = sessionStorage.getItem(PROJECT_ID);
if (!currentProjectId) {
sessionStorage.setItem(PROJECT_ID, user.lastProjectId);
} else {
let v = user.userGroups.filter(ug => ug.group && ug.group.type === 'PROJECT')
.filter(ug => ug.sourceId === currentProjectId);
if (v.length === 0) {
sessionStorage.setItem(PROJECT_ID, user.lastProjectId);
}
} }
if (!sessionStorage.getItem(WORKSPACE_ID)) { if (!sessionStorage.getItem(WORKSPACE_ID)) {
sessionStorage.setItem(WORKSPACE_ID, response.data.lastWorkspaceId); sessionStorage.setItem(WORKSPACE_ID, user.lastWorkspaceId);
} }
} }
@ -491,7 +509,7 @@ export function getTranslateOptions(data) {
let options = []; let options = [];
data.forEach(i => { data.forEach(i => {
let option = {}; let option = {};
Object.assign(option, i) Object.assign(option, i);
option.text = i18n.t(option.text); option.text = i18n.t(option.text);
options.push(option); options.push(option);
}); });