monitor-dashboard/src/app/users/users.component.ts

110 lines
4.0 KiB
TypeScript

import {Component, Input, OnInit} from "@angular/core";
import {StakeholderService} from "../openaireLibrary/monitor/services/stakeholder.service";
import {properties} from "../../environments/environment";
import {Subscriber} from "rxjs";
import {Title} from "@angular/platform-browser";
import {Composer} from "../openaireLibrary/utils/email/composer";
import {Email} from "../openaireLibrary/utils/email/email";
import {ActivatedRoute} from "@angular/router";
import {NotificationUtils} from "../openaireLibrary/notifications/notification-utils";
import {Notification} from "../openaireLibrary/notifications/notifications";
import {User} from "../openaireLibrary/login/utils/helper.class";
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
type Tab = 'member' | 'manager';
@Component({
selector: 'users',
templateUrl: 'users.component.html'
})
export class UsersComponent implements OnInit {
public alias: string;
public name: string;
public logo: string;
public type: string;
public link: string;
public loading: boolean;
public messages: Map<Tab, string> = new Map<Tab, string>();
public tab: Tab = 'manager';
public user: User;
public properties: EnvProperties = properties;
private subscriptions = [];
public emailComposer: Function = (name, recipient, role): Email => {
return Composer.composeEmailForMonitorDashboard(name, recipient, role);
}
public notificationFn: Function = (name, recipient, role, invitation): Notification => {
if(invitation) {
invitation.link = this.link + invitation.link;
}
let notification: Notification = NotificationUtils.invite(this.name, role, 'user', invitation);
notification.entity = recipient;
notification.stakeholderType = this.type;
notification.stakeholder = this.alias;
notification.name = this.user.firstname;
notification.surname = this.user.lastname;
notification.groups = [recipient];
return notification;
}
public stickyPageHeader: boolean = false;
constructor(private stakeholderService: StakeholderService,
private userManagementService: UserManagementService,
private route: ActivatedRoute,
private title: Title) {
}
ngOnInit() {
this.loading = true;
this.subscriptions.push(this.route.fragment.subscribe((fragment: Tab) => {
if (this.isTab(fragment)) {
this.tab = fragment;
} else {
this.tab = 'manager';
}
}));
this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
if (stakeholder) {
this.alias = stakeholder.alias;
this.name = stakeholder.name;
this.logo = stakeholder.logoUrl;
this.title.setTitle(this.name + " | Users");
this.type = stakeholder.type;
this.link = this.getURL(this.alias);
this.messages.set("member", 'A member has the right to view the <b>restricted access</b> areas of this indicator\'s profile. ' +
'A member has <b>no access</b> to the administration part of the profile.');
this.messages.set("manager", 'A manager has the right to access the administration part of this indicator\'s profile, ' +
'where he is able to customize and manage indicators, and invite other users as members.');
this.loading = false;
}
}));
this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
this.user = user;
}));
}
ngOnDestroy() {
this.subscriptions.forEach(value => {
if (value instanceof Subscriber) {
value.unsubscribe();
}
});
}
private isTab(tab: Tab): boolean {
switch (tab) {
case "manager":
return true;
case "member":
return true;
default:
return false;
}
}
private getURL(id: string): string {
return properties.domain + properties.baseLink + "/" + id + "?verify=";
}
}