UserQueryWidget.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/i18n",
  20. "dojo/i18n!./nls/hpcc",
  21. "dojo/_base/array",
  22. "dojo/dom",
  23. "dojo/dom-form",
  24. "dojo/on",
  25. "dijit/registry",
  26. "dijit/Menu",
  27. "dijit/MenuItem",
  28. "dijit/MenuSeparator",
  29. "dgrid/OnDemandGrid",
  30. "dgrid/Keyboard",
  31. "dgrid/Selection",
  32. "dgrid/selector",
  33. "dgrid/extensions/ColumnResizer",
  34. "dgrid/extensions/DijitRegistry",
  35. "hpcc/_TabContainerWidget",
  36. "hpcc/ws_access",
  37. "hpcc/ESPUtil",
  38. "hpcc/UserDetailsWidget",
  39. "hpcc/GroupDetailsWidget",
  40. "dojo/text!../templates/UserQueryWidget.html",
  41. "dijit/layout/BorderContainer",
  42. "dijit/layout/TabContainer",
  43. "dijit/layout/ContentPane",
  44. "dijit/Toolbar",
  45. "dijit/form/Form",
  46. "dijit/form/Button",
  47. "dijit/form/DropDownButton",
  48. "dijit/form/ValidationTextBox",
  49. "dijit/ToolbarSeparator",
  50. "dijit/form/TextBox",
  51. "dijit/Dialog",
  52. "dojox/form/PasswordValidator",
  53. "hpcc/TableContainer"
  54. ], function (declare, lang, i18n, nlsHPCC, arrayUtil, dom, domForm, on,
  55. registry, Menu, MenuItem, MenuSeparator,
  56. OnDemandGrid, Keyboard, Selection, selector, ColumnResizer, DijitRegistry,
  57. _TabContainerWidget, WsAccess, ESPUtil, UserDetailsWidget, GroupDetailsWidget,
  58. template) {
  59. return declare("UserQueryWidget", [_TabContainerWidget], {
  60. templateString: template,
  61. baseClass: "UserQueryWidget",
  62. i18n: nlsHPCC,
  63. usersTab: null,
  64. usersGrid: null,
  65. postCreate: function (args) {
  66. this.inherited(arguments);
  67. this.addGroupForm = registry.byId(this.id + "AddGroupForm");
  68. this.groupsTab = registry.byId(this.id + "_Groups");
  69. this.addUserForm = registry.byId(this.id + "AddUserForm");
  70. this.usersTab = registry.byId(this.id + "_Users");
  71. },
  72. // Hitched actions ---
  73. // Groups ---
  74. _onRefreshGroups: function () {
  75. this.refreshGroupsGrid();
  76. },
  77. _onEditGroup: function (event) {
  78. var selections = this.groupsGrid.getSelected();
  79. var firstTab = null;
  80. for (var i = selections.length - 1; i >= 0; --i) {
  81. var tab = this.ensureGroupPane(this.id + "_Group" + selections[i].name, {
  82. Name: selections[i].name
  83. });
  84. if (i == 0) {
  85. firstTab = tab;
  86. }
  87. }
  88. if (firstTab) {
  89. this.selectChild(firstTab);
  90. }
  91. },
  92. _onDeleteGroup: function (params) {
  93. if (confirm(this.i18n.DeleteSelectedGroups)) {
  94. var selections = this.groupsGrid.getSelected();
  95. var request = {
  96. ActionType: "delete"
  97. };
  98. arrayUtil.forEach(selections, function (item, idx) {
  99. request["groupnames_i" + idx] = item.name;
  100. }, this);
  101. var context = this;
  102. WsAccess.GroupAction({
  103. request: request
  104. }).then(function (response) {
  105. context.refreshGroupsGrid(true);
  106. });
  107. }
  108. },
  109. _onGroupsRowDblClick: function (name) {
  110. var groupTab = this.ensureGroupPane(this.id + "_Group" + name, {
  111. Name: name
  112. });
  113. this.selectChild(groupTab);
  114. },
  115. _onAddGroupSubmit: function () {
  116. if (this.addGroupForm.validate()) {
  117. var context = this;
  118. var request = domForm.toObject(this.addGroupForm.id);
  119. WsAccess.GroupAdd({
  120. request: request
  121. }).then(function (response) {
  122. context.refreshGroupsGrid();
  123. });
  124. registry.byId(this.id + "AddGroupsDropDown").closeDropDown();
  125. }
  126. },
  127. // Users ---
  128. _onRefreshUsers: function () {
  129. this.refreshUsersGrid();
  130. },
  131. _onEditUser: function (event) {
  132. var selections = this.usersGrid.getSelected();
  133. var firstTab = null;
  134. for (var i = selections.length - 1; i >= 0; --i) {
  135. var tab = this.ensureUserPane(this.id + "_User" + selections[i].username, {
  136. Username: selections[i].username,
  137. Fullname: selections[i].fullname,
  138. Passwordexpiration: selections[i].passwordexpiration
  139. });
  140. if (i == 0) {
  141. firstTab = tab;
  142. }
  143. }
  144. if (firstTab) {
  145. this.selectChild(firstTab);
  146. }
  147. },
  148. _onDeleteUser: function (params) {
  149. var selections = this.usersGrid.getSelected();
  150. if (confirm(this.i18n.DeleteSelectedUsers)) {
  151. request = {
  152. ActionType: "delete"
  153. };
  154. arrayUtil.forEach(selections, function (item, idx) {
  155. request["usernames_i" + idx] = item.username;
  156. }, this);
  157. var context = this;
  158. WsAccess.UserAction({
  159. request: request
  160. }).then(function (response) {
  161. context.refreshUsersGrid(true);
  162. });
  163. }
  164. },
  165. _onUsersRowDblClick: function (username, fullname, passwordexpiration) {
  166. var userTab = this.ensureUserPane(this.id + "_" + username, {
  167. Username: username,
  168. Fullname: fullname,
  169. Passwordexpiration: passwordexpiration
  170. });
  171. this.selectChild(userTab);
  172. },
  173. _onSubmitAddUserDialog: function (event) {
  174. if (this.addUserForm.validate()) {
  175. var context = this;
  176. var request = domForm.toObject(this.addUserForm.id);
  177. lang.mixin(request, {
  178. password1: request.password,
  179. password2: request.password
  180. })
  181. WsAccess.AddUser({
  182. request: request
  183. }).then(function (response) {
  184. context.refreshUsersGrid();
  185. });
  186. registry.byId(this.id + "AddUsersDropDown").closeDropDown();
  187. }
  188. },
  189. // Implementation ---
  190. init: function (params) {
  191. if (this.inherited(arguments))
  192. return;
  193. this.initGroupsGrid();
  194. this.initUsersGrid();
  195. this.refreshActionState();
  196. },
  197. // Groups ---
  198. initGroupsGrid: function () {
  199. this.initGroupsContextMenu();
  200. var store = WsAccess.CreateGroupsStore();
  201. this.groupsGrid = declare([OnDemandGrid, Keyboard, Selection, ColumnResizer, DijitRegistry, ESPUtil.GridHelper])({
  202. allowSelectAll: true,
  203. deselectOnRefresh: false,
  204. store: store,
  205. columns: {
  206. check: selector({
  207. width: 27,
  208. label: " "
  209. }, "checkbox"),
  210. name: {
  211. label: this.i18n.GroupName
  212. }
  213. },
  214. }, this.id + "GroupsGrid");
  215. this.groupsGrid.noDataMessage = "<span class='dojoxGridNoData'>" + this.i18n.noDataMessage + "</span>";
  216. var context = this;
  217. on(document, ".WuidClick:click", function (evt) {
  218. if (context._onGroupsRowDblClick) {
  219. var item = context.groupsGrid.row(evt).data;
  220. context._onGroupsRowDblClick(item.name);
  221. }
  222. });
  223. this.groupsGrid.on(".dgrid-row:dblclick", function (evt) {
  224. if (context._onGroupsRowDblClick) {
  225. var item = context.groupsGrid.row(evt).data;
  226. context._onGroupsRowDblClick(item.name);
  227. }
  228. });
  229. this.groupsGrid.onSelectionChanged(function (event) {
  230. context.refreshActionState();
  231. });
  232. this.groupsGrid.onContentChanged(function (event) {
  233. context.refreshActionState();
  234. });
  235. this.groupsGrid.startup();
  236. },
  237. initGroupsContextMenu: function () {
  238. var context = this;
  239. var pMenu = new Menu({
  240. targetNodeIds: [this.id + "GroupsGrid"]
  241. });
  242. pMenu.addChild(new MenuItem({
  243. label: this.i18n.Add,
  244. onClick: function (args) {
  245. registry.byId(context.id + "AddGroupsDropDown").openDropDown();
  246. }
  247. }));
  248. pMenu.addChild(new MenuItem({
  249. label: this.i18n.Edit,
  250. onClick: function (args) { context._onEditGroup(); }
  251. }));
  252. pMenu.addChild(new MenuItem({
  253. label: this.i18n.Delete,
  254. onClick: function (args) { context._onDeleteGroup(); }
  255. }));
  256. pMenu.addChild(new MenuSeparator());
  257. pMenu.addChild(new MenuItem({
  258. label: this.i18n.Refresh,
  259. onClick: function (args) { context._onRefreshGroups(); }
  260. }));
  261. },
  262. refreshGroupsGrid: function (clearSelection) {
  263. this.groupsGrid.set("query", {
  264. id: "*"
  265. });
  266. if (clearSelection) {
  267. this.groupsGrid.clearSelection();
  268. }
  269. },
  270. ensureGroupPane: function (id, params) {
  271. var retVal = registry.byId(id);
  272. if (!retVal) {
  273. retVal = new GroupDetailsWidget({
  274. id: id,
  275. title: params.Name,
  276. closable: true,
  277. params: params
  278. });
  279. this.addChild(retVal, 2);
  280. }
  281. return retVal;
  282. },
  283. // Users ---
  284. initUsersGrid: function () {
  285. this.initUsersContextMenu();
  286. var store = WsAccess.CreateUsersStore();
  287. this.usersGrid = declare([OnDemandGrid, Keyboard, Selection, ColumnResizer, DijitRegistry, ESPUtil.GridHelper])({
  288. allowSelectAll: true,
  289. deselectOnRefresh: false,
  290. store: store,
  291. columns: {
  292. check: selector({
  293. width: 27,
  294. label: " "
  295. },"checkbox"),
  296. username: {
  297. width: 180,
  298. label: this.i18n.Username
  299. },
  300. fullname: {
  301. label: this.i18n.FullName
  302. },
  303. passwordexpiration: {
  304. width: 180,
  305. label: this.i18n.PasswordExpiration
  306. }
  307. },
  308. }, this.id + "UsersGrid");
  309. this.usersGrid.noDataMessage = "<span class='dojoxGridNoData'>" + this.i18n.noDataMessage + "</span>";
  310. var context = this;
  311. on(document, ".WuidClick:click", function (evt) {
  312. if (context._onUsersRowDblClick) {
  313. var item = context.usersGrid.row(evt).data;
  314. context._onUsersRowDblClick(item.username,item.fullname,item.passwordexpiration);
  315. }
  316. });
  317. this.usersGrid.on(".dgrid-row:dblclick", function (evt) {
  318. if (context._onUsersRowDblClick) {
  319. var item = context.usersGrid.row(evt).data;
  320. context._onUsersRowDblClick(item.username,item.fullname,item.passwordexpiration);
  321. }
  322. });
  323. this.usersGrid.onSelectionChanged(function (event) {
  324. context.refreshActionState();
  325. });
  326. this.usersGrid.onContentChanged(function (event) {
  327. context.refreshActionState();
  328. });
  329. this.usersGrid.startup();
  330. },
  331. initUsersContextMenu: function () {
  332. var context = this;
  333. var pMenu = new Menu({
  334. targetNodeIds: [this.id + "UsersGrid"]
  335. });
  336. pMenu.addChild(new MenuItem({
  337. label: this.i18n.Add,
  338. onClick: function (args) {
  339. registry.byId(context.id + "AddUsersDropDown").openDropDown();
  340. }
  341. }));
  342. pMenu.addChild(new MenuItem({
  343. label: this.i18n.Edit,
  344. onClick: function (args) { context._onEditUser(); }
  345. }));
  346. pMenu.addChild(new MenuItem({
  347. label: this.i18n.Delete,
  348. onClick: function (args) { context._onDeleteUser(); }
  349. }));
  350. pMenu.addChild(new MenuSeparator());
  351. pMenu.addChild(new MenuItem({
  352. label: this.i18n.Refresh,
  353. onClick: function (args) { context._onRefreshUsers(); }
  354. }));
  355. },
  356. ensureUserPane: function (id, params) {
  357. var retVal = registry.byId(id);
  358. if (!retVal) {
  359. retVal = new UserDetailsWidget({
  360. id: id,
  361. title: params.Username,
  362. closable: true,
  363. params: params
  364. });
  365. this.addChild(retVal, 2);
  366. }
  367. return retVal;
  368. },
  369. refreshUsersGrid: function (clearSelection) {
  370. this.usersGrid.set("query",{
  371. id: "*"
  372. });
  373. if (clearSelection) {
  374. this.usersGrid.clearSelection();
  375. }
  376. },
  377. // --- ---
  378. initTab: function () {
  379. var currSel = this.getSelectedChild();
  380. if (currSel && !currSel.initalized) {
  381. if (currSel.id === this.groupsTab.id) {
  382. } else if (currSel.id === this.usersTab.id) {
  383. } else {
  384. if (!currSel.initalized) {
  385. currSel.init(currSel.params);
  386. }
  387. }
  388. }
  389. },
  390. refreshActionState: function () {
  391. var userSelection = this.usersGrid.getSelected();
  392. var hasUserSelection = userSelection.length;
  393. registry.byId(this.id + "EditUsers").set("disabled", !hasUserSelection);
  394. registry.byId(this.id + "DeleteUsers").set("disabled", !hasUserSelection);
  395. var groupSelection = this.groupsGrid.getSelected();
  396. var hasGroupSelection = groupSelection.length;
  397. registry.byId(this.id + "EditGroups").set("disabled", !hasGroupSelection);
  398. registry.byId(this.id + "DeleteGroups").set("disabled", !hasGroupSelection);
  399. }
  400. });
  401. });