123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <template>
- <div>
- <modal title="Edit User">
- <template #body v-if="user && user._id">
- <div class="section">
- <label class="label"> Change username </label>
- <p class="control is-grouped">
- <span class="control is-expanded">
- <input
- v-model="user.username"
- class="input"
- type="text"
- placeholder="Username"
- autofocus
- />
- </span>
- <span class="control">
- <a class="button is-info" @click="updateUsername()"
- >Update Username</a
- >
- </span>
- </p>
- <label class="label"> Change email address </label>
- <p class="control is-grouped">
- <span class="control is-expanded">
- <input
- v-model="user.email.address"
- class="input"
- type="text"
- placeholder="Email Address"
- autofocus
- />
- </span>
- <span class="control">
- <a class="button is-info" @click="updateEmail()"
- >Update Email Address</a
- >
- </span>
- </p>
- <label class="label"> Change user role </label>
- <div class="control is-grouped">
- <div class="control is-expanded select">
- <select v-model="user.role">
- <option>default</option>
- <option>admin</option>
- </select>
- </div>
- <p class="control">
- <a class="button is-info" @click="updateRole()"
- >Update Role</a
- >
- </p>
- </div>
- </div>
- <div class="section">
- <label class="label"> Punish/Ban User </label>
- <p class="control is-grouped">
- <span class="control select">
- <select v-model="ban.expiresAt">
- <option value="1h">1 Hour</option>
- <option value="12h">12 Hours</option>
- <option value="1d">1 Day</option>
- <option value="1w">1 Week</option>
- <option value="1m">1 Month</option>
- <option value="3m">3 Months</option>
- <option value="6m">6 Months</option>
- <option value="1y">1 Year</option>
- </select>
- </span>
- <span class="control is-expanded">
- <input
- v-model="ban.reason"
- class="input"
- type="text"
- placeholder="Ban reason"
- autofocus
- />
- </span>
- <span class="control">
- <a class="button is-danger" @click="banUser()">
- Ban user
- </a>
- </span>
- </p>
- </div>
- </template>
- <template #footer>
- <quick-confirm @confirm="resendVerificationEmail()">
- <a class="button is-warning"> Resend verification email </a>
- </quick-confirm>
- <quick-confirm @confirm="requestPasswordReset()">
- <a class="button is-warning"> Request password reset </a>
- </quick-confirm>
- <quick-confirm @confirm="removeSessions()">
- <a class="button is-warning"> Remove all sessions </a>
- </quick-confirm>
- <quick-confirm @confirm="removeAccount()">
- <a class="button is-danger"> Remove account </a>
- </quick-confirm>
- </template>
- </modal>
- </div>
- </template>
- <script>
- import { mapGetters, mapActions } from "vuex";
- import Toast from "toasters";
- import validation from "@/validation";
- import ws from "@/ws";
- import { mapModalState, mapModalActions } from "@/vuex_helpers";
- export default {
- props: {
- modalUuid: { type: String, default: "" }
- },
- data() {
- return {
- ban: {
- expiresAt: "1h"
- }
- };
- },
- computed: {
- ...mapModalState("modals/editUser/MODAL_UUID", {
- userId: state => state.userId,
- user: state => state.user
- }),
- ...mapGetters({
- socket: "websockets/getSocket"
- })
- },
- watch: {
- // When the userId changes, run init. There can be a delay between the modal opening and the required data (userId) being available
- userId() {
- // Note: is it possible for this to run before the socket is ready?
- this.init();
- }
- },
- mounted() {
- ws.onConnect(this.init);
- },
- beforeUnmount() {
- this.socket.dispatch(
- "apis.leaveRoom",
- `edit-user.${this.userId}`,
- () => {}
- );
- // Delete the VueX module that was created for this modal, after all other cleanup tasks are performed
- this.$store.unregisterModule(["modals", "editUser", this.modalUuid]);
- },
- methods: {
- init() {
- if (this.userId)
- this.socket.dispatch(
- `users.getUserFromId`,
- this.userId,
- res => {
- if (res.status === "success") {
- const user = res.data;
- this.setUser(user);
- this.socket.dispatch(
- "apis.joinRoom",
- `edit-user.${this.userId}`
- );
- this.socket.on(
- "event:user.removed",
- res => {
- if (res.data.userId === this.userId)
- this.closeModal("editUser");
- },
- { modal: "editUser" }
- );
- } else {
- new Toast("User with that ID not found");
- this.closeModal("editUser");
- }
- }
- );
- },
- updateUsername() {
- const { username } = this.user;
- if (!validation.isLength(username, 2, 32))
- return new Toast(
- "Username must have between 2 and 32 characters."
- );
- if (!validation.regex.custom("a-zA-Z0-9_-").test(username))
- return new Toast(
- "Invalid username format. Allowed characters: a-z, A-Z, 0-9, _ and -."
- );
- return this.socket.dispatch(
- `users.updateUsername`,
- this.user._id,
- username,
- res => {
- new Toast(res.message);
- }
- );
- },
- updateEmail() {
- const email = this.user.email.address;
- if (!validation.isLength(email, 3, 254))
- return new Toast(
- "Email must have between 3 and 254 characters."
- );
- if (
- email.indexOf("@") !== email.lastIndexOf("@") ||
- !validation.regex.emailSimple.test(email) ||
- !validation.regex.ascii.test(email)
- )
- return new Toast("Invalid email format.");
- return this.socket.dispatch(
- `users.updateEmail`,
- this.user._id,
- email,
- res => {
- new Toast(res.message);
- }
- );
- },
- updateRole() {
- this.socket.dispatch(
- `users.updateRole`,
- this.user._id,
- this.user.role,
- res => {
- new Toast(res.message);
- }
- );
- },
- banUser() {
- const { reason } = this.ban;
- if (!validation.isLength(reason, 1, 64))
- return new Toast(
- "Reason must have between 1 and 64 characters."
- );
- if (!validation.regex.ascii.test(reason))
- return new Toast(
- "Invalid reason format. Only ascii characters are allowed."
- );
- return this.socket.dispatch(
- `users.banUserById`,
- this.user._id,
- this.ban.reason,
- this.ban.expiresAt,
- res => {
- new Toast(res.message);
- }
- );
- },
- resendVerificationEmail() {
- this.socket.dispatch(
- `users.resendVerifyEmail`,
- this.user._id,
- res => {
- new Toast(res.message);
- }
- );
- },
- requestPasswordReset() {
- this.socket.dispatch(
- `users.adminRequestPasswordReset`,
- this.user._id,
- res => {
- new Toast(res.message);
- }
- );
- },
- removeAccount() {
- this.socket.dispatch(`users.adminRemove`, this.user._id, res => {
- new Toast(res.message);
- });
- },
- removeSessions() {
- this.socket.dispatch(`users.removeSessions`, this.user._id, res => {
- new Toast(res.message);
- });
- },
- ...mapModalActions("modals/editUser/MODAL_UUID", ["setUser"]),
- ...mapActions("modalVisibility", ["closeModal"])
- }
- };
- </script>
- <style lang="less" scoped>
- .night-mode .section {
- background-color: transparent !important;
- }
- .section {
- padding: 15px 0 !important;
- }
- .save-changes {
- color: var(--white);
- }
- .tag:not(:last-child) {
- margin-right: 5px;
- }
- .select:after {
- border-color: var(--primary-color);
- }
- </style>
|