UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 5.72 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../config.js";import{id as r}from"../../kernel.js";import s from"../../request.js";import{result as i}from"../../core/asyncUtils.js";import o from"../../core/Error.js";import a from"../../core/Logger.js";import{destroyMaybe as l}from"../../core/maybe.js";import{throwIfAborted as n,isAbortError as p,throwIfAbortError as u}from"../../core/promiseUtils.js";import{hasSameCanonicalPortal as c,hasSamePortal as d,normalize as m}from"../../core/urlUtils.js";import{property as h}from"../../core/accessorSupport/decorators/property.js";import"../../core/has.js";import"../../core/RandomLCG.js";import{reader as f}from"../../core/accessorSupport/decorators/reader.js";import{subclass as g}from"../../core/accessorSupport/decorators/subclass.js";import{writer as y}from"../../core/accessorSupport/decorators/writer.js";import{getOwningPortalUrl as I}from"../support/layerUtils.js";import v from"../../portal/Portal.js";import P from"../../portal/PortalItem.js";import U from"../../portal/PortalUser.js";import{getUserPrivileges as w}from"../../portal/support/portalItemUtils.js";const j=j=>{let E=class extends j{constructor(){super(...arguments),this.resourceReferences={portalItem:null,paths:[]},this.userHasEditingPrivileges=!0,this.userHasFullEditingPrivileges=!1,this.userHasUpdateItemPrivileges=!1}destroy(){this.portalItem=l(this.portalItem),this.resourceReferences.portalItem=null,this.resourceReferences.paths.length=0}set portalItem(e){e!==this._get("portalItem")&&(this.removeOrigin("portal-item"),this._set("portalItem",e))}readPortalItem(e,t,r){if(t.itemId)return new P({id:t.itemId,portal:r?.portal})}writePortalItem(e,t){e?.id&&(t.itemId=e.id)}async loadFromPortal(e,t){if(this.portalItem?.id)try{const{load:r}=await import("../../portal/support/layersLoader.js");return n(t),await r({instance:this,supportedTypes:e.supportedTypes,validateItem:e.validateItem,supportsData:e.supportsData,layerModuleTypeMap:e.layerModuleTypeMap,populateGroupLayer:e.populateGroupLayer},t)}catch(r){throw p(r)||a.getLogger(this).warn(`Failed to load layer (${this.title}, ${this.id}) portal item (${this.portalItem.id})\n ${r}`),r}}async finishLoadEditablePortalLayer(e){this._set("userHasEditingPrivileges",await this._fetchUserHasEditingPrivileges(e).catch((e=>(u(e),!0))))}async setUserPrivileges(e,r){if(!t.userPrivilegesApplied)return this.finishLoadEditablePortalLayer(r);if(this.url)try{const{features:{edit:t,fullEdit:s},content:{updateItem:i}}=await this._fetchUserPrivileges(e,r);this._set("userHasEditingPrivileges",t),this._set("userHasFullEditingPrivileges",s),this._set("userHasUpdateItemPrivileges",i)}catch(s){u(s)}}async _fetchUserPrivileges(e,t){let s=this.portalItem;if(!e||!s||!s.loaded||s.sourceUrl)return this._fetchFallbackUserPrivileges(t);const i=!r?.findCredential(this.url),o=e===s.id;if(o&&s.portal.user)return this._getUserPrivileges(s,i);let a,l;if(o)a=s.portal.url;else try{a=await I(this.url,t)}catch(m){u(m)}if(!a||!c(a,s.portal.url))return this._fetchFallbackUserPrivileges(t);try{const e=null!=t?t.signal:null;l=await(r?.getCredential(`${a}/sharing`,{prompt:!1,signal:e}))}catch(m){u(m)}const n=!0,p=!1,d=!1;if(!l)return{features:{edit:n,fullEdit:p},content:{updateItem:d}};try{if(o?await s.reload():(s=new P({id:e,portal:{url:a}}),await s.load(t)),s.portal.user)return this._getUserPrivileges(s,i)}catch(m){u(m)}return{features:{edit:n,fullEdit:p},content:{updateItem:d}}}_getUserPrivileges(e,t){const r=w(e);return t&&(r.features.edit=!0),r}async _fetchFallbackUserPrivileges(e){let t=!0;try{t=await this._fetchUserHasEditingPrivileges(e)}catch(r){u(r)}return{features:{edit:t,fullEdit:!1},content:{updateItem:!1}}}async _fetchUserHasEditingPrivileges(e){const t=this.url?r?.findCredential(this.url):null;if(!t)return!0;const s=_.credential===t?_.user:await this._fetchEditingUser(e);return _.credential=t,_.user=s,null==s?.privileges||s.privileges.includes("features:user:edit")}async _fetchEditingUser(e){const t=this.portalItem?.portal?.user;if(t)return t;const o=r?.findServerInfo(this.url??"");if(!o?.owningSystemUrl)return null;const a=`${o.owningSystemUrl}/sharing/rest`,l=v.getDefault();if(l&&l.loaded&&m(l.restUrl)===m(a))return l.user;const n=`${a}/community/self`,p=null!=e?e.signal:null,u=await i(s(n,{authMode:"no-prompt",query:{f:"json"},signal:p}));return u.ok?U.fromJSON(u.value.data):null}read(e,t){t&&(t.layer=this),super.read(e,t)}write(e,t){const r=t?.portal,s=this.portalItem?.id&&(this.portalItem.portal||v.getDefault());return r&&s&&!d(s.restUrl,r.restUrl)?(t.messages&&t.messages.push(new o("layer:cross-portal",`The layer '${this.title} (${this.id})' cannot be persisted because it refers to an item on a different portal than the one being saved to. To save, set layer.portalItem to null or save to the same portal as the item associated with the layer`,{layer:this})),null):super.write(e,{...t,layer:this})}};return e([h({type:P})],E.prototype,"portalItem",null),e([f("web-document","portalItem",["itemId"])],E.prototype,"readPortalItem",null),e([y("web-document","portalItem",{itemId:{type:String}})],E.prototype,"writePortalItem",null),e([h({clonable:!1})],E.prototype,"resourceReferences",void 0),e([h({type:Boolean,readOnly:!0})],E.prototype,"userHasEditingPrivileges",void 0),e([h({type:Boolean,readOnly:!0})],E.prototype,"userHasFullEditingPrivileges",void 0),e([h({type:Boolean,readOnly:!0})],E.prototype,"userHasUpdateItemPrivileges",void 0),E=e([g("esri.layers.mixins.PortalLayer")],E),E},_={credential:null,user:null};export{j as PortalLayer};