2018-11-27 18:33:17 +01:00
|
|
|
import { Component, OnDestroy, OnInit } from '@angular/core';
|
2019-01-21 12:14:20 +01:00
|
|
|
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
|
2018-11-27 18:33:17 +01:00
|
|
|
import { ActivatedRoute, Params, Router } from '@angular/router';
|
2019-12-11 15:51:03 +01:00
|
|
|
import { CultureInfo } from '@app/core/model/culture-info';
|
|
|
|
import { DmpModel } from '@app/core/model/dmp/dmp';
|
|
|
|
import { UserListingModel } from '@app/core/model/user/user-listing';
|
|
|
|
import { AuthService } from '@app/core/services/auth/auth.service';
|
|
|
|
import { CultureService } from '@app/core/services/culture/culture-service';
|
|
|
|
import { UserService } from '@app/core/services/user/user.service';
|
|
|
|
import { BaseComponent } from '@common/base/base.component';
|
2018-08-24 17:21:02 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2018-11-27 18:33:17 +01:00
|
|
|
import * as moment from 'moment-timezone';
|
2019-09-23 10:17:03 +02:00
|
|
|
import { Observable, of } from 'rxjs';
|
|
|
|
import { map, takeUntil } from 'rxjs/operators';
|
2020-01-23 17:35:11 +01:00
|
|
|
import { LanguageService } from '@app/core/services/language/language.service';
|
2020-04-10 16:16:37 +02:00
|
|
|
import { isNullOrUndefined } from 'util';
|
|
|
|
import { Oauth2DialogService } from '../misc/oauth2-dialog/service/oauth2-dialog.service';
|
|
|
|
import { ConfigurationService } from '@app/core/services/configuration/configuration.service';
|
2020-10-23 09:49:09 +02:00
|
|
|
import { MultipleAutoCompleteConfiguration } from '@app/library/auto-complete/multiple/multiple-auto-complete-configuration';
|
|
|
|
import { ExternalSourceItemModel } from '@app/core/model/external-sources/external-source-item';
|
|
|
|
import { ExternalSourcesService } from '@app/core/services/external-sources/external-sources.service';
|
|
|
|
import { UserProfileEditorModel } from './user-profile-editor.model';
|
|
|
|
import { RoleOrganizationType } from '@app/core/common/enum/role-organization-type';
|
|
|
|
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
|
2020-10-26 10:57:06 +01:00
|
|
|
import { AddAccountDialogComponent } from './add-account/add-account-dialog.component';
|
|
|
|
import { MatDialog } from '@angular/material';
|
|
|
|
import { UserCredentialModel } from '@app/core/model/user/user-credential';
|
2020-10-26 13:37:34 +01:00
|
|
|
import { AuthProvider } from '@app/core/common/enum/auth-provider';
|
2020-10-26 17:18:26 +01:00
|
|
|
import { MergeEmailConfirmationService } from '@app/core/services/merge-email-confirmation/merge-email-confirmation.service';
|
2018-08-24 17:21:02 +02:00
|
|
|
|
2018-08-30 13:09:36 +02:00
|
|
|
const availableLanguages: any[] = require('../../../assets/resources/language.json');
|
2018-08-24 17:21:02 +02:00
|
|
|
|
|
|
|
@Component({
|
2018-10-05 17:00:54 +02:00
|
|
|
selector: 'app-user-profile',
|
|
|
|
templateUrl: './user-profile.component.html',
|
|
|
|
styleUrls: ['./user-profile.component.scss'],
|
2018-08-24 17:21:02 +02:00
|
|
|
})
|
2018-11-27 18:33:17 +01:00
|
|
|
export class UserProfileComponent extends BaseComponent implements OnInit, OnDestroy {
|
2018-08-24 17:21:02 +02:00
|
|
|
|
2020-10-23 09:49:09 +02:00
|
|
|
userProfileEditorModel: UserProfileEditorModel;
|
2018-10-05 17:00:54 +02:00
|
|
|
user: Observable<UserListingModel>;
|
2020-10-26 10:57:06 +01:00
|
|
|
userCredentials: UserCredentialModel[];
|
2018-10-05 17:00:54 +02:00
|
|
|
currentUserId: string;
|
|
|
|
cultures: Observable<CultureInfo[]>;
|
|
|
|
timezones: Observable<any[]>;
|
|
|
|
editMode = false;
|
|
|
|
languages = availableLanguages;
|
2020-04-10 16:16:37 +02:00
|
|
|
zenodoToken: string;
|
2020-07-17 12:19:10 +02:00
|
|
|
zenodoEmail: String;
|
2020-10-23 09:49:09 +02:00
|
|
|
roleOrganizationEnum = RoleOrganizationType;
|
2020-10-26 13:37:34 +01:00
|
|
|
authProviderEnum = AuthProvider;
|
2020-10-27 11:31:45 +01:00
|
|
|
private oauthLock: boolean;
|
2020-10-23 09:49:09 +02:00
|
|
|
|
|
|
|
organisationsAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = {
|
|
|
|
filterFn: this.filterOrganisations.bind(this),
|
|
|
|
initialItems: (excludedItems: any[]) => this.filterOrganisations('').pipe(map(result => result.filter(resultItem => (excludedItems || []).map(x => x.id).indexOf(resultItem.id) === -1))),
|
|
|
|
displayFn: (item) => item['name'],
|
|
|
|
titleFn: (item) => item['name'],
|
|
|
|
subtitleFn: (item) => item['tag'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['tag'] : (item['key'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['key'] : this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.NO-SOURCE'))
|
|
|
|
};
|
2018-10-05 17:00:54 +02:00
|
|
|
|
|
|
|
formGroup: FormGroup;
|
|
|
|
constructor(
|
2019-01-18 18:03:45 +01:00
|
|
|
private userService: UserService,
|
2018-10-05 17:00:54 +02:00
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private authService: AuthService,
|
|
|
|
private language: TranslateService,
|
|
|
|
private cultureService: CultureService,
|
2020-10-23 09:49:09 +02:00
|
|
|
private externalSourcesService: ExternalSourcesService,
|
2020-01-23 17:35:11 +01:00
|
|
|
private authentication: AuthService,
|
2020-04-10 16:16:37 +02:00
|
|
|
private languageService: LanguageService,
|
|
|
|
private configurationService: ConfigurationService,
|
2020-10-23 09:49:09 +02:00
|
|
|
private oauth2DialogService: Oauth2DialogService,
|
2020-10-26 10:57:06 +01:00
|
|
|
private dialog: MatDialog,
|
2020-10-26 17:18:26 +01:00
|
|
|
public enumUtils: EnumUtils,
|
|
|
|
private mergeEmailConfirmation: MergeEmailConfirmationService
|
2018-11-27 18:33:17 +01:00
|
|
|
) { super(); }
|
2018-10-05 17:00:54 +02:00
|
|
|
|
|
|
|
ngOnInit() {
|
2018-11-27 18:33:17 +01:00
|
|
|
this.route.params
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe((params: Params) => {
|
2019-01-21 12:14:20 +01:00
|
|
|
this.currentUserId = this.authService.current().id;
|
|
|
|
const userId = !params['id'] ? 'me' : params['id'];
|
2019-09-23 10:17:03 +02:00
|
|
|
this.user = this.userService.getUser(userId).pipe(map(result => {
|
2020-07-17 12:19:10 +02:00
|
|
|
//result['additionalinfo'] = JSON.parse(result['additionalinfo']);
|
|
|
|
//this.zenodoToken = result['additionalinfo']['zenodoToken'];
|
|
|
|
this.zenodoEmail = result['zenodoEmail'];
|
2020-10-23 09:49:09 +02:00
|
|
|
|
|
|
|
this.userProfileEditorModel = new UserProfileEditorModel().fromModel(result);
|
|
|
|
this.formGroup = this.userProfileEditorModel.buildForm();
|
|
|
|
// this.formGroup = new FormBuilder().group({
|
|
|
|
// language: new FormControl(result['language'] ? availableLanguages.filter(x => x.value === result['language']['value']).pop() : '', [Validators.required]),
|
|
|
|
// timezone: new FormControl(result['timezone'], [Validators.required]),
|
|
|
|
// culture: new FormControl(result['culture'], [Validators.required])
|
|
|
|
// });
|
|
|
|
|
2018-11-27 18:33:17 +01:00
|
|
|
//this.formGroup.get('language').valueChanges.pipe(takeUntil(this._destroyed)).subscribe(x => { if (x) this.translate.use(x.value) })
|
|
|
|
this.formGroup.get('timezone').valueChanges
|
2020-10-23 09:49:09 +02:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(x => { if (x) { this.timezones = this._filterTimezone(x); } });
|
2018-11-27 18:33:17 +01:00
|
|
|
this.formGroup.get('culture').valueChanges
|
2020-10-23 09:49:09 +02:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(x => { if (x) { this.cultures = this._filterCulture(x); } });
|
|
|
|
// this.initializeDisabledFormGroup();
|
|
|
|
this.unlock();
|
2018-11-27 18:33:17 +01:00
|
|
|
return result;
|
2019-09-23 10:17:03 +02:00
|
|
|
}));
|
2020-10-26 10:57:06 +01:00
|
|
|
|
|
|
|
this.userService.getEmails(userId).pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(result => {
|
|
|
|
this.userCredentials = result;
|
|
|
|
});
|
2018-10-05 17:00:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:17:31 +02:00
|
|
|
ngOnDestroy(): void {
|
|
|
|
}
|
|
|
|
|
2019-06-05 09:20:17 +02:00
|
|
|
logout(): void {
|
|
|
|
this.authentication.logout();
|
|
|
|
}
|
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
getUserRole(dmp: DmpModel) {
|
2020-03-09 15:50:12 +01:00
|
|
|
if (dmp.creator.id === this.currentUserId) {
|
2020-04-10 16:16:37 +02:00
|
|
|
return this.language.instant('USER-PROFILE.DMPS.CREATOR');
|
|
|
|
} else if (dmp.associatedUsers.map(x => x.id).indexOf(this.currentUserId) !== -1) {
|
|
|
|
return this.language.instant('USER-PROFILE.DMPS.MEMBER');
|
|
|
|
}
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
showAllDmps() {
|
2019-01-18 18:03:45 +01:00
|
|
|
this.router.navigate(['/plans']);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
navigateToDmp(dmp: DmpModel) {
|
|
|
|
this.router.navigate(['/plans/edit/' + dmp.id]);
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
2020-10-23 09:49:09 +02:00
|
|
|
private filterOrganisations(value: string): Observable<ExternalSourceItemModel[]> {
|
|
|
|
return this.externalSourcesService.searchDMPOrganizations(value);
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
private _filterTimezone(value: string): Observable<any[]> {
|
|
|
|
if (value && typeof value === 'string') {
|
|
|
|
const filterValue = value.toLowerCase();
|
2019-09-23 10:17:03 +02:00
|
|
|
return of(moment.tz.names().filter(option => option.toLowerCase().includes(filterValue)));
|
2018-10-05 17:00:54 +02:00
|
|
|
} else {
|
2019-09-23 10:17:03 +02:00
|
|
|
return of(moment.tz.names());
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _filterCulture(value: string): Observable<any[]> {
|
|
|
|
if (value && typeof value === 'string') {
|
|
|
|
const filterValue = value.toLowerCase();
|
2019-09-23 10:17:03 +02:00
|
|
|
return of(this.cultureService.getCultureValues().filter(option => option.displayName.toLowerCase().includes(filterValue)));
|
2018-10-05 17:00:54 +02:00
|
|
|
} else {
|
2019-09-23 10:17:03 +02:00
|
|
|
return of(this.cultureService.getCultureValues());
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
displayFn(culture?: CultureInfo): string | undefined {
|
|
|
|
return culture ? culture.displayName + '-' + culture.nativeName : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
2020-10-23 09:49:09 +02:00
|
|
|
if (!this.formGroup.valid) { return; }
|
|
|
|
this.userService.updateUserSettings(this.formGroup.value)
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
x => {
|
|
|
|
this.editMode = false;
|
|
|
|
this.languageService.changeLanguage(this.formGroup.value.language.value);
|
|
|
|
this.authService.current().culture = this.formGroup.value.culture.name;
|
|
|
|
this.authService.me()
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(result => this.router.navigate(['/profile']));
|
|
|
|
// .subscribe(result => window.location.reload());
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public unlock() {
|
|
|
|
this.editMode = true;
|
|
|
|
this.formGroup.enable();
|
|
|
|
}
|
|
|
|
|
2020-10-23 09:49:09 +02:00
|
|
|
public initializeDisabledFormGroup() {
|
|
|
|
this.formGroup.disable();
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:00:54 +02:00
|
|
|
public lock() {
|
2019-01-21 12:14:20 +01:00
|
|
|
if (!this.formGroup.valid) { return; }
|
2019-01-18 18:03:45 +01:00
|
|
|
this.userService.updateUserSettings(this.formGroup.value)
|
2018-11-27 18:33:17 +01:00
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
x => {
|
|
|
|
this.editMode = false;
|
2020-01-23 17:35:11 +01:00
|
|
|
this.languageService.changeLanguage(this.formGroup.value.language.value);
|
2018-11-27 18:33:17 +01:00
|
|
|
this.authService.current().culture = this.formGroup.value.culture.name;
|
|
|
|
this.formGroup.disable();
|
|
|
|
this.authService.me()
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
2019-06-24 17:26:40 +02:00
|
|
|
.subscribe(result => this.router.navigate(['/profile']));
|
2019-09-23 10:17:03 +02:00
|
|
|
// .subscribe(result => window.location.reload());
|
2018-11-27 18:33:17 +01:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
2018-10-05 17:00:54 +02:00
|
|
|
}
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2020-02-17 16:39:33 +01:00
|
|
|
public applyFallbackAvatar(ev: Event) {
|
|
|
|
(ev.target as HTMLImageElement).src = 'assets/images/profile-placeholder.png';
|
|
|
|
}
|
|
|
|
|
2020-04-10 16:16:37 +02:00
|
|
|
public hasZenodo(): boolean {
|
2020-07-17 12:19:10 +02:00
|
|
|
return !isNullOrUndefined(this.zenodoEmail) && this.zenodoEmail !== "";
|
2020-04-10 16:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public loginToZenodo() {
|
|
|
|
this.showOauth2Dialog(this.getAccessUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
getAccessUrl(): string {
|
|
|
|
const redirectUri = this.configurationService.app + 'oauth2';
|
|
|
|
const url = this.configurationService.loginProviders.zenodoConfiguration.oauthUrl
|
|
|
|
+ '?client_id=' + this.configurationService.loginProviders.zenodoConfiguration.clientId
|
|
|
|
+ '&response_type=code&scope=deposit:write+deposit:actions+user:email&state=astate&redirect_uri='
|
|
|
|
+ redirectUri;
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
showOauth2Dialog(url: string) {
|
|
|
|
this.oauth2DialogService.login(url)
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
2020-10-26 17:18:26 +01:00
|
|
|
.subscribe(result => {
|
2020-10-27 11:04:57 +01:00
|
|
|
if (result !== undefined) {
|
2020-10-27 11:31:45 +01:00
|
|
|
if (result.oauthCode !== undefined && result.oauthCode !== null && !this.oauthLock) {
|
2020-10-27 11:04:57 +01:00
|
|
|
this.userService.registerDOIToken(result.oauthCode, this.configurationService.app + 'oauth2')
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(() => this.router.navigate(['/reload']).then(() => this.router.navigate(['/profile'])));
|
2020-10-27 11:31:45 +01:00
|
|
|
this.oauthLock = true;
|
2020-10-27 11:04:57 +01:00
|
|
|
}
|
2020-10-27 11:31:45 +01:00
|
|
|
} else {
|
|
|
|
this.oauthLock = false;
|
2020-04-10 16:16:37 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public RemoveZenodo() {
|
|
|
|
this.userService.deleteDOIToken()
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(() => this.router.navigate(['/reload']).then(() => this.router.navigate(['/profile'])));
|
|
|
|
}
|
|
|
|
|
2020-10-26 10:57:06 +01:00
|
|
|
public removeAccount() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public addAccount() {
|
|
|
|
const dialogRef = this.dialog.open(AddAccountDialogComponent, {
|
|
|
|
restoreFocus: false,
|
|
|
|
autoFocus: false,
|
2020-10-26 12:24:39 +01:00
|
|
|
width: '653px',
|
2020-10-26 10:57:06 +01:00
|
|
|
data: {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => {
|
|
|
|
if (result) {
|
2020-10-27 11:04:57 +01:00
|
|
|
this.mergeEmailConfirmation.sendConfirmationEmail(result).pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(res => {}, err => {});
|
2020-10-26 10:57:06 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-24 17:21:02 +02:00
|
|
|
}
|