Skip to content
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

fix: auto-populating groups in project creation #1225

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions packages/common/src/projects/HubProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import { cloneObject, maybePush } from "../util";
import { createProject, editorToProject, updateProject } from "./edit";
import { ProjectEditorType } from "./_internal/ProjectSchema";
import { enrichEntity } from "../core/enrichEntity";
import { getProp } from "../objects";
import { IGroup } from "@esri/arcgis-rest-types";

/**
* Hub Project Class
Expand Down Expand Up @@ -202,17 +204,30 @@ export class HubProject
* on project creation, we want to pre-populate the sharing
* field with the core and collaboration groups if the user
* has the appropriate shareToGroup portal privilege
*
* TODO: we should re-evaluate this "auto-populate" pattern
*/
const { access: canShare } = this.checkPermission(
"platform:portal:user:shareToGroup"
);
if (!editor.id && canShare) {
// TODO: at what point can we remove this "auto-share" behavior?
editor._groups = maybePush(editorContext.contentGroupId, editor._groups);
editor._groups = maybePush(
const currentUserGroups: IGroup[] =
getProp(this.context, "currentUser.groups") || [];
const defaultShareWithGroups = [
editorContext.contentGroupId,
editorContext.collaborationGroupId,
editor._groups
);
].reduce((acc, groupId) => {
const group = currentUserGroups.find((g: IGroup) => g.id === groupId);
const canShareToGroup =
!!group &&
(!group.isViewOnly ||
(group.isViewOnly &&
["owner", "admin"].includes(group.memberType)));

canShareToGroup && acc.push(groupId);
return acc;
}, []);
editor._groups = [...editor._groups, ...defaultShareWithGroups];
}

return editor;
Expand Down
72 changes: 57 additions & 15 deletions packages/common/test/projects/HubProject.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as PortalModule from "@esri/arcgis-rest-portal";
import { IHubProject, IResolvedMetric, getProp } from "../../src";
import {
IHubProject,
IResolvedMetric,
cloneObject,
getProp,
mergeObjects,
} from "../../src";
import { Catalog } from "../../src/search";
import { ArcGISContextManager } from "../../src/ArcGISContextManager";
import { HubProject } from "../../src/projects/HubProject";
Expand All @@ -12,6 +18,25 @@ import * as ResolveMetricModule from "../../src/metrics/resolveMetric";
import { HubItemEntity } from "../../src/core/HubItemEntity";
import * as EnrichEntityModule from "../../src/core/enrichEntity";

const initContextManager = async (opts = {}) => {
const defaults = {
authentication: MOCK_AUTH,
currentUser: {
username: "casey",
privileges: ["portal:user:shareToGroup"],
} as unknown as PortalModule.IUser,
portal: {
name: "DC R&D Center",
id: "BRXFAKE",
urlKey: "fake-org",
} as unknown as PortalModule.IPortal,
portalUrl: "https://myserver.com",
};
return await ArcGISContextManager.create(
mergeObjects(opts, defaults, ["currentUser"])
);
};

describe("HubProject Class:", () => {
let authdCtxMgr: ArcGISContextManager;
let unauthdCtxMgr: ArcGISContextManager;
Expand All @@ -20,19 +45,7 @@ describe("HubProject Class:", () => {
// When we pass in all this information, the context
// manager will not try to fetch anything, so no need
// to mock those calls
authdCtxMgr = await ArcGISContextManager.create({
authentication: MOCK_AUTH,
currentUser: {
username: "casey",
privileges: ["portal:user:shareToGroup"],
} as unknown as PortalModule.IUser,
portal: {
name: "DC R&D Center",
id: "BRXFAKE",
urlKey: "fake-org",
} as unknown as PortalModule.IPortal,
portalUrl: "https://myserver.com",
});
authdCtxMgr = await initContextManager();
});

describe("static methods:", () => {
Expand Down Expand Up @@ -363,7 +376,7 @@ describe("HubProject Class:", () => {

expect(enrichEntitySpy).toHaveBeenCalledTimes(1);
});
it("toEditor converst entity to correct structure", async () => {
it("toEditor converts entity to correct structure", async () => {
const chk = HubProject.fromJson(
{
id: "bc3",
Expand All @@ -381,6 +394,35 @@ describe("HubProject Class:", () => {
);
expect(result._groups).toEqual([]);
});
describe('auto-populating "shareWith" groups', () => {
let projectInstance: any;
beforeEach(async () => {
const _authdCtxMgr = await initContextManager({
currentUser: {
groups: [
{ id: "00a", isViewOnly: false },
{ id: "00b", isViewOnly: true, memberType: "admin" },
{ id: "00d", isViewOnly: false },
] as PortalModule.IGroup[],
},
});
projectInstance = HubProject.fromJson({}, _authdCtxMgr.context);
});
it('handles auto-populating "shareWith" groups that the current user can share to', async () => {
const result = await projectInstance.toEditor({
contentGroupId: "00a",
collaborationGroupId: "00b",
});
expect(result._groups).toEqual(["00a", "00b"]);
});
it('does not auto-populate "shareWith" gruops that the current user cannot share to', async () => {
const result = await projectInstance.toEditor({
contentGroupId: "00e",
collaborationGroupId: "00f",
});
expect(result._groups).toEqual([]);
});
});
});

describe("fromEditor:", () => {
Expand Down