AuthenticationManager.ts
4.21 KB
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
import {DefaultTokenInterceptor} from '../http/DefaultTokenInterceptor';
import {Privilege} from './Privilege';
import Authenticity from "./Authenticity";
export abstract class AuthenticationManager {
/**
* TOKEN存储KEY
*/
public static readonly TOKEN = 'token';
/**
*
* @param token 登录认证token
* @param authenticity 登录认证用户
*/
constructor(protected token: string | null, protected authenticity: Authenticity | null) {
this.loadStorageToken();
this.loadStorageAuthentication();
}
/**
* 更新TOKEN
*/
updateToken(token: string) {
this.token = token;
localStorage.setItem(AuthenticationManager.TOKEN, token);
}
/**
* 获取TOKEN
*/
getToken() {
return this.token;
}
/**
* 删除TOKEN
*/
removeToken() {
this.token = null;
localStorage.removeItem(AuthenticationManager.TOKEN);
}
/**
* 删除登录信息
*/
removeAuthentication() {
this.authenticity = null;
this.deleteAuthentication();
}
/**
* 退出
*/
logout() {
this.removeToken();
this.removeAuthentication();
}
getAuthentication(): Authenticity | null {
return this.authenticity;
}
setAuthentication(authentication: Authenticity) {
this.authenticity = authentication;
}
/**
* 更新登录认证
* @ param authentication
*/
updateAuthentication(authentication: Authenticity) {
this.updateToken(authentication.token);
this.authenticity = authentication;
this.saveAuthentication(authentication);
}
/**
* 判断是否登录
*/
isLogin(): boolean {
return this.getAuthentication() != null;
}
/**
* 判断是否有权限
* @ param privilegeCode
*/
hasPrivilege(privilegeCode: string): boolean {
if (this.isLogin()) {
return this.recursivePrivilegeCode(this.authenticity.privileges, privilegeCode);
} else {
return false;
}
}
recursivePrivilegeCode(privileges: Privilege[], privilegeCode: string): boolean {
for (const privilege of privileges) {
if (privilege.code === privilegeCode) {
return true;
} else if (privilege.children != null && privilege.children.length > 0) {
const hp = this.recursivePrivilegeCode(privilege.children, privilegeCode);
if (hp) {
return true;
}
}
}
return false;
}
recursivePrivilegeValue(privileges: Privilege[], path: string): boolean {
for (const privilege of privileges) {
if (privilege.value === path) {
return true;
} else if (privilege.children != null && privilege.children.length > 0) {
const hp = this.recursivePrivilegeValue(privilege.children, path);
if (hp) {
return true;
}
}
}
return false;
}
/**
* 获取TOKEN
*/
getStorageToken(): string {
return localStorage.getItem(AuthenticationManager.TOKEN);
}
deleteAuthentication(): void {
localStorage.removeItem(DefaultTokenInterceptor.AUTHORIZATION);
}
getStorageAuthentication(): string {
return localStorage.getItem(DefaultTokenInterceptor.AUTHORIZATION);
}
saveAuthentication(authentication: Authenticity): void {
localStorage.setItem(DefaultTokenInterceptor.AUTHORIZATION, JSON.stringify(authentication));
}
loadStorageAuthentication(): Promise<any> {
this.authenticity = JSON.parse(localStorage.getItem(DefaultTokenInterceptor.AUTHORIZATION));
return Promise.resolve(this.authenticity);
}
loadStorageToken(): Promise<any> {
this.token = localStorage.getItem(AuthenticationManager.TOKEN);
return Promise.resolve(this.token);
}
hasRole(roleCode: any): boolean {
if (this.authenticity && this.authenticity.authorities) {
return this.authenticity.authorities.includes(roleCode);
} else {
return false;
}
}
}