Skip to content

Commit

Permalink
fix(权限信息): 处理hsweb-autz值为undefined是无法进入系统
Browse files Browse the repository at this point in the history
  • Loading branch information
Lind-pro committed Nov 23, 2020
1 parent a6a37cc commit 0a6eb75
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/pages/Authorized.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AuthComponent: React.FC<AuthComponentProps> = ({
return (
<Authorized
authority={getRouteAuthority(location.pathname, routes) || ''}
noMatch={isLogin ? <Redirect to="/exception/403" /> : <Redirect to="/user/login" />}
noMatch={isLogin ? <Redirect to="/exception/403" /> : <Redirect to="/" />}
>
{children}
</Authorized>
Expand Down
33 changes: 19 additions & 14 deletions src/utils/authority.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { router } from 'umi';
import { reloadAuthorized } from './Authorized';

// use localStorage to store the authority info, which might be sent from server in actual project.
Expand All @@ -23,21 +24,25 @@ export function getAuthority(): string | string[] {
// }
// return authority;
const storage = localStorage.getItem('hsweb-autz');
const autz = storage ? JSON.parse(storage) : null;
if (autz !== null) {
let authority =[];
if(autz.currentAuthority){
authority=autz.currentAuthority;
}else{
authority=autz.permissions.map((item:any)=>item.id);
if (storage) {
try {
const autz = storage && JSON.parse(storage)
let authority = [];
if (autz.currentAuthority) {
authority = autz.currentAuthority;
} else {
authority = autz.permissions.map((item: any) => item.id);
}
if (autz.user?.username === 'admin') {
return ['admin'];
}
return authority;
} catch (error) {
localStorage.removeItem('hsweb-autz');
location.reload();
return ['guest'];
}
if (autz.user?.username === 'admin') {
return ['admin'];
}
return authority;

}
return ['guest'];
}

export function setAuthority(authority: string | string[]): void {
Expand Down Expand Up @@ -191,7 +196,7 @@ export function clearAutz() {
export function setAutz(info: Authentication): Authentication {
window.top.hsweb_autz = new Authentication(info);
const autz = new Authentication(info);
if(JSON.stringify(info)!=='undefined'){
if (JSON.stringify(info) !== 'undefined') {
localStorage.setItem('hsweb-autz', JSON.stringify(info));
}
return autz;
Expand Down

0 comments on commit 0a6eb75

Please sign in to comment.