[ADF-2373] added filtering for membership on site dropdown (#3031)

* [ADF-2373] added filtering for membership on site dropdown

* [ADF-2373] added test for new site service method

* [ADF-2373] added filtering for site membering to the sites dropdown

* [ADF-2373] added another check into sites tests

* [ADF-2373] added a fix for the site dropdown tests

* [ADF-2373] fixed broken test check

* [ADF-2373] added PR review changes
This commit is contained in:
Vito 2018-03-06 16:28:30 +00:00 committed by Eugenio Romano
parent 0f64af39d9
commit 52df355e90
7 changed files with 213 additions and 20 deletions

View File

@ -29,7 +29,7 @@
</mat-accordion> </mat-accordion>
<div class="adf-site-container-style" id="site-container"> <div class="adf-site-container-style" id="site-container">
<adf-sites-dropdown (change)="onSiteChange($event)" [hideMyFiles]="false" > <adf-sites-dropdown (change)="onSiteChange($event)" [hideMyFiles]="false" [relations]="'members'">
</adf-sites-dropdown> </adf-sites-dropdown>
</div> </div>
<div class="document-list-container" fxLayout="row" fxLayoutAlign="start stretch" fxLayoutGap="16px"> <div class="document-list-container" fxLayout="row" fxLayoutAlign="start stretch" fxLayoutGap="16px">

View File

@ -24,6 +24,7 @@ Displays a dropdown menu to show and interact with the sites of the current user
| siteList | [SitePaging](https://github.com/Alfresco/alfresco-js-api/blob/master/src/alfresco-core-rest-api/docs/SitePaging.md) | null | A custom list of sites to be displayed by the dropdown. If no value is given, the sites of the current user are displayed by default. A list of objects only with properties 'title' and 'guid' is enough to be able to display the dropdown. | | siteList | [SitePaging](https://github.com/Alfresco/alfresco-js-api/blob/master/src/alfresco-core-rest-api/docs/SitePaging.md) | null | A custom list of sites to be displayed by the dropdown. If no value is given, the sites of the current user are displayed by default. A list of objects only with properties 'title' and 'guid' is enough to be able to display the dropdown. |
| placeholder | string | 'DROPDOWN.PLACEHOLDER_LABEL' | The placeholder text/the key from translation files for the placeholder text to be shown by default | | placeholder | string | 'DROPDOWN.PLACEHOLDER_LABEL' | The placeholder text/the key from translation files for the placeholder text to be shown by default |
| value | string | null | Id of the select site | | value | string | null | Id of the select site |
| relations | string | null | This parameter will allow to perform sites query filtering the results.It could have two possible values: **members** and **containers**. When **members** is used the site list will be filtered with only the sites where the user is a member of. |
### Events ### Events

View File

@ -18,7 +18,9 @@
import { DebugElement } from '@angular/core'; import { DebugElement } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser'; import { By } from '@angular/platform-browser';
import { DropdownSitesComponent } from './sites-dropdown.component'; import { DropdownSitesComponent, Relations } from './sites-dropdown.component';
import { SitesService, LogService } from '@alfresco/adf-core';
import { Observable } from 'rxjs/Observable';
declare let jasmine: any; declare let jasmine: any;
@ -29,13 +31,16 @@ describe('DropdownSitesComponent', () => {
let debug: DebugElement; let debug: DebugElement;
let element: HTMLElement; let element: HTMLElement;
let sitesList: any; let sitesList: any;
let siteListWitMembers: any;
let siteService: SitesService;
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [ declarations: [
DropdownSitesComponent DropdownSitesComponent
] ],
providers: [SitesService, LogService]
}).compileComponents(); }).compileComponents();
})); }));
@ -44,6 +49,7 @@ describe('DropdownSitesComponent', () => {
debug = fixture.debugElement; debug = fixture.debugElement;
element = fixture.nativeElement; element = fixture.nativeElement;
component = fixture.componentInstance; component = fixture.componentInstance;
siteService = TestBed.get(SitesService);
sitesList = { sitesList = {
'list': { 'list': {
@ -80,6 +86,126 @@ describe('DropdownSitesComponent', () => {
] ]
} }
}; };
siteListWitMembers = {
'list': {
'entries': [{
'entry': {
'visibility': 'MODERATED',
'guid': 'b4cff62a-664d-4d45-9302-98723eac1319',
'description': 'This is a Sample Alfresco Team site.',
'id': 'MODERATED-SITE',
'preset': 'site-dashboard',
'title': 'FAKE-MODERATED-SITE'
},
'relations': {
'members': {
'list': {
'pagination': {
'count': 3,
'hasMoreItems': false,
'skipCount': 0,
'maxItems': 100
},
'entries': [
{
'entry': {
'role': 'SiteManager',
'person': {
'firstName': 'Administrator',
'emailNotificationsEnabled': true,
'company': {},
'id': 'admin',
'enabled': true,
'email': 'admin@alfresco.com'
},
'id': 'admin'
}
},
{
'entry': {
'role': 'SiteCollaborator',
'person': {
'lastName': 'Beecher',
'userStatus': 'Helping to design the look and feel of the new web site',
'jobTitle': 'Graphic Designer',
'statusUpdatedAt': '2011-02-15T20:20:13.432+0000',
'mobile': '0112211001100',
'emailNotificationsEnabled': true,
'description': 'Alice is a demo user for the sample Alfresco Team site.',
'telephone': '0112211001100',
'enabled': false,
'firstName': 'Alice',
'skypeId': 'abeecher',
'avatarId': '198500fc-1e99-4f5f-8926-248cea433366',
'location': 'Tilbury, UK',
'company': {
'organization': 'Moresby, Garland and Wedge',
'address1': '200 Butterwick Street',
'address2': 'Tilbury',
'address3': 'UK',
'postcode': 'ALF1 SAM1'
},
'id': 'abeecher',
'email': 'abeecher@example.com'
},
'id': 'abeecher'
}
}
]
}
}
}
}, {
'entry': {
'visibility': 'PUBLIC',
'guid': 'b4cff62a-664d-4d45-9302-98723eac1319',
'description': 'This is a Sample Alfresco Team site.',
'id': 'PUBLIC-SITE',
'preset': 'site-dashboard',
'title': 'FAKE-SITE-PUBLIC'
}
}, {
'entry': {
'visibility': 'PRIVATE',
'guid': 'b4cff62a-664d-4d45-9302-98723eac1319',
'description': 'This is a Sample Alfresco Team site.',
'id': 'MEMBER-SITE',
'preset': 'site-dashboard',
'title': 'FAKE-PRIVATE-SITE-MEMBER'
},
'relations': {
'members': {
'list': {
'pagination': {
'count': 3,
'hasMoreItems': false,
'skipCount': 0,
'maxItems': 100
},
'entries': [
{
'entry': {
'role': 'SiteManager',
'person': {
'firstName': 'Administrator',
'emailNotificationsEnabled': true,
'company': {},
'id': 'admin',
'enabled': true,
'email': 'admin@alfresco.com'
},
'id': 'test'
}
}
]
}
}
}
}
]
}
};
}); });
describe('Rendering tests', () => { describe('Rendering tests', () => {
@ -280,5 +406,43 @@ describe('DropdownSitesComponent', () => {
}); });
}); });
it('should show only sites which logged user is member of when member relation is set', async(() => {
spyOn(siteService, 'getEcmCurrentLoggedUserName').and.returnValue('test');
spyOn(siteService, 'getSites').and.returnValue(Observable.of(siteListWitMembers));
component.relations = Relations.Members;
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
debug.query(By.css('.mat-select-trigger')).triggerEventHandler('click', null);
fixture.detectChanges();
fixture.whenStable().then(() => {
let options: any = debug.queryAll(By.css('mat-option'));
expect(options[1].nativeElement.innerText).toContain('FAKE-SITE-PUBLIC');
expect(options[2].nativeElement.innerText).toContain('FAKE-PRIVATE-SITE-MEMBER');
expect(options[3]).toBeUndefined();
});
});
}));
it('should show all the sites if no relation is set', async(() => {
spyOn(siteService, 'getEcmCurrentLoggedUserName').and.returnValue('test');
spyOn(siteService, 'getSites').and.returnValue(Observable.of(siteListWitMembers));
component.siteList = null;
component.relations = null;
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
debug.query(By.css('.mat-select-trigger')).triggerEventHandler('click', null);
fixture.detectChanges();
fixture.whenStable().then(() => {
let options: any = debug.queryAll(By.css('mat-option'));
expect(options[1].nativeElement.innerText).toContain('FAKE-MODERATED-SITE');
expect(options[2].nativeElement.innerText).toContain('FAKE-SITE-PUBLIC');
expect(options[3].nativeElement.innerText).toContain('FAKE-PRIVATE-SITE-MEMBER');
});
});
}));
}); });
}); });

