Skip to content

Commit

Permalink
rework group membership checking, fix UserSession.getUser scope issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dbouwman committed Sep 7, 2018
1 parent da410d3 commit fd4ec66
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/arcgis-rest-auth/src/UserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ export class UserSession implements IAuthenticationManager {
*/
getUser(requestOptions?: IRequestOptions): Promise<IUser> {
if (this._user && this._user.username === this.username) {
return new Promise(resolve => resolve(this._user));
return Promise.resolve(this._user);
} else {
const url = `${this.portal}/community/users/${encodeURIComponent(
this.username
Expand Down
14 changes: 8 additions & 6 deletions packages/arcgis-rest-sharing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
"types": "dist/esm/index.d.ts",
"license": "Apache-2.0",
"dependencies": {
"tslib": "^1.7.1"
"tslib": "^1.8.0"
},
"peerDependencies": {
"@esri/arcgis-rest-auth": "^1.7.1",
"@esri/arcgis-rest-common-types": "^1.7.1",
"@esri/arcgis-rest-request": "^1.7.1"
"@esri/arcgis-rest-auth": "^1.8.0",
"@esri/arcgis-rest-common-types": "^1.8.0",
"@esri/arcgis-rest-request": "^1.8.0",
"@esri/arcgis-rest-groups": "^1.8.0"
},
"devDependencies": {
"@esri/arcgis-rest-auth": "^1.8.0",
"@esri/arcgis-rest-common-types": "^1.7.1",
"@esri/arcgis-rest-request": "^1.8.0"
"@esri/arcgis-rest-common-types": "^1.8.0",
"@esri/arcgis-rest-request": "^1.8.0",
"@esri/arcgis-rest-groups": "^1.8.0"
},
"files": [
"dist/**"
Expand Down
13 changes: 8 additions & 5 deletions packages/arcgis-rest-sharing/src/group-sharing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function changeGroupSharing(
return response;
} else {
// next check to ensure the user is a member of the group
return getUserMembership(requestOptions)
return getUserMembership(requestOptions.groupId, requestOptions)
.then(membership => {
if (membership === "nonmember") {
// abort and reject promise
Expand All @@ -120,7 +120,7 @@ function changeGroupSharing(
}`;
} else {
// if they are a group admin/owner, use the bare item url
if (membership === "admin") {
if (membership === "admin" || membership === "owner") {
return `${getPortalUrl(requestOptions)}/content/items/${
requestOptions.id
}/${requestOptions.action}`;
Expand Down Expand Up @@ -179,15 +179,18 @@ function isItemSharedWithGroup(
sortField: "title"
};

// we need to append some params into requestOptions, so make a clone
// instead of mutating the params on the inbound requestOptions object
const ro = { ...requestOptions };
// instead of calling out to "@esri/arcgis-rest-items, make the request manually to forgoe another dependency
requestOptions.params = {
ro.params = {
...query,
...requestOptions.params
};

const url = `${getPortalUrl(requestOptions)}/search`;
const url = `${getPortalUrl(ro)}/search`;

return request(url, requestOptions).then(searchResponse => {
return request(url, ro).then(searchResponse => {
// if there are no search results at all, we know the item hasnt already been shared with the group
if (searchResponse.total === 0) {
return false;
Expand Down
64 changes: 28 additions & 36 deletions packages/arcgis-rest-sharing/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import {
request,
IRequestOptions,
getPortalUrl
} from "@esri/arcgis-rest-request";
import { IRequestOptions, getPortalUrl } from "@esri/arcgis-rest-request";
import { UserSession } from "@esri/arcgis-rest-auth";
import { IUser, IGroup } from "@esri/arcgis-rest-common-types";
import { IGroupSharingRequestOptions } from "./group-sharing";
import { IGroup } from "@esri/arcgis-rest-common-types";
import { getGroup } from "@esri/arcgis-rest-groups";

export interface ISharingRequestOptions extends IRequestOptions {
/**
Expand Down Expand Up @@ -44,12 +40,17 @@ export function isItemOwner(requestOptions: ISharingRequestOptions): boolean {
return owner === username;
}

/**
* Check it the user is a full org_admin
* @param requestOptions
* @returns {Promise<string>} Promise resolving in a boolean indicating if the user is a full Org Admin
*/
export function isOrgAdmin(
requestOptions: ISharingRequestOptions
): Promise<boolean> {
const session = requestOptions.authentication as UserSession;

return session.getUser(requestOptions).then(user => {
return session.getUser(requestOptions).then((user: any) => {
if (!user || user.role !== "org_admin") {
return false;
} else {
Expand All @@ -58,35 +59,26 @@ export function isOrgAdmin(
});
}

/**
* Get the User Membership for a particular group. Use this if all you have is the groupId.
* If you have the group object, check the `userMembership.memberType` property instead of calling
* this method.
*
* @export
* @param {string} groupId
* @param {IGroupSharingRequestOptions} requestOptions
* @returns {Promise<string>}
*/
export function getUserMembership(
requestOptions: IGroupSharingRequestOptions
groupId: string,
requestOptions: IRequestOptions
): Promise<string> {
// start by assuming the user does not belong to the group
let result = "nonmember";
const session = requestOptions.authentication as UserSession;

// the response to this call is cached. yay!
return session
.getUser(requestOptions)
.then((user: IUser) => {
if (user.groups) {
user.groups.some(function(group: IGroup) {
const matchedGroup = group.id === requestOptions.groupId;
if (matchedGroup) {
result = group.userMembership.memberType;
}
return matchedGroup;
});
}
return result;
// fetch the group...
return getGroup(groupId, requestOptions)
.then((group: IGroup) => {
return group.userMembership.memberType;
})
.catch(
/* istanbul ignore next */ err => {
throw Error(
`failure determining membership of ${session.username} in group:${
requestOptions.groupId
}: ${err}`
);
}
);
.catch(ex => {
return "nonmember";
});
}
Loading

0 comments on commit fd4ec66

Please sign in to comment.