settingsview.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. var View = OC.Backbone.View.extend({
  33. _template: undefined,
  34. template: function (data) {
  35. if (!this._template) {
  36. this._template = Handlebars.compile(TEMPLATE);
  37. }
  38. return this._template(data);
  39. },
  40. _loading: undefined,
  41. _enabled: undefined,
  42. _total: undefined,
  43. _used: undefined,
  44. _codes: undefined,
  45. events: {
  46. 'click #generate-backup-codes': '_onGenerateBackupCodes',
  47. 'click #print-backup-codes': '_onPrintBackupCodes',
  48. },
  49. initialize: function () {
  50. this._load();
  51. },
  52. render: function () {
  53. this.$el.html(this.template({
  54. enabled: this._enabled,
  55. total: this._total,
  56. used: this._used,
  57. codes: this._codes,
  58. download: this._getDownloadDataHref()
  59. }));
  60. },
  61. _getDownloadDataHref: function () {
  62. if (!this._codes) {
  63. return '';
  64. }
  65. return 'data:text/plain,' + encodeURIComponent(_.reduce(this._codes, function (prev, code) {
  66. return prev + code + "\r\n";
  67. }, ''));
  68. },
  69. _load: function () {
  70. this._loading = true;
  71. var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/state');
  72. var loading = $.ajax(url, {
  73. method: 'GET',
  74. });
  75. $.when(loading).done(function (data) {
  76. this._enabled = data.enabled;
  77. this._total = data.total;
  78. this._used = data.used;
  79. }.bind(this));
  80. $.when(loading).always(function () {
  81. this._loading = false;
  82. this.render();
  83. }.bind(this));
  84. },
  85. _onGenerateBackupCodes: function () {
  86. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  87. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onGenerateBackupCodes, this));
  88. return;
  89. }
  90. // Hide old codes
  91. this._enabled = false;
  92. this.render();
  93. $('#generate-backup-codes').addClass('icon-loading-small');
  94. var url = OC.generateUrl('/apps/twofactor_backupcodes/settings/create');
  95. $.ajax(url, {
  96. method: 'POST'
  97. }).done(function (data) {
  98. this._enabled = data.state.enabled;
  99. this._total = data.state.total;
  100. this._used = data.state.used;
  101. this._codes = data.codes;
  102. this.render();
  103. }.bind(this)).fail(function () {
  104. OC.Notification.showTemporary(t('twofactor_backupcodes', 'An error occurred while generating your backup codes'));
  105. $('#generate-backup-codes').removeClass('icon-loading-small');
  106. });
  107. },
  108. _onPrintBackupCodes: function () {
  109. var url = this._getDownloadDataHref();
  110. window.open(url, t('twofactor_backupcodes', 'Nextcloud backup codes'));
  111. window.print();
  112. window.close();
  113. }
  114. });
  115. OC.Settings.TwoFactorBackupCodes.View = View;
  116. })(OC, Handlebars, $, _);