UIWindowFinalizeUserDeletion.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import UIWindow from '../UIWindow.js'
  20. async function UIWindowFinalizeUserDeletion(options){
  21. return new Promise(async (resolve) => {
  22. options = options ?? {};
  23. let h = '';
  24. // if user is temporary, ask them to type in 'confirm' to delete their account
  25. if(user.is_temp){
  26. h += `<div style="padding: 20px;">`;
  27. h += `<div class="generic-close-window-button disable-user-select"> &times; </div>`;
  28. h += `<img src="${window.icons['danger.svg']}" class="account-deletion-confirmation-icon">`;
  29. h += `<p class="account-deletion-confirmation-prompt">${i18n('type_confirm_to_delete_account')}</p>`;
  30. // error message
  31. h += `<div class="error-message"></div>`;
  32. // input field
  33. h += `<input type="text" class="confirm-temporary-user-deletion" placeholder="${i18n('type_confirm_to_delete_account')}">`;
  34. h += `<button class="button button-block button-danger proceed-with-user-deletion">${i18n('delete_account')}</button>`;
  35. h += `<button class="button button-block button-secondary cancel-user-deletion">${i18n('cancel')}</button>`;
  36. h += `</div>`;
  37. }
  38. // otherwise ask for password
  39. else{
  40. h += `<div style="padding: 20px;">`;
  41. h += `<div class="generic-close-window-button disable-user-select"> &times; </div>`;
  42. h += `<img src="${window.icons['danger.svg']}" class="account-deletion-confirmation-icon">`;
  43. h += `<p class="account-deletion-confirmation-prompt">${i18n('enter_password_to_confirm_delete_user')}</p>`;
  44. // error message
  45. h += `<div class="error-message"></div>`;
  46. // input field
  47. h += `<input type="password" class="confirm-user-deletion-password" placeholder="${i18n('current_password')}">`;
  48. h += `<button class="button button-block button-danger proceed-with-user-deletion">${i18n('delete_account')}</button>`;
  49. h += `<button class="button button-block button-secondary cancel-user-deletion">${i18n('cancel')}</button>`;
  50. h += `</div>`;
  51. }
  52. const el_window = await UIWindow({
  53. title: i18n('confirm_delete_user_title'),
  54. icon: null,
  55. uid: null,
  56. is_dir: false,
  57. body_content: h,
  58. has_head: false,
  59. selectable_body: false,
  60. draggable_body: false,
  61. allow_context_menu: false,
  62. is_draggable: true,
  63. is_resizable: false,
  64. is_droppable: false,
  65. init_center: true,
  66. allow_native_ctxmenu: true,
  67. allow_user_select: true,
  68. backdrop: true,
  69. onAppend: function(el_window){
  70. },
  71. width: 500,
  72. dominant: false,
  73. window_css: {
  74. height: 'initial',
  75. padding: '0',
  76. border: 'none',
  77. boxShadow: '0 0 10px rgba(0,0,0,.2)',
  78. }
  79. });
  80. $(el_window).find('.generic-close-window-button').on('click', function(){
  81. $(el_window).close();
  82. });
  83. $(el_window).find('.cancel-user-deletion').on('click', function(){
  84. $(el_window).close();
  85. });
  86. $(el_window).find('.proceed-with-user-deletion').on('click', function(){
  87. $(el_window).find('.error-message').hide();
  88. // if user is temporary, check if they typed 'confirm'
  89. if(user.is_temp){
  90. if($(el_window).find('.confirm-temporary-user-deletion').val() !== 'confirm'){
  91. $(el_window).find('.error-message').html(i18n('type_confirm_to_delete_account'), false);
  92. $(el_window).find('.error-message').show();
  93. return;
  94. }
  95. }
  96. // otherwise, check if password is correct
  97. else{
  98. if($(el_window).find('.confirm-user-deletion-password').val() === ''){
  99. $(el_window).find('.error-message').html(i18n('all_fields_required'), false);
  100. $(el_window).find('.error-message').show();
  101. return;
  102. }
  103. }
  104. // delete user
  105. $.ajax({
  106. url: api_origin + "/delete-own-user",
  107. type: 'POST',
  108. async: true,
  109. contentType: "application/json",
  110. headers: {
  111. "Authorization": "Bearer " + auth_token
  112. },
  113. data: JSON.stringify({
  114. password: $(el_window).find('.confirm-user-deletion-password').val(),
  115. }),
  116. statusCode: {
  117. 401: function () {
  118. logout();
  119. },
  120. 400: function(){
  121. $(el_window).find('.error-message').html(i18n('incorrect_password'));
  122. $(el_window).find('.error-message').show();
  123. }
  124. },
  125. success: function(data){
  126. if(data.success){
  127. // mark user as deleted
  128. window.user.deleted = true;
  129. // log user out
  130. logout();
  131. }
  132. else{
  133. $(el_window).find('.error-message').html(data.error);
  134. $(el_window).find('.error-message').show();
  135. }
  136. }
  137. });
  138. });
  139. })
  140. }
  141. export default UIWindowFinalizeUserDeletion;