-
Notifications
You must be signed in to change notification settings - Fork 14
/
IdentityService.ts
157 lines (135 loc) · 5.99 KB
/
IdentityService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import * as SDK from "azure-devops-extension-sdk";
import { IIdentity, IPeoplePickerProvider } from "azure-devops-ui/Components/IdentityPickerDropdown/SharedIdentityPicker.Props";
let identityService: IVssIdentityService;
/**
* Contribution ids of identity services which can be obtained from DevOps.getService
*/
export const enum IdentityServiceIds {
/**
* Provides a way to search for identities.
*/
IdentityService = "ms.vss-features.identity-service"
}
export interface IdentitiesGetConnectionsResponseModel {
successors?: IIdentity[];
managers?: IIdentity[];
directReports?: IIdentity[];
}
export interface IdentitiesSearchRequestModel {
query: string;
identityTypes?: string[];
operationScopes?: string[];
queryTypeHint?: string;
pagingToken?: string;
properties?: string[];
filterByAncestorEntityIds?: string[];
filterByEntityIds?: string[];
options?: any;
}
export interface IVssIdentityService {
/**
* Get a list of the Most Recently Used (MRU) identities
*
* @returns list of the Most Recently Used (MRU) identities
*/
getIdentityMruAsync(): Promise<IIdentity[]>;
/**
* Given a search request model, return a list of Entities. If the filterIdentity callback is added, additionally filter the values before returning
*
* @param query The query to search the identities type with.
* @param identityTypes The types of identities to search (default "user" and "group")
* @param operationScopes The scope you would like to search (default "ims", "source")
* @param queryTypeHint A hint of what property your query is trying to search
* @param filterIdentity A filter passed in to alter the results of the identities found
* @param options Additional options to pass into the search
* @returns The list of filtered identities from the search.
*/
searchIdentitiesAsync(
query: string,
identityTypes?: string[],
operationScopes?: string[],
queryTypeHint?: string,
options?: any,
filterIdentity?: (returnedEntities: IIdentity[]) => IIdentity[]
): Promise<IIdentity[]>;
/**
* Add a list of identities to the MRU
*
* @param identities list of IdentityRefs to add to the MRU
* @returns True if the item was added, false otherwise
*/
addMruIdentitiesAsync(identities: IIdentity[]): Promise<boolean>;
/**
* Remove a list of identities from the MRU
*
* @param identities list of IdentityRefs to remove from the MRU
* @returns True if the item was removed, false otherwise
*/
removeMruIdentitiesAsync(identity: IIdentity[]): Promise<boolean>;
/**
* Gets a list of connections for a given identity
*
* @param identity Entity to look up connections
* @returns Connections for the given identity
*/
getConnections(identity: IIdentity, getDirectReports?: boolean): Promise<IdentitiesGetConnectionsResponseModel>;
}
export async function getIdentityService(): Promise<IVssIdentityService> {
if (!identityService) {
identityService = await SDK.getService<IVssIdentityService>(IdentityServiceIds.IdentityService);
}
return identityService;
}
export class PeoplePickerProvider implements IPeoplePickerProvider {
constructor(private _identityTypes: string[] = ["user", "group"]) {}
public async addIdentitiesToMRU(identities: IIdentity[]): Promise<boolean> {
const identityService = await getIdentityService();
return identityService.addMruIdentitiesAsync(identities);
}
public getEntityFromUniqueAttribute(entityId: string): IIdentity | PromiseLike<IIdentity> {
return getIdentityService().then(identityService => {
return identityService.searchIdentitiesAsync(entityId, this._identityTypes, ["ims", "source"], "uid").then(x => x[0]);
});
}
public onEmptyInputFocus(): IIdentity[] | PromiseLike<IIdentity[]> {
return getIdentityService().then(identityService => {
return identityService.getIdentityMruAsync().then(identities => {
return identities.map(this._identityMapper);
});
});
}
public onFilterIdentities(filter: string, selectedItems?: IIdentity[]): Promise<IIdentity[]> | IIdentity[] {
return this._onSearchPersona(filter, selectedItems ? selectedItems : []);
}
public onRequestConnectionInformation(
entity: IIdentity,
getDirectReports?: boolean
): IdentitiesGetConnectionsResponseModel | PromiseLike<IdentitiesGetConnectionsResponseModel> {
return getIdentityService().then(identityService => {
return identityService.getConnections(entity, getDirectReports);
});
}
public async removeIdentitiesFromMRU(identities: IIdentity[]): Promise<boolean> {
const identityService = await getIdentityService();
return identityService.removeMruIdentitiesAsync(identities);
}
private async _onSearchPersona(searchText: string, items: IIdentity[]): Promise<IIdentity[]> {
const identityService = await getIdentityService();
const identities = await identityService.searchIdentitiesAsync(searchText, this._identityTypes, ["ims", "source"]);
return identities
.filter(identity => !items.some(selectedIdentity => selectedIdentity.entityId === identity.entityId))
.map(this._identityMapper);
}
private _identityMapper = (identity: IIdentity): IIdentity => {
return identity.image ? { ...identity, image: this._convertImage(identity.image) } : identity;
};
private _convertImage(image: string): string {
if (image.startsWith("http")) {
return image;
} else if (image.startsWith("/")) {
return `https://dev.azure.com${image}`;
} else {
return `https://dev.azure.com/${image}`;
}
}
}