Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
HIAST-Clinics
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
almohanad.hafez
HIAST-Clinics
Commits
ba3c5ec5
You need to sign in or sign up before continuing.
Commit
ba3c5ec5
authored
Aug 22, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(F) Add role-based guard
parent
0669d39a
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
4 deletions
+48
-4
app-routing.module.ts
Clinics.Frontend/src/app/app-routing.module.ts
+14
-2
roles.ts
Clinics.Frontend/src/app/classes/Authentication/roles.ts
+1
-0
role-guard.ts
...tend/src/app/services/authentication/guards/role-guard.ts
+31
-0
authentication.interceptor.ts
.../authentication/interceptor/authentication.interceptor.ts
+2
-2
No files found.
Clinics.Frontend/src/app/app-routing.module.ts
View file @
ba3c5ec5
import
{
NgModule
}
from
'@angular/core'
;
import
{
RouterModule
,
Routes
}
from
'@angular/router'
;
import
{
HomeComponent
}
from
'./components/home/home.component'
;
import
{
RoleGuard
}
from
'./services/authentication/guards/role-guard'
;
import
{
Roles
}
from
'./classes/Authentication/roles'
;
const
routes
:
Routes
=
[
{
path
:
''
,
redirectTo
:
'home'
,
pathMatch
:
'full'
},
{
path
:
'home'
,
component
:
HomeComponent
}
{
path
:
''
,
redirectTo
:
'home'
,
pathMatch
:
'full'
,
},
{
path
:
'home'
,
component
:
HomeComponent
,
canActivate
:
[
RoleGuard
],
data
:
{
role
:
Roles
.
NotRegistered
}
}
];
@
NgModule
({
imports
:
[
RouterModule
.
forRoot
(
routes
)],
...
...
Clinics.Frontend/src/app/classes/Authentication/roles.ts
View file @
ba3c5ec5
...
...
@@ -2,4 +2,5 @@ export class Roles {
public
static
readonly
Admin
:
string
=
"admin"
;
public
static
readonly
Doctor
:
string
=
"doctor"
;
public
static
readonly
Receptionist
:
string
=
"receptionist"
;
public
static
readonly
NotRegistered
:
string
=
"notRegistered"
;
}
\ No newline at end of file
Clinics.Frontend/src/app/services/authentication/guards/role-guard.ts
0 → 100644
View file @
ba3c5ec5
import
{
Injectable
}
from
'@angular/core'
;
import
{
CanActivate
,
ActivatedRouteSnapshot
,
RouterStateSnapshot
}
from
'@angular/router'
;
import
{
AuthenticationService
}
from
'../authentication.service'
;
import
{
Roles
}
from
'../../../classes/Authentication/roles'
;
import
{
UserData
}
from
'../../../classes/Authentication/user-data'
;
@
Injectable
({
providedIn
:
'root'
})
export
class
RoleGuard
implements
CanActivate
{
constructor
(
private
authenticationService
:
AuthenticationService
)
{
}
canActivate
(
route
:
ActivatedRouteSnapshot
,
state
:
RouterStateSnapshot
):
boolean
{
const
userData
:
UserData
|
null
=
this
.
authenticationService
.
getUserData
();
const
requiredRole
:
string
=
route
.
data
[
'role'
];
if
(
!
requiredRole
)
return
true
;
if
(
requiredRole
===
Roles
.
NotRegistered
)
return
true
;
if
(
!
userData
)
return
false
;
return
userData
.
role
===
requiredRole
;
}
}
\ No newline at end of file
Clinics.Frontend/src/app/services/authentication/interceptor/authentication.interceptor.ts
View file @
ba3c5ec5
...
...
@@ -15,7 +15,7 @@ export class AuthenticationInterceptor implements HttpInterceptor {
intercept
(
req
:
HttpRequest
<
any
>
,
next
:
HttpHandler
):
Observable
<
HttpEvent
<
any
>>
{
const
jwt
=
JWTHandler
.
getJwtFromCookie
();
console
.
log
(
req
);
//
console.log(req);
req
=
req
.
clone
({
headers
:
this
.
HTTP_HEADERS
});
...
...
@@ -26,7 +26,7 @@ export class AuthenticationInterceptor implements HttpInterceptor {
}
});
}
console
.
log
(
req
);
//
console.log(req);
return
next
.
handle
(
req
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment