diff --git a/lib/core/models/general-user.model.ts b/lib/core/models/general-user.model.ts new file mode 100644 index 0000000000..e5643e4d1c --- /dev/null +++ b/lib/core/models/general-user.model.ts @@ -0,0 +1,22 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { EcmUserModel } from './ecm-user.model'; +import { IdentityUserModel } from './identity-user.model'; +import { UserProcessModel } from './user-process.model'; + +export type User = (EcmUserModel | UserProcessModel | IdentityUserModel) & { displayName?: string } & { username?: string }; diff --git a/lib/core/models/public-api.ts b/lib/core/models/public-api.ts index c7d0eff903..7f75e92597 100644 --- a/lib/core/models/public-api.ts +++ b/lib/core/models/public-api.ts @@ -37,3 +37,4 @@ export * from './search-text-input.model'; export * from './node-metadata.model'; export * from './application-access.model'; export * from './user-access.model'; +export * from './general-user.model'; diff --git a/lib/core/pipes/full-name.pipe.ts b/lib/core/pipes/full-name.pipe.ts index 350c415b6e..0b28005db4 100644 --- a/lib/core/pipes/full-name.pipe.ts +++ b/lib/core/pipes/full-name.pipe.ts @@ -16,35 +16,25 @@ */ import { Pipe, PipeTransform } from '@angular/core'; +import { User } from '../models/general-user.model'; @Pipe({ name: 'fullName' }) export class FullNamePipe implements PipeTransform { - transform(user: any): string { - let fullName = ''; - if (user) { - if (user.displayName) { - const firstAndLastName = user.displayName.split('\ (.*)'); - if (firstAndLastName[0]) { - fullName += firstAndLastName[0]; - } - if (firstAndLastName[1]) { - fullName += fullName.length > 0 ? ' ' : ''; - fullName += firstAndLastName[1]; - } - } else { - if (user.firstName && user.firstName !== 'null') { - fullName += user.firstName; - } - if (user.lastName && user.lastName !== 'null') { - fullName += fullName.length > 0 ? ' ' : ''; - fullName += user.lastName; - } - if (!fullName) { - fullName += user.username ? user.username : user.email ? user.email : ''; - } - } - } - return fullName; + transform(user: User): string { + return this.buildFullName(user) ? this.buildFullName(user) : this.buildFromUsernameOrEmail(user); + } + + buildFullName(user: User): string { + const fullName: string[] = []; + + fullName.push(user?.firstName); + fullName.push(user?.lastName); + + return fullName.join(' ').trim(); + } + + buildFromUsernameOrEmail(user: User): string { + return (user?.username || user?.email) ?? '' ; } } diff --git a/lib/core/pipes/user-initial.pipe.spec.ts b/lib/core/pipes/user-initial.pipe.spec.ts index d1948f3e3f..e26e1588bd 100644 --- a/lib/core/pipes/user-initial.pipe.spec.ts +++ b/lib/core/pipes/user-initial.pipe.spec.ts @@ -16,7 +16,7 @@ */ import { DomSanitizer } from '@angular/platform-browser'; -import { UserProcessModel } from '../models/user-process.model'; +import { User } from '../models/general-user.model'; import { InitialUsernamePipe } from './user-initial.pipe'; class FakeSanitizer extends DomSanitizer { @@ -53,11 +53,11 @@ class FakeSanitizer extends DomSanitizer { describe('UserInitialPipe', () => { let pipe: InitialUsernamePipe; - let fakeUser: UserProcessModel; + let fakeUser: User; beforeEach(() => { pipe = new InitialUsernamePipe(new FakeSanitizer()); - fakeUser = new UserProcessModel(); + fakeUser = { username: '' } as User; }); it('should return a div with the user initials', () => { @@ -88,6 +88,38 @@ describe('UserInitialPipe', () => { expect(result).toBe('
F
'); }); + it('should return a single letter into div when only username is defined', () => { + fakeUser.firstName = undefined; + fakeUser.lastName = undefined; + fakeUser.username = 'USERNAME-FAKE'; + const result = pipe.transform(fakeUser); + expect(result).toBe('
U
'); + }); + + it('should return a single letter into div of firstName when only firstName and username is defined', () => { + fakeUser.firstName = 'FAKE-NAME'; + fakeUser.lastName = undefined; + fakeUser.username = 'USERNAME-FAKE'; + const result = pipe.transform(fakeUser); + expect(result).toBe('
F
'); + }); + + it('should return two letter into div of username and lastName when firstName is undefined', () => { + fakeUser.firstName = undefined; + fakeUser.lastName = 'FAKE-SURNAME'; + fakeUser.username = 'USERNAME-FAKE'; + const result = pipe.transform(fakeUser); + expect(result).toBe('
UF
'); + }); + + it('should return a div with the user initials when firstName, lastName and username is defined', () => { + fakeUser.firstName = 'FAKE-NAME'; + fakeUser.lastName = 'FAKE-SURNAME'; + fakeUser.username = 'USERNAME-FAKE'; + const result = pipe.transform(fakeUser); + expect(result).toBe('
FF
'); + }); + it('should return an empty string when user is null', () => { const result = pipe.transform(null); expect(result).toBe(''); diff --git a/lib/core/pipes/user-initial.pipe.ts b/lib/core/pipes/user-initial.pipe.ts index 393253b19d..024f23b413 100644 --- a/lib/core/pipes/user-initial.pipe.ts +++ b/lib/core/pipes/user-initial.pipe.ts @@ -17,11 +17,7 @@ import { Pipe, PipeTransform } from '@angular/core'; import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; -import { UserProcessModel } from '../models/user-process.model'; -import { EcmUserModel } from '../models/ecm-user.model'; -import { IdentityUserModel } from '../models/identity-user.model'; - -export type User = (EcmUserModel | UserProcessModel | IdentityUserModel) & { displayName?: string }; +import { User } from '../models/general-user.model'; @Pipe({ name: 'usernameInitials' @@ -34,7 +30,7 @@ export class InitialUsernamePipe implements PipeTransform { transform(user: User, className: string = '', delimiter: string = ''): SafeHtml { let safeHtml: SafeHtml = ''; if (user) { - const initialResult = this.getInitialUserName(user.firstName || user.displayName, user.lastName, delimiter); + const initialResult = this.getInitialUserName(user.firstName || user.displayName || user.username, user.lastName, delimiter); safeHtml = this.sanitized.bypassSecurityTrustHtml(`
${initialResult}
`); } return safeHtml; diff --git a/lib/core/userinfo/components/user-info.component.spec.ts b/lib/core/userinfo/components/user-info.component.spec.ts index 2aa709e289..6298aefa25 100644 --- a/lib/core/userinfo/components/user-info.component.spec.ts +++ b/lib/core/userinfo/components/user-info.component.spec.ts @@ -354,34 +354,6 @@ describe('User info component', () => { expect(fullNameElement.textContent).not.toContain('fake-first-name'); }); - it('should not show first name if it is null string', async () => { - const wrongFirstNameBpmUser: BpmUserModel = new BpmUserModel({ - firstName: 'null', - lastName: 'fake-last-name' - }); - getCurrentUserInfoStub.and.returnValue(of(wrongFirstNameBpmUser)); - await whenFixtureReady(); - - expect(element.querySelector('#userinfo_container')).toBeDefined(); - expect(element.querySelector('#adf-userinfo-bpm-name-display')).toBeDefined(); - expect(element.querySelector('#adf-userinfo-bpm-name-display').textContent).toContain('fake-last-name'); - expect(element.querySelector('#adf-userinfo-bpm-name-display').textContent).not.toContain('null'); - }); - - it('should not show last name if it is null string', async () => { - const wrongLastNameBpmUser: BpmUserModel = new BpmUserModel({ - firstName: 'fake-first-name', - lastName: 'null' - }); - getCurrentUserInfoStub.and.returnValue(of(wrongLastNameBpmUser)); - await whenFixtureReady(); - - expect(element.querySelector('#userinfo_container')).toBeDefined(); - expect(element.querySelector('#adf-userinfo-bpm-name-display')).toBeDefined(); - expect(element.querySelector('#adf-userinfo-bpm-name-display').textContent).toContain('fake-first-name'); - expect(element.querySelector('#adf-userinfo-bpm-name-display').textContent).not.toContain('null'); - }); - it('should not show the tabs', async () => { await whenFixtureReady(); openUserInfo();