diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-09 21:45:21 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-09 21:45:21 +0200 |
commit | 7da18e4420c4b71a8ecfda07f39324fbfec081c3 (patch) | |
tree | 2755feedd730a78cdc407e7a79bcbfce3ebe71c8 /client/src/app/shared | |
parent | 68a3b9f2aacb0225ae8b883b561b144bac339cbd (diff) | |
download | PeerTube-7da18e4420c4b71a8ecfda07f39324fbfec081c3.tar.gz PeerTube-7da18e4420c4b71a8ecfda07f39324fbfec081c3.tar.zst PeerTube-7da18e4420c4b71a8ecfda07f39324fbfec081c3.zip |
Client: add user management
Diffstat (limited to 'client/src/app/shared')
-rw-r--r-- | client/src/app/shared/auth/auth-user.model.ts (renamed from client/src/app/shared/auth/user.model.ts) | 28 | ||||
-rw-r--r-- | client/src/app/shared/auth/auth.service.ts | 20 | ||||
-rw-r--r-- | client/src/app/shared/auth/index.ts | 2 | ||||
-rw-r--r-- | client/src/app/shared/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/shared/users/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/shared/users/user.model.ts | 15 |
6 files changed, 46 insertions, 21 deletions
diff --git a/client/src/app/shared/auth/user.model.ts b/client/src/app/shared/auth/auth-user.model.ts index e486873ab..bdd5ea5a9 100644 --- a/client/src/app/shared/auth/user.model.ts +++ b/client/src/app/shared/auth/auth-user.model.ts | |||
@@ -1,4 +1,6 @@ | |||
1 | export class User { | 1 | import { User } from '../users'; |
2 | |||
3 | export class AuthUser extends User { | ||
2 | private static KEYS = { | 4 | private static KEYS = { |
3 | ID: 'id', | 5 | ID: 'id', |
4 | ROLE: 'role', | 6 | ROLE: 'role', |
@@ -13,10 +15,12 @@ export class User { | |||
13 | static load() { | 15 | static load() { |
14 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); | 16 | const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME); |
15 | if (usernameLocalStorage) { | 17 | if (usernameLocalStorage) { |
16 | return new User( | 18 | return new AuthUser( |
17 | localStorage.getItem(this.KEYS.ID), | 19 | { |
18 | localStorage.getItem(this.KEYS.USERNAME), | 20 | id: localStorage.getItem(this.KEYS.ID), |
19 | localStorage.getItem(this.KEYS.ROLE), | 21 | username: localStorage.getItem(this.KEYS.USERNAME), |
22 | role: localStorage.getItem(this.KEYS.ROLE) | ||
23 | }, | ||
20 | Tokens.load() | 24 | Tokens.load() |
21 | ); | 25 | ); |
22 | } | 26 | } |
@@ -31,11 +35,9 @@ export class User { | |||
31 | Tokens.flush(); | 35 | Tokens.flush(); |
32 | } | 36 | } |
33 | 37 | ||
34 | constructor(id: string, username: string, role: string, hash_tokens: any) { | 38 | constructor(userHash: { id: string, username: string, role: string }, hashTokens: any) { |
35 | this.id = id; | 39 | super(userHash); |
36 | this.username = username; | 40 | this.tokens = new Tokens(hashTokens); |
37 | this.role = role; | ||
38 | this.tokens = new Tokens(hash_tokens); | ||
39 | } | 41 | } |
40 | 42 | ||
41 | getAccessToken() { | 43 | getAccessToken() { |
@@ -56,9 +58,9 @@ export class User { | |||
56 | } | 58 | } |
57 | 59 | ||
58 | save() { | 60 | save() { |
59 | localStorage.setItem(User.KEYS.ID, this.id); | 61 | localStorage.setItem(AuthUser.KEYS.ID, this.id); |
60 | localStorage.setItem(User.KEYS.USERNAME, this.username); | 62 | localStorage.setItem(AuthUser.KEYS.USERNAME, this.username); |
61 | localStorage.setItem(User.KEYS.ROLE, this.role); | 63 | localStorage.setItem(AuthUser.KEYS.ROLE, this.role); |
62 | this.tokens.save(); | 64 | this.tokens.save(); |
63 | } | 65 | } |
64 | } | 66 | } |
diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts index 24d1a4fa2..8eea0c4bf 100644 --- a/client/src/app/shared/auth/auth.service.ts +++ b/client/src/app/shared/auth/auth.service.ts | |||
@@ -4,7 +4,7 @@ import { Observable } from 'rxjs/Observable'; | |||
4 | import { Subject } from 'rxjs/Subject'; | 4 | import { Subject } from 'rxjs/Subject'; |
5 | 5 | ||
6 | import { AuthStatus } from './auth-status.model'; | 6 | import { AuthStatus } from './auth-status.model'; |
7 | import { User } from './user.model'; | 7 | import { AuthUser } from './auth-user.model'; |
8 | 8 | ||
9 | @Injectable() | 9 | @Injectable() |
10 | export class AuthService { | 10 | export class AuthService { |
@@ -17,7 +17,7 @@ export class AuthService { | |||
17 | private clientId: string; | 17 | private clientId: string; |
18 | private clientSecret: string; | 18 | private clientSecret: string; |
19 | private loginChanged: Subject<AuthStatus>; | 19 | private loginChanged: Subject<AuthStatus>; |
20 | private user: User = null; | 20 | private user: AuthUser = null; |
21 | 21 | ||
22 | constructor(private http: Http) { | 22 | constructor(private http: Http) { |
23 | this.loginChanged = new Subject<AuthStatus>(); | 23 | this.loginChanged = new Subject<AuthStatus>(); |
@@ -40,7 +40,7 @@ export class AuthService { | |||
40 | ); | 40 | ); |
41 | 41 | ||
42 | // Return null if there is nothing to load | 42 | // Return null if there is nothing to load |
43 | this.user = User.load(); | 43 | this.user = AuthUser.load(); |
44 | } | 44 | } |
45 | 45 | ||
46 | getRefreshToken() { | 46 | getRefreshToken() { |
@@ -65,10 +65,16 @@ export class AuthService { | |||
65 | return this.user.getTokenType(); | 65 | return this.user.getTokenType(); |
66 | } | 66 | } |
67 | 67 | ||
68 | getUser(): User { | 68 | getUser(): AuthUser { |
69 | return this.user; | 69 | return this.user; |
70 | } | 70 | } |
71 | 71 | ||
72 | isAdmin() { | ||
73 | if (this.user === null) return false; | ||
74 | |||
75 | return this.user.isAdmin(); | ||
76 | } | ||
77 | |||
72 | isLoggedIn() { | 78 | isLoggedIn() { |
73 | if (this.getAccessToken()) { | 79 | if (this.getAccessToken()) { |
74 | return true; | 80 | return true; |
@@ -108,7 +114,7 @@ export class AuthService { | |||
108 | logout() { | 114 | logout() { |
109 | // TODO: make an HTTP request to revoke the tokens | 115 | // TODO: make an HTTP request to revoke the tokens |
110 | this.user = null; | 116 | this.user = null; |
111 | User.flush(); | 117 | AuthUser.flush(); |
112 | 118 | ||
113 | this.setStatus(AuthStatus.LoggedOut); | 119 | this.setStatus(AuthStatus.LoggedOut); |
114 | } | 120 | } |
@@ -163,13 +169,13 @@ export class AuthService { | |||
163 | const id = obj.id; | 169 | const id = obj.id; |
164 | const username = obj.username; | 170 | const username = obj.username; |
165 | const role = obj.role; | 171 | const role = obj.role; |
166 | const hash_tokens = { | 172 | const hashTokens = { |
167 | access_token: obj.access_token, | 173 | access_token: obj.access_token, |
168 | token_type: obj.token_type, | 174 | token_type: obj.token_type, |
169 | refresh_token: obj.refresh_token | 175 | refresh_token: obj.refresh_token |
170 | }; | 176 | }; |
171 | 177 | ||
172 | this.user = new User(id, username, role, hash_tokens); | 178 | this.user = new AuthUser({ id, username, role }, hashTokens); |
173 | this.user.save(); | 179 | this.user.save(); |
174 | 180 | ||
175 | this.setStatus(AuthStatus.LoggedIn); | 181 | this.setStatus(AuthStatus.LoggedIn); |
diff --git a/client/src/app/shared/auth/index.ts b/client/src/app/shared/auth/index.ts index aafaacbf1..ebd9e14cd 100644 --- a/client/src/app/shared/auth/index.ts +++ b/client/src/app/shared/auth/index.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | export * from './auth-http.service'; | 1 | export * from './auth-http.service'; |
2 | export * from './auth-status.model'; | 2 | export * from './auth-status.model'; |
3 | export * from './auth.service'; | 3 | export * from './auth.service'; |
4 | export * from './user.model'; | 4 | export * from './auth-user.model'; |
diff --git a/client/src/app/shared/index.ts b/client/src/app/shared/index.ts index dfea4c67c..c05e8d253 100644 --- a/client/src/app/shared/index.ts +++ b/client/src/app/shared/index.ts | |||
@@ -1,2 +1,3 @@ | |||
1 | export * from './auth'; | 1 | export * from './auth'; |
2 | export * from './search'; | 2 | export * from './search'; |
3 | export * from './users'; | ||
diff --git a/client/src/app/shared/users/index.ts b/client/src/app/shared/users/index.ts new file mode 100644 index 000000000..5a670ce8f --- /dev/null +++ b/client/src/app/shared/users/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './user.model'; | |||
diff --git a/client/src/app/shared/users/user.model.ts b/client/src/app/shared/users/user.model.ts new file mode 100644 index 000000000..0f34d4480 --- /dev/null +++ b/client/src/app/shared/users/user.model.ts | |||
@@ -0,0 +1,15 @@ | |||
1 | export class User { | ||
2 | id: string; | ||
3 | username: string; | ||
4 | role: string; | ||
5 | |||
6 | constructor(hash: { id: string, username: string, role: string }) { | ||
7 | this.id = hash.id; | ||
8 | this.username = hash.username; | ||
9 | this.role = hash.role; | ||
10 | } | ||
11 | |||
12 | isAdmin() { | ||
13 | return this.role === 'admin'; | ||
14 | } | ||
15 | } | ||