Improve Angular project structure and code organization (#1328)
This commit is contained in:
+120
@@ -0,0 +1,120 @@
|
||||
<form [formGroup]="userForm" (ngSubmit)="createUser()" class="space-y-6 p-6">
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Full Name:</label>
|
||||
<input type="text" pInputText formControlName="name" placeholder="Enter full name..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
@if (userForm.get('name')?.invalid && (userForm.get('name')?.dirty || userForm.get('name')?.touched)) {
|
||||
<small class="text-red-500 pl-44">
|
||||
Name is required and must be at least 3 characters.
|
||||
</small>
|
||||
}
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Email:</label>
|
||||
<input type="email" pInputText formControlName="email" placeholder="Enter email..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
@if (userForm.get('email')?.invalid && (userForm.get('email')?.dirty || userForm.get('email')?.touched)) {
|
||||
<small class="text-red-500 pl-44">
|
||||
Enter a valid email address.
|
||||
</small>
|
||||
}
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Username:</label>
|
||||
<input type="text" pInputText formControlName="username" placeholder="Enter username..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
@if (userForm.get('username')?.invalid && (userForm.get('username')?.dirty || userForm.get('username')?.touched)) {
|
||||
<small class="text-red-500 pl-44">
|
||||
Username is required.
|
||||
</small>
|
||||
}
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Password:</label>
|
||||
<input type="password" pInputText formControlName="password" placeholder="Enter password..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
@if (userForm.get('password')?.invalid && (userForm.get('password')?.dirty || userForm.get('password')?.touched)) {
|
||||
<small class="text-red-500 pl-44">
|
||||
Password must be at least 6 characters long.
|
||||
</small>
|
||||
}
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Accessible Libraries:</label>
|
||||
<p-multiselect
|
||||
[options]="libraries"
|
||||
formControlName="selectedLibraries"
|
||||
placeholder="Select Libraries"
|
||||
optionLabel="name"
|
||||
display="chip"
|
||||
class="w-full md:w-80"
|
||||
appendTo="body">
|
||||
</p-multiselect>
|
||||
</div>
|
||||
@if (userForm.get('selectedLibraries')?.invalid && (userForm.get('selectedLibraries')?.dirty || userForm.get('selectedLibraries')?.touched)) {
|
||||
<small class="text-red-500 pl-44">
|
||||
At least one library must be selected.
|
||||
</small>
|
||||
}
|
||||
|
||||
<div>
|
||||
<p class="text-right w-40 font-medium">Permissions:</p>
|
||||
<div class="flex flex-col gap-2 pl-44">
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionUpload" [binary]="true"></p-checkbox>
|
||||
<label>Upload Books</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionDownload" [binary]="true"></p-checkbox>
|
||||
<label>Download Books</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionEditMetadata" [binary]="true"></p-checkbox>
|
||||
<label>Edit Metadata</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionManipulateLibrary" [binary]="true"></p-checkbox>
|
||||
<label>Manage Library</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionEmailBook" [binary]="true"></p-checkbox>
|
||||
<label>Email Book</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionDeleteBook" [binary]="true"></p-checkbox>
|
||||
<label>Delete Book</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionAccessOpds" [binary]="true"></p-checkbox>
|
||||
<label>Access OPDS</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionSyncKoreader" [binary]="true"></p-checkbox>
|
||||
<label>KOReader Sync</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionSyncKobo" [binary]="true"></p-checkbox>
|
||||
<label>Kobo Sync</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox formControlName="permissionAdmin" [binary]="true"></p-checkbox>
|
||||
<label class="text-orange-500 font-bold">Admin</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex justify-end">
|
||||
<p-button label="Create User" [disabled]="userForm.invalid" type="submit">
|
||||
</p-button>
|
||||
</div>
|
||||
</form>
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators} from '@angular/forms';
|
||||
import {Checkbox} from 'primeng/checkbox';
|
||||
import {MultiSelectModule} from 'primeng/multiselect';
|
||||
import {Library} from '../../../book/model/library.model';
|
||||
import {Button} from 'primeng/button';
|
||||
import {LibraryService} from '../../../book/service/library.service';
|
||||
import {UserService} from '../user.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'app-create-user-dialog',
|
||||
standalone: true,
|
||||
imports: [
|
||||
InputText,
|
||||
ReactiveFormsModule,
|
||||
FormsModule,
|
||||
Checkbox,
|
||||
MultiSelectModule,
|
||||
Button
|
||||
],
|
||||
templateUrl: './create-user-dialog.component.html',
|
||||
styleUrl: './create-user-dialog.component.scss'
|
||||
})
|
||||
export class CreateUserDialogComponent implements OnInit {
|
||||
userForm!: FormGroup;
|
||||
libraries: Library[] = [];
|
||||
|
||||
private fb = inject(FormBuilder);
|
||||
private libraryService = inject(LibraryService);
|
||||
private userService = inject(UserService);
|
||||
private messageService = inject(MessageService);
|
||||
private ref = inject(DynamicDialogRef);
|
||||
|
||||
ngOnInit() {
|
||||
this.libraries = this.libraryService.getLibrariesFromState();
|
||||
|
||||
this.userForm = this.fb.group({
|
||||
name: ['', [Validators.required, Validators.minLength(3)]],
|
||||
email: ['', [Validators.required, Validators.email]],
|
||||
username: ['', Validators.required],
|
||||
password: ['', [Validators.required, Validators.minLength(6)]],
|
||||
selectedLibraries: [[], Validators.required],
|
||||
permissionUpload: [false],
|
||||
permissionDownload: [false],
|
||||
permissionEditMetadata: [false],
|
||||
permissionManipulateLibrary: [false],
|
||||
permissionEmailBook: [false],
|
||||
permissionDeleteBook: [false],
|
||||
permissionAccessOpds: [false],
|
||||
permissionSyncKoreader: [false],
|
||||
permissionSyncKobo: [false],
|
||||
permissionAdmin: [false],
|
||||
});
|
||||
}
|
||||
|
||||
createUser() {
|
||||
if (this.userForm.invalid) {
|
||||
this.messageService.add({
|
||||
severity: 'warn',
|
||||
summary: 'Validation Error',
|
||||
detail: 'Please correct errors before submitting.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const userData = {
|
||||
...this.userForm.value,
|
||||
selectedLibraries: this.userForm.value.selectedLibraries.map((lib: Library) => lib.id)
|
||||
};
|
||||
|
||||
this.userService.createUser(userData).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'User Created',
|
||||
detail: 'The user has been successfully created.'
|
||||
});
|
||||
this.ref.close(true);
|
||||
},
|
||||
error: (err) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'User Creation Failed',
|
||||
detail: err?.error?.message
|
||||
? `Unable to create user: ${err.error.message}`
|
||||
: 'An unexpected error occurred while creating the user. Please try again later.'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
<div class="main-container enclosing-container">
|
||||
<div class="settings-header">
|
||||
<h2 class="settings-title">
|
||||
<i class="pi pi-users"></i>
|
||||
User Management
|
||||
</h2>
|
||||
<p class="settings-description">
|
||||
Manage Booklore users, their permissions, and library access. Configure user roles and capabilities for your book collection.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="settings-content">
|
||||
<div class="users-section">
|
||||
<div class="section-header">
|
||||
<div class="section-title-group">
|
||||
<h3 class="section-title">
|
||||
<i class="pi pi-user"></i>
|
||||
Current Users
|
||||
</h3>
|
||||
<p-button
|
||||
icon="pi pi-plus"
|
||||
label="Create User"
|
||||
severity="success"
|
||||
size="small"
|
||||
outlined
|
||||
(onClick)="openCreateUserDialog()">
|
||||
</p-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="table-card">
|
||||
<p-table [value]="users" [scrollable]="true" scrollHeight="flex">
|
||||
<ng-template pTemplate="header">
|
||||
<tr>
|
||||
<th>
|
||||
<div class="header-content">
|
||||
<i class="pi pi-user"></i>
|
||||
<span>Username</span>
|
||||
</div>
|
||||
</th>
|
||||
<th>
|
||||
<div class="header-content">
|
||||
<i class="pi pi-tag"></i>
|
||||
<span>Type</span>
|
||||
</div>
|
||||
</th>
|
||||
<th class="full-name-column">
|
||||
<div class="header-content">
|
||||
<i class="pi pi-id-card"></i>
|
||||
<span>Full Name</span>
|
||||
</div>
|
||||
</th>
|
||||
<th class="email-column">
|
||||
<div class="header-content">
|
||||
<i class="pi pi-envelope"></i>
|
||||
<span>Email</span>
|
||||
</div>
|
||||
</th>
|
||||
<th class="libraries-column">
|
||||
<div class="header-content">
|
||||
<i class="pi pi-book"></i>
|
||||
<span>Assigned Libraries</span>
|
||||
</div>
|
||||
</th>
|
||||
<th class="permission-header">Admin</th>
|
||||
<th class="permission-header">Upload</th>
|
||||
<th class="permission-header">Download</th>
|
||||
<th class="permission-header">Manage Metadata</th>
|
||||
<th class="permission-header">Manage Library</th>
|
||||
<th class="permission-header">Email Books</th>
|
||||
<th class="permission-header">Delete Books</th>
|
||||
<th class="permission-header">Access OPDS</th>
|
||||
<th class="permission-header">KOReader Sync</th>
|
||||
<th class="permission-header">Kobo Sync</th>
|
||||
<th class="actions-header">
|
||||
<div class="header-content">
|
||||
<i class="pi pi-cog"></i>
|
||||
<span>Edit</span>
|
||||
</div>
|
||||
</th>
|
||||
<th class="actions-header">
|
||||
<div class="header-content">
|
||||
<i class="pi pi-key"></i>
|
||||
<span>Password</span>
|
||||
</div>
|
||||
</th>
|
||||
<th class="actions-header">
|
||||
<div class="header-content">
|
||||
<i class="pi pi-trash"></i>
|
||||
<span>Delete</span>
|
||||
</div>
|
||||
</th>
|
||||
</tr>
|
||||
</ng-template>
|
||||
|
||||
<ng-template pTemplate="body" let-user>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="user-info">
|
||||
<div class="user-avatar">
|
||||
{{ user.username.charAt(0).toUpperCase() }}
|
||||
</div>
|
||||
<span class="username">{{ user.username }}</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<span class="user-type-badge">
|
||||
{{ (user.provisioningMethod || 'LOCAL') | lowercase | titlecase }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="full-name-column">
|
||||
@if (user.isEditing) {
|
||||
<input type="text" [(ngModel)]="user.name" class="p-inputtext w-full" size="small"/>
|
||||
}
|
||||
@if (!user.isEditing) {
|
||||
<span>{{ user.name }}</span>
|
||||
}
|
||||
</td>
|
||||
<td class="email-column">
|
||||
@if (user.isEditing) {
|
||||
<input type="email" [(ngModel)]="user.email" class="p-inputtext w-full" size="small"/>
|
||||
}
|
||||
@if (!user.isEditing) {
|
||||
<span>{{ user.email }}</span>
|
||||
}
|
||||
</td>
|
||||
<td class="libraries-column">
|
||||
@if (user.isEditing) {
|
||||
<p-multiSelect
|
||||
[options]="allLibraries"
|
||||
optionLabel="name"
|
||||
optionValue="id"
|
||||
[(ngModel)]="editingLibraryIds"
|
||||
placeholder="Select Libraries"
|
||||
appendTo="body"
|
||||
size="small">
|
||||
</p-multiSelect>
|
||||
}
|
||||
@if (!user.isEditing) {
|
||||
<span class="library-names">{{ user.libraryNames }}</span>
|
||||
}
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.admin" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canUpload" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canDownload" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canEditMetadata" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canManipulateLibrary" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canEmailBook" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canDeleteBook" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canAccessOpds" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canSyncKoReader" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canSyncKobo" [disabled]="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="actions-cell">
|
||||
@if (!user.isEditing) {
|
||||
<p-button
|
||||
icon="pi pi-pencil"
|
||||
severity="info"
|
||||
size="small"
|
||||
[outlined]="true"
|
||||
[rounded]="true"
|
||||
(onClick)="toggleEdit(user)"
|
||||
pTooltip="Edit user">
|
||||
</p-button>
|
||||
}
|
||||
@if (user.isEditing) {
|
||||
<div class="flex gap-1">
|
||||
<p-button
|
||||
icon="pi pi-check"
|
||||
severity="success"
|
||||
size="small"
|
||||
[outlined]="true"
|
||||
[rounded]="true"
|
||||
(onClick)="saveUser(user)"
|
||||
pTooltip="Save changes">
|
||||
</p-button>
|
||||
<p-button
|
||||
icon="pi pi-times"
|
||||
severity="danger"
|
||||
size="small"
|
||||
[outlined]="true"
|
||||
[rounded]="true"
|
||||
(onClick)="toggleEdit(user)"
|
||||
pTooltip="Cancel">
|
||||
</p-button>
|
||||
</div>
|
||||
}
|
||||
</td>
|
||||
<td class="actions-cell">
|
||||
<p-button
|
||||
icon="pi pi-key"
|
||||
severity="warn"
|
||||
size="small"
|
||||
[outlined]="true"
|
||||
[rounded]="true"
|
||||
(onClick)="openChangePasswordDialog(user)"
|
||||
pTooltip="Change password">
|
||||
</p-button>
|
||||
</td>
|
||||
<td class="actions-cell">
|
||||
<p-button
|
||||
[disabled]="user.id === currentUser?.id"
|
||||
icon="pi pi-trash"
|
||||
severity="danger"
|
||||
size="small"
|
||||
[outlined]="true"
|
||||
[rounded]="true"
|
||||
(onClick)="deleteUser(user)"
|
||||
pTooltip="Delete user">
|
||||
</p-button>
|
||||
</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
</p-table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p-dialog
|
||||
header="Change Password"
|
||||
[(visible)]="isPasswordDialogVisible"
|
||||
[modal]="true"
|
||||
styleClass="user-dialog"
|
||||
[style]="{width: '400px'}">
|
||||
<div class="dialog-form">
|
||||
<div class="form-field">
|
||||
<label for="newPassword">New Password</label>
|
||||
<p-password
|
||||
id="newPassword"
|
||||
[(ngModel)]="newPassword"
|
||||
placeholder="Enter new password"
|
||||
[feedback]="false"
|
||||
fluid>
|
||||
</p-password>
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<label for="confirmPassword">Confirm Password</label>
|
||||
<p-password
|
||||
id="confirmPassword"
|
||||
[(ngModel)]="confirmNewPassword"
|
||||
placeholder="Confirm new password"
|
||||
[feedback]="false"
|
||||
fluid>
|
||||
</p-password>
|
||||
</div>
|
||||
@if (passwordError) {
|
||||
<div class="error-message">
|
||||
<i class="pi pi-exclamation-triangle"></i>
|
||||
<span>{{ passwordError }}</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<ng-template pTemplate="footer">
|
||||
<div class="dialog-actions">
|
||||
<p-button
|
||||
label="Cancel"
|
||||
severity="secondary"
|
||||
outlined="true"
|
||||
(onClick)="isPasswordDialogVisible = false">
|
||||
</p-button>
|
||||
<p-button
|
||||
label="Change Password"
|
||||
severity="success"
|
||||
(onClick)="submitPasswordChange()">
|
||||
</p-button>
|
||||
</div>
|
||||
</ng-template>
|
||||
</p-dialog>
|
||||
</div>
|
||||
@@ -0,0 +1,243 @@
|
||||
.main-container {
|
||||
width: 100%;
|
||||
padding: 1rem;
|
||||
height: calc(100dvh - 10.5rem);
|
||||
overflow-y: auto;
|
||||
border-width: 1px;
|
||||
border-radius: 0.5rem;
|
||||
|
||||
@media (min-width: 768px) {
|
||||
height: calc(100dvh - 11.65rem);
|
||||
}
|
||||
}
|
||||
|
||||
.enclosing-container {
|
||||
border-color: var(--p-content-border-color);
|
||||
background: var(--p-content-background);
|
||||
}
|
||||
|
||||
.settings-header {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.settings-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
font-size: 1.25rem;
|
||||
font-weight: 700;
|
||||
color: var(--p-text-color);
|
||||
margin: 0 0 0.75rem 0;
|
||||
|
||||
.pi {
|
||||
color: var(--p-primary-color);
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
}
|
||||
|
||||
.settings-description {
|
||||
color: var(--p-text-muted-color);
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.5;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.settings-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.users-section {
|
||||
@media (min-width: 768px) {
|
||||
padding: 0.5rem 1rem 0 1rem;
|
||||
}
|
||||
|
||||
.section-title-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
}
|
||||
|
||||
.section-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
color: var(--p-text-color);
|
||||
|
||||
.pi {
|
||||
color: var(--p-primary-color);
|
||||
}
|
||||
}
|
||||
|
||||
.table-card {
|
||||
border: 1px solid var(--p-content-border-color);
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
background: var(--p-content-background);
|
||||
}
|
||||
|
||||
.p-datatable {
|
||||
.p-datatable-table {
|
||||
border-collapse: separate;
|
||||
border-spacing: 0;
|
||||
}
|
||||
|
||||
.p-datatable-thead > tr > th {
|
||||
background: var(--p-surface-100);
|
||||
border-bottom: 2px solid var(--p-content-border-color);
|
||||
padding: 1rem;
|
||||
font-weight: 600;
|
||||
color: var(--p-text-color);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.p-datatable-tbody > tr {
|
||||
transition: background-color 0.2s;
|
||||
|
||||
&:hover {
|
||||
background: var(--p-surface-50);
|
||||
}
|
||||
|
||||
&:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
}
|
||||
|
||||
.p-datatable-tbody > tr > td {
|
||||
padding: 1rem;
|
||||
border: none;
|
||||
}
|
||||
}
|
||||
|
||||
.p-datatable th .header-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: flex-start;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.permission-header {
|
||||
text-align: center;
|
||||
font-size: 0.875rem;
|
||||
padding: 0.75rem 0.5rem !important;
|
||||
min-width: 50px;
|
||||
width: 50px;
|
||||
}
|
||||
|
||||
.actions-header {
|
||||
text-align: center;
|
||||
min-width: 80px;
|
||||
}
|
||||
|
||||
.user-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.user-avatar {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
border-radius: 50%;
|
||||
background: var(--p-primary-color);
|
||||
color: white;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-weight: 600;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.username {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.user-type-badge {
|
||||
display: inline-block;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border: 1px solid var(--p-primary-color);
|
||||
border-radius: 4px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
color: var(--p-text-color);
|
||||
}
|
||||
|
||||
.library-names {
|
||||
font-size: 0.875rem;
|
||||
color: var(--p-text-color);
|
||||
}
|
||||
|
||||
.actions-cell {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.user-dialog {
|
||||
|
||||
.p-dialog-content {
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.p-dialog-footer {
|
||||
padding: 1rem 1.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
.dialog-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.form-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
|
||||
label {
|
||||
font-weight: 600;
|
||||
color: var(--p-text-color);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
}
|
||||
|
||||
.error-message {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.75rem;
|
||||
background: var(--p-red-50);
|
||||
border: 1px solid var(--p-red-200);
|
||||
border-radius: 4px;
|
||||
color: var(--p-red-700);
|
||||
font-size: 0.875rem;
|
||||
|
||||
.pi {
|
||||
color: var(--p-red-500);
|
||||
}
|
||||
}
|
||||
|
||||
.dialog-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.full-name-column {
|
||||
min-width: 150px;
|
||||
width: 150px;
|
||||
}
|
||||
|
||||
.email-column {
|
||||
min-width: 150px;
|
||||
width: 200px;
|
||||
}
|
||||
|
||||
.libraries-column {
|
||||
min-width: 200px;
|
||||
width: 250px;
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {Button} from 'primeng/button';
|
||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {CreateUserDialogComponent} from './create-user-dialog/create-user-dialog.component';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {LowerCasePipe, TitleCasePipe} from '@angular/common';
|
||||
import {User, UserService} from './user.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {Checkbox} from 'primeng/checkbox';
|
||||
import {MultiSelect} from 'primeng/multiselect';
|
||||
import {Library} from '../../book/model/library.model';
|
||||
import {LibraryService} from '../../book/service/library.service';
|
||||
import {Dialog} from 'primeng/dialog';
|
||||
import {Password} from 'primeng/password';
|
||||
import {filter, take, takeUntil} from 'rxjs/operators';
|
||||
import {Subject} from 'rxjs';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
|
||||
@Component({
|
||||
selector: 'app-user-management',
|
||||
imports: [
|
||||
FormsModule,
|
||||
Button,
|
||||
TableModule,
|
||||
Checkbox,
|
||||
MultiSelect,
|
||||
Dialog,
|
||||
Password,
|
||||
LowerCasePipe,
|
||||
TitleCasePipe,
|
||||
Tooltip
|
||||
],
|
||||
templateUrl: './user-management.component.html',
|
||||
styleUrls: ['./user-management.component.scss'],
|
||||
})
|
||||
export class UserManagementComponent implements OnInit, OnDestroy {
|
||||
ref: DynamicDialogRef | undefined;
|
||||
private dialogService = inject(DialogService);
|
||||
private userService = inject(UserService);
|
||||
private libraryService = inject(LibraryService);
|
||||
private messageService = inject(MessageService);
|
||||
private readonly destroy$ = new Subject<void>();
|
||||
|
||||
users: User[] = [];
|
||||
currentUser: User | null = null;
|
||||
editingLibraryIds: number[] = [];
|
||||
allLibraries: Library[] = [];
|
||||
|
||||
isPasswordDialogVisible = false;
|
||||
selectedUser: User | null = null;
|
||||
newPassword = '';
|
||||
confirmNewPassword = '';
|
||||
passwordError = '';
|
||||
isAdmin = false;
|
||||
|
||||
ngOnInit() {
|
||||
this.loadUsers();
|
||||
|
||||
this.userService.userState$
|
||||
.pipe(
|
||||
filter(userState => !!userState?.user && userState.loaded),
|
||||
take(1),
|
||||
takeUntil(this.destroy$)
|
||||
)
|
||||
.subscribe(userState => {
|
||||
this.currentUser = userState.user;
|
||||
this.isAdmin = userState.user?.permissions?.admin || false;
|
||||
});
|
||||
|
||||
this.libraryService.libraryState$
|
||||
.pipe(
|
||||
filter(state => !!state?.loaded),
|
||||
take(1),
|
||||
takeUntil(this.destroy$)
|
||||
)
|
||||
.subscribe(libraries => this.allLibraries = libraries.libraries ?? []);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
}
|
||||
|
||||
|
||||
loadUsers() {
|
||||
this.userService.getUsers().subscribe({
|
||||
next: (data) => {
|
||||
this.users = data.map((user) => ({
|
||||
...user,
|
||||
isEditing: false,
|
||||
selectedLibraryIds: user.assignedLibraries?.map((lib) => lib.id) || [],
|
||||
libraryNames:
|
||||
user.assignedLibraries?.map((lib) => lib.name).join(', ') || '',
|
||||
}));
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to fetch users',
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
openCreateUserDialog() {
|
||||
this.ref = this.dialogService.open(CreateUserDialogComponent, {
|
||||
header: 'Create New User',
|
||||
modal: true,
|
||||
closable: true,
|
||||
style: {position: 'absolute', top: '15%'},
|
||||
});
|
||||
this.ref.onClose.subscribe((result) => {
|
||||
if (result) {
|
||||
this.loadUsers();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
toggleEdit(user: any) {
|
||||
user.isEditing = !user.isEditing;
|
||||
if (user.isEditing) {
|
||||
this.editingLibraryIds = [...user.selectedLibraryIds];
|
||||
} else {
|
||||
user.libraryNames =
|
||||
user.assignedLibraries
|
||||
?.map((lib: Library) => lib.name)
|
||||
.join(', ') || '';
|
||||
}
|
||||
}
|
||||
|
||||
saveUser(user: any) {
|
||||
user.selectedLibraryIds = [...this.editingLibraryIds];
|
||||
this.userService
|
||||
.updateUser(user.id, {
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
permissions: user.permissions,
|
||||
assignedLibraries: user.selectedLibraryIds,
|
||||
})
|
||||
.subscribe({
|
||||
next: () => {
|
||||
user.isEditing = false;
|
||||
this.loadUsers();
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Success',
|
||||
detail: 'User updated successfully',
|
||||
});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to update user',
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
deleteUser(user: User) {
|
||||
if (confirm(`Are you sure you want to delete ${user.username}?`)) {
|
||||
this.userService.deleteUser(user.id).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Success',
|
||||
detail: `User ${user.username} deleted successfully`,
|
||||
});
|
||||
this.loadUsers();
|
||||
},
|
||||
error: (err) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail:
|
||||
err.error?.message ||
|
||||
`Failed to delete user ${user.username}`,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
openChangePasswordDialog(user: User) {
|
||||
this.selectedUser = user;
|
||||
this.newPassword = '';
|
||||
this.confirmNewPassword = '';
|
||||
this.passwordError = '';
|
||||
this.isPasswordDialogVisible = true;
|
||||
}
|
||||
|
||||
submitPasswordChange() {
|
||||
if (!this.newPassword || !this.confirmNewPassword) {
|
||||
this.passwordError = 'Both fields are required';
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.newPassword !== this.confirmNewPassword) {
|
||||
this.passwordError = 'Passwords do not match';
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.selectedUser) {
|
||||
this.userService
|
||||
.changeUserPassword(this.selectedUser.id, this.newPassword)
|
||||
.subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Success',
|
||||
detail: 'Password changed successfully',
|
||||
});
|
||||
this.isPasswordDialogVisible = false;
|
||||
},
|
||||
error: (err) => {
|
||||
this.passwordError = err;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {BehaviorSubject, Observable, throwError} from 'rxjs';
|
||||
import {API_CONFIG} from '../../../core/config/api-config';
|
||||
import {Library} from '../../book/model/library.model';
|
||||
import {catchError, distinctUntilChanged, finalize, shareReplay, tap} from 'rxjs/operators';
|
||||
import {AuthService} from '../../../shared/service/auth.service';
|
||||
|
||||
export interface EntityViewPreferences {
|
||||
global: EntityViewPreference;
|
||||
overrides: EntityViewPreferenceOverride[];
|
||||
}
|
||||
|
||||
export interface EntityViewPreference {
|
||||
sortKey: string;
|
||||
sortDir: 'ASC' | 'DESC';
|
||||
view: 'GRID' | 'TABLE';
|
||||
coverSize: number;
|
||||
seriesCollapsed: boolean;
|
||||
}
|
||||
|
||||
export interface EntityViewPreferenceOverride {
|
||||
entityType: 'LIBRARY' | 'SHELF';
|
||||
entityId: number;
|
||||
preferences: EntityViewPreference;
|
||||
}
|
||||
|
||||
export interface SidebarLibrarySorting {
|
||||
field: string;
|
||||
order: string;
|
||||
}
|
||||
|
||||
export interface SidebarShelfSorting {
|
||||
field: string;
|
||||
order: string;
|
||||
}
|
||||
|
||||
export interface PerBookSetting {
|
||||
pdf: string;
|
||||
epub: string;
|
||||
cbx: string;
|
||||
}
|
||||
|
||||
export type PageSpread = 'off' | 'even' | 'odd';
|
||||
|
||||
export enum CbxBackgroundColor {
|
||||
GRAY = 'GRAY',
|
||||
BLACK = 'BLACK',
|
||||
WHITE = 'WHITE'
|
||||
}
|
||||
|
||||
export interface PdfReaderSetting {
|
||||
pageSpread: PageSpread;
|
||||
pageZoom: string;
|
||||
showSidebar: boolean;
|
||||
}
|
||||
|
||||
export enum CbxPageViewMode {
|
||||
SINGLE_PAGE = 'SINGLE_PAGE',
|
||||
TWO_PAGE = 'TWO_PAGE',
|
||||
}
|
||||
|
||||
export enum CbxPageSpread {
|
||||
EVEN = 'EVEN',
|
||||
ODD = 'ODD',
|
||||
}
|
||||
|
||||
export enum PdfPageViewMode {
|
||||
SINGLE_PAGE = 'SINGLE_PAGE',
|
||||
TWO_PAGE = 'TWO_PAGE',
|
||||
}
|
||||
|
||||
export enum PdfPageSpread {
|
||||
EVEN = 'EVEN',
|
||||
ODD = 'ODD',
|
||||
}
|
||||
|
||||
export enum CbxFitMode {
|
||||
ACTUAL_SIZE = 'ACTUAL_SIZE',
|
||||
FIT_PAGE = 'FIT_PAGE',
|
||||
FIT_WIDTH = 'FIT_WIDTH',
|
||||
FIT_HEIGHT = 'FIT_HEIGHT',
|
||||
AUTO = 'AUTO'
|
||||
}
|
||||
|
||||
export enum CbxScrollMode {
|
||||
PAGINATED = 'PAGINATED',
|
||||
INFINITE = 'INFINITE'
|
||||
}
|
||||
|
||||
export interface EpubReaderSetting {
|
||||
theme: string;
|
||||
font: string;
|
||||
fontSize: number;
|
||||
flow: string;
|
||||
spread: string;
|
||||
lineHeight: number;
|
||||
margin: number;
|
||||
letterSpacing: number;
|
||||
}
|
||||
|
||||
export interface CbxReaderSetting {
|
||||
pageSpread: CbxPageSpread;
|
||||
pageViewMode: CbxPageViewMode;
|
||||
fitMode: CbxFitMode;
|
||||
scrollMode?: CbxScrollMode;
|
||||
backgroundColor?: CbxBackgroundColor;
|
||||
}
|
||||
|
||||
export interface NewPdfReaderSetting {
|
||||
pageSpread: PdfPageSpread;
|
||||
pageViewMode: PdfPageViewMode;
|
||||
}
|
||||
|
||||
export interface TableColumnPreference {
|
||||
field: string;
|
||||
visible: boolean;
|
||||
order: number;
|
||||
}
|
||||
|
||||
export interface UserSettings {
|
||||
perBookSetting: PerBookSetting;
|
||||
pdfReaderSetting: PdfReaderSetting;
|
||||
epubReaderSetting: EpubReaderSetting;
|
||||
cbxReaderSetting: CbxReaderSetting;
|
||||
newPdfReaderSetting: NewPdfReaderSetting;
|
||||
sidebarLibrarySorting: SidebarLibrarySorting;
|
||||
sidebarShelfSorting: SidebarShelfSorting;
|
||||
filterSortingMode: 'alphabetical' | 'count';
|
||||
metadataCenterViewMode: 'route' | 'dialog';
|
||||
entityViewPreferences: EntityViewPreferences;
|
||||
tableColumnPreference?: TableColumnPreference[];
|
||||
koReaderEnabled: boolean;
|
||||
}
|
||||
|
||||
export interface User {
|
||||
id: number;
|
||||
username: string;
|
||||
name: string;
|
||||
email: string;
|
||||
assignedLibraries: Library[];
|
||||
permissions: {
|
||||
admin: boolean;
|
||||
canUpload: boolean;
|
||||
canDownload: boolean;
|
||||
canEmailBook: boolean;
|
||||
canDeleteBook: boolean;
|
||||
canEditMetadata: boolean;
|
||||
canManipulateLibrary: boolean;
|
||||
canSyncKoReader: boolean;
|
||||
canSyncKobo: boolean;
|
||||
canAccessOpds: boolean;
|
||||
};
|
||||
userSettings: UserSettings;
|
||||
provisioningMethod?: 'LOCAL' | 'OIDC' | 'REMOTE';
|
||||
}
|
||||
|
||||
export interface UserState {
|
||||
user: User | null;
|
||||
loaded: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserService {
|
||||
private readonly apiUrl = `${API_CONFIG.BASE_URL}/api/v1/auth/register`;
|
||||
private readonly userUrl = `${API_CONFIG.BASE_URL}/api/v1/users`;
|
||||
|
||||
private http = inject(HttpClient);
|
||||
private authService = inject(AuthService);
|
||||
|
||||
private userStateSubject = new BehaviorSubject<UserState>({
|
||||
user: null,
|
||||
loaded: false,
|
||||
error: null,
|
||||
});
|
||||
|
||||
private loading$: Observable<User> | null = null;
|
||||
|
||||
constructor() {
|
||||
this.authService.token$.pipe(
|
||||
distinctUntilChanged()
|
||||
).subscribe(token => {
|
||||
if (token === null) {
|
||||
this.userStateSubject.next({
|
||||
user: null,
|
||||
loaded: true,
|
||||
error: null,
|
||||
});
|
||||
this.loading$ = null;
|
||||
} else {
|
||||
const current = this.userStateSubject.value;
|
||||
if (current.loaded && !current.user) {
|
||||
this.userStateSubject.next({
|
||||
user: null,
|
||||
loaded: false,
|
||||
error: null,
|
||||
});
|
||||
this.loading$ = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
userState$ = this.userStateSubject.asObservable().pipe(
|
||||
tap(state => {
|
||||
if (!state.loaded && !state.error && !this.loading$) {
|
||||
this.loading$ = this.fetchMyself().pipe(
|
||||
shareReplay(1),
|
||||
finalize(() => (this.loading$ = null))
|
||||
);
|
||||
this.loading$.subscribe();
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
private fetchMyself(): Observable<User> {
|
||||
return this.http.get<User>(`${this.userUrl}/me`).pipe(
|
||||
tap(user => this.userStateSubject.next({ user, loaded: true, error: null })),
|
||||
catchError(err => {
|
||||
const curr = this.userStateSubject.value;
|
||||
this.userStateSubject.next({ user: curr.user, loaded: true, error: err.message });
|
||||
throw err;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public setInitialUser(user: User): void {
|
||||
this.userStateSubject.next({ user, loaded: true, error: null });
|
||||
}
|
||||
|
||||
getCurrentUser(): User | null {
|
||||
return this.userStateSubject.value.user;
|
||||
}
|
||||
|
||||
getMyself(): Observable<User> {
|
||||
return this.http.get<User>(`${this.userUrl}/me`);
|
||||
}
|
||||
|
||||
createUser(userData: Omit<User, 'id'>): Observable<void> {
|
||||
return this.http.post<void>(this.apiUrl, userData);
|
||||
}
|
||||
|
||||
getUsers(): Observable<User[]> {
|
||||
return this.http.get<User[]>(this.userUrl);
|
||||
}
|
||||
|
||||
updateUser(userId: number, updateData: Partial<User>): Observable<User> {
|
||||
return this.http.put<User>(`${this.userUrl}/${userId}`, updateData);
|
||||
}
|
||||
|
||||
deleteUser(userId: number): Observable<void> {
|
||||
return this.http.delete<void>(`${this.userUrl}/${userId}`);
|
||||
}
|
||||
|
||||
changeUserPassword(userId: number, newPassword: string): Observable<void> {
|
||||
const payload = {
|
||||
userId: userId,
|
||||
newPassword: newPassword
|
||||
};
|
||||
return this.http.put<void>(`${this.userUrl}/change-user-password`, payload).pipe(
|
||||
catchError((error) => {
|
||||
const errorMessage = error?.error?.message || 'An unexpected error occurred. Please try again.';
|
||||
return throwError(() => new Error(errorMessage));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
changePassword(currentPassword: string, newPassword: string): Observable<void> {
|
||||
const payload = {
|
||||
currentPassword: currentPassword,
|
||||
newPassword: newPassword
|
||||
};
|
||||
return this.http.put<void>(`${this.userUrl}/change-password`, payload).pipe(
|
||||
catchError((error) => {
|
||||
const errorMessage = error?.error?.message || 'An unexpected error occurred. Please try again.';
|
||||
return throwError(() => new Error(errorMessage));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
updateUserSetting(userId: number, key: string, value: any): void {
|
||||
const payload = {
|
||||
key,
|
||||
value
|
||||
};
|
||||
this.http.put<void>(`${this.userUrl}/${userId}/settings`, payload, {
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
responseType: 'text' as 'json'
|
||||
}).subscribe(() => {
|
||||
const currentState = this.userStateSubject.value;
|
||||
if (currentState.user) {
|
||||
const updatedSettings = {...currentState.user.userSettings, [key]: value};
|
||||
const updatedUser = {...currentState.user, userSettings: updatedSettings};
|
||||
this.userStateSubject.next({...currentState, user: updatedUser});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user