-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(caring): methods to un/share items with groups
AFFECTS PACKAGES: @esri/arcgis-rest-sharing @esri/arcgis-rest-auth
- Loading branch information
Showing
11 changed files
with
893 additions
and
45 deletions.
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
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,28 @@ | ||
import { UserSession } from "./UserSession"; | ||
|
||
import { IUserRequestOptions } from "./authenticated-request-options"; | ||
import { getPortalUrl, request } from "@esri/arcgis-rest-request"; | ||
|
||
export interface IUserInfo { | ||
role?: string; | ||
} | ||
|
||
/** | ||
* Used internally by packages for requests that require user authentication. | ||
*/ | ||
export function getUserInfo(session: UserSession): Promise<IUserInfo> { | ||
if (session.userInfo) { | ||
return new Promise(resolve => resolve(session.userInfo)); | ||
} else { | ||
const url = `${session.portal}/community/users/${encodeURIComponent( | ||
session.username | ||
)}`; | ||
return request(url, { | ||
authentication: session, | ||
httpMethod: "GET" | ||
}).then(response => { | ||
session.userInfo = response; | ||
return response; | ||
}); | ||
} | ||
} |
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
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,206 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { | ||
request, | ||
IRequestOptions, | ||
getPortalUrl | ||
} from "@esri/arcgis-rest-request"; | ||
|
||
import { | ||
ISharingRequestOptions, | ||
ISharingResponse, | ||
isOrgAdmin, | ||
getUserMembership | ||
} from "./helpers"; | ||
|
||
export interface IGroupSharingRequestOptions extends ISharingRequestOptions { | ||
/** | ||
* Group identifier | ||
*/ | ||
groupId: string; | ||
confirmItemControl?: boolean; | ||
} | ||
|
||
interface IGroupSharingUnsharingRequestOptions | ||
extends IGroupSharingRequestOptions { | ||
action: "share" | "unshare"; | ||
} | ||
|
||
/** | ||
* Share an item with a group. | ||
* | ||
* ```js | ||
* import { shareItemWithGroup } from '@esri/arcgis-rest-sharing'; | ||
* | ||
* shareItemWithGroup({ | ||
* id: "abc123", | ||
* groupId: "xyz987", | ||
* authentication: session | ||
* }) | ||
* ``` | ||
* | ||
* @param requestOptions - Options for the request. | ||
* @returns A Promise that will resolve with the data from the response. | ||
*/ | ||
export function shareItemWithGroup( | ||
requestOptions: IGroupSharingRequestOptions | ||
): Promise<ISharingResponse> { | ||
return changeGroupSharing({ action: "share", ...requestOptions }); | ||
} | ||
|
||
/** | ||
* Stop sharing an item with a group. | ||
* | ||
* ```js | ||
* import { unshareItemWithGroup } from '@esri/arcgis-rest-sharing'; | ||
* | ||
* unshareItemWithGroup({ | ||
* id: "abc123", | ||
* groupId: "xyz987", | ||
* authentication: session | ||
* }) | ||
* ``` | ||
* | ||
* @param requestOptions - Options for the request. | ||
* @returns A Promise that will resolve with the data from the response. | ||
*/ | ||
export function unshareItemWithGroup( | ||
requestOptions: IGroupSharingRequestOptions | ||
): Promise<ISharingResponse> { | ||
return changeGroupSharing({ action: "unshare", ...requestOptions }); | ||
} | ||
|
||
/** | ||
* @param requestOptions - Options for the request. | ||
* @returns A Promise that will resolve with the data from the response. | ||
*/ | ||
function changeGroupSharing( | ||
requestOptions: IGroupSharingUnsharingRequestOptions | ||
): Promise<ISharingResponse> { | ||
const username = requestOptions.authentication.username; | ||
const owner = requestOptions.owner || username; | ||
|
||
return isOrgAdmin(requestOptions).then(admin => { | ||
const resultProp = | ||
requestOptions.action === "share" ? "notSharedWith" : "notUnsharedFrom"; | ||
// check if the item has already been shared with the group... | ||
return isItemSharedWithGroup(requestOptions).then(result => { | ||
// console.log(admin); | ||
// if we are sharing and result is true OR we are unsharing and result is false... short circuit | ||
if ( | ||
(requestOptions.action === "share" && result === true) || | ||
(requestOptions.action === "unshare" && result === false) | ||
) { | ||
// and send back the same response structure ArcGIS Online would | ||
const response = { itemId: requestOptions.id, shortcut: true } as any; | ||
response[resultProp] = []; | ||
return response; | ||
} else { | ||
// next check to ensure the user is a member of the group | ||
return getUserMembership(requestOptions) | ||
.then(membership => { | ||
if (membership === "nonmember") { | ||
// abort and reject promise | ||
throw Error( | ||
`This item can not be ${ | ||
requestOptions.action | ||
}d by ${username} as they are not a member of the specified group ${ | ||
requestOptions.groupId | ||
}.` | ||
); | ||
} else { | ||
// if orgAdmin or owner (and member of group) share using the owner url | ||
if (owner === username || admin) { | ||
return `${getPortalUrl( | ||
requestOptions | ||
)}/content/users/${owner}/items/${requestOptions.id}/${ | ||
requestOptions.action | ||
}`; | ||
} else { | ||
// if they are a group admin/owner, use the bare item url | ||
if (membership === "admin") { | ||
return `${getPortalUrl(requestOptions)}/content/items/${ | ||
requestOptions.id | ||
}/${requestOptions.action}`; | ||
} else { | ||
// otherwise abort | ||
throw Error( | ||
`This item can not be ${ | ||
requestOptions.action | ||
}d by ${username} as they are neither the owner, a groupAdmin of ${ | ||
requestOptions.groupId | ||
}, nor an org_admin.` | ||
); | ||
} | ||
} | ||
} | ||
}) | ||
.then(url => { | ||
// now its finally time to do the sharing | ||
requestOptions.params = { | ||
groups: requestOptions.groupId, | ||
confirmItemControl: requestOptions.confirmItemControl | ||
}; | ||
// dont mixin to ensure that old query parameters from the search request arent included | ||
return request(url, requestOptions); | ||
}) | ||
.then(sharingResponse => { | ||
if (sharingResponse[resultProp].length) { | ||
throw Error( | ||
`Item ${requestOptions.id} could not be ${ | ||
requestOptions.action | ||
}d to group ${requestOptions.groupId}.` | ||
); | ||
} else { | ||
// all is well | ||
return sharingResponse; | ||
} | ||
}); | ||
} // else | ||
}); // then | ||
}); | ||
} | ||
|
||
/** | ||
* Find out whether or not an item is already shared with a group. | ||
* | ||
* @param requestOptions - Options for the request. | ||
* @returns A Promise that will resolve with the data from the response. | ||
*/ | ||
function isItemSharedWithGroup( | ||
requestOptions: IGroupSharingRequestOptions | ||
): Promise<boolean> { | ||
const query = { | ||
q: `id: ${requestOptions.id} AND group: ${requestOptions.groupId}`, | ||
start: 1, | ||
num: 10, | ||
sortField: "title" | ||
}; | ||
|
||
// instead of calling out to "@esri/arcgis-rest-items, make the request manually to forgoe another dependency | ||
requestOptions.params = { | ||
...query, | ||
...requestOptions.params | ||
}; | ||
|
||
const url = `${getPortalUrl(requestOptions)}/search`; | ||
|
||
return request(url, requestOptions).then(searchResult => { | ||
// if there are no search results at all, we know the item hasnt already been shared with the group | ||
if (searchResult.total === 0) { | ||
return false; | ||
} else { | ||
// otherwise loop through and search for the id | ||
const results = searchResult.results; | ||
const itm = results.find((shadowedItm: { id: string }) => { | ||
return shadowedItm.id === requestOptions.id; | ||
}); | ||
if (itm) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
}); | ||
} |
Oops, something went wrong.