etincidunt 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (226) hide show
  1. package/.prettierignore +2 -0
  2. package/.travis.yml +29 -0
  3. package/.vscode/launch.json +24 -0
  4. package/.vscode/settings.json +3 -0
  5. package/demos/ago-node-cli/README.md +29 -0
  6. package/demos/ago-node-cli/ago.js +32 -0
  7. package/demos/ago-node-cli/index.js +11 -0
  8. package/demos/ago-node-cli/lib/item-export-command.js +48 -0
  9. package/demos/ago-node-cli/lib/item-search-command.js +35 -0
  10. package/demos/ago-node-cli/package-lock.json +172 -0
  11. package/demos/ago-node-cli/package.json +30 -0
  12. package/demos/attachments/README.md +5 -0
  13. package/demos/attachments/index.html +164 -0
  14. package/demos/attachments/package-lock.json +182 -0
  15. package/demos/attachments/package.json +18 -0
  16. package/demos/batch-geocoder-node/NYC_Restaurant_Inspection_Results.csv +100 -0
  17. package/demos/batch-geocoder-node/README.md +14 -0
  18. package/demos/batch-geocoder-node/batch-geocode.js +112 -0
  19. package/demos/batch-geocoder-node/config-template.js +18 -0
  20. package/demos/batch-geocoder-node/package-lock.json +109 -0
  21. package/demos/batch-geocoder-node/package.json +37 -0
  22. package/demos/express/README.md +10 -0
  23. package/demos/express/config.json.template +3 -0
  24. package/demos/express/package-lock.json +388 -0
  25. package/demos/express/package.json +18 -0
  26. package/demos/express/server.js +28 -0
  27. package/demos/feature-service-browser/README.md +6 -0
  28. package/demos/feature-service-browser/index.html +122 -0
  29. package/demos/feature-service-browser/package-lock.json +182 -0
  30. package/demos/feature-service-browser/package.json +18 -0
  31. package/demos/geocoder-browser/README.md +10 -0
  32. package/demos/geocoder-browser/config.js.template +1 -0
  33. package/demos/geocoder-browser/index.html +131 -0
  34. package/demos/geocoder-browser/package-lock.json +163 -0
  35. package/demos/geocoder-browser/package.json +19 -0
  36. package/demos/geocoder-browser/post-sign-in.html +25 -0
  37. package/demos/jsapi-integration/README.md +8 -0
  38. package/demos/jsapi-integration/config.js +6 -0
  39. package/demos/jsapi-integration/index.html +79 -0
  40. package/demos/jsapi-integration/package-lock.json +184 -0
  41. package/demos/jsapi-integration/package.json +19 -0
  42. package/demos/oauth2-browser/README.md +12 -0
  43. package/demos/oauth2-browser/authenticate.html +32 -0
  44. package/demos/oauth2-browser/config.js.template +6 -0
  45. package/demos/oauth2-browser/index.html +202 -0
  46. package/demos/oauth2-browser/logo.svg +4 -0
  47. package/demos/oauth2-browser/package-lock.json +163 -0
  48. package/demos/oauth2-browser/package.json +18 -0
  49. package/demos/oauth2-browser/style.css +36 -0
  50. package/demos/vue/.babelrc +6 -0
  51. package/demos/vue/.env.example +8 -0
  52. package/demos/vue/README.md +17 -0
  53. package/demos/vue/index.html +21 -0
  54. package/demos/vue/package-lock.json +7236 -0
  55. package/demos/vue/package.json +39 -0
  56. package/demos/vue/src/assets/logo.svg +29 -0
  57. package/demos/vue/src/components/App.vue +302 -0
  58. package/demos/vue/src/components/Authenticate.vue +68 -0
  59. package/demos/vue/src/components/Loader.vue +216 -0
  60. package/demos/vue/src/main.js +75 -0
  61. package/demos/vue/webpack.config.js +84 -0
  62. package/docs/FAQ.md +28 -0
  63. package/docs/HISTORY.md +62 -0
  64. package/docs/acetate.config.js +214 -0
  65. package/docs/build-typedoc.js +301 -0
  66. package/docs/src/_layout.html +82 -0
  67. package/docs/src/api/_declaration.html +496 -0
  68. package/docs/src/api/_layout.html +127 -0
  69. package/docs/src/api/_package.html +13 -0
  70. package/docs/src/api/index.html +23 -0
  71. package/docs/src/guides/_layout.html +24 -0
  72. package/docs/src/guides/amd-requirejs-dojo.md +40 -0
  73. package/docs/src/guides/babel-and-rollup.md +30 -0
  74. package/docs/src/guides/babel-and-webpack.md +30 -0
  75. package/docs/src/guides/browser-authentication.md +9 -0
  76. package/docs/src/guides/browserify.md +9 -0
  77. package/docs/src/guides/cli-authentication.md +9 -0
  78. package/docs/src/guides/client-server-authentication.md +9 -0
  79. package/docs/src/guides/from-a-cdn.md +36 -0
  80. package/docs/src/guides/index.md +52 -0
  81. package/docs/src/guides/node.md +30 -0
  82. package/docs/src/guides/package-overview.md +8 -0
  83. package/docs/src/guides/server-authentication.md +9 -0
  84. package/docs/src/guides/typescript-and-webpack.md +9 -0
  85. package/docs/src/img/icons.png +0 -0
  86. package/docs/src/img/icons@2x.png +0 -0
  87. package/docs/src/index.html +12 -0
  88. package/docs/src/js/api-search.js +113 -0
  89. package/docs/src/js/index.js +1 -0
  90. package/docs/src/js/nav-toggle.js +41 -0
  91. package/docs/src/sass/_highlight.scss +96 -0
  92. package/docs/src/sass/_icons.scss +157 -0
  93. package/docs/src/sass/style.scss +169 -0
  94. package/jasmine.json +7 -0
  95. package/karma.conf.js +100 -0
  96. package/lerna.json +8 -0
  97. package/notes/README.md +88 -0
  98. package/package.json +91 -0
  99. package/packages/arcgis-rest-auth/README.md +64 -0
  100. package/packages/arcgis-rest-auth/package-lock.json +11 -0
  101. package/packages/arcgis-rest-auth/package.json +51 -0
  102. package/packages/arcgis-rest-auth/src/ApplicationSession.ts +91 -0
  103. package/packages/arcgis-rest-auth/src/UserSession.ts +829 -0
  104. package/packages/arcgis-rest-auth/src/authenticated-request-options.ts +21 -0
  105. package/packages/arcgis-rest-auth/src/fetch-token.ts +55 -0
  106. package/packages/arcgis-rest-auth/src/generate-token.ts +36 -0
  107. package/packages/arcgis-rest-auth/src/index.ts +5 -0
  108. package/packages/arcgis-rest-auth/test/ApplicationSession.test.ts +121 -0
  109. package/packages/arcgis-rest-auth/test/UserSession.test.ts +883 -0
  110. package/packages/arcgis-rest-auth/test/fetchToken.test.ts +76 -0
  111. package/packages/arcgis-rest-auth/test/generateToken.test.ts +36 -0
  112. package/packages/arcgis-rest-auth/test/utils.ts +11 -0
  113. package/packages/arcgis-rest-auth/tsconfig.json +6 -0
  114. package/packages/arcgis-rest-common-types/README.md +61 -0
  115. package/packages/arcgis-rest-common-types/package.json +38 -0
  116. package/packages/arcgis-rest-common-types/src/group.ts +51 -0
  117. package/packages/arcgis-rest-common-types/src/index.ts +467 -0
  118. package/packages/arcgis-rest-common-types/src/item.ts +45 -0
  119. package/packages/arcgis-rest-common-types/src/webmap.ts +1225 -0
  120. package/packages/arcgis-rest-common-types/tsconfig.json +11 -0
  121. package/packages/arcgis-rest-feature-service/README.md +70 -0
  122. package/packages/arcgis-rest-feature-service/package-lock.json +11 -0
  123. package/packages/arcgis-rest-feature-service/package.json +50 -0
  124. package/packages/arcgis-rest-feature-service/src/add.ts +82 -0
  125. package/packages/arcgis-rest-feature-service/src/addAttachment.ts +65 -0
  126. package/packages/arcgis-rest-feature-service/src/delete.ts +85 -0
  127. package/packages/arcgis-rest-feature-service/src/deleteAttachments.ts +68 -0
  128. package/packages/arcgis-rest-feature-service/src/getAttachments.ts +64 -0
  129. package/packages/arcgis-rest-feature-service/src/helpers.ts +77 -0
  130. package/packages/arcgis-rest-feature-service/src/index.ts +8 -0
  131. package/packages/arcgis-rest-feature-service/src/query.ts +174 -0
  132. package/packages/arcgis-rest-feature-service/src/update.ts +81 -0
  133. package/packages/arcgis-rest-feature-service/src/updateAttachment.ts +74 -0
  134. package/packages/arcgis-rest-feature-service/test/attachments.test.ts +179 -0
  135. package/packages/arcgis-rest-feature-service/test/features.test.ts +172 -0
  136. package/packages/arcgis-rest-feature-service/test/mocks/feature.ts +220 -0
  137. package/packages/arcgis-rest-feature-service/test/mocks/foo.txt +1 -0
  138. package/packages/arcgis-rest-feature-service/tsconfig.json +6 -0
  139. package/packages/arcgis-rest-geocoder/README.md +73 -0
  140. package/packages/arcgis-rest-geocoder/package-lock.json +11 -0
  141. package/packages/arcgis-rest-geocoder/package.json +52 -0
  142. package/packages/arcgis-rest-geocoder/src/bulk.ts +102 -0
  143. package/packages/arcgis-rest-geocoder/src/geocode.ts +117 -0
  144. package/packages/arcgis-rest-geocoder/src/helpers.ts +81 -0
  145. package/packages/arcgis-rest-geocoder/src/index.ts +4 -0
  146. package/packages/arcgis-rest-geocoder/src/reverse.ts +84 -0
  147. package/packages/arcgis-rest-geocoder/src/suggest.ts +72 -0
  148. package/packages/arcgis-rest-geocoder/test/geocoder.test.ts +510 -0
  149. package/packages/arcgis-rest-geocoder/test/mocks/responses.ts +588 -0
  150. package/packages/arcgis-rest-geocoder/tsconfig.json +6 -0
  151. package/packages/arcgis-rest-groups/README.md +64 -0
  152. package/packages/arcgis-rest-groups/package-lock.json +11 -0
  153. package/packages/arcgis-rest-groups/package.json +52 -0
  154. package/packages/arcgis-rest-groups/src/groups.ts +272 -0
  155. package/packages/arcgis-rest-groups/src/index.ts +1 -0
  156. package/packages/arcgis-rest-groups/test/groups.test.ts +280 -0
  157. package/packages/arcgis-rest-groups/test/mocks/responses.ts +137 -0
  158. package/packages/arcgis-rest-groups/tsconfig.json +6 -0
  159. package/packages/arcgis-rest-items/README.md +66 -0
  160. package/packages/arcgis-rest-items/package-lock.json +11 -0
  161. package/packages/arcgis-rest-items/package.json +52 -0
  162. package/packages/arcgis-rest-items/src/index.ts +1 -0
  163. package/packages/arcgis-rest-items/src/items.ts +498 -0
  164. package/packages/arcgis-rest-items/test/items.test.ts +1153 -0
  165. package/packages/arcgis-rest-items/test/mocks/foo.zip +0 -0
  166. package/packages/arcgis-rest-items/test/mocks/item.ts +30 -0
  167. package/packages/arcgis-rest-items/test/mocks/resources.ts +28 -0
  168. package/packages/arcgis-rest-items/test/mocks/search.ts +60 -0
  169. package/packages/arcgis-rest-items/tsconfig.json +6 -0
  170. package/packages/arcgis-rest-request/README.md +65 -0
  171. package/packages/arcgis-rest-request/package-lock.json +11 -0
  172. package/packages/arcgis-rest-request/package.json +42 -0
  173. package/packages/arcgis-rest-request/src/index.ts +10 -0
  174. package/packages/arcgis-rest-request/src/request.ts +259 -0
  175. package/packages/arcgis-rest-request/src/utils/ArcGISAuthError.ts +67 -0
  176. package/packages/arcgis-rest-request/src/utils/ArcGISRequestError.ts +73 -0
  177. package/packages/arcgis-rest-request/src/utils/ErrorTypes.ts +29 -0
  178. package/packages/arcgis-rest-request/src/utils/check-for-errors.ts +65 -0
  179. package/packages/arcgis-rest-request/src/utils/encode-form-data.ts +29 -0
  180. package/packages/arcgis-rest-request/src/utils/encode-query-string.ts +23 -0
  181. package/packages/arcgis-rest-request/src/utils/get-portal-url.ts +25 -0
  182. package/packages/arcgis-rest-request/src/utils/get-portal.ts +45 -0
  183. package/packages/arcgis-rest-request/src/utils/process-params.ts +99 -0
  184. package/packages/arcgis-rest-request/test/mocks/errors.ts +59 -0
  185. package/packages/arcgis-rest-request/test/mocks/geojson-feature-collection.ts +10 -0
  186. package/packages/arcgis-rest-request/test/mocks/portal.ts +109 -0
  187. package/packages/arcgis-rest-request/test/mocks/sharing-rest-info.ts +38 -0
  188. package/packages/arcgis-rest-request/test/mocks/webmap.ts +38 -0
  189. package/packages/arcgis-rest-request/test/request.test.ts +296 -0
  190. package/packages/arcgis-rest-request/test/utils/ArcGISAuthError.test.ts +167 -0
  191. package/packages/arcgis-rest-request/test/utils/ArcGISRequestError.test.ts +40 -0
  192. package/packages/arcgis-rest-request/test/utils/check-for-errors.test.ts +101 -0
  193. package/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts +112 -0
  194. package/packages/arcgis-rest-request/test/utils/get-portal-url.test.ts +34 -0
  195. package/packages/arcgis-rest-request/test/utils/portal.test.ts +94 -0
  196. package/packages/arcgis-rest-request/test/utils/process-params.test.ts +190 -0
  197. package/packages/arcgis-rest-request/tsconfig.json +6 -0
  198. package/packages/arcgis-rest-sharing/README.md +67 -0
  199. package/packages/arcgis-rest-sharing/package-lock.json +11 -0
  200. package/packages/arcgis-rest-sharing/package.json +55 -0
  201. package/packages/arcgis-rest-sharing/src/access.ts +91 -0
  202. package/packages/arcgis-rest-sharing/src/group-sharing.ts +212 -0
  203. package/packages/arcgis-rest-sharing/src/helpers.ts +92 -0
  204. package/packages/arcgis-rest-sharing/src/index.ts +2 -0
  205. package/packages/arcgis-rest-sharing/test/access.test.ts +153 -0
  206. package/packages/arcgis-rest-sharing/test/group-sharing.test.ts +436 -0
  207. package/packages/arcgis-rest-sharing/test/mocks/sharing.ts +15 -0
  208. package/packages/arcgis-rest-sharing/tsconfig.json +6 -0
  209. package/packages/arcgis-rest-users/README.md +71 -0
  210. package/packages/arcgis-rest-users/package-lock.json +11 -0
  211. package/packages/arcgis-rest-users/package.json +51 -0
  212. package/packages/arcgis-rest-users/src/index.ts +1 -0
  213. package/packages/arcgis-rest-users/src/users.ts +70 -0
  214. package/packages/arcgis-rest-users/test/mocks/responses.ts +170 -0
  215. package/packages/arcgis-rest-users/test/users.test.ts +97 -0
  216. package/packages/arcgis-rest-users/tsconfig.json +6 -0
  217. package/support/FormData.d.ts +1 -0
  218. package/support/changelog.js +388 -0
  219. package/support/commit-template.txt +19 -0
  220. package/support/deploy-doc-site.js +16 -0
  221. package/support/publish.sh +40 -0
  222. package/support/test-helpers.js +8 -0
  223. package/tsconfig.json +69 -0
  224. package/tslint.json +14 -0
  225. package/umd-base-profile.js +82 -0
  226. package/umd-production-profile.js +13 -0
@@ -0,0 +1,212 @@
1
+ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
2
+ * Apache-2.0 */
3
+
4
+ import {
5
+ request,
6
+ IRequestOptions,
7
+ getPortalUrl
8
+ } from "@esri/arcgis-rest-request";
9
+ import { IItem } from "@esri/arcgis-rest-common-types";
10
+
11
+ import {
12
+ ISharingRequestOptions,
13
+ ISharingResponse,
14
+ isOrgAdmin,
15
+ getUserMembership
16
+ } from "./helpers";
17
+
18
+ export interface IGroupSharingRequestOptions extends ISharingRequestOptions {
19
+ /**
20
+ * Group identifier
21
+ */
22
+ groupId: string;
23
+ confirmItemControl?: boolean;
24
+ }
25
+
26
+ interface IGroupSharingUnsharingRequestOptions
27
+ extends IGroupSharingRequestOptions {
28
+ action: "share" | "unshare";
29
+ }
30
+
31
+ /**
32
+ * Share an item with a group.
33
+ *
34
+ * ```js
35
+ * import { shareItemWithGroup } from '@esri/arcgis-rest-sharing';
36
+ *
37
+ * shareItemWithGroup({
38
+ * id: "abc123",
39
+ * groupId: "xyz987",
40
+ * authentication: session
41
+ * })
42
+ * ```
43
+ *
44
+ * @param requestOptions - Options for the request.
45
+ * @returns A Promise that will resolve with the data from the response.
46
+ */
47
+ export function shareItemWithGroup(
48
+ requestOptions: IGroupSharingRequestOptions
49
+ ): Promise<ISharingResponse> {
50
+ return changeGroupSharing({ action: "share", ...requestOptions });
51
+ }
52
+
53
+ /**
54
+ * Stop sharing an item with a group.
55
+ *
56
+ * ```js
57
+ * import { unshareItemWithGroup } from '@esri/arcgis-rest-sharing';
58
+ *
59
+ * unshareItemWithGroup({
60
+ * id: "abc123",
61
+ * groupId: "xyz987",
62
+ * authentication: session
63
+ * })
64
+ * ```
65
+ *
66
+ * @param requestOptions - Options for the request.
67
+ * @returns A Promise that will resolve with the data from the response.
68
+ */
69
+ export function unshareItemWithGroup(
70
+ requestOptions: IGroupSharingRequestOptions
71
+ ): Promise<ISharingResponse> {
72
+ return changeGroupSharing({ action: "unshare", ...requestOptions });
73
+ }
74
+
75
+ /**
76
+ * @param requestOptions - Options for the request.
77
+ * @returns A Promise that will resolve with the data from the response.
78
+ */
79
+ function changeGroupSharing(
80
+ requestOptions: IGroupSharingUnsharingRequestOptions
81
+ ): Promise<ISharingResponse> {
82
+ const username = requestOptions.authentication.username;
83
+ const owner = requestOptions.owner || username;
84
+
85
+ return isOrgAdmin(requestOptions).then(admin => {
86
+ const resultProp =
87
+ requestOptions.action === "share" ? "notSharedWith" : "notUnsharedFrom";
88
+ // check if the item has already been shared with the group...
89
+ return isItemSharedWithGroup(requestOptions).then(result => {
90
+ // console.log(admin);
91
+ // if we are sharing and result is true OR we are unsharing and result is false... short circuit
92
+ if (
93
+ (requestOptions.action === "share" && result === true) ||
94
+ (requestOptions.action === "unshare" && result === false)
95
+ ) {
96
+ // and send back the same response structure ArcGIS Online would
97
+ const response = { itemId: requestOptions.id, shortcut: true } as any;
98
+ response[resultProp] = [];
99
+ return response;
100
+ } else {
101
+ // next check to ensure the user is a member of the group
102
+ return getUserMembership(requestOptions)
103
+ .then(membership => {
104
+ if (membership === "nonmember") {
105
+ // abort and reject promise
106
+ throw Error(
107
+ `This item can not be ${
108
+ requestOptions.action
109
+ }d by ${username} as they are not a member of the specified group ${
110
+ requestOptions.groupId
111
+ }.`
112
+ );
113
+ } else {
114
+ // if orgAdmin or owner (and member of group) share using the owner url
115
+ if (owner === username || admin) {
116
+ return `${getPortalUrl(
117
+ requestOptions
118
+ )}/content/users/${owner}/items/${requestOptions.id}/${
119
+ requestOptions.action
120
+ }`;
121
+ } else {
122
+ // if they are a group admin/owner, use the bare item url
123
+ if (membership === "admin") {
124
+ return `${getPortalUrl(requestOptions)}/content/items/${
125
+ requestOptions.id
126
+ }/${requestOptions.action}`;
127
+ } else {
128
+ // otherwise abort
129
+ throw Error(
130
+ `This item can not be ${
131
+ requestOptions.action
132
+ }d by ${username} as they are neither the owner, a groupAdmin of ${
133
+ requestOptions.groupId
134
+ }, nor an org_admin.`
135
+ );
136
+ }
137
+ }
138
+ }
139
+ })
140
+ .then(url => {
141
+ // now its finally time to do the sharing
142
+ requestOptions.params = {
143
+ groups: requestOptions.groupId,
144
+ confirmItemControl: requestOptions.confirmItemControl
145
+ };
146
+ // dont mixin to ensure that old query parameters from the search request arent included
147
+ return request(url, requestOptions);
148
+ })
149
+ .then(sharingResponse => {
150
+ if (sharingResponse[resultProp].length) {
151
+ throw Error(
152
+ `Item ${requestOptions.id} could not be ${
153
+ requestOptions.action
154
+ }d to group ${requestOptions.groupId}.`
155
+ );
156
+ } else {
157
+ // all is well
158
+ return sharingResponse;
159
+ }
160
+ });
161
+ } // else
162
+ }); // then
163
+ });
164
+ }
165
+
166
+ /**
167
+ * Find out whether or not an item is already shared with a group.
168
+ *
169
+ * @param requestOptions - Options for the request.
170
+ * @returns A Promise that will resolve with the data from the response.
171
+ */
172
+ function isItemSharedWithGroup(
173
+ requestOptions: IGroupSharingRequestOptions
174
+ ): Promise<boolean> {
175
+ const query = {
176
+ q: `id: ${requestOptions.id} AND group: ${requestOptions.groupId}`,
177
+ start: 1,
178
+ num: 10,
179
+ sortField: "title"
180
+ };
181
+
182
+ // instead of calling out to "@esri/arcgis-rest-items, make the request manually to forgoe another dependency
183
+ requestOptions.params = {
184
+ ...query,
185
+ ...requestOptions.params
186
+ };
187
+
188
+ const url = `${getPortalUrl(requestOptions)}/search`;
189
+
190
+ return request(url, requestOptions).then(searchResponse => {
191
+ // if there are no search results at all, we know the item hasnt already been shared with the group
192
+ if (searchResponse.total === 0) {
193
+ return false;
194
+ } else {
195
+ let sharedItem: IItem;
196
+ // otherwise loop through and search for the id
197
+ searchResponse.results.some((item: IItem) => {
198
+ const matchedItem = item.id === requestOptions.id;
199
+ if (matchedItem) {
200
+ sharedItem = item;
201
+ }
202
+ return matchedItem;
203
+ });
204
+
205
+ if (sharedItem) {
206
+ return true;
207
+ } else {
208
+ return false;
209
+ }
210
+ }
211
+ });
212
+ }
@@ -0,0 +1,92 @@
1
+ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
2
+ * Apache-2.0 */
3
+ import {
4
+ request,
5
+ IRequestOptions,
6
+ getPortalUrl
7
+ } from "@esri/arcgis-rest-request";
8
+ import { UserSession } from "@esri/arcgis-rest-auth";
9
+ import { IUser, IGroup } from "@esri/arcgis-rest-common-types";
10
+ import { IGroupSharingRequestOptions } from "./group-sharing";
11
+
12
+ export interface ISharingRequestOptions extends IRequestOptions {
13
+ /**
14
+ * Unique identifier for the item.
15
+ */
16
+ id: string;
17
+ /**
18
+ * Represents a user with privileges to update item sharing.
19
+ */
20
+ authentication: UserSession;
21
+ /**
22
+ * Item owner, if different from the authenticated user.
23
+ */
24
+ owner?: string;
25
+ }
26
+
27
+ export interface ISharingResponse {
28
+ notSharedWith?: string[];
29
+ notUnsharedFrom?: string[];
30
+ itemId: string;
31
+ }
32
+
33
+ export function getSharingUrl(requestOptions: ISharingRequestOptions): string {
34
+ const username = requestOptions.authentication.username;
35
+ const owner = requestOptions.owner || username;
36
+ return `${getPortalUrl(requestOptions)}/content/users/${encodeURIComponent(
37
+ owner
38
+ )}/items/${requestOptions.id}/share`;
39
+ }
40
+
41
+ export function isItemOwner(requestOptions: ISharingRequestOptions): boolean {
42
+ const username = requestOptions.authentication.username;
43
+ const owner = requestOptions.owner || username;
44
+ return owner === username;
45
+ }
46
+
47
+ export function isOrgAdmin(
48
+ requestOptions: ISharingRequestOptions
49
+ ): Promise<boolean> {
50
+ const session = requestOptions.authentication as UserSession;
51
+
52
+ return session.getUser().then(user => {
53
+ if (!user || user.role !== "org_admin") {
54
+ return false;
55
+ } else {
56
+ return true;
57
+ }
58
+ });
59
+ }
60
+
61
+ export function getUserMembership(
62
+ requestOptions: IGroupSharingRequestOptions
63
+ ): Promise<string> {
64
+ // start by assuming the user does not belong to the group
65
+ let result = "nonmember";
66
+ const session = requestOptions.authentication as UserSession;
67
+
68
+ // the response to this call is cached. yay!
69
+ return session
70
+ .getUser()
71
+ .then((user: IUser) => {
72
+ if (user.groups) {
73
+ user.groups.some(function(group: IGroup) {
74
+ const matchedGroup = group.id === requestOptions.groupId;
75
+ if (matchedGroup) {
76
+ result = group.userMembership.memberType;
77
+ }
78
+ return matchedGroup;
79
+ });
80
+ }
81
+ return result;
82
+ })
83
+ .catch(
84
+ /* istanbul ignore next */ err => {
85
+ throw Error(
86
+ `failure determining membership of ${session.username} in group:${
87
+ requestOptions.groupId
88
+ }: ${err}`
89
+ );
90
+ }
91
+ );
92
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./access";
2
+ export * from "./group-sharing";
@@ -0,0 +1,153 @@
1
+ import { setItemAccess } from "../src/index";
2
+ import * as fetchMock from "fetch-mock";
3
+ import { MOCK_USER_SESSION } from "./mocks/sharing";
4
+ import {
5
+ AnonUserResponse,
6
+ GroupMemberUserResponse,
7
+ GroupAdminUserResponse,
8
+ OrgAdminUserResponse
9
+ } from "../../arcgis-rest-users/test/mocks/responses";
10
+
11
+ const SharingResponse = {
12
+ notSharedWith: [] as any,
13
+ itemId: "abc123"
14
+ };
15
+
16
+ describe("setItemAccess()", () => {
17
+ // make sure session doesnt cache metadata
18
+ beforeEach(function() {
19
+ MOCK_USER_SESSION._user = null;
20
+ });
21
+
22
+ afterEach(fetchMock.restore);
23
+
24
+ it("should share an item with everyone", done => {
25
+ fetchMock.once("*", SharingResponse);
26
+
27
+ setItemAccess({
28
+ authentication: MOCK_USER_SESSION,
29
+ id: "abc123",
30
+ access: "public"
31
+ })
32
+ .then(response => {
33
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
34
+ expect(url).toEqual(
35
+ "https://myorg.maps.arcgis.com/sharing/rest/content/users/jsmith/items/abc123/share"
36
+ );
37
+ expect(options.method).toBe("POST");
38
+ expect(response).toEqual(SharingResponse);
39
+ expect(options.body).toContain("f=json");
40
+ expect(options.body).toContain("everyone=true");
41
+ expect(options.body).toContain("org=true");
42
+ done();
43
+ })
44
+ .catch(e => {
45
+ fail(e);
46
+ });
47
+ });
48
+
49
+ it("should share an item with an organization", done => {
50
+ fetchMock.once("*", SharingResponse);
51
+
52
+ setItemAccess({
53
+ authentication: MOCK_USER_SESSION,
54
+ id: "abc123",
55
+ access: "org"
56
+ })
57
+ .then(response => {
58
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
59
+ expect(url).toEqual(
60
+ "https://myorg.maps.arcgis.com/sharing/rest/content/users/jsmith/items/abc123/share"
61
+ );
62
+ expect(options.method).toBe("POST");
63
+ expect(response).toEqual(SharingResponse);
64
+ expect(options.body).toContain("f=json");
65
+ expect(options.body).toContain("everyone=false");
66
+ expect(options.body).toContain("org=true");
67
+ done();
68
+ })
69
+ .catch(e => {
70
+ fail(e);
71
+ });
72
+ });
73
+
74
+ it("should stop sharing an item entirely", done => {
75
+ fetchMock.once("*", SharingResponse);
76
+
77
+ setItemAccess({
78
+ authentication: MOCK_USER_SESSION,
79
+ id: "abc123",
80
+ access: "private"
81
+ })
82
+ .then(response => {
83
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
84
+ expect(url).toEqual(
85
+ "https://myorg.maps.arcgis.com/sharing/rest/content/users/jsmith/items/abc123/share"
86
+ );
87
+ expect(options.method).toBe("POST");
88
+ expect(response).toEqual(SharingResponse);
89
+ expect(options.body).toContain("f=json");
90
+ expect(options.body).toContain("everyone=false");
91
+ expect(options.body).toContain("org=false");
92
+ expect(options.body).toContain("groups=");
93
+ done();
94
+ })
95
+ .catch(e => {
96
+ fail(e);
97
+ });
98
+ });
99
+
100
+ it("should share another persons item if an org admin makes the request", done => {
101
+ fetchMock.once(
102
+ "https://myorg.maps.arcgis.com/sharing/rest/community/users/jsmith?f=json&token=fake-token",
103
+ OrgAdminUserResponse
104
+ );
105
+
106
+ fetchMock.once(
107
+ "begin:https://myorg.maps.arcgis.com/sharing/rest/content/users/casey/items/abc123/share",
108
+ SharingResponse
109
+ );
110
+
111
+ setItemAccess({
112
+ authentication: MOCK_USER_SESSION,
113
+ id: "abc123",
114
+ access: "private",
115
+ owner: "casey"
116
+ })
117
+ .then(response => {
118
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall();
119
+ expect(url).toEqual(
120
+ "https://myorg.maps.arcgis.com/sharing/rest/content/users/casey/items/abc123/share"
121
+ );
122
+ expect(options.method).toBe("POST");
123
+ expect(response).toEqual(SharingResponse);
124
+ expect(options.body).toContain("f=json");
125
+ expect(options.body).toContain("everyone=false");
126
+ expect(options.body).toContain("org=false");
127
+ expect(options.body).toContain("groups=");
128
+ done();
129
+ })
130
+ .catch(e => {
131
+ fail(e);
132
+ });
133
+ });
134
+
135
+ it("should throw if the person trying to share doesnt own the item and is not an admin", done => {
136
+ fetchMock.once(
137
+ "https://myorg.maps.arcgis.com/sharing/rest/community/users/jsmith?f=json&token=fake-token",
138
+ AnonUserResponse
139
+ );
140
+
141
+ setItemAccess({
142
+ authentication: MOCK_USER_SESSION,
143
+ id: "abc123",
144
+ access: "private",
145
+ owner: "casey"
146
+ }).catch(e => {
147
+ expect(e.message).toEqual(
148
+ "This item can not be shared by jsmith. They are neither the item owner nor an organization admin."
149
+ );
150
+ done();
151
+ });
152
+ });
153
+ });