UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 5.55 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";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 a from"../../core/Error.js";import o 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 d,hasSamePortal as c,normalize as m}from"../../core/urlUtils.js";import{property as h,subclass as f}from"../../core/accessorSupport/decorators.js";import{getOwningPortalUrl as g}from"../support/layerUtils.js";import y from"../../portal/Portal.js";import I from"../../portal/PortalItem.js";import v from"../../portal/PortalUser.js";import{getUserPrivileges as P}from"../../portal/support/portalItemUtils.js";import{reader as U}from"../../core/accessorSupport/decorators/reader.js";import{writer as w}from"../../core/accessorSupport/decorators/writer.js";const _=_=>{const E=_;let b=class extends E{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}get portalItem(){return this._get("portalItem")}set portalItem(e){e!==this._get("portalItem")&&(this.removeOrigin("portal-item"),this._set("portalItem",e))}readPortalItem(e,t,r){if(t.itemId)return new I({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)||o.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),a=e===s.id;if(a&&s.portal.user)return this._getUserPrivileges(s,i);let o,l;if(a)o=s.portal.url;else try{o=await g(this.url,t)}catch(m){u(m)}if(!o||!d(o,s.portal.url))return this._fetchFallbackUserPrivileges(t);try{const e=null!=t?t.signal:null;l=await(r?.getCredential(`${o}/sharing`,{prompt:!1,signal:e}))}catch(m){u(m)}const n=!0,p=!1,c=!1;if(!l)return{features:{edit:n,fullEdit:p},content:{updateItem:c}};try{if(a?await s.reload():(s=new I({id:e,portal:{url:o}}),await s.load(t)),s.portal.user)return this._getUserPrivileges(s,i)}catch(m){u(m)}return{features:{edit:n,fullEdit:p},content:{updateItem:c}}}_getUserPrivileges(e,t){const r=P(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=j.credential===t?j.user:await this._fetchEditingUser(e);return j.credential=t,j.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 a=r?.findServerInfo(this.url??"");if(!a?.owningSystemUrl)return null;const o=`${a.owningSystemUrl}/sharing/rest`,l=y.getDefault();if(l&&l.loaded&&m(l.restUrl)===m(o))return l.user;const n=`${o}/community/self`,p=null!=e?e.signal:null,u=await i(s(n,{authMode:"no-prompt",query:{f:"json"},signal:p}));return u.ok?v.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||y.getDefault());return r&&s&&!c(s.restUrl,r.restUrl)?(t.messages&&t.messages.push(new a("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:I})],b.prototype,"portalItem",null),e([U("web-document","portalItem",["itemId"])],b.prototype,"readPortalItem",null),e([w("web-document","portalItem",{itemId:{type:String}})],b.prototype,"writePortalItem",null),e([h({clonable:!1})],b.prototype,"resourceReferences",void 0),e([h({type:Boolean,readOnly:!0})],b.prototype,"userHasEditingPrivileges",void 0),e([h({type:Boolean,readOnly:!0})],b.prototype,"userHasFullEditingPrivileges",void 0),e([h({type:Boolean,readOnly:!0})],b.prototype,"userHasUpdateItemPrivileges",void 0),b=e([f("esri.layers.mixins.PortalLayer")],b),b},j={credential:null,user:null};export{_ as PortalLayer};