View File

@ -16,9 +16,14 @@
*/ */
import { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation } from '@angular/core'; import { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation } from '@angular/core';
import { SitesService } from '@alfresco/adf-core'; import { SitesService, LogService } from '@alfresco/adf-core';
import { SitePaging, SiteEntry } from 'alfresco-js-api'; import { SitePaging, SiteEntry } from 'alfresco-js-api';
export enum Relations {
Members = 'members',
Containers = 'containers'
}
@Component({ @Component({
selector: 'adf-sites-dropdown', selector: 'adf-sites-dropdown',
styleUrls: ['./sites-dropdown.component.scss'], styleUrls: ['./sites-dropdown.component.scss'],
@ -48,6 +53,9 @@ export class DropdownSitesComponent implements OnInit {
@Input() @Input()
placeholder: string = 'DROPDOWN.PLACEHOLDER_LABEL'; placeholder: string = 'DROPDOWN.PLACEHOLDER_LABEL';
@Input()
relations: string;
/** Emitted when the user selects a site. When the default option is selected, /** Emitted when the user selects a site. When the default option is selected,
* an empty model is emitted. * an empty model is emitted.
*/ */
@ -58,14 +66,14 @@ export class DropdownSitesComponent implements OnInit {
public MY_FILES_VALUE = '-my-'; public MY_FILES_VALUE = '-my-';
constructor(private sitesService: SitesService) { constructor(private sitesService: SitesService,
private logService: LogService) {
} }
ngOnInit() { ngOnInit() {
if (!this.siteList) { if (!this.siteList) {
this.setDefaultSiteList(); this.setDefaultSiteList();
} }
} }
selectedSite(event: any) { selectedSite(event: any) {
@ -73,8 +81,13 @@ export class DropdownSitesComponent implements OnInit {
} }
private setDefaultSiteList() { private setDefaultSiteList() {
this.sitesService.getSites().subscribe((result) => { let extendedOptions = null;
this.siteList = result; if (this.relations) {
extendedOptions = { relations: [this.relations] };
}
this.sitesService.getSites(extendedOptions).subscribe((result) => {
this.siteList = this.relations === Relations.Members ? this.filteredResultsByMember(result) : result;
if (!this.hideMyFiles) { if (!this.hideMyFiles) {
let myItem = { entry: { id: '-my-', guid: '-my-', title: 'DROPDOWN.MY_FILES_OPTION' } }; let myItem = { entry: { id: '-my-', guid: '-my-', title: 'DROPDOWN.MY_FILES_OPTION' } };
@ -89,8 +102,21 @@ export class DropdownSitesComponent implements OnInit {
this.selected = this.siteList.list.entries.find(site => site.entry.id === this.value); this.selected = this.siteList.list.entries.find(site => site.entry.id === this.value);
}, },
(error) => { (error) => {
this.logService.error(error);
}); });
}
private filteredResultsByMember(sites: SitePaging): SitePaging {
const loggedUserName = this.sitesService.getEcmCurrentLoggedUserName();
sites.list.entries = sites.list.entries.filter( (site) => this.isCurrentUserMember(site, loggedUserName));
return sites;
}
private isCurrentUserMember(site, loggedUserName): boolean {
return site.entry.visibility === 'PUBLIC' ||
!!site.relations.members.list.entries.find((member) => {
return member.entry.id === loggedUserName;
});
} }
} }

View File

@ -48,4 +48,3 @@ export class VersionManagerComponent {
this.uploadError.emit(event); this.uploadError.emit(event);
} }
} }

View File

@ -46,4 +46,3 @@ export class VersionUploadComponent {
} }
} }

View File

@ -81,6 +81,10 @@ export class SitesService {
return this.getSite(siteId, { relations: ['members'] }); return this.getSite(siteId, { relations: ['members'] });
} }
getEcmCurrentLoggedUserName(): string {
return this.apiService.getInstance().ecmAuth.username;
}
private handleError(error: Response): any { private handleError(error: Response): any {
console.error(error); console.error(error);
return Observable.throw(error || 'Server error'); return Observable.throw(error || 'Server error');