PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/openid-connect-server-webapp/src/main/webapp/resources/js/whitelist.js

http://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server
JavaScript | 596 lines | 449 code | 115 blank | 32 comment | 33 complexity | a91203379f99cb720b3243fb309c7a00 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright 2018 The MIT Internet Trust Consortium
  3. *
  4. * Portions copyright 2011-2013 The MITRE Corporation
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *******************************************************************************/
  18. var WhiteListModel = Backbone.Model.extend({
  19. idAttribute: "id",
  20. initialize: function() {
  21. },
  22. urlRoot: "api/whitelist"
  23. });
  24. var WhiteListCollection = Backbone.Collection.extend({
  25. initialize: function() {
  26. // this.fetch();
  27. },
  28. getByClientId: function(clientId) {
  29. var clients = this.where({
  30. clientId: clientId
  31. });
  32. if (clients.length == 1) {
  33. return clients[0];
  34. } else {
  35. return null;
  36. }
  37. },
  38. model: WhiteListModel,
  39. url: "api/whitelist"
  40. });
  41. var WhiteListListView = Backbone.View.extend({
  42. tagName: 'span',
  43. initialize: function(options) {
  44. this.options = options;
  45. },
  46. load: function(callback) {
  47. if (this.model.isFetched && this.options.clientList.isFetched && this.options.systemScopeList.isFetched) {
  48. callback();
  49. return;
  50. }
  51. $('#loadingbox').sheet('show');
  52. $('#loading').html('<span class="label" id="loading-whitelist">' + $.t('whitelist.whitelist') + '</span> ' + '<span class="label" id="loading-clients">' + $.t('common.clients') + '</span> ' + '<span class="label" id="loading-scopes">' + $.t('common.scopes') + '</span> ');
  53. $.when(this.model.fetchIfNeeded({
  54. success: function(e) {
  55. $('#loading-whitelist').addClass('label-success');
  56. },
  57. error: app.errorHandlerView.handleError()
  58. }), this.options.clientList.fetchIfNeeded({
  59. success: function(e) {
  60. $('#loading-clients').addClass('label-success');
  61. },
  62. error: app.errorHandlerView.handleError()
  63. }), this.options.systemScopeList.fetchIfNeeded({
  64. success: function(e) {
  65. $('#loading-scopes').addClass('label-success');
  66. },
  67. error: app.errorHandlerView.handleError()
  68. })).done(function() {
  69. $('#loadingbox').sheet('hide');
  70. callback();
  71. });
  72. },
  73. events: {
  74. "click .refresh-table": "refreshTable"
  75. },
  76. render: function(eventName) {
  77. $(this.el).html($('#tmpl-whitelist-table').html());
  78. var _self = this;
  79. _.each(this.model.models, function(whiteList) {
  80. // look up client
  81. var client = _self.options.clientList.getByClientId(whiteList.get('clientId'));
  82. // if there's no client ID, this is an error!
  83. if (client != null) {
  84. var view = new WhiteListView({
  85. model: whiteList,
  86. client: client,
  87. systemScopeList: _self.options.systemScopeList
  88. });
  89. view.parentView = _self;
  90. $('#whitelist-table', _self.el).append(view.render().el);
  91. }
  92. }, this);
  93. this.togglePlaceholder();
  94. $(this.el).i18n();
  95. return this;
  96. },
  97. togglePlaceholder: function() {
  98. if (this.model.length > 0) {
  99. $('#whitelist-table', this.el).show();
  100. $('#whitelist-table-empty', this.el).hide();
  101. } else {
  102. $('#whitelist-table', this.el).hide();
  103. $('#whitelist-table-empty', this.el).show();
  104. }
  105. },
  106. refreshTable: function(e) {
  107. e.preventDefault();
  108. var _self = this;
  109. $('#loadingbox').sheet('show');
  110. $('#loading').html('<span class="label" id="loading-whitelist">' + $.t('whitelist.whitelist') + '</span> ' + '<span class="label" id="loading-clients">' + $.t('common.clients') + '</span> ' + '<span class="label" id="loading-scopes">' + $.t('common.scopes') + '</span> ');
  111. $.when(this.model.fetch({
  112. success: function(e) {
  113. $('#loading-whitelist').addClass('label-success');
  114. },
  115. error: app.errorHandlerView.handleError()
  116. }), this.options.clientList.fetch({
  117. success: function(e) {
  118. $('#loading-clients').addClass('label-success');
  119. },
  120. error: app.errorHandlerView.handleError()
  121. }), this.options.systemScopeList.fetch({
  122. success: function(e) {
  123. $('#loading-scopes').addClass('label-success');
  124. },
  125. error: app.errorHandlerView.handleError()
  126. })).done(function() {
  127. $('#loadingbox').sheet('hide');
  128. _self.render();
  129. });
  130. }
  131. });
  132. var WhiteListView = Backbone.View.extend({
  133. tagName: 'tr',
  134. initialize: function(options) {
  135. this.options = options;
  136. if (!this.template) {
  137. this.template = _.template($('#tmpl-whitelist').html());
  138. }
  139. if (!this.scopeTemplate) {
  140. this.scopeTemplate = _.template($('#tmpl-scope-list').html());
  141. }
  142. if (!this.moreInfoTemplate) {
  143. this.moreInfoTemplate = _.template($('#tmpl-client-more-info-block').html());
  144. }
  145. this.model.bind('change', this.render, this);
  146. },
  147. render: function(eventName) {
  148. var json = {
  149. whiteList: this.model.toJSON(),
  150. client: this.options.client.toJSON()
  151. };
  152. this.$el.html(this.template(json));
  153. $('.scope-list', this.el).html(this.scopeTemplate({
  154. scopes: this.model.get('allowedScopes'),
  155. systemScopes: this.options.systemScopeList
  156. }));
  157. $('.client-more-info-block', this.el).html(this.moreInfoTemplate({
  158. client: this.options.client.toJSON()
  159. }));
  160. this.$('.dynamically-registered').tooltip({
  161. title: $.t('common.dynamically-registered')
  162. });
  163. $(this.el).i18n();
  164. return this;
  165. },
  166. events: {
  167. 'click .btn-edit': 'editWhitelist',
  168. 'click .btn-delete': 'deleteWhitelist',
  169. 'click .toggleMoreInformation': 'toggleMoreInformation'
  170. },
  171. editWhitelist: function(e) {
  172. e.preventDefault();
  173. app.navigate('admin/whitelist/' + this.model.get('id'), {
  174. trigger: true
  175. });
  176. },
  177. deleteWhitelist: function(e) {
  178. e.preventDefault();
  179. if (confirm($.t('whitelist.confirm'))) {
  180. var _self = this;
  181. this.model.destroy({
  182. dataType: false,
  183. processData: false,
  184. success: function() {
  185. _self.$el.fadeTo("fast", 0.00, function() { // fade
  186. $(this).slideUp("fast", function() { // slide up
  187. $(this).remove(); // then remove from the DOM
  188. // check the placeholder in case it's empty now
  189. _self.parentView.togglePlaceholder();
  190. });
  191. });
  192. },
  193. error: app.errorHandlerView.handleError()
  194. });
  195. _self.parentView.delegateEvents();
  196. }
  197. return false;
  198. },
  199. toggleMoreInformation: function(e) {
  200. e.preventDefault();
  201. if ($('.moreInformation', this.el).is(':visible')) {
  202. // hide it
  203. $('.moreInformation', this.el).hide('fast');
  204. $('.toggleMoreInformation i', this.el).attr('class', 'icon-chevron-right');
  205. $('.moreInformationContainer', this.el).removeClass('alert').removeClass('alert-info').addClass('muted');
  206. } else {
  207. // show it
  208. $('.moreInformation', this.el).show('fast');
  209. $('.toggleMoreInformation i', this.el).attr('class', 'icon-chevron-down');
  210. $('.moreInformationContainer', this.el).addClass('alert').addClass('alert-info').removeClass('muted');
  211. }
  212. },
  213. close: function() {
  214. $(this.el).unbind();
  215. $(this.el).empty();
  216. }
  217. });
  218. var WhiteListFormView = Backbone.View.extend({
  219. tagName: 'span',
  220. initialize: function(options) {
  221. this.options = options;
  222. if (!this.template) {
  223. this.template = _.template($('#tmpl-whitelist-form').html());
  224. }
  225. this.scopeCollection = new Backbone.Collection();
  226. this.listWidgetViews = [];
  227. },
  228. load: function(callback) {
  229. if (this.options.client) {
  230. // we know what client we're dealing with already
  231. if (this.model.isFetched && this.options.client.isFetched) {
  232. callback();
  233. return;
  234. }
  235. $('#loadingbox').sheet('show');
  236. $('#loading').html('<span class="label" id="loading-whitelist">' + $.t('whitelist.whitelist') + '</span> ' + '<span class="label" id="loading-clients">' + $.t('common.clients') + '</span> ' + '<span class="label" id="loading-scopes">' + $.t('common.scopes') + '</span> ');
  237. $.when(this.model.fetchIfNeeded({
  238. success: function(e) {
  239. $('#loading-whitelist').addClass('label-success');
  240. },
  241. error: app.errorHandlerView.handleError()
  242. }), this.options.client.fetchIfNeeded({
  243. success: function(e) {
  244. $('#loading-clients').addClass('label-success');
  245. },
  246. error: app.errorHandlerView.handleError()
  247. }), this.options.systemScopeList.fetchIfNeeded({
  248. success: function(e) {
  249. $('#loading-scopes').addClass('label-success');
  250. },
  251. error: app.errorHandlerView.handleError()
  252. })).done(function() {
  253. $('#loadingbox').sheet('hide');
  254. callback();
  255. });
  256. } else {
  257. // we need to get the client information from the list
  258. if (this.model.isFetched && this.options.clientList.isFetched && this.options.systemScopeList.isFetched) {
  259. var client = this.options.clientList.getByClientId(this.model.get('clientId'));
  260. this.options.client = client;
  261. callback();
  262. return;
  263. }
  264. $('#loadingbox').sheet('show');
  265. $('#loading').html('<span class="label" id="loading-whitelist">' + $.t('whitelist.whitelist') + '</span> ' + '<span class="label" id="loading-clients">' + $.t('common.clients') + '</span> ' + '<span class="label" id="loading-scopes">' + $.t('common.scopes') + '</span> ');
  266. var _self = this;
  267. $.when(this.model.fetchIfNeeded({
  268. success: function(e) {
  269. $('#loading-whitelist').addClass('label-success');
  270. },
  271. error: app.errorHandlerView.handleError()
  272. }), this.options.clientList.fetchIfNeeded({
  273. success: function(e) {
  274. $('#loading-clients').addClass('label-success');
  275. },
  276. error: app.errorHandlerView.handleError()
  277. }), this.options.systemScopeList.fetchIfNeeded({
  278. success: function(e) {
  279. $('#loading-scopes').addClass('label-success');
  280. },
  281. error: app.errorHandlerView.handleError()
  282. })).done(function() {
  283. var client = _self.options.clientList.getByClientId(_self.model.get('clientId'));
  284. _self.options.client = client;
  285. $('#loadingbox').sheet('hide');
  286. callback();
  287. });
  288. }
  289. },
  290. events: {
  291. 'click .btn-save': 'saveWhiteList',
  292. 'click .btn-cancel': 'cancelWhiteList',
  293. },
  294. saveWhiteList: function(e) {
  295. e.preventDefault();
  296. $('.control-group').removeClass('error');
  297. // sync any leftover collection items
  298. _.each(this.listWidgetViews, function(v) {
  299. v.addItem($.Event('click'));
  300. });
  301. // process allowed scopes
  302. var allowedScopes = this.scopeCollection.pluck("item");
  303. this.model.set({
  304. clientId: this.options.client.get('clientId')
  305. }, {
  306. silent: true
  307. });
  308. var valid = this.model.set({
  309. allowedScopes: allowedScopes
  310. });
  311. if (valid) {
  312. var _self = this;
  313. this.model.save({}, {
  314. success: function() {
  315. app.whiteListList.add(_self.model);
  316. app.navigate('admin/whitelists', {
  317. trigger: true
  318. });
  319. },
  320. error: app.errorHandlerView.handleError()
  321. });
  322. }
  323. return false;
  324. },
  325. cancelWhiteList: function(e) {
  326. e.preventDefault();
  327. // TODO: figure out where we came from and go back there instead
  328. if (this.model.get('id') == null) {
  329. // if it's a new whitelist entry, go back to the client listing page
  330. app.navigate('admin/clients', {
  331. trigger: true
  332. });
  333. } else {
  334. // if we're editing a whitelist, go back to the whitelists page
  335. app.navigate('admin/whitelists', {
  336. trigger: true
  337. });
  338. }
  339. },
  340. render: function(eventName) {
  341. var json = {
  342. whiteList: this.model.toJSON(),
  343. client: this.options.client.toJSON()
  344. };
  345. this.$el.html(this.template(json));
  346. this.listWidgetViews = [];
  347. var _self = this;
  348. // build and bind scopes
  349. _.each(this.model.get("allowedScopes"), function(scope) {
  350. _self.scopeCollection.add(new Backbone.Model({
  351. item: scope
  352. }));
  353. });
  354. var scopeView = new ListWidgetView({
  355. placeholder: $.t('whitelist.whitelist-form.scope-placeholder'),
  356. autocomplete: this.options.client.get("scope"),
  357. helpBlockText: $.t('whitelist.whitelist-form.scope-help'),
  358. collection: this.scopeCollection
  359. });
  360. $("#scope .controls", this.el).html(scopeView.render().el);
  361. this.listWidgetViews.push(scopeView);
  362. $(this.el).i18n();
  363. return this;
  364. }
  365. });
  366. ui.routes.push({
  367. path: "admin/whitelists",
  368. name: "whiteList",
  369. callback: function() {
  370. if (!isAdmin()) {
  371. this.root();
  372. return;
  373. }
  374. this.updateSidebar('admin/whitelists');
  375. this.breadCrumbView.collection.reset();
  376. this.breadCrumbView.collection.add([{
  377. text: $.t('admin.home'),
  378. href: ""
  379. }, {
  380. text: $.t('whitelist.manage'),
  381. href: "manage/#admin/whitelists"
  382. }]);
  383. var view = new WhiteListListView({
  384. model: this.whiteListList,
  385. clientList: this.clientList,
  386. systemScopeList: this.systemScopeList
  387. });
  388. view.load(function() {
  389. $('#content').html(view.render().el);
  390. view.delegateEvents();
  391. setPageTitle($.t('whitelist.manage'));
  392. });
  393. }
  394. });
  395. ui.routes.push({
  396. path: "admin/whitelist/new/:cid",
  397. name: "newWhitelist",
  398. callback: function(cid) {
  399. if (!isAdmin()) {
  400. this.root();
  401. return;
  402. }
  403. this.breadCrumbView.collection.reset();
  404. this.breadCrumbView.collection.add([{
  405. text: $.t('admin.home'),
  406. href: ""
  407. }, {
  408. text: $.t('whitelist.manage'),
  409. href: "manage/#admin/whitelists"
  410. }, {
  411. text: $.t('whitelist.new'),
  412. href: "manage/#admin/whitelist/new/" + cid
  413. }]);
  414. this.updateSidebar('admin/whitelists');
  415. var whiteList = new WhiteListModel();
  416. var client = this.clientList.get(cid);
  417. if (!client) {
  418. client = new ClientModel({
  419. id: cid
  420. });
  421. }
  422. var view = new WhiteListFormView({
  423. model: whiteList,
  424. client: client,
  425. systemScopeList: this.systemScopeList
  426. });
  427. view.load(function() {
  428. // set the scopes on the model now that everything's loaded
  429. whiteList.set({
  430. allowedScopes: client.get('scope')
  431. }, {
  432. silent: true
  433. });
  434. $('#content').html(view.render().el);
  435. view.delegateEvents();
  436. setPageTitle($.t('whitelist.manage'));
  437. });
  438. }
  439. });
  440. ui.routes.push({
  441. path: "admin/whitelist/:id",
  442. name: "editWhitelist",
  443. callback: function(id) {
  444. if (!isAdmin()) {
  445. this.root();
  446. return;
  447. }
  448. this.breadCrumbView.collection.reset();
  449. this.breadCrumbView.collection.add([{
  450. text: $.t('admin.home'),
  451. href: ""
  452. }, {
  453. text: $.t('whitelist.manage'),
  454. href: "manage/#admin/whitelists"
  455. }, {
  456. text: $.t('whitelist.edit'),
  457. href: "manage/#admin/whitelist/" + id
  458. }]);
  459. this.updateSidebar('admin/whitelists');
  460. var whiteList = this.whiteListList.get(id);
  461. if (!whiteList) {
  462. whiteList = new WhiteListModel({
  463. id: id
  464. });
  465. }
  466. var view = new WhiteListFormView({
  467. model: whiteList,
  468. clientList: this.clientList,
  469. systemScopeList: this.systemScopeList
  470. });
  471. view.load(function() {
  472. $('#content').html(view.render().el);
  473. view.delegateEvents();
  474. setPageTitle($.t('whitelist.manage'));
  475. });
  476. }
  477. });
  478. ui.templates.push('resources/template/whitelist.html');
  479. ui.init.push(function(app) {
  480. app.whiteListList = new WhiteListCollection();
  481. });