EditUser.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <div>
  3. <modal title='Edit User'>
  4. <div slot='body'>
  5. <p class="control has-addons">
  6. <input class='input is-expanded' type='text' placeholder='Username' v-model='editing.username' autofocus>
  7. <a class="button is-info" @click='updateUsername()'>Update Username</a>
  8. </p>
  9. <p class="control has-addons">
  10. <input class='input is-expanded' type='text' placeholder='Username' v-model='editing.email' autofocus>
  11. <a class="button is-info" @click='updateEmail()'>Update Email Address</a>
  12. </p>
  13. <p class="control has-addons">
  14. <span class="select">
  15. <select v-model="editing.role">
  16. <option>default</option>
  17. <option>admin</option>
  18. </select>
  19. </span>
  20. <a class="button is-info" @click='updateRole()'>Update Role</a>
  21. </p>
  22. <hr>
  23. <p class="control has-addons">
  24. <input class='input is-expanded' type='text' placeholder='Ban reason' v-model='ban.reason' autofocus>
  25. <a class="button is-error" @click='banUser()'>Ban user</a>
  26. </p>
  27. </div>
  28. <div slot='footer'>
  29. <!--button class='button is-warning'>
  30. <span>&nbsp;Send Verification Email</span>
  31. </button>
  32. <button class='button is-warning'>
  33. <span>&nbsp;Send Password Reset Email</span>
  34. </button-->
  35. <button class='button is-warning' @click='removeSessions()'>
  36. <span>&nbsp;Remove all sessions</span>
  37. </button>
  38. <button class='button is-danger' @click='$parent.toggleModal()'>
  39. <span>&nbsp;Close</span>
  40. </button>
  41. </div>
  42. </modal>
  43. </div>
  44. </template>
  45. <script>
  46. import io from '../../io';
  47. import { Toast } from 'vue-roaster';
  48. import Modal from './Modal.vue';
  49. import validation from '../../validation';
  50. export default {
  51. components: { Modal },
  52. data() {
  53. return {
  54. editing: {},
  55. ban: {}
  56. }
  57. },
  58. methods: {
  59. updateUsername: function () {
  60. const username = this.editing.username;
  61. if (!validation.isLength(username, 2, 32)) return Toast.methods.addToast('Username must have between 2 and 32 characters.', 8000);
  62. if (!validation.regex.azAZ09_.test(username)) return Toast.methods.addToast('Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _.', 8000);
  63. this.socket.emit(`users.updateUsername`, this.editing._id, username, res => {
  64. Toast.methods.addToast(res.message, 4000);
  65. });
  66. },
  67. updateEmail: function () {
  68. const email = this.editing.email;
  69. if (!validation.isLength(email, 3, 254)) return Toast.methods.addToast('Email must have between 3 and 254 characters.', 8000);
  70. if (email.indexOf('@') !== email.lastIndexOf('@') || !validation.regex.emailSimple.test(email)) return Toast.methods.addToast('Invalid email format.', 8000);
  71. this.socket.emit(`users.updateEmail`, this.editing._id, email, res => {
  72. Toast.methods.addToast(res.message, 4000);
  73. });
  74. },
  75. updateRole: function () {
  76. this.socket.emit(`users.updateRole`, this.editing._id, this.editing.role, res => {
  77. Toast.methods.addToast(res.message, 4000);
  78. if (
  79. res.status === 'success' &&
  80. this.editing.role === 'default' &&
  81. this.editing._id === this.$parent.$parent.$parent.userId
  82. ) location.reload();
  83. });
  84. },
  85. banUser: function () {
  86. const reason = this.ban.reason;
  87. if (!validation.isLength(reason, 1, 64)) return Toast.methods.addToast('Reason must have between 1 and 64 characters.', 8000);
  88. if (!validation.regex.ascii.test(reason)) return Toast.methods.addToast('Invalid reason format. Only ascii characters are allowed.', 8000);
  89. this.socket.emit(`users.banUserById`, this.editing._id, this.ban.reason, '1h', res => {
  90. Toast.methods.addToast(res.message, 4000);
  91. });
  92. },
  93. removeSessions: function () {
  94. this.socket.emit(`users.removeSessions`, this.editing._id, res => {
  95. Toast.methods.addToast(res.message, 4000);
  96. });
  97. }
  98. },
  99. ready: function () {
  100. let _this = this;
  101. io.getSocket(socket => _this.socket = socket );
  102. },
  103. events: {
  104. closeModal: function () {
  105. this.$parent.modals.editUser = false;
  106. },
  107. editUser: function (user) {
  108. this.editing = {
  109. _id: user._id,
  110. username: user.username,
  111. email: user.email.address,
  112. role: user.role
  113. };
  114. this.$parent.toggleModal();
  115. }
  116. }
  117. }
  118. </script>
  119. <style type='scss' scoped>
  120. .save-changes { color: #fff; }
  121. .tag:not(:last-child) { margin-right: 5px; }
  122. .select:after { border-color: #029ce3; }
  123. </style>