Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
PT
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Ali Arshad
PT
Commits
4ee17252
Commit
4ee17252
authored
Feb 09, 2018
by
Ali Arshad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added and setuped permissions class
parent
2188a7ee
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
180 additions
and
11 deletions
+180
-11
constants.ts
src/app/constants.ts
+38
-0
protected.component.pug
src/app/layouts/protected/protected.component.pug
+5
-3
protected.component.ts
src/app/layouts/protected/protected.component.ts
+11
-3
permissions.service.ts
src/app/users/permissions.service.ts
+107
-0
user.service.ts
src/app/users/user.service.ts
+15
-3
users.module.ts
src/app/users/users.module.ts
+4
-2
No files found.
src/app/constants.ts
View file @
4ee17252
...
...
@@ -6,6 +6,35 @@ const APP_URLS = {
forgetPassword
:
'/forgot'
,
newPassword
:
'/set-new'
};
const
PERMISSIONS
=
{
/** Admin Dashboard */
AdminDashboard
:
'AdminDashboard'
,
/** Users Dashboard */
UserDashboard
:
'UserDashboard'
,
/** Management Dashboard */
Dashboard_UsersLogins
:
'Dashboard_UsersLogins'
,
Dashboard_UsersLoginsReport
:
'Dashboard_UsersLoginsReport'
,
Dashboard_ManagersLogins
:
'Dashboard_ManagersLogins'
,
Dashboard_ManagersLoginsReport
:
'Dashboard_ManagersLoginsReport'
,
Dashboard_Projects
:
'Dashboard_Projects'
,
Dashboard_ProjectsReports
:
'Dashboard_ProjectsReports'
,
_Dashboard
:
'Dashboard_'
,
// generic permission for dashboard access
/** Projects */
Projects_All
:
'Projects_All'
,
Projects_Own
:
'Projects_Own'
,
_Projects
:
'Projects_'
// generic permission for projects access
};
const
RIGHTS
=
{
Create
:
'create'
,
Read
:
'read'
,
Update
:
'update'
,
Delete
:
'delete'
};
if
(
window
.
location
.
hostname
.
indexOf
(
'stage'
)
===
-
1
)
{
env
=
'stage'
;
}
else
{
...
...
@@ -22,7 +51,16 @@ export class Constants {
public
static
get
API_URL
():
string
{
return
API_URL
;
}
public
static
get
APP_URLS
():
any
{
return
APP_URLS
;
}
public
static
get
PERMISSIONS
():
any
{
return
PERMISSIONS
;
}
public
static
get
RIGHTS
():
any
{
return
RIGHTS
;
}
}
src/app/layouts/protected/protected.component.pug
View file @
4ee17252
...
...
@@ -23,11 +23,13 @@
.navbar-collapse.collapse
ul.nav.navbar-nav
li
a(
href="#"
) Admin
a(
*ngIf='permissionsService.showAdminMenu()'
) Admin
li
a(
href="#"
) Management
a(
*ngIf='permissionsService.showManagementMenu()'
) Management
li
a(href="#") Projects
a(*ngIf='permissionsService.showProjectsMenu()') Projects
li
a(*ngIf='permissionsService.showUsersMenu()') Users
.container.last
.row
.col-lg-3.col-lg-offset-9.col-md-3.col-md-offset-.col-sm-3.col-sm-offset-9.col-xs-5.col-xs-offset-7(style="padding-right: 0;")
...
...
src/app/layouts/protected/protected.component.ts
View file @
4ee17252
import
{
Component
,
OnInit
}
from
'@angular/core'
;
import
{
Constants
}
from
'../../constants'
;
import
{
UserService
}
from
'../../users/user.service'
;
import
{
Router
}
from
'@angular/router'
;
import
{
PermissionsService
}
from
'../../users/permissions.service'
;
import
{
UserService
}
from
'../../users/user.service'
;
@
Component
({
selector
:
'app-protected'
,
...
...
@@ -12,8 +13,16 @@ export class ProtectedComponent implements OnInit {
currentYear
;
userPopup
=
false
;
dashboardURL
=
Constants
.
APP_URLS
.
dashboard
;
menuPermission
=
{
adminDashboard
:
false
,
managementDashboard
:
false
,
projects
:
false
,
users
:
false
};
constructor
(
private
user
:
UserService
,
private
router
:
Router
)
{
constructor
(
private
permissionsService
:
PermissionsService
,
private
user
:
UserService
,
private
router
:
Router
)
{
}
ngOnInit
()
{
...
...
@@ -21,7 +30,6 @@ export class ProtectedComponent implements OnInit {
}
toggleUserPopup
(
event
)
{
console
.
log
(
event
);
event
.
stopPropagation
();
this
.
userPopup
=
!
this
.
userPopup
;
}
...
...
src/app/users/permissions.service.ts
0 → 100644
View file @
4ee17252
import
{
Injectable
}
from
'@angular/core'
;
import
{
HttpService
}
from
'../app-services/http.service'
;
import
{
LocalStoreService
}
from
'../app-services/local-store.service'
;
import
{
ToastsManager
}
from
'ng2-toastr'
;
import
{
Constants
}
from
'../constants'
;
@
Injectable
()
export
class
PermissionsService
{
public
user_role
;
private
PERMISSIONS
=
'api-user-role/permissions/'
;
private
permissions
=
{};
constructor
(
private
http
:
HttpService
,
private
localStoreService
:
LocalStoreService
,
private
toaster
:
ToastsManager
,
private
localStore
:
LocalStoreService
)
{
this
.
getPermissionsToLocalstore
();
}
setUserRole
(
user_role
)
{
this
.
user_role
=
user_role
;
}
savePermissionsToLocalstore
()
{
this
.
localStore
.
set
(
'permissions'
,
JSON
.
stringify
(
this
.
permissions
));
}
getPermissionsToLocalstore
()
{
try
{
this
.
permissions
=
JSON
.
parse
(
this
.
localStore
.
get
(
'permissions'
));
}
catch
(
e
)
{
// invalid JSON, lets ignore
}
}
getUserPermissions
()
{
const
showErrors
=
{
e401
:
false
,
};
this
.
http
.
get
(
this
.
PERMISSIONS
,
this
.
user_role
,
showErrors
).
subscribe
(
permissions
=>
{
this
.
parsePermissions
(
permissions
);
},
error
=>
{
this
.
toaster
.
error
(
'We are facing technical difficulties, please contact administrator.'
);
});
}
parsePermissions
(
permissionsData
)
{
permissionsData
.
forEach
(
permission
=>
{
this
.
permissions
[
permission
.
permission
]
=
{};
Object
.
keys
(
Constants
.
RIGHTS
).
forEach
(
right
=>
{
this
.
permissions
[
permission
.
permission
][
Constants
.
RIGHTS
[
right
]]
=
permission
[
Constants
.
RIGHTS
[
right
]]
||
0
;
});
});
this
.
setGenericPermissions
();
}
setGenericPermissions
()
{
let
dashboardGenericPermissions
=
[];
let
projectsGenericPermissions
=
[];
dashboardGenericPermissions
=
Object
.
keys
(
this
.
permissions
).
filter
(
item
=>
item
.
indexOf
(
Constants
.
PERMISSIONS
.
_Dashboard
)
>
-
1
);
projectsGenericPermissions
=
Object
.
keys
(
this
.
permissions
).
filter
(
item
=>
item
.
indexOf
(
Constants
.
PERMISSIONS
.
_Projects
)
>
-
1
);
this
.
permissions
[
Constants
.
PERMISSIONS
.
_Dashboard
]
=
{};
this
.
permissions
[
Constants
.
PERMISSIONS
.
_Projects
]
=
{};
Object
.
keys
(
Constants
.
RIGHTS
).
forEach
(
right
=>
{
dashboardGenericPermissions
.
some
(
item
=>
{
this
.
permissions
[
Constants
.
PERMISSIONS
.
_Dashboard
][
Constants
.
RIGHTS
[
right
]]
=
this
.
permissions
[
item
][
Constants
.
RIGHTS
[
right
]];
if
(
this
.
permissions
[
Constants
.
PERMISSIONS
.
_Dashboard
][
Constants
.
RIGHTS
[
right
]])
{
return
true
;
}
});
projectsGenericPermissions
.
some
(
item
=>
{
this
.
permissions
[
Constants
.
PERMISSIONS
.
_Projects
][
Constants
.
RIGHTS
[
right
]]
=
this
.
permissions
[
item
][
Constants
.
RIGHTS
[
right
]];
if
(
this
.
permissions
[
Constants
.
PERMISSIONS
.
_Projects
][
Constants
.
RIGHTS
[
right
]])
{
return
true
;
}
});
});
this
.
savePermissionsToLocalstore
();
}
can
(
permission
,
access
)
{
return
this
.
permissions
[
permission
]
&&
this
.
permissions
[
permission
][
access
];
}
showAdminMenu
()
{
return
this
.
user_role
&&
this
.
can
(
Constants
.
PERMISSIONS
.
AdminDashboard
,
Constants
.
RIGHTS
.
Read
);
}
showManagementMenu
()
{
return
this
.
user_role
&&
this
.
can
(
Constants
.
PERMISSIONS
.
_Dashboard
,
Constants
.
RIGHTS
.
Read
);
}
showProjectsMenu
()
{
return
this
.
user_role
&&
this
.
can
(
Constants
.
PERMISSIONS
.
_Projects
,
Constants
.
RIGHTS
.
Read
);
}
showUsersMenu
()
{
return
this
.
user_role
&&
this
.
can
(
Constants
.
PERMISSIONS
.
AdminDashboard
,
Constants
.
RIGHTS
.
Read
);
}
}
src/app/users/user.service.ts
View file @
4ee17252
...
...
@@ -3,6 +3,9 @@ import {HttpService} from '../app-services/http.service';
import
{
LocalStoreService
}
from
'../app-services/local-store.service'
;
import
{
Observable
}
from
'rxjs/Observable'
;
import
{
Subject
}
from
'rxjs/Subject'
;
import
{
ToastsManager
}
from
'ng2-toastr'
;
import
{
Constants
}
from
'../constants'
;
import
{
PermissionsService
}
from
'./permissions.service'
;
@
Injectable
()
export
class
UserService
{
...
...
@@ -47,7 +50,9 @@ export class UserService {
onSessionDestroyed
=
this
.
emitSessionDestroyed
.
asObservable
();
constructor
(
private
http
:
HttpService
,
private
localStoreService
:
LocalStoreService
)
{
private
localStoreService
:
LocalStoreService
,
private
toaster
:
ToastsManager
,
private
permissions
:
PermissionsService
)
{
}
...
...
@@ -136,7 +141,7 @@ export class UserService {
this
.
http
.
setTokken
(
data
.
access_token
);
this
.
populate
(
data
.
user
);
this
.
localStoreService
.
set
(
'access_token'
,
data
.
access_token
);
this
.
startAlivePolling
();
this
.
postLoginSteps
();
},
error
=>
{
});
...
...
@@ -151,6 +156,7 @@ export class UserService {
if
(
this
.
pollingIntervalId
)
{
clearInterval
(
this
.
pollingIntervalId
);
}
this
.
permissions
.
setUserRole
(
null
);
return
this
.
http
.
get
(
this
.
LOGOUT
,
''
,
data
);
}
...
...
@@ -167,7 +173,7 @@ export class UserService {
this
.
populate
(
data
.
user
);
observer
.
next
(
data
.
user
);
observer
.
complete
();
this
.
startAlivePolling
();
this
.
postLoginSteps
();
}
else
{
this
.
localStoreService
.
remove
(
'access_token'
);
observer
.
error
(
'Unable to veryfy access_token'
);
...
...
@@ -181,4 +187,10 @@ export class UserService {
}
});
}
postLoginSteps
()
{
this
.
startAlivePolling
();
this
.
permissions
.
setUserRole
(
this
.
user_role
);
this
.
permissions
.
getUserPermissions
();
}
}
src/app/users/users.module.ts
View file @
4ee17252
...
...
@@ -7,8 +7,9 @@ import {LoginComponent} from './login/login.component';
import
{
UserService
}
from
'./user.service'
;
import
{
LoaderComponent
}
from
'../shared/loader/loader.component'
;
import
{
ForgotComponent
}
from
'./forgot/forgot.component'
;
import
{
NewPasswordComponent
}
from
'./new-password/new-password.component'
;
import
{
ForgotComponent
}
from
'./forgot/forgot.component'
;
import
{
NewPasswordComponent
}
from
'./new-password/new-password.component'
;
import
{
PermissionsService
}
from
'./permissions.service'
;
@
NgModule
({
imports
:
[
...
...
@@ -18,6 +19,7 @@ import { NewPasswordComponent } from './new-password/new-password.component';
],
providers
:
[
UserService
,
PermissionsService
,
LocalStoreService
],
declarations
:
[
LoginComponent
,
LoaderComponent
,
ForgotComponent
,
NewPasswordComponent
]
...
...
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