Skip to content

Commit

Permalink
feat: partnered orgs added to context (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
benstoltz authored Sep 18, 2023
1 parent 51a2969 commit 7979571
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 32 deletions.
46 changes: 17 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 47 additions & 1 deletion packages/common/src/ArcGISContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IRequestOptions } from "@esri/arcgis-rest-request";
import { HubServiceStatus } from "./core";
import { getProp, getWithDefault } from "./objects";
import { HubEnvironment, HubLicense, IFeatureFlags } from "./permissions/types";
import { IHubRequestOptions } from "./types";
import { IHubRequestOptions, IHubTrustedOrgsResponse } from "./types";
import { getEnvironmentFromPortalUrl } from "./utils/getEnvironmentFromPortalUrl";

/**
Expand Down Expand Up @@ -186,6 +186,16 @@ export interface IArcGISContext {
* Hash of feature flags
*/
featureFlags: IFeatureFlags;

/**
* Array of Trusted Org Ids
*/
trustedOrgIds: string[];

/**
* Trusted orgs xhr response
*/
trustedOrgs: IHubTrustedOrgsResponse[];
}

/**
Expand Down Expand Up @@ -242,6 +252,16 @@ export interface IArcGISContextOptions {
* Hash of feature flags
*/
featureFlags?: IFeatureFlags;

/**
* Array of Trusted Org Ids
*/
trustedOrgIds?: string[];

/**
* Trusted orgs xhr response
*/
trustedOrgs?: IHubTrustedOrgsResponse[];
}

/**
Expand Down Expand Up @@ -281,6 +301,10 @@ export class ArcGISContext implements IArcGISContext {

private _featureFlags: IFeatureFlags = {};

private _trustedOrgIds: string[];

private _trustedOrgs: IHubTrustedOrgsResponse[];

/**
* Create a new instance of `ArcGISContext`.
*
Expand All @@ -306,6 +330,14 @@ export class ArcGISContext implements IArcGISContext {
this._properties = opts.properties;
}

if (opts.trustedOrgIds) {
this._trustedOrgIds = opts.trustedOrgIds;
}

if (opts.trustedOrgs) {
this._trustedOrgs = opts.trustedOrgs;
}

this._featureFlags = opts.featureFlags || {};
}

Expand Down Expand Up @@ -623,4 +655,18 @@ export class ArcGISContext implements IArcGISContext {
public get properties(): Record<string, any> {
return this._properties;
}

/**
* Returns the array of Trusted Org Ids
*/
public get trustedOrgIds(): string[] {
return this._trustedOrgIds;
}

/**
* Returns the array of Trusted Orgs
*/
public get trustedOrgs(): IHubTrustedOrgsResponse[] {
return this._trustedOrgs;
}
}
74 changes: 72 additions & 2 deletions packages/common/src/ArcGISContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { HubServiceStatus } from "./core";
import { cloneObject } from "./util";
import { base64ToUnicode, unicodeToBase64 } from "./utils/encoding";
import { IFeatureFlags } from "./permissions";
import { IHubTrustedOrgsResponse } from "./types";
import { request } from "@esri/arcgis-rest-request";
import { failSafe } from "./utils/fail-safe";

/**
* Options that can be passed into `ArcGISContextManager.create`
Expand Down Expand Up @@ -73,6 +76,16 @@ export interface IArcGISContextManagerOptions {
* Optional hash of feature flags
*/
featureFlags?: IFeatureFlags;

/**
* Array of Trusted Org Ids
*/
trustedOrgIds?: string[];

/**
* Trusted orgs xhr response
*/
trustedOrgs?: IHubTrustedOrgsResponse[];
}

/**
Expand Down Expand Up @@ -117,6 +130,10 @@ export class ArcGISContextManager {

private _featureFlags: IFeatureFlags = {};

private _trustedOrgIds: string[];

private _trustedOrgs: IHubTrustedOrgsResponse[];

/**
* Private constructor. Use `ArcGISContextManager.create(...)` to
* instantiate an instance
Expand Down Expand Up @@ -164,6 +181,14 @@ export class ArcGISContextManager {
if (opts.featureFlags) {
this._featureFlags = cloneObject(opts.featureFlags);
}

if (opts.trustedOrgIds) {
this._trustedOrgIds = cloneObject(opts.trustedOrgIds);
}

if (opts.trustedOrgs) {
this._trustedOrgs = cloneObject(opts.trustedOrgs);
}
}

/**
Expand Down Expand Up @@ -310,6 +335,12 @@ export class ArcGISContextManager {
if (this._properties) {
state.properties = this._properties;
}
if (this._trustedOrgIds) {
state.trustedOrgIds = this._trustedOrgIds;
}
if (this._trustedOrgs) {
state.trustedOrgs = this._trustedOrgs;
}

return unicodeToBase64(JSON.stringify(state));
}
Expand All @@ -323,14 +354,21 @@ export class ArcGISContextManager {
if (this._authentication && (!this._portalSelf || !this._currentUser)) {
Logger.debug(`ArcGISContextManager-${this.id}: Initializing`);
const username = this._authentication.username;
const requests: [Promise<IPortal>, Promise<IUser>] = [
const requests: [
Promise<IPortal>,
Promise<IUser>,
Promise<IHubTrustedOrgsResponse[]>
] = [
getSelf({ authentication: this._authentication }),
getUser({ username, authentication: this._authentication }),
getTrustedOrgs(this._portalUrl, this._authentication),
];
try {
const [portal, user] = await Promise.all(requests);
const [portal, user, trustedOrgs] = await Promise.all(requests);
this._portalSelf = portal;
this._currentUser = user;
this._trustedOrgs = trustedOrgs;
this._trustedOrgIds = getTrustedOrgIds(trustedOrgs);
Logger.debug(
`ArcGISContextManager-${this.id}: received portalSelf and currentUser`
);
Expand Down Expand Up @@ -372,6 +410,12 @@ export class ArcGISContextManager {
if (this._currentUser) {
contextOpts.currentUser = this._currentUser;
}
if (this._trustedOrgIds) {
contextOpts.trustedOrgIds = this._trustedOrgIds;
}
if (this._trustedOrgs) {
contextOpts.trustedOrgs = this._trustedOrgs;
}

return contextOpts;
}
Expand All @@ -390,6 +434,32 @@ function getServiceStatus(portalUrl: string): Promise<HubServiceStatus> {
return Promise.resolve(status);
}

/**
* Get trusted orgs w/ a failSafe to return an empty array
*/
async function getTrustedOrgs(
_portalUrl: string,
_authentication: UserSession
): Promise<IHubTrustedOrgsResponse[]> {
const failSafeTrustedOrgs = failSafe(request, { trustedOrgs: [] });
const trustedOrgs = await failSafeTrustedOrgs(
`${_portalUrl}/sharing/rest/portals/self/trustedOrgs?f=json`,
{
params: {
token: _authentication.token,
},
}
);
return trustedOrgs.trustedOrgs;
}

/**
* Extract trustedOrg ids from trustedOrgs response
*/
function getTrustedOrgIds(trustedOrgs: IHubTrustedOrgsResponse[]): string[] {
return trustedOrgs.map((org) => org.to.orgId);
}

const HUB_SERVICE_STATUS: HubServiceStatus = {
portal: "online",
discussions: "online",
Expand Down
15 changes: 15 additions & 0 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ export interface IHubUserRequestOptions extends IHubRequestOptions {
authentication: UserSession;
}

export interface IHubTrustedOrgsResponse {
from: IHubTrustedOrgsRelationship;
to: IHubTrustedOrgsRelationship;
}

export interface IHubTrustedOrgsRelationship {
orgId: string;
usersAccess: boolean;
established: number;
name?: string;
hub: boolean;
state: string;
[propName: string]: any;
}

export interface IItemResource {
type?: string;
url: string;
Expand Down
Loading

0 comments on commit 7979571

Please sign in to comment.