Create button for ending impersonation
This commit is contained in:
parent
76c395d613
commit
522c5785a2
6 changed files with 134 additions and 59 deletions
|
|
@ -8,46 +8,70 @@
|
|||
<i class="bi bi-people"></i> Impersonate User
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row mb-3">
|
||||
<div class="col-md-6">
|
||||
<label for="userSearch" class="form-label">Search Users</label>
|
||||
<UserSelector
|
||||
v-model="selectedUser"
|
||||
label="Choose a user"
|
||||
placeholder="Select a user..."
|
||||
/>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label for="userRole" class="form-label">Filter by Role</label>
|
||||
<select class="form-select" id="userRole">
|
||||
<option value="">All Roles</option>
|
||||
<option value="admin">Admin</option>
|
||||
<option value="user">Standard User</option>
|
||||
<option value="support">Support</option>
|
||||
<option value="premium">Premium User</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row mb-3">
|
||||
<button class="btn btn-danger" @click="doImpersonation" type="submit">
|
||||
Impersonate
|
||||
<template v-if="isImpersonating && impersonatedUser">
|
||||
<h1>You're impersonating</h1>
|
||||
<p>{{ impersonatedUser.username }}</p>
|
||||
<button class="btn btn-primary" @click="doImpersonationEnd">
|
||||
End Impersonation
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<template v-else>
|
||||
<div class="row mb-3">
|
||||
<div class="col-md-6">
|
||||
<label for="userSearch" class="form-label">Search Users</label>
|
||||
<UserSelector
|
||||
v-model="selectedUser"
|
||||
label="Choose a user"
|
||||
placeholder="Select a user..."
|
||||
/>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label for="userRole" class="form-label">Filter by Role</label>
|
||||
<select class="form-select" id="userRole">
|
||||
<option value="">All Roles</option>
|
||||
<option value="admin">Admin</option>
|
||||
<option value="user">Standard User</option>
|
||||
<option value="support">Support</option>
|
||||
<option value="premium">Premium User</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row mb-3">
|
||||
<button class="btn btn-danger" @click="doImpersonationStart">
|
||||
Impersonate
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import { onMounted, ref } from "vue";
|
||||
import { useSessionStore } from "@/store/session";
|
||||
import { useUserStore } from "@/store/user";
|
||||
import UserSelector from "@/components/UserSelector.vue";
|
||||
import type { User } from "@/types";
|
||||
import type { Session, User } from "@/types";
|
||||
|
||||
const session = useSessionStore();
|
||||
const user = useUserStore();
|
||||
|
||||
const impersonatedUser = ref<User | null>(null);
|
||||
const isImpersonating = ref<boolean>(false);
|
||||
const selectedUser = ref<User | null>(null);
|
||||
|
||||
const doImpersonation = async () => {
|
||||
const doImpersonationEnd = async () => {
|
||||
const url = session.urls!.api.impersonation;
|
||||
const response = await fetch(url, {
|
||||
method: "DELETE",
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to end impersonation: ${response.statusText}`);
|
||||
}
|
||||
const new_session = await session.fetchSession();
|
||||
console.log("session is now", new_session);
|
||||
};
|
||||
const doImpersonationStart = async () => {
|
||||
if (!selectedUser.value) {
|
||||
console.log("Can't impersonate, null user");
|
||||
return;
|
||||
|
|
@ -72,4 +96,19 @@ const doImpersonation = async () => {
|
|||
const new_session = await session.fetchSession();
|
||||
console.log("session is now", new_session);
|
||||
};
|
||||
onMounted(() => {
|
||||
session.get().then((session: Session) => {
|
||||
if (session.impersonating) {
|
||||
isImpersonating.value = true;
|
||||
console.log("is impersonating, but who?");
|
||||
user.byURI(session.impersonating).then((user: User | null) => {
|
||||
impersonatedUser.value = user;
|
||||
console.log("is impersonating", user);
|
||||
});
|
||||
} else {
|
||||
isImpersonating.value = false;
|
||||
impersonatedUser.value = null;
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue