-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from AurelicButter/feature/966
[Feature/966] Account Permissions Redesign
- Loading branch information
Showing
21 changed files
with
184 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import { Component } from "@angular/core"; | ||
import { LocalCookiesService } from "src/app/services/authentication/local-cookies.service"; | ||
import { AuthUser } from "src/app/models/AuthUser"; | ||
import { AuthUserService } from "src/app/services/authentication/auth-user.service"; | ||
|
||
@Component({ | ||
selector: "home-panel", | ||
templateUrl: "./home-panel.component.html", | ||
styleUrls: ["./home-panel.component.css"] | ||
}) | ||
export class HomePanelComponent { | ||
user: any; | ||
user: AuthUser; | ||
|
||
constructor(private cookieService: LocalCookiesService) { | ||
this.cookieService.userEvent.subscribe((value) => { | ||
this.user = value; | ||
constructor(private AuthUser: AuthUserService) { | ||
this.AuthUser.userEvent.subscribe((value) => { | ||
if (value !== null) { | ||
this.user = value; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum UserPermissions { | ||
APPROVE_CORRECTIONS = "approve_corrections", | ||
VIEW_CORRECTIONS = "view_corrections", | ||
UPDATE_USERS = "update_users", | ||
ADD_ENTRY = "add_entry", | ||
DELETE_ENTRY = "delete_entry", | ||
MODIFY_ENTRY = "modify_entry", | ||
STAFF = "staff" | ||
} |
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...es/authentication/authentication.guard.ts → src/app/guards/authentication.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface AuthUser { | ||
user_id: number; | ||
username: string; | ||
create_date: string; | ||
last_login: string; | ||
role_name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...hentication/local-cookies.service.spec.ts → .../authentication/auth-user.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { BehaviorSubject } from "rxjs"; | ||
import { AuthUser } from "../../models/AuthUser"; | ||
import { UserPermissions } from "../../constants/UserPermissions"; | ||
|
||
@Injectable({ | ||
providedIn: "root" | ||
}) | ||
export class AuthUserService { | ||
userEvent = new BehaviorSubject<AuthUser | null>(null); | ||
user: AuthUser | null = null; | ||
private userPermissions: string[]; | ||
|
||
constructor() { | ||
this.userEvent.subscribe((value) => { | ||
this.user = value; | ||
}); | ||
|
||
const cachedAuth = localStorage.getItem("user"); | ||
|
||
if (cachedAuth) { | ||
this.user = JSON.parse(cachedAuth); | ||
} | ||
this.userEvent.next(this.user); | ||
} | ||
|
||
isLoggedIn() { | ||
return this.user !== null; | ||
} | ||
|
||
isModerator() { | ||
return this.user !== null && this.userPermissions.includes(UserPermissions.STAFF); | ||
} | ||
|
||
isSameUser(username: string) { | ||
return this.user !== null && this.user.username === username; | ||
} | ||
|
||
setPermissions(permissions: UserPermissions[]) { | ||
this.userPermissions = permissions; | ||
} | ||
|
||
hasPermission(...permissions: UserPermissions[]) { | ||
for (let i = 0; i < permissions.length; i++) { | ||
if (!this.userPermissions.includes(permissions[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
updateUser(userObj: any) { | ||
this.userPermissions = userObj["permissions"]; | ||
delete userObj["permissions"]; | ||
this.user = userObj; | ||
localStorage.setItem("user", JSON.stringify(userObj)); | ||
this.userEvent.next(this.user); | ||
} | ||
|
||
deleteUser() { | ||
localStorage.removeItem("user"); | ||
this.user = null; | ||
this.userPermissions = []; | ||
this.userEvent.next(this.user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.