mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.DTOs;
|
|
using API.Entities;
|
|
using API.Extensions;
|
|
using API.Interfaces;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
[Authorize]
|
|
public class UsersController : BaseApiController
|
|
{
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly ILibraryRepository _libraryRepository;
|
|
|
|
public UsersController(IUserRepository userRepository, ILibraryRepository libraryRepository)
|
|
{
|
|
_userRepository = userRepository;
|
|
_libraryRepository = libraryRepository;
|
|
}
|
|
|
|
[Authorize(Policy = "RequireAdminRole")]
|
|
[HttpPost("add-library")]
|
|
public async Task<ActionResult> AddLibrary(CreateLibraryDto createLibraryDto)
|
|
{
|
|
// NOTE: I think we should move this into library controller because it gets added to all admins
|
|
|
|
var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
|
|
|
|
if (user == null) return BadRequest("Could not validate user");
|
|
|
|
|
|
if (await _libraryRepository.LibraryExists(createLibraryDto.Name))
|
|
{
|
|
return BadRequest("Library name already exists. Please choose a unique name to the server.");
|
|
}
|
|
|
|
var library = new Library
|
|
{
|
|
Name = createLibraryDto.Name.ToLower(),
|
|
Type = createLibraryDto.Type,
|
|
AppUsers = new List<AppUser>() { user }
|
|
};
|
|
|
|
library.Folders = createLibraryDto.Folders.Select(x => new FolderPath
|
|
{
|
|
Path = x,
|
|
Library = library
|
|
}).ToList();
|
|
|
|
user.Libraries ??= new List<Library>(); // If user is null, then set it
|
|
|
|
user.Libraries.Add(library);
|
|
|
|
if (await _userRepository.SaveAllAsync())
|
|
{
|
|
return Ok();
|
|
}
|
|
|
|
return BadRequest("Not implemented");
|
|
}
|
|
|
|
[Authorize(Policy = "RequireAdminRole")]
|
|
[HttpDelete("delete-user")]
|
|
public async Task<ActionResult> DeleteUser(string username)
|
|
{
|
|
var user = await _userRepository.GetUserByUsernameAsync(username);
|
|
_userRepository.Delete(user);
|
|
|
|
if (await _userRepository.SaveAllAsync())
|
|
{
|
|
return Ok();
|
|
}
|
|
|
|
return BadRequest("Could not delete the user.");
|
|
}
|
|
|
|
[Authorize(Policy = "RequireAdminRole")]
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<MemberDto>>> GetUsers()
|
|
{
|
|
return Ok(await _userRepository.GetMembersAsync());
|
|
}
|
|
}
|
|
} |