[ADF-3191] Fix userprefrence oauth2 (#3488)

* remove user preference setting save

* fix host setting test

* remove userPreferences test

* fix title service test

* remove unused imports

* restore input align

* fix service import in test tag rating
This commit is contained in:
Eugenio Romano
2018-06-15 08:32:16 +01:00
committed by GitHub
parent af2cde0791
commit 8966e22487
43 changed files with 417 additions and 528 deletions

View File

@@ -15,9 +15,8 @@
* limitations under the License.
*/
import {
Component, ElementRef, EventEmitter, Input, OnInit,
Output, TemplateRef, ViewEncapsulation
import { Component, ElementRef, EventEmitter,
Input, OnInit, Output, TemplateRef, ViewEncapsulation
} from '@angular/core';
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
@@ -30,6 +29,11 @@ import { UserPreferencesService } from '../../services/user-preferences.service'
import { LoginErrorEvent } from '../models/login-error.event';
import { LoginSubmitEvent } from '../models/login-submit.event';
import { LoginSuccessEvent } from '../models/login-success.event';
import {
AppConfigService,
AppConfigValues
} from '../../app-config/app-config.service';
import { OauthConfigModel } from '../../models/oauth-config.model';
enum LoginSteps {
Landing = 0,
@@ -43,12 +47,11 @@ enum LoginSteps {
styleUrls: ['./login.component.scss'],
encapsulation: ViewEncapsulation.None,
host: {
'class': 'adf-login',
class: 'adf-login',
'(blur)': 'onBlur($event)'
}
})
export class LoginComponent implements OnInit {
isPasswordShow: boolean = false;
/**
@@ -136,25 +139,29 @@ export class LoginComponent implements OnInit {
* @param authService
* @param translate
*/
constructor(private _fb: FormBuilder,
private authService: AuthenticationService,
private translateService: TranslationService,
private logService: LogService,
private elementRef: ElementRef,
private router: Router,
private userPreferences: UserPreferencesService) {
constructor(
private _fb: FormBuilder,
private authService: AuthenticationService,
private translateService: TranslationService,
private logService: LogService,
private elementRef: ElementRef,
private router: Router,
private appConfig: AppConfigService,
private userPreferences: UserPreferencesService
) {
this.initFormError();
this.initFormFieldsMessages();
}
ngOnInit() {
if (this.authService.isOauth()) {
if (this.userPreferences.oauthConfig && this.userPreferences.oauthConfig.implicitFlow) {
let oauth: OauthConfigModel = this.appConfig.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
if (oauth && oauth.implicitFlow) {
this.implicitFlow = true;
}
}
if (this.hasCustomFiledsValidation()) {
if (this.hasCustomFieldsValidation()) {
this.form = this._fb.group(this.fieldsValidation);
} else {
this.initFormFieldsDefault();
@@ -170,11 +177,13 @@ export class LoginComponent implements OnInit {
*/
onSubmit(values: any) {
if (this.disableCsrf !== null && this.disableCsrf !== undefined) {
this.userPreferences.disableCSRF = this.disableCsrf;
this.appConfig.get<boolean>(AppConfigValues.DISABLECSRF);
}
this.disableError();
const args = new LoginSubmitEvent({ controls: { username: this.form.controls.username } });
const args = new LoginSubmitEvent({
controls: { username: this.form.controls.username }
});
this.executeSubmit.emit(args);
if (args.defaultPrevented) {
@@ -185,7 +194,7 @@ export class LoginComponent implements OnInit {
}
implicitLogin() {
this.authService.ssoImplictiLogin();
this.authService.ssoImplicitLogin();
}
/**
@@ -197,12 +206,15 @@ export class LoginComponent implements OnInit {
for (let field in this.formError) {
if (field) {
this.formError[field] = '';
let hasError = (this.form.controls[field].errors && data[field] !== '') ||
(this.form.controls[field].dirty && !this.form.controls[field].valid);
let hasError =
(this.form.controls[field].errors && data[field] !== '') ||
(this.form.controls[field].dirty &&
!this.form.controls[field].valid);
if (hasError) {
for (let key in this.form.controls[field].errors) {
if (key) {
this.formError[field] += this._message[field][key] + '';
this.formError[field] +=
this._message[field][key] + '';
}
}
}
@@ -216,15 +228,20 @@ export class LoginComponent implements OnInit {
*/
private performLogin(values: any) {
this.actualLoginStep = LoginSteps.Checking;
this.authService.login(values.username, values.password, this.rememberMe)
this.authService
.login(values.username, values.password, this.rememberMe)
.subscribe(
(token: any) => {
const redirectUrl = this.authService.getRedirect(this.providers);
const redirectUrl = this.authService.getRedirect(
this.providers
);
this.actualLoginStep = LoginSteps.Welcome;
this.userPreferences.setStoragePrefix(values.username);
values.password = null;
this.success.emit(new LoginSuccessEvent(token, values.username, null));
this.success.emit(
new LoginSuccessEvent(token, values.username, null)
);
if (redirectUrl) {
this.authService.setRedirect(null);
@@ -247,16 +264,25 @@ export class LoginComponent implements OnInit {
* Check and display the right error message in the UI
*/
private displayErrorMessage(err: any): void {
if (err.error && err.error.crossDomain && err.error.message.indexOf('Access-Control-Allow-Origin') !== -1) {
if (
err.error &&
err.error.crossDomain &&
err.error.message.indexOf('Access-Control-Allow-Origin') !== -1
) {
this.errorMsg = err.error.message;
} else if (err.status === 403 && err.message.indexOf('Invalid CSRF-token') !== -1) {
} else if (
err.status === 403 &&
err.message.indexOf('Invalid CSRF-token') !== -1
) {
this.errorMsg = 'LOGIN.MESSAGES.LOGIN-ERROR-CSRF';
} else if (err.status === 403 && err.message.indexOf('The system is currently in read-only mode') !== -1) {
} else if (
err.status === 403 &&
err.message.indexOf('The system is currently in read-only mode') !==
-1
) {
this.errorMsg = 'LOGIN.MESSAGES.LOGIN-ECM-LICENSE';
} else {
this.errorMsg = 'LOGIN.MESSAGES.LOGIN-ERROR-CREDENTIALS';
}
}
@@ -275,7 +301,12 @@ export class LoginComponent implements OnInit {
* @param ruleId - i.e. required | minlength | maxlength
* @param msg
*/
addCustomValidationError(field: string, ruleId: string, msg: string, params?: any) {
addCustomValidationError(
field: string,
ruleId: string,
msg: string,
params?: any
) {
if (params) {
this.translateService.get(msg, params).subscribe((res: string) => {
this._message[field][ruleId] = res;
@@ -290,7 +321,10 @@ export class LoginComponent implements OnInit {
*/
toggleShowPassword() {
this.isPasswordShow = !this.isPasswordShow;
this.elementRef.nativeElement.querySelector('#password').type = this.isPasswordShow ? 'text' : 'password';
this.elementRef.nativeElement.querySelector('#password').type = this
.isPasswordShow
? 'text'
: 'password';
}
/**
@@ -313,8 +347,8 @@ export class LoginComponent implements OnInit {
*/
private initFormError() {
this.formError = {
'username': '',
'password': ''
username: '',
password: ''
};
}
@@ -323,8 +357,8 @@ export class LoginComponent implements OnInit {
*/
private initFormFieldsMessages() {
this._message = {
'username': {},
'password': {}
username: {},
password: {}
};
}
@@ -333,17 +367,19 @@ export class LoginComponent implements OnInit {
*/
private initFormFieldsMessagesDefault() {
this._message = {
'username': {
'required': 'LOGIN.MESSAGES.USERNAME-REQUIRED'
username: {
required: 'LOGIN.MESSAGES.USERNAME-REQUIRED'
},
'password': {
'required': 'LOGIN.MESSAGES.PASSWORD-REQUIRED'
password: {
required: 'LOGIN.MESSAGES.PASSWORD-REQUIRED'
}
};
this.translateService.get('LOGIN.MESSAGES.USERNAME-MIN', { minLength: this.minLength }).subscribe((res: string) => {
this._message['username']['minlength'] = res;
});
this.translateService
.get('LOGIN.MESSAGES.USERNAME-MIN', { minLength: this.minLength })
.subscribe((res: string) => {
this._message['username']['minlength'] = res;
});
}
private initFormFieldsDefault() {
@@ -368,7 +404,7 @@ export class LoginComponent implements OnInit {
this.isError = true;
}
private hasCustomFiledsValidation(): boolean {
private hasCustomFieldsValidation(): boolean {
return this.fieldsValidation !== undefined;
}
}