PageRenderTime 87ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/jslee1/OpenID-Connect-Java-Spring-Server
JavaScript | 275 lines | 198 code | 55 blank | 22 comment | 27 complexity | 0204dc12af9a755bcfc05591f3374a13 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright 2016 The MITRE Corporation
  3. * and the MIT Internet Trust Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *******************************************************************************/
  17. var ApprovedSiteModel = Backbone.Model.extend({
  18. idAttribute: 'id',
  19. initialize: function() { },
  20. urlRoot: 'api/approved'
  21. });
  22. var ApprovedSiteCollection = Backbone.Collection.extend({
  23. initialize: function() { },
  24. model: ApprovedSiteModel,
  25. url: 'api/approved'
  26. });
  27. var ApprovedSiteListView = Backbone.View.extend({
  28. tagName: 'span',
  29. initialize:function(options) {
  30. this.options = options;
  31. },
  32. load:function(callback) {
  33. if (this.model.isFetched &&
  34. this.options.clientList.isFetched &&
  35. this.options.systemScopeList.isFetched) {
  36. callback();
  37. return;
  38. }
  39. $('#loadingbox').sheet('show');
  40. $('#loading').html(
  41. '<span class="label" id="loading-grants">' + $.t('grant.grant-table.approved-sites') + '</span> ' +
  42. '<span class="label" id="loading-clients">' + $.t('common.clients') + '</span> ' +
  43. '<span class="label" id="loading-scopes">' + $.t('common.scopes') + '</span> '
  44. );
  45. $.when(this.model.fetchIfNeeded({success:function(e) {$('#loading-grants').addClass('label-success');}}),
  46. this.options.clientList.fetchIfNeeded({success:function(e) {$('#loading-clients').addClass('label-success');}}),
  47. this.options.systemScopeList.fetchIfNeeded({success:function(e) {$('#loading-scopes').addClass('label-success');}}))
  48. .done(function() {
  49. $('#loadingbox').sheet('hide');
  50. callback();
  51. });
  52. },
  53. events: {
  54. "click .refresh-table":"refreshTable"
  55. },
  56. render:function (eventName) {
  57. $(this.el).html($('#tmpl-grant-table').html());
  58. var approvedSiteCount = 0;
  59. var _self = this;
  60. _.each(this.model.models, function(approvedSite) {
  61. // look up client
  62. var client = this.options.clientList.getByClientId(approvedSite.get('clientId'));
  63. if (client != null) {
  64. var view = new ApprovedSiteView({model: approvedSite, client: client, systemScopeList: this.options.systemScopeList});
  65. view.parentView = _self;
  66. $('#grant-table', this.el).append(view.render().el);
  67. approvedSiteCount = approvedSiteCount + 1;
  68. }
  69. }, this);
  70. this.togglePlaceholder();
  71. $(this.el).i18n();
  72. return this;
  73. },
  74. togglePlaceholder:function() {
  75. // count entries
  76. if (this.model.length > 0) {
  77. $('#grant-table', this.el).show();
  78. $('#grant-table-empty', this.el).hide();
  79. } else {
  80. $('#grant-table', this.el).hide();
  81. $('#grant-table-empty', this.el).show();
  82. }
  83. },
  84. refreshTable:function(e) {
  85. e.preventDefault();
  86. var _self = this;
  87. $('#loadingbox').sheet('show');
  88. $('#loading').html(
  89. '<span class="label" id="loading-grants">' + $.t('grant.grant-table.approved-sites') + '</span> ' +
  90. '<span class="label" id="loading-clients">' + $.t('common.clients') + '</span> ' +
  91. '<span class="label" id="loading-scopes">' + $.t('common.scopes') + '</span> '
  92. );
  93. $.when(this.model.fetch({success:function(e) {$('#loading-grants').addClass('label-success');}}),
  94. this.options.clientList.fetch({success:function(e) {$('#loading-clients').addClass('label-success');}}),
  95. this.options.systemScopeList.fetch({success:function(e) {$('#loading-scopes').addClass('label-success');}}))
  96. .done(function() {
  97. $('#loadingbox').sheet('hide');
  98. _self.render();
  99. });
  100. }
  101. });
  102. var ApprovedSiteView = Backbone.View.extend({
  103. tagName: 'tr',
  104. initialize: function(options) {
  105. this.options = options;
  106. if (!this.template) {
  107. this.template = _.template($('#tmpl-grant').html());
  108. }
  109. if (!this.scopeTemplate) {
  110. this.scopeTemplate = _.template($('#tmpl-scope-list').html());
  111. }
  112. if (!this.moreInfoTemplate) {
  113. this.moreInfoTemplate = _.template($('#tmpl-client-more-info-block').html());
  114. }
  115. },
  116. render: function() {
  117. var creationDate = this.model.get("creationDate");
  118. var accessDate = this.model.get("accessDate");
  119. var timeoutDate = this.model.get("timeoutDate");
  120. var displayCreationDate = $.t('grant.grant-table.unknown');
  121. var hoverCreationDate = "";
  122. if ((creationDate != null) && moment(creationDate).isValid()) {
  123. creationDate = moment(creationDate);
  124. if (moment().diff(creationDate, 'months') < 6) {
  125. displayCreationDate = creationDate.fromNow();
  126. } else {
  127. displayCreationDate = creationDate.format("LL");
  128. }
  129. hoverCreationDate = creationDate.format("LLL");
  130. }
  131. var displayAccessDate = $.t('grant.grant-table.unknown');
  132. var hoverAccessDate = "";
  133. if ((accessDate != null) && moment(accessDate).isValid()) {
  134. accessDate = moment(accessDate);
  135. if (moment().diff(accessDate, 'months') < 6) {
  136. displayAccessDate = accessDate.fromNow();
  137. } else {
  138. displayAccessDate = accessDate.format("LL");
  139. }
  140. hoverAccessDate = accessDate.format("LLL");
  141. }
  142. var displayTimeoutDate = $.t('grant.grant-table.unknown');
  143. var hoverTimeoutDate = "";
  144. if (timeoutDate == null) {
  145. displayTimeoutDate = $.t('grant.grant-table.never');
  146. } else if(moment(timeoutDate).isValid()) {
  147. timeoutDate = moment(timeoutDate);
  148. if (moment().diff(timeoutDate, 'months') < 6) {
  149. displayTimeoutDate = timeoutDate.fromNow();
  150. } else {
  151. displayTimeoutDate = timeoutDate.format("LL");
  152. }
  153. hoverTimeoutDate = timeoutDate.format("LLL");
  154. }
  155. var formattedDate = {displayCreationDate: displayCreationDate, hoverCreationDate: hoverCreationDate,
  156. displayAccessDate: displayAccessDate, hoverAccessDate: hoverAccessDate,
  157. displayTimeoutDate: displayTimeoutDate, hoverTimeoutDate: hoverTimeoutDate};
  158. var json = {grant: this.model.toJSON(), client: this.options.client.toJSON(), formattedDate: formattedDate};
  159. this.$el.html(this.template(json));
  160. $('.scope-list', this.el).html(this.scopeTemplate({scopes: this.model.get('allowedScopes'), systemScopes: this.options.systemScopeList}));
  161. $('.client-more-info-block', this.el).html(this.moreInfoTemplate({client: this.options.client.toJSON()}));
  162. this.$('.dynamically-registered').tooltip({title: $.t('grant.grant-table.dynamically-registered')});
  163. this.$('.tokens').tooltip({title: $.t('grant.grant-table.active-tokens')});
  164. $(this.el).i18n();
  165. return this;
  166. },
  167. events: {
  168. 'click .btn-delete': 'deleteApprovedSite',
  169. 'click .toggleMoreInformation': 'toggleMoreInformation'
  170. },
  171. deleteApprovedSite:function(e) {
  172. e.preventDefault();
  173. if (confirm("Are you sure you want to revoke access to this site?")) {
  174. var self = this;
  175. this.model.destroy({
  176. dataType: false, processData: false,
  177. success:function () {
  178. self.$el.fadeTo("fast", 0.00, function () { //fade
  179. $(this).slideUp("fast", function () { //slide up
  180. $(this).remove(); //then remove from the DOM
  181. self.parentView.togglePlaceholder();
  182. });
  183. });
  184. },
  185. error:function (error, response) {
  186. //Pull out the response text.
  187. var responseJson = JSON.parse(response.responseText);
  188. //Display an alert with an error message
  189. $('#modalAlert div.modal-header').html(responseJson.error);
  190. $('#modalAlert div.modal-body').html(responseJson.error_description);
  191. $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
  192. "backdrop" : "static",
  193. "keyboard" : true,
  194. "show" : true // ensure the modal is shown immediately
  195. });
  196. }
  197. });
  198. this.parentView.delegateEvents();
  199. }
  200. return false;
  201. },
  202. toggleMoreInformation:function(e) {
  203. e.preventDefault();
  204. if ($('.moreInformation', this.el).is(':visible')) {
  205. // hide it
  206. $('.moreInformation', this.el).hide('fast');
  207. $('.toggleMoreInformation i', this.el).attr('class', 'icon-chevron-right');
  208. $('.moreInformationContainer', this.el).removeClass('alert').removeClass('alert-info').addClass('muted');
  209. } else {
  210. // show it
  211. $('.moreInformation', this.el).show('fast');
  212. $('.toggleMoreInformation i', this.el).attr('class', 'icon-chevron-down');
  213. $('.moreInformationContainer', this.el).addClass('alert').addClass('alert-info').removeClass('muted');
  214. }
  215. },
  216. close:function() {
  217. $(this.el).unbind();
  218. $(this.el).empty();
  219. }
  220. });