1
0

settingsview.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* global Backbone, Handlebars, OC, _ */
  2. (function(OC, Handlebars, $, _) {
  3. 'use strict';
  4. OC.Settings = OC.Settings || {};
  5. OC.Settings.TwoFactorBackupCodes = OC.Settings.TwoFactorBackupCodes || {};
  6. var TEMPLATE = '<div>'
  7. + '{{#unless enabled}}'
  8. + '<button id="generate-backup-codes">' + t('twofactor_backupcodes', 'Generate backup codes') + '</button>'
  9. + '{{else}}'
  10. + '<p>'
  11. + '{{#unless codes}}'
  12. + t('twofactor_backupcodes', 'Backup codes have been generated. {{used}} of {{total}} codes have been used.')
  13. + '{{else}}'
  14. + t('twofactor_backupcodes', 'These are your backup codes. Please save and/or print them as you will not be able to read the codes again later')
  15. + '<ul>'
  16. + '{{#each codes}}'
  17. + '<li class="backup-code">{{this}}</li>'
  18. + '{{/each}}'
  19. + '</ul>'
  20. + '<a href="{{download}}" class="button" download="Nextcloud-backup-codes.txt">' + t('twofactor_backupcodes', 'Save backup codes') + '</a>'
  21. + '<button id="print-backup-codes" class="button">' + t('twofactor_backupcodes', 'Print backup codes') + '</button>'
  22. + '{{/unless}}'
  23. + '</p>'
  24. + '<p>'
  25. + '<button id="generate-backup-codes">' + t('twofactor_backupcodes', 'Regenerate backup codes') + '</button>'
  26. + '</p>'
  27. + '<p>'
  28. + t('twofactor_backupcodes', 'If you regenerate backup codes, you automatically invalidate old codes.')
  29. + '</p>'
  30. + '{{/unless}}'
  31. + '</div';
  32. /**
  33. * @class OC.Settings.TwoFactorBackupCodes.View
  34. */
  35. var View = OC.Backbone.View.extend({
  36. /**
  37. * @type {undefined|Function}
  38. */
  39. _template: undefined,
  40. /**
  41. * @param {Object} data
  42. * @returns {string}
  43. */
  44. template: function(data) {
  45. if (!this._template) {
  46. this._template = Handlebars.compile(TEMPLATE);
  47. }
  48. return this._template(data);
  49. },
  50. /**
  51. * @type {boolean}
  52. */
  53. _loading: undefined,
  54. /**
  55. * @type {boolean}
  56. */
  57. _enabled: undefined,
  58. /**
  59. * @type {Number}
  60. */
  61. _total: undefined,
  62. /**
  63. * @type {Number}
  64. */
  65. _used: undefined,
  66. /**
  67. * @type {Array}
  68. */
  69. _codes: undefined,
  70. events: {
  71. 'click #generate-backup-codes': '_onGenerateBackupCodes',
  72. 'click #print-backup-codes': '_onPrintBackupCodes'
  73. },
  74. /**
  75. * @returns {undefined}
  76. */
  77. initialize: function() {
  78. this._load();
  79. },
  80. /**
  81. * @returns {self}
  82. */
  83. render: function() {
  84. this.$el.html(this.template({
  85. enabled: this._enabled,
  86. total: this._total,
  87. used: this._used,
  88. codes: this._codes,
  89. download: this._getDownloadData()
  90. }));
  91. return this;
  92. },
  93. /**
  94. * @private
  95. * @returns {String}
  96. */
  97. _getDownloadData: function() {
  98. if (!this._codes) {
  99. return '';
  100. }
  101. return 'data:text/plain,' + encodeURIComponent(_.reduce(this._codes, function(prev, code) {
  102. return prev + code + '\r\n';
  103. }, ''));
  104. },
  105. /**
  106. * @private
  107. * @returns {String}
  108. */
  109. _getPrintData: function() {
  110. if (!this._codes) {
  111. return '';
  112. }
  113. return _.reduce(this._codes, function(prev, code) {
  114. return prev + code + "<br>";
  115. }, '');
  116. },
  117. /**
  118. * Load codes from the server
  119. *
  120. * @returns {undefined}
  121. */
  122. _load: function() {
  123. this._loading = true;
  124. var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/state');
  125. var loading = $.ajax(url, {
  126. method: 'GET'
  127. });
  128. $.when(loading).done(function(data) {
  129. this._enabled = data.enabled;
  130. this._total = data.total;
  131. this._used = data.used;
  132. }.bind(this));
  133. $.when(loading).always(function() {
  134. this._loading = false;
  135. this.render();
  136. }.bind(this));
  137. },
  138. /**
  139. * Event handler to generate the codes
  140. *
  141. * @returns {undefined}
  142. */
  143. _onGenerateBackupCodes: function() {
  144. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  145. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onGenerateBackupCodes, this));
  146. return;
  147. }
  148. // Hide old codes
  149. this._enabled = false;
  150. this.render();
  151. $('#generate-backup-codes').addClass('icon-loading-small');
  152. var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/create');
  153. $.ajax(url, {
  154. method: 'POST'
  155. }).done(function(data) {
  156. this._enabled = data.state.enabled;
  157. this._total = data.state.total;
  158. this._used = data.state.used;
  159. this._codes = data.codes;
  160. this.render();
  161. }.bind(this)).fail(function() {
  162. OC.Notification.showTemporary(t('twofactor_backupcodes', 'An error occurred while generating your backup codes'));
  163. $('#generate-backup-codes').removeClass('icon-loading-small');
  164. });
  165. },
  166. /**
  167. * Event handler to print the codes
  168. *
  169. * @returns {undefined}
  170. */
  171. _onPrintBackupCodes: function() {
  172. var data = this._getPrintData();
  173. var newTab = window.open('', t('twofactor_backupcodes', 'Nextcloud backup codes'));
  174. newTab.document.write('<h1>' + t('twofactor_backupcodes', 'Nextcloud backup codes') + '</h1>');
  175. newTab.document.write(data);
  176. newTab.print();
  177. newTab.close();
  178. }
  179. });
  180. OC.Settings.TwoFactorBackupCodes.View = View;
  181. })(OC, Handlebars, $, _);