-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(portal): new addGroupUsers() function #585
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { | ||
request, | ||
IRequestOptions, | ||
ArcGISRequestError | ||
} from "@esri/arcgis-rest-request"; | ||
|
||
import { getPortalUrl } from "../util/get-portal-url"; | ||
import { chunk } from "../util/array"; | ||
|
||
export interface IAddGroupUsersOptions extends IRequestOptions { | ||
/** | ||
* Group ID | ||
*/ | ||
id: string; | ||
/** | ||
* An array of usernames to be added to the group as group members | ||
*/ | ||
users?: string[]; | ||
/** | ||
* An array of usernames to be added to the group as group admins | ||
*/ | ||
admins?: string[]; | ||
} | ||
|
||
export interface IAddGroupUsersResult { | ||
/** | ||
* An array of usernames that were not added | ||
*/ | ||
notAdded?: string[]; | ||
/** | ||
* An array of request errors | ||
*/ | ||
errors?: ArcGISRequestError[]; | ||
} | ||
|
||
/** | ||
* ```js | ||
* import { addGroupUsers } from "@esri/arcgis-rest-portal"; | ||
* // | ||
* addGroupUsers({ | ||
* id: groupId, | ||
* users: ["username1", "username2"], | ||
* admins: ["username3"], | ||
* authentication | ||
* }) | ||
* .then(response); | ||
* ``` | ||
* Add users to a group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/add-users-to-group.htm) for more information. | ||
* | ||
* @param requestOptions - Options for the request | ||
* @returns A Promise | ||
*/ | ||
export function addGroupUsers( | ||
requestOptions: IAddGroupUsersOptions | ||
): Promise<IAddGroupUsersResult> { | ||
const id = requestOptions.id; | ||
const url = `${getPortalUrl(requestOptions)}/community/groups/${id}/addUsers`; | ||
const baseOptions = Object.assign({}, requestOptions, { | ||
admins: undefined, | ||
users: undefined | ||
}); | ||
|
||
const batchRequestOptions = [ | ||
..._prepareRequests("users", requestOptions.users, baseOptions), | ||
..._prepareRequests("admins", requestOptions.admins, baseOptions) | ||
]; | ||
|
||
const promises = batchRequestOptions.map(options => | ||
_sendSafeRequest(url, options) | ||
); | ||
|
||
return Promise.all(promises).then(_consolidateRequestResults); | ||
} | ||
|
||
function _prepareRequests( | ||
type: "admins" | "users", | ||
usernames: string[], | ||
baseOptions: IAddGroupUsersOptions | ||
): IAddGroupUsersOptions[] { | ||
if (!usernames || usernames.length < 1) { | ||
return []; | ||
} | ||
|
||
// the ArcGIS REST API only allows to add no more than 25 users per request, | ||
// see https://developers.arcgis.com/rest/users-groups-and-items/add-users-to-group.htm | ||
const userChunks: string[][] = chunk<string>(usernames, 25); | ||
|
||
return userChunks.map(users => | ||
_generateRequestOptions(type, users, baseOptions) | ||
); | ||
} | ||
|
||
function _generateRequestOptions( | ||
type: "admins" | "users", | ||
usernames: string[], | ||
baseOptions: IAddGroupUsersOptions | ||
) { | ||
return Object.assign({}, baseOptions, { | ||
[type]: usernames, | ||
params: { | ||
...baseOptions.params, | ||
[type]: usernames | ||
} | ||
}); | ||
} | ||
|
||
function _sendSafeRequest( | ||
url: string, | ||
requestOptions: IAddGroupUsersOptions | ||
): Promise<IAddGroupUsersResult> { | ||
return request(url, requestOptions).catch(error => { | ||
return { | ||
// the request should either add users or admins, not both | ||
notAdded: requestOptions.users || requestOptions.admins, | ||
errors: [error] | ||
}; | ||
}); | ||
} | ||
|
||
function _consolidateRequestResults( | ||
results: IAddGroupUsersResult[] | ||
): IAddGroupUsersResult { | ||
const notAdded = results | ||
.filter(result => result.notAdded) | ||
.reduce((collection, result) => collection.concat(result.notAdded), []); | ||
|
||
const errors = results | ||
.filter(result => result.errors) | ||
.reduce((collection, result) => collection.concat(result.errors), []); | ||
|
||
const consolidated: IAddGroupUsersResult = { notAdded }; | ||
|
||
if (errors.length > 0) { | ||
consolidated.errors = errors; | ||
} | ||
|
||
return consolidated; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* Copyright (c) 2019 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
export function chunk<T>(array: T[], size: number) { | ||
if (array.length === 0) { | ||
return []; | ||
} | ||
|
||
const chunks = []; | ||
|
||
for (let i = 0; i < array.length; i += size) { | ||
chunks.push(array.slice(i, i + size)); | ||
} | ||
|
||
return chunks; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should populate
notAdded
when there are errors. We don't want them to get mixed up w/ any that were not added b/c they were already in the group.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the code