Commit 4ee17252 by Ali Arshad

Added and setuped permissions class

parent 2188a7ee
......@@ -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;
}
}
......@@ -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;")
......
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;
}
......
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);
}
}
......@@ -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();
}
}
......@@ -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]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment