ws_access.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. define([
  17. "dojo/_base/declare",
  18. "dojo/_base/lang",
  19. "dojo/_base/array",
  20. "dojo/Deferred",
  21. "dojo/promise/all",
  22. "dojo/store/Memory",
  23. "dojo/store/Observable",
  24. "dojo/store/util/QueryResults",
  25. "hpcc/ESPRequest",
  26. "hpcc/ESPUtil"
  27. ], function (declare, lang, arrayUtil, Deferred, all, Memory, Observable, QueryResults,
  28. ESPRequest, ESPUtil) {
  29. var UsersStore = declare([Memory], {
  30. constructor: function () {
  31. this.idProperty = "__hpcc_id";
  32. },
  33. put: function (object, options) {
  34. var retVal = this.inherited(arguments);
  35. self.UserGroupEdit({
  36. request: {
  37. username: object.username,
  38. action: object.isMember ? "add" : "delete",
  39. groupnames_i1: object.__hpcc_groupname
  40. }
  41. });
  42. return retVal;
  43. },
  44. query: function (query, options) {
  45. var results = all([
  46. this.refreshUsers(),
  47. this.refreshGroupUsers()
  48. ]).then(lang.hitch(this, function (response) {
  49. var groupUsers = {};
  50. arrayUtil.forEach(response[1], function (item, idx) {
  51. groupUsers[item.username] = true;
  52. }, this);
  53. var data = [];
  54. arrayUtil.forEach(response[0], function (item, idx) {
  55. data.push(lang.mixin(item, {
  56. __hpcc_groupname: this.groupname,
  57. __hpcc_id: item.username,
  58. isMember: groupUsers[item.username] ? true : false
  59. }));
  60. }, this);
  61. this.setData(data);
  62. return this.data;
  63. }));
  64. return QueryResults(results);
  65. },
  66. refreshUsers: function () {
  67. return self.Users().then(function (response) {
  68. if (lang.exists("UserResponse.Users.User", response)) {
  69. return response.UserResponse.Users.User;
  70. }
  71. return [];
  72. });
  73. },
  74. refreshGroupUsers: function (query) {
  75. if (!this.groupname) {
  76. var deferred = new Deferred;
  77. deferred.resolve([]);
  78. return deferred.promise;
  79. };
  80. return self.GroupEdit({
  81. request: {
  82. groupname: this.groupname
  83. }
  84. }).then(function (response) {
  85. if (lang.exists("GroupEditResponse.Users.User", response)) {
  86. return response.GroupEditResponse.Users.User;
  87. }
  88. return [];
  89. });
  90. }
  91. });
  92. var GroupsStore = declare([Memory], {
  93. constructor: function () {
  94. this.idProperty = "__hpcc_id";
  95. },
  96. put: function (object, options) {
  97. var retVal = this.inherited(arguments);
  98. self.UserGroupEdit({
  99. request: {
  100. username: object.__hpcc_username,
  101. action: object.isMember ? "add" : "delete",
  102. groupnames_i1: object.name
  103. }
  104. });
  105. return retVal;
  106. },
  107. query: function (query, options) {
  108. var results = all([
  109. this.refreshGroups(),
  110. this.refreshUserGroups()
  111. ]).then(lang.hitch(this, function (response) {
  112. var userGroups = {};
  113. arrayUtil.forEach(response[1], function (item, idx) {
  114. userGroups[item.name] = true;
  115. }, this);
  116. var data = [];
  117. arrayUtil.forEach(response[0], function (item, idx) {
  118. if (item.name !== "Authenticated Users") {
  119. data.push(lang.mixin(item, {
  120. __hpcc_id: item.name,
  121. __hpcc_username: this.username,
  122. isMember: userGroups[item.name] ? true : false
  123. }));
  124. }
  125. }, this);
  126. this.setData(data);
  127. return this.data;
  128. }));
  129. return QueryResults(results);
  130. },
  131. refreshGroups: function () {
  132. return self.Groups().then(function (response) {
  133. if (lang.exists("GroupResponse.Groups.Group", response)) {
  134. return response.GroupResponse.Groups.Group;
  135. }
  136. return [];
  137. });
  138. },
  139. refreshUserGroups: function (query) {
  140. return self.UserEdit({
  141. request: {
  142. username: this.username
  143. }
  144. }).then(function (response) {
  145. if (lang.exists("UserEditResponse.Groups.Group", response)) {
  146. return response.UserEditResponse.Groups.Group;
  147. }
  148. return [];
  149. });
  150. }
  151. });
  152. var CONCAT_SYMBOL = ":";
  153. var ResourcesStore = declare([Memory], {
  154. constructor: function () {
  155. this.idProperty = "__hpcc_id";
  156. },
  157. put: function (row) {
  158. var item = this.get(row.__hpcc_id);
  159. var retVal = this.inherited(arguments);
  160. var request = {
  161. account_name: this.groupname ? this.groupname : this.username,
  162. account_type: this.groupname ? 1 : 0,
  163. basedn: row.__hpcc_parent.basedn,
  164. rname: row.name,
  165. action: "update"
  166. };
  167. lang.mixin(request, row);
  168. self.PermissionAction({
  169. request: request
  170. });
  171. return retVal;
  172. },
  173. query: function (query, options) {
  174. var results = all([
  175. this.refreshResources(query),
  176. this.refreshAccountPermissions(query)
  177. ]).then(lang.hitch(this, function (response) {
  178. var accountPermissions = {};
  179. arrayUtil.forEach(response[1], function (item, idx) {
  180. accountPermissions[item.PermissionName] = item;
  181. }, this);
  182. var data = [];
  183. arrayUtil.forEach(response[0], function (item, idx) {
  184. var accountPermission = accountPermissions[item.name];
  185. data.push(lang.mixin(item, {
  186. __hpcc_type: "Resources",
  187. __hpcc_id: this.parentRow.__hpcc_id + CONCAT_SYMBOL + item.name,
  188. __hpcc_parent: this.parentRow,
  189. DisplayName: item.description ? item.description : item.name,
  190. allow_access: accountPermission ? accountPermission.allow_access : false,
  191. allow_read: accountPermission ? accountPermission.allow_read : false,
  192. allow_write: accountPermission ? accountPermission.allow_write : false,
  193. allow_full: accountPermission ? accountPermission.allow_full : false,
  194. deny_access: accountPermission ? accountPermission.deny_access : false,
  195. deny_read: accountPermission ? accountPermission.deny_read : false,
  196. deny_write: accountPermission ? accountPermission.deny_write : false,
  197. deny_full: accountPermission ? accountPermission.deny_full : false
  198. }));
  199. }, this);
  200. this.setData(data);
  201. return data;
  202. }));
  203. return QueryResults(results);
  204. },
  205. refreshResources: function (query) {
  206. return self.Resources({
  207. request: {
  208. basedn: this.basedn,
  209. }
  210. }).then(lang.hitch(this, function (response) {
  211. if (lang.exists("ResourcesResponse.Resources.Resource", response)) {
  212. return response.ResourcesResponse.Resources.Resource;
  213. }
  214. return [];
  215. }));
  216. },
  217. refreshAccountPermissions: function () {
  218. return self.AccountPermissions({
  219. request: {
  220. AccountName: this.groupname ? this.groupname : this.username,
  221. IsGroup: this.groupname ? true : false,
  222. IncludeGroup: false
  223. }
  224. }).then(lang.hitch(this, function (response) {
  225. if (lang.exists("AccountPermissionsResponse.Permissions.Permission", response)) {
  226. return response.AccountPermissionsResponse.Permissions.Permission;
  227. }
  228. return [];
  229. }));
  230. }
  231. });
  232. var PermissionsStore = declare([Memory], {
  233. service: "ws_access",
  234. action: "Permissions",
  235. responseQualifier: "BasednsResponse.Basedns.Basedn",
  236. idProperty: "__hpcc_id",
  237. constructor: function () {
  238. this.idProperty = "__hpcc_id";
  239. },
  240. get: function (id) {
  241. var tmp = id.split(CONCAT_SYMBOL);
  242. var retVal = null;
  243. if (tmp.length > 0) {
  244. var parentID = tmp[0];
  245. var parent = this.inherited(arguments, [parentID]);
  246. if (tmp.length === 1) {
  247. return parent;
  248. }
  249. var child = parent.children.get(id);
  250. if (child) {
  251. return child;
  252. }
  253. return parent;
  254. }
  255. return null;
  256. },
  257. putChild: function (row) {
  258. var parent = row.__hpcc_parent;
  259. return parent.children.put(row);
  260. },
  261. getChildren: function (parent, options) {
  262. return parent.children.query();
  263. },
  264. mayHaveChildren: function (object) {
  265. return object.__hpcc_type === "Permission";
  266. },
  267. query: function (query, options) {
  268. var results = self.Permissions().then(lang.hitch(this, function (response) {
  269. var data = [];
  270. if (lang.exists("BasednsResponse.Basedns.Basedn", response)) {
  271. arrayUtil.forEach(response.BasednsResponse.Basedns.Basedn, function (item, idx) {
  272. data.push(lang.mixin(item, {
  273. __hpcc_type: "Permission",
  274. __hpcc_id: item.basedn,
  275. DisplayName: item.name,
  276. children: lang.mixin(self.CreateResourcesStore(this.groupname, this.username, item.basedn), {
  277. parent: this,
  278. parentRow: item
  279. })
  280. }));
  281. }, this);
  282. }
  283. this.setData(data);
  284. return data;
  285. }));
  286. return QueryResults(results);
  287. }
  288. });
  289. var self = {
  290. checkError: function (response, sourceMethod) {
  291. var retCode = lang.getObject(sourceMethod + "Response.retcode", false, response);
  292. var retMsg = lang.getObject(sourceMethod + "Response.retmsg", false, response);
  293. if (retCode) {
  294. dojo.publish("hpcc/brToaster", {
  295. Severity: "Error",
  296. Source: "WsAccess." + sourceMethod,
  297. Exceptions: [{ Message: retMsg }]
  298. });
  299. }
  300. },
  301. _doCall: function (action, params) {
  302. var context = this;
  303. return ESPRequest.send("ws_access", action, params).then(function (response) {
  304. context.checkError(response, action);
  305. return response;
  306. });
  307. },
  308. Users: function (params) {
  309. return this._doCall("Users", params);
  310. },
  311. UserAction: function (params) {
  312. return this._doCall("UserAction", params);
  313. },
  314. AddUser: function (params) {
  315. return this._doCall("AddUser", params);
  316. },
  317. UserEdit: function (params) {
  318. return this._doCall("UserEdit", params);
  319. },
  320. UserInfoEditInput: function (params) {
  321. return this._doCall("UserInfoEditInput", params);
  322. },
  323. UserInfoEdit: function (params) {
  324. return this._doCall("UserInfoEdit", params);
  325. },
  326. UserResetPass: function (params) {
  327. return this._doCall("UserResetPass", params);
  328. },
  329. UserGroupEdit: function (params) {
  330. return this._doCall("UserGroupEdit", params);
  331. },
  332. GroupAdd: function (params) {
  333. return this._doCall("GroupAdd", params);
  334. },
  335. GroupAction: function (params) {
  336. return this._doCall("GroupAction", params);
  337. },
  338. GroupEdit: function (params) {
  339. return this._doCall("GroupEdit", params);
  340. },
  341. Groups: function (params) {
  342. return this._doCall("Groups", params);
  343. },
  344. Permissions: function (params) {
  345. return this._doCall("Permissions", params);
  346. },
  347. Resources: function (params) {
  348. return this._doCall("Resources", params);
  349. },
  350. AccountPermissions: function (params) {
  351. return this._doCall("AccountPermissions", params);
  352. },
  353. PermissionAction: function (params) {
  354. return this._doCall("PermissionAction", params);
  355. },
  356. CreateUsersStore: function (groupname) {
  357. var store = new UsersStore();
  358. store.groupname = groupname;
  359. return Observable(store);
  360. },
  361. CreateGroupsStore: function (username) {
  362. var store = new GroupsStore();
  363. store.username = username;
  364. return Observable(store);
  365. },
  366. CreatePermissionsStore: function (groupname, username) {
  367. var store = new PermissionsStore();
  368. store.groupname = groupname;
  369. store.username = username;
  370. return Observable(store);
  371. },
  372. CreateResourcesStore: function (groupname, username, basedn) {
  373. var store = new ResourcesStore();
  374. store.groupname = groupname;
  375. store.username = username;
  376. store.basedn = basedn;
  377. return Observable(store);
  378. }
  379. };
  380. return self;
  381. });