list users
This commit is contained in:
@@ -228,3 +228,19 @@ pub enum CreateUserError {
|
||||
#[error(transparent)]
|
||||
Unknown(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
/// An admin request to list all users
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct ListUsersRequest {}
|
||||
|
||||
impl ListUsersRequest {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ListUsersError {
|
||||
#[error(transparent)]
|
||||
Unknown(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
@@ -59,6 +59,9 @@ pub trait AuthMetrics: Clone + Send + Sync + 'static {
|
||||
fn record_user_creation_success(&self) -> impl Future<Output = ()> + Send;
|
||||
fn record_user_creation_failure(&self) -> impl Future<Output = ()> + Send;
|
||||
|
||||
fn record_user_list_success(&self) -> impl Future<Output = ()> + Send;
|
||||
fn record_user_list_failure(&self) -> impl Future<Output = ()> + Send;
|
||||
|
||||
fn record_auth_session_creation_success(&self) -> impl Future<Output = ()> + Send;
|
||||
fn record_auth_session_creation_failure(&self) -> impl Future<Output = ()> + Send;
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ use super::models::{
|
||||
FilePath, ListFilesError, ListFilesRequest, RenameEntryError, RenameEntryRequest,
|
||||
},
|
||||
user::{
|
||||
CreateUserError, CreateUserRequest, LoginUserError, LoginUserRequest, LoginUserResponse,
|
||||
RegisterUserError, RegisterUserRequest, User,
|
||||
CreateUserError, CreateUserRequest, ListUsersError, ListUsersRequest, LoginUserError,
|
||||
LoginUserRequest, LoginUserResponse, RegisterUserError, RegisterUserRequest, User,
|
||||
},
|
||||
user_warren::{
|
||||
UserWarren,
|
||||
@@ -125,6 +125,10 @@ pub trait AuthService: Clone + Send + Sync + 'static {
|
||||
&self,
|
||||
request: AuthRequest<CreateUserRequest>,
|
||||
) -> impl Future<Output = Result<User, AuthError<CreateUserError>>> + Send;
|
||||
fn list_users(
|
||||
&self,
|
||||
request: AuthRequest<ListUsersRequest>,
|
||||
) -> impl Future<Output = Result<Vec<User>, AuthError<ListUsersError>>> + Send;
|
||||
|
||||
fn create_auth_session(
|
||||
&self,
|
||||
|
||||
@@ -74,6 +74,11 @@ pub trait AuthNotifier: Clone + Send + Sync + 'static {
|
||||
fn user_registered(&self, user: &User) -> impl Future<Output = ()> + Send;
|
||||
fn user_logged_in(&self, response: &LoginUserResponse) -> impl Future<Output = ()> + Send;
|
||||
fn user_created(&self, creator: &User, created: &User) -> impl Future<Output = ()> + Send;
|
||||
/// Lists all the users (admin action)
|
||||
///
|
||||
/// * `user`: The user who requested the list
|
||||
/// * `users`: The users from the list
|
||||
fn users_listed(&self, user: &User, users: &Vec<User>) -> impl Future<Output = ()> + Send;
|
||||
|
||||
fn auth_session_created(&self, user_id: &Uuid) -> impl Future<Output = ()> + Send;
|
||||
fn auth_session_fetched(
|
||||
|
||||
@@ -12,8 +12,8 @@ use crate::domain::warren::models::{
|
||||
FilePath, ListFilesError, ListFilesRequest, RenameEntryError, RenameEntryRequest,
|
||||
},
|
||||
user::{
|
||||
CreateUserError, CreateUserRequest, User, VerifyUserPasswordError,
|
||||
VerifyUserPasswordRequest,
|
||||
CreateUserError, CreateUserRequest, ListUsersError, ListUsersRequest, User,
|
||||
VerifyUserPasswordError, VerifyUserPasswordRequest,
|
||||
},
|
||||
user_warren::{
|
||||
UserWarren,
|
||||
@@ -74,10 +74,16 @@ pub trait AuthRepository: Clone + Send + Sync + 'static {
|
||||
&self,
|
||||
request: CreateUserRequest,
|
||||
) -> impl Future<Output = Result<User, CreateUserError>> + Send;
|
||||
fn list_users(
|
||||
&self,
|
||||
request: ListUsersRequest,
|
||||
) -> impl Future<Output = Result<Vec<User>, ListUsersError>> + Send;
|
||||
|
||||
fn verify_user_password(
|
||||
&self,
|
||||
request: VerifyUserPasswordRequest,
|
||||
) -> impl Future<Output = Result<User, VerifyUserPasswordError>> + Send;
|
||||
|
||||
fn create_auth_session(
|
||||
&self,
|
||||
request: CreateAuthSessionRequest,
|
||||
|
||||
@@ -10,8 +10,9 @@ use crate::{
|
||||
},
|
||||
},
|
||||
user::{
|
||||
CreateUserError, CreateUserRequest, LoginUserError, LoginUserRequest,
|
||||
LoginUserResponse, RegisterUserError, RegisterUserRequest, User,
|
||||
CreateUserError, CreateUserRequest, ListUsersError, ListUsersRequest,
|
||||
LoginUserError, LoginUserRequest, LoginUserResponse, RegisterUserError,
|
||||
RegisterUserRequest, User,
|
||||
},
|
||||
user_warren::{
|
||||
UserWarren,
|
||||
@@ -165,6 +166,32 @@ where
|
||||
result.map_err(AuthError::Custom)
|
||||
}
|
||||
|
||||
async fn list_users(
|
||||
&self,
|
||||
request: AuthRequest<ListUsersRequest>,
|
||||
) -> Result<Vec<User>, AuthError<ListUsersError>> {
|
||||
let (session, request) = request.unpack();
|
||||
|
||||
let response = self
|
||||
.fetch_auth_session(FetchAuthSessionRequest::new(session.session_id().clone()))
|
||||
.await?;
|
||||
|
||||
if !response.user().admin() {
|
||||
return Err(AuthError::InsufficientPermissions);
|
||||
}
|
||||
|
||||
let result = self.repository.list_users(request).await;
|
||||
|
||||
if let Ok(users) = result.as_ref() {
|
||||
self.metrics.record_user_list_success().await;
|
||||
self.notifier.users_listed(response.user(), users).await;
|
||||
} else {
|
||||
self.metrics.record_user_list_failure().await;
|
||||
}
|
||||
|
||||
result.map_err(AuthError::Custom)
|
||||
}
|
||||
|
||||
async fn create_auth_session(
|
||||
&self,
|
||||
request: CreateAuthSessionRequest,
|
||||
|
||||
32
backend/src/lib/inbound/http/handlers/admin/list_users.rs
Normal file
32
backend/src/lib/inbound/http/handlers/admin/list_users.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use axum::{extract::State, http::StatusCode};
|
||||
|
||||
use crate::{
|
||||
domain::warren::{
|
||||
models::{
|
||||
auth_session::AuthRequest,
|
||||
user::{ListUsersRequest, User},
|
||||
},
|
||||
ports::{AuthService, WarrenService},
|
||||
},
|
||||
inbound::http::{
|
||||
AppState,
|
||||
handlers::{UserData, extractors::SessionIdHeader},
|
||||
responses::{ApiError, ApiSuccess},
|
||||
},
|
||||
};
|
||||
|
||||
pub async fn list_users<WS: WarrenService, AS: AuthService>(
|
||||
State(state): State<AppState<WS, AS>>,
|
||||
SessionIdHeader(session): SessionIdHeader,
|
||||
) -> Result<ApiSuccess<Vec<UserData>>, ApiError> {
|
||||
state
|
||||
.auth_service
|
||||
.list_users(AuthRequest::new(session, ListUsersRequest::new()))
|
||||
.await
|
||||
.map(|users| ApiSuccess::new(StatusCode::OK, get_user_data(users)))
|
||||
.map_err(ApiError::from)
|
||||
}
|
||||
|
||||
fn get_user_data(users: Vec<User>) -> Vec<UserData> {
|
||||
users.into_iter().map(Into::into).collect()
|
||||
}
|
||||
@@ -1,7 +1,13 @@
|
||||
mod create_user;
|
||||
use create_user::create_user;
|
||||
mod list_users;
|
||||
|
||||
use axum::{Router, routing::post};
|
||||
use create_user::create_user;
|
||||
use list_users::list_users;
|
||||
|
||||
use axum::{
|
||||
Router,
|
||||
routing::{get, post},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
domain::warren::ports::{AuthService, WarrenService},
|
||||
@@ -9,5 +15,7 @@ use crate::{
|
||||
};
|
||||
|
||||
pub fn routes<WS: WarrenService, AS: AuthService>() -> Router<AppState<WS, AS>> {
|
||||
Router::new().route("/users", post(create_user))
|
||||
Router::new()
|
||||
.route("/users", get(list_users))
|
||||
.route("/users", post(create_user))
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ pub mod warrens;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
/// A session user that can be safely sent to the client
|
||||
/// A user that can be safely sent to the client
|
||||
pub(super) struct UserData {
|
||||
id: Uuid,
|
||||
name: String,
|
||||
|
||||
@@ -145,6 +145,13 @@ impl AuthMetrics for MetricsDebugLogger {
|
||||
tracing::debug!("[Metrics] User creation failed");
|
||||
}
|
||||
|
||||
async fn record_user_list_success(&self) -> () {
|
||||
tracing::debug!("[Metrics] User list succeeded");
|
||||
}
|
||||
async fn record_user_list_failure(&self) -> () {
|
||||
tracing::debug!("[Metrics] User list failed");
|
||||
}
|
||||
|
||||
async fn record_auth_session_creation_success(&self) {
|
||||
tracing::debug!("[Metrics] Auth session creation succeeded");
|
||||
}
|
||||
|
||||
@@ -129,6 +129,14 @@ impl AuthNotifier for NotifierDebugLogger {
|
||||
);
|
||||
}
|
||||
|
||||
async fn users_listed(&self, user: &User, users: &Vec<User>) {
|
||||
tracing::debug!(
|
||||
"[Notifier] Admin user {} listed {} user(s)",
|
||||
user.name(),
|
||||
users.len()
|
||||
);
|
||||
}
|
||||
|
||||
async fn user_logged_in(&self, response: &LoginUserResponse) {
|
||||
tracing::debug!("[Notifier] Logged in user {}", response.user().name());
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ use crate::domain::warren::{
|
||||
},
|
||||
},
|
||||
user::{
|
||||
CreateUserError, CreateUserRequest, User, UserEmail, UserName, UserPassword,
|
||||
VerifyUserPasswordError, VerifyUserPasswordRequest,
|
||||
CreateUserError, CreateUserRequest, ListUsersError, ListUsersRequest, User, UserEmail,
|
||||
UserName, UserPassword, VerifyUserPasswordError, VerifyUserPasswordRequest,
|
||||
},
|
||||
user_warren::{
|
||||
UserWarren,
|
||||
@@ -348,6 +348,21 @@ impl Postgres {
|
||||
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
async fn fetch_users(&self, connection: &mut PgConnection) -> Result<Vec<User>, sqlx::Error> {
|
||||
let users: Vec<User> = sqlx::query_as(
|
||||
"
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
users
|
||||
",
|
||||
)
|
||||
.fetch_all(connection)
|
||||
.await?;
|
||||
|
||||
Ok(users)
|
||||
}
|
||||
}
|
||||
|
||||
impl WarrenRepository for Postgres {
|
||||
@@ -519,6 +534,21 @@ impl AuthRepository for Postgres {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async fn list_users(&self, _request: ListUsersRequest) -> Result<Vec<User>, ListUsersError> {
|
||||
let mut connection = self
|
||||
.pool
|
||||
.acquire()
|
||||
.await
|
||||
.context("Failed to get a PostgreSQL connection")?;
|
||||
|
||||
let users = self
|
||||
.fetch_users(&mut connection)
|
||||
.await
|
||||
.map_err(|e| anyhow!(e))?;
|
||||
|
||||
Ok(users)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_not_found_error(err: &sqlx::Error) -> bool {
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
BreadcrumbPage,
|
||||
BreadcrumbSeparator,
|
||||
} from '@/components/ui/breadcrumb';
|
||||
import type { BreadcrumbData } from '~/types';
|
||||
import type { BreadcrumbData } from '#shared/types';
|
||||
const route = useRoute();
|
||||
const store = useWarrenStore();
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
ContextMenuSeparator,
|
||||
} from '@/components/ui/context-menu';
|
||||
import { deleteWarrenDirectory, deleteWarrenFile } from '~/lib/api/warrens';
|
||||
import type { DirectoryEntry } from '~/types';
|
||||
import type { DirectoryEntry } from '#shared/types';
|
||||
|
||||
const warrenStore = useWarrenStore();
|
||||
const renameDialog = useRenameDirectoryDialog();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import type { DirectoryEntry } from '~/types';
|
||||
import type { DirectoryEntry } from '#shared/types';
|
||||
const { entries } = defineProps<{
|
||||
entries: DirectoryEntry[];
|
||||
}>();
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
} from '@/components/ui/dropdown-menu';
|
||||
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar';
|
||||
import { logout } from '~/lib/api/auth/logout';
|
||||
import type { AuthUser } from '~/types/auth';
|
||||
import type { AuthUser } from '#shared/types/auth';
|
||||
|
||||
const { isMobile } = useSidebar();
|
||||
const { user } = defineProps<{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import byteSize from 'byte-size';
|
||||
import type { UploadFile } from '~/types';
|
||||
import type { UploadFile } from '#shared/types';
|
||||
|
||||
const emit = defineEmits(['removeFile']);
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from '@/components/ui/alert-dialog';
|
||||
import type { AuthUser } from '~/types/auth';
|
||||
import type { AuthUser } from '#shared/types/auth';
|
||||
|
||||
const adminStore = useAdminStore();
|
||||
// We'll only update this value if there is a user to prevent layout shifts on close
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import type { AuthUser } from '~/types/auth';
|
||||
import type { AuthUser } from '#shared/types/auth';
|
||||
|
||||
const { user } = defineProps<{
|
||||
user: AuthUser;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { AuthSession } from '~/types/auth';
|
||||
import type { AuthSession } from '#shared/types/auth';
|
||||
|
||||
const SAME_SITE_SETTINGS = ['strict', 'lax', 'none'] as const;
|
||||
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
<script setup lang="ts"></script>
|
||||
<script setup lang="ts">
|
||||
import { listUsers } from '~/lib/api/admin/listUsers';
|
||||
|
||||
const adminStore = useAdminStore();
|
||||
|
||||
await useAsyncData('users', async () => {
|
||||
const response = await listUsers();
|
||||
|
||||
if (response.success) {
|
||||
adminStore.users = response.users;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NuxtLayout name="default">
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { ApiResponse } from '~/types/api';
|
||||
import type { AuthUser, AuthUserFields } from '~/types/auth';
|
||||
import type { ApiResponse } from '#shared/types/api';
|
||||
import type { AuthUser, AuthUserFields } from '#shared/types/auth';
|
||||
import { getApiHeaders } from '..';
|
||||
|
||||
/** Admin function to create a new user */
|
||||
@@ -32,6 +32,8 @@ export async function createUser(
|
||||
};
|
||||
}
|
||||
|
||||
await refreshNuxtData('users');
|
||||
|
||||
toast.success('Create user', {
|
||||
description: 'Successfully created user',
|
||||
});
|
||||
|
||||
27
frontend/lib/api/admin/listUsers.ts
Normal file
27
frontend/lib/api/admin/listUsers.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { ApiResponse } from '~/shared/types/api';
|
||||
import type { AuthUser } from '~/shared/types/auth';
|
||||
import { getApiHeaders } from '..';
|
||||
|
||||
export async function listUsers(): Promise<
|
||||
{ success: true; users: AuthUser[] } | { success: false }
|
||||
> {
|
||||
const { data } = await useFetch<ApiResponse<AuthUser[]>>(
|
||||
getApiUrl('admin/users'),
|
||||
{
|
||||
method: 'GET',
|
||||
headers: getApiHeaders(),
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
|
||||
if (data.value == null) {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
users: data.value.data,
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { AuthSessionType, AuthUser } from '~/types/auth';
|
||||
import type { ApiResponse } from '~/types/api';
|
||||
import type { AuthSessionType, AuthUser } from '#shared/types/auth';
|
||||
import type { ApiResponse } from '#shared/types/api';
|
||||
|
||||
export async function getAuthSessionData(params: {
|
||||
sessionType: AuthSessionType;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { ApiResponse } from '~/types/api';
|
||||
import type { AuthUser } from '~/types/auth';
|
||||
import type { ApiResponse } from '#shared/types/api';
|
||||
import type { AuthUser } from '#shared/types/auth';
|
||||
import { getApiHeaders } from '..';
|
||||
|
||||
export async function loginUser(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { ApiResponse } from '~/types/api';
|
||||
import type { ApiResponse } from '#shared/types/api';
|
||||
|
||||
export async function registerUser(
|
||||
username: string,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { DirectoryEntry } from '~/types';
|
||||
import type { ApiResponse } from '~/types/api';
|
||||
import type { Warren } from '~/types/warrens';
|
||||
import type { DirectoryEntry } from '#shared/types';
|
||||
import type { ApiResponse } from '#shared/types/api';
|
||||
import type { Warren } from '#shared/types/warrens';
|
||||
import { getApiHeaders } from '.';
|
||||
|
||||
export async function getWarrens(): Promise<Record<string, Warren>> {
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
<script setup lang="ts">
|
||||
import type { AuthUser } from '~/types/auth';
|
||||
|
||||
definePageMeta({
|
||||
layout: 'admin',
|
||||
middleware: ['is-admin'],
|
||||
@@ -8,21 +6,6 @@ definePageMeta({
|
||||
|
||||
const session = useAuthSession();
|
||||
const adminStore = useAdminStore();
|
||||
|
||||
const users: AuthUser[] = [
|
||||
{
|
||||
id: '5a307466-bf2e-4cf2-9b11-61f024e8fa71',
|
||||
name: '409',
|
||||
email: '409dev@protonmail.com',
|
||||
admin: true,
|
||||
},
|
||||
{
|
||||
id: '99132ce4-045c-4d4b-b957-61f5e99e708b',
|
||||
name: 'test-user',
|
||||
email: 'test@user.com',
|
||||
admin: false,
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -41,7 +24,7 @@ const users: AuthUser[] = [
|
||||
<ScrollArea class="max-h-96">
|
||||
<div class="flex flex-col gap-4">
|
||||
<AdminUserListing
|
||||
v-for="user in users"
|
||||
v-for="user in adminStore.users"
|
||||
:key="user.id"
|
||||
:user
|
||||
class="group/user flex flex-row items-center justify-between gap-4"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import type { Warren } from '~/types/warrens';
|
||||
import type { Warren } from '#shared/types/warrens';
|
||||
|
||||
definePageMeta({
|
||||
middleware: ['authenticated'],
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import type { AuthUser, AuthUserFields } from '~/types/auth';
|
||||
import type { AuthUser, AuthUserFields } from '#shared/types/auth';
|
||||
|
||||
export const useAdminStore = defineStore('admin', {
|
||||
state: () => ({
|
||||
users: [] as AuthUser[],
|
||||
createUserDialog: null as { user: AuthUserFields } | null,
|
||||
deleteUserDialog: null as { user: AuthUser } | null,
|
||||
}),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import type { DirectoryEntry } from '~/types';
|
||||
import type { Warren } from '~/types/warrens';
|
||||
import type { DirectoryEntry } from '#shared/types';
|
||||
import type { Warren } from '#shared/types/warrens';
|
||||
|
||||
export const useWarrenStore = defineStore('warrens', {
|
||||
state: () => ({
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { UploadFile } from '~/types';
|
||||
import type { UploadFile } from '#shared/types';
|
||||
|
||||
export const useUploadStore = defineStore<
|
||||
'warren-upload',
|
||||
|
||||
Reference in New Issue
Block a user