EditUser.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <div>
  3. <modal title="Edit User">
  4. <div slot="body">
  5. <p class="control has-addons">
  6. <input
  7. v-model="editing.username"
  8. class="input is-expanded"
  9. type="text"
  10. placeholder="Username"
  11. autofocus
  12. />
  13. <a class="button is-info" v-on:click="updateUsername()"
  14. >Update Username</a
  15. >
  16. </p>
  17. <p class="control has-addons">
  18. <input
  19. v-model="editing.email.address"
  20. class="input is-expanded"
  21. type="text"
  22. placeholder="Email Address"
  23. autofocus
  24. />
  25. <a class="button is-info" v-on:click="updateEmail()"
  26. >Update Email Address</a
  27. >
  28. </p>
  29. <p class="control has-addons">
  30. <span class="select">
  31. <select v-model="editing.role">
  32. <option>default</option>
  33. <option>admin</option>
  34. </select>
  35. </span>
  36. <a class="button is-info" v-on:click="updateRole()"
  37. >Update Role</a
  38. >
  39. </p>
  40. <hr />
  41. <p class="control has-addons">
  42. <span class="select">
  43. <select v-model="ban.expiresAt">
  44. <option value="1h">1 Hour</option>
  45. <option value="12h">12 Hours</option>
  46. <option value="1d">1 Day</option>
  47. <option value="1w">1 Week</option>
  48. <option value="1m">1 Month</option>
  49. <option value="3m">3 Months</option>
  50. <option value="6m">6 Months</option>
  51. <option value="1y">1 Year</option>
  52. </select>
  53. </span>
  54. <input
  55. v-model="ban.reason"
  56. class="input is-expanded"
  57. type="text"
  58. placeholder="Ban reason"
  59. autofocus
  60. />
  61. <a class="button is-error" v-on:click="banUser()"
  62. >Ban user</a
  63. >
  64. </p>
  65. </div>
  66. <div slot="footer">
  67. <!--button class='button is-warning'>
  68. <span>&nbsp;Send Verification Email</span>
  69. </button>
  70. <button class='button is-warning'>
  71. <span>&nbsp;Send Password Reset Email</span>
  72. </button-->
  73. <button class="button is-warning" v-on:click="removeSessions()">
  74. <span>&nbsp;Remove all sessions</span>
  75. </button>
  76. <button
  77. class="button is-danger"
  78. @click="
  79. closeModal({
  80. sector: 'admin',
  81. modal: 'editUser'
  82. })
  83. "
  84. >
  85. <span>&nbsp;Close</span>
  86. </button>
  87. </div>
  88. </modal>
  89. </div>
  90. </template>
  91. <script>
  92. import { mapState, mapActions } from "vuex";
  93. import Toast from "toasters";
  94. import io from "../../io";
  95. import Modal from "./Modal.vue";
  96. import validation from "../../validation";
  97. export default {
  98. components: { Modal },
  99. data() {
  100. return {
  101. ban: {
  102. expiresAt: "1h"
  103. }
  104. };
  105. },
  106. computed: {
  107. ...mapState("admin/users", {
  108. editing: state => state.editing
  109. }),
  110. ...mapState({
  111. userId: state => state.user.auth.userId
  112. })
  113. },
  114. methods: {
  115. updateUsername() {
  116. const { username } = this.editing;
  117. if (!validation.isLength(username, 2, 32))
  118. return new Toast({
  119. content: "Username must have between 2 and 32 characters.",
  120. timeout: 8000
  121. });
  122. if (!validation.regex.custom("a-zA-Z0-9_-").test(username))
  123. return new Toast({
  124. content:
  125. "Invalid username format. Allowed characters: a-z, A-Z, 0-9, _ and -.",
  126. timeout: 8000
  127. });
  128. return this.socket.emit(
  129. `users.updateUsername`,
  130. this.editing._id,
  131. username,
  132. res => {
  133. new Toast({ content: res.message, timeout: 4000 });
  134. }
  135. );
  136. },
  137. updateEmail() {
  138. const email = this.editing.email.address;
  139. if (!validation.isLength(email, 3, 254))
  140. return new Toast({
  141. content: "Email must have between 3 and 254 characters.",
  142. timeout: 8000
  143. });
  144. if (
  145. email.indexOf("@") !== email.lastIndexOf("@") ||
  146. !validation.regex.emailSimple.test(email) ||
  147. !validation.regex.ascii.test(email)
  148. )
  149. return new Toast({
  150. content: "Invalid email format.",
  151. timeout: 8000
  152. });
  153. return this.socket.emit(
  154. `users.updateEmail`,
  155. this.editing._id,
  156. email,
  157. res => {
  158. new Toast({ content: res.message, timeout: 4000 });
  159. }
  160. );
  161. },
  162. updateRole() {
  163. this.socket.emit(
  164. `users.updateRole`,
  165. this.editing._id,
  166. this.editing.role,
  167. res => {
  168. new Toast({ content: res.message, timeout: 4000 });
  169. if (
  170. res.status === "success" &&
  171. this.editing.role === "default" &&
  172. this.editing._id === this.userId
  173. )
  174. window.location.reload();
  175. }
  176. );
  177. },
  178. banUser() {
  179. const { reason } = this.ban;
  180. if (!validation.isLength(reason, 1, 64))
  181. return new Toast({
  182. content: "Reason must have between 1 and 64 characters.",
  183. timeout: 8000
  184. });
  185. if (!validation.regex.ascii.test(reason))
  186. return new Toast({
  187. content:
  188. "Invalid reason format. Only ascii characters are allowed.",
  189. timeout: 8000
  190. });
  191. return this.socket.emit(
  192. `users.banUserById`,
  193. this.editing._id,
  194. this.ban.reason,
  195. this.ban.expiresAt,
  196. res => {
  197. new Toast({ content: res.message, timeout: 4000 });
  198. }
  199. );
  200. },
  201. removeSessions() {
  202. this.socket.emit(`users.removeSessions`, this.editing._id, res => {
  203. new Toast({ content: res.message, timeout: 4000 });
  204. });
  205. },
  206. ...mapActions("modals", ["closeModal"])
  207. },
  208. mounted() {
  209. io.getSocket(socket => {
  210. this.socket = socket;
  211. return socket;
  212. });
  213. }
  214. };
  215. </script>
  216. <style lang="scss" scoped>
  217. @import "styles/global.scss";
  218. .save-changes {
  219. color: $white;
  220. }
  221. .tag:not(:last-child) {
  222. margin-right: 5px;
  223. }
  224. .select:after {
  225. border-color: $primary-color;
  226. }
  227. </style>