v1.0
This commit is contained in:
119
ProjectManager.Backend/Controllers/ProjectController.cs
Normal file
119
ProjectManager.Backend/Controllers/ProjectController.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ProjectManager.Backend.Apis;
|
||||
using ProjectManager.Backend.Entities;
|
||||
using ProjectManager.Backend.Options;
|
||||
using ProjectManager.Backend.Security;
|
||||
|
||||
namespace ProjectManager.Backend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("projects")]
|
||||
public class ProjectController : ControllerBase {
|
||||
private readonly IProjectApi _projects;
|
||||
private readonly ITokenContext _context;
|
||||
private readonly IUserApi _users;
|
||||
private readonly IDockerApi _docker;
|
||||
private readonly ProxyOptions _options;
|
||||
|
||||
public ProjectController(
|
||||
IProjectApi projects,
|
||||
ITokenContext context,
|
||||
IUserApi users,
|
||||
IDockerApi docker,
|
||||
IOptions<ProxyOptions> options
|
||||
) {
|
||||
_projects = projects;
|
||||
_context = context;
|
||||
_users = users;
|
||||
_docker = docker;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetProjects() {
|
||||
var projects = _projects.GetProjects(_context.UserId);
|
||||
var running = new bool[projects.Length];
|
||||
for (int i = 0; i < projects.Length; i++)
|
||||
running[i] = await _docker.IsContainerStarted(projects[i].ContainerName);
|
||||
return Ok(new { projects, running });
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet("{projectId}")]
|
||||
public IActionResult GetProject(string projectId) {
|
||||
var project = _projects.GetProject(projectId);
|
||||
if (project == null) return NotFound();
|
||||
if (project.OwnerId != _context.UserId) return Unauthorized();
|
||||
return Ok(project);
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> AddProject([FromBody] ProjectEdit edit) {
|
||||
if (!_users.CanCreateProject(_context.UserId)) return Forbid();
|
||||
var projectId = await _projects.AddProject(edit.Name, _context.UserId);
|
||||
if (projectId == null) return BadRequest();
|
||||
return Ok(new { ProjectId = projectId });
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpDelete("{projectId}")]
|
||||
public async Task<IActionResult> DeleteProject(string projectId) {
|
||||
var project = _projects.GetProject(projectId);
|
||||
if (project == null) return NotFound();
|
||||
if (project.OwnerId != _context.UserId) return Unauthorized();
|
||||
await _projects.DeleteProject(projectId);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpPut("{projectId}")]
|
||||
public IActionResult EditProject(string projectId, [FromBody] ProjectEdit edit) {
|
||||
var project = _projects.GetProject(projectId);
|
||||
if (project == null) return NotFound();
|
||||
if (project.OwnerId != _context.UserId) return Unauthorized();
|
||||
_projects.EditProject(projectId, edit.Name);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet("{projectId}/url")]
|
||||
public IActionResult GetProjectUrl(string projectId) {
|
||||
var project = _projects.GetProject(projectId);
|
||||
if (project == null) return NotFound();
|
||||
if (project.OwnerId != _context.UserId) return Unauthorized();
|
||||
if (_options.Enable) return Redirect($"https://{projectId}.{_options.Domain}/_/");
|
||||
return Redirect($"http://{_options.Host}:{project.Port}/_/");
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet("{projectId}/start")]
|
||||
public async Task<IActionResult> StartProject(string projectId) {
|
||||
var project = _projects.GetProject(projectId);
|
||||
if (project == null) return NotFound();
|
||||
if (project.OwnerId != _context.UserId) return Unauthorized();
|
||||
await _docker.StartContainer(project.ContainerName);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet("{projectId}/stop")]
|
||||
public async Task<IActionResult> StopProject(string projectId) {
|
||||
var project = _projects.GetProject(projectId);
|
||||
if (project == null) return NotFound();
|
||||
if (project.OwnerId != _context.UserId) return Unauthorized();
|
||||
await _docker.StopContainer(project.ContainerName);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet("{projectId}/status")]
|
||||
public async Task<IActionResult> ProjectStatus(string projectId) {
|
||||
var project = _projects.GetProject(projectId);
|
||||
if (project == null) return NotFound();
|
||||
if (project.OwnerId != _context.UserId) return Unauthorized();
|
||||
return Ok(new { Running = await _docker.IsContainerStarted(project.ContainerName) });
|
||||
}
|
||||
}
|
||||
87
ProjectManager.Backend/Controllers/UserController.cs
Normal file
87
ProjectManager.Backend/Controllers/UserController.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using ProjectManager.Backend.Entities;
|
||||
using ProjectManager.Backend.Security;
|
||||
using ProjectManager.Backend.Apis;
|
||||
|
||||
namespace ProjectManager.Backend.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("users")]
|
||||
public sealed class UserController : ControllerBase {
|
||||
private readonly IUserApi _users;
|
||||
private readonly ITokenApi _tokens;
|
||||
private readonly ITokenContext _context;
|
||||
|
||||
public UserController(IUserApi users, ITokenApi tokens, ITokenContext context) {
|
||||
_users = users;
|
||||
_tokens = tokens;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public IActionResult Login([FromBody] User login) {
|
||||
var user = _users.Login(login);
|
||||
if (user == null) return Conflict();
|
||||
return Ok(new {Token = _tokens.GetValidToken(user.UserId, HttpContext.Connection.RemoteIpAddress?.ToString())});
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public IActionResult Register([FromBody] User register) {
|
||||
var user = _users.Register(register);
|
||||
if (user is null) return Conflict();
|
||||
return Ok(new {Token = _tokens.GetValidToken(user.UserId, HttpContext.Connection.RemoteIpAddress?.ToString())});
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet("token")]
|
||||
public IActionResult CheckToken() {
|
||||
return Ok(new {Valid = true});
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet("me")]
|
||||
public IActionResult GetMe() {
|
||||
return GetUser(_context.UserId);
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet]
|
||||
public IActionResult GetUsers() {
|
||||
return Ok(_users.GetUsers().Select(user => new User {
|
||||
UserId = user.UserId,
|
||||
Email = user.Email,
|
||||
Username = user.Username
|
||||
}));
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpGet("{userId}")]
|
||||
public IActionResult GetUser(string userId) {
|
||||
var user = _users.GetUser(userId);
|
||||
|
||||
if (user is null) return NotFound();
|
||||
|
||||
user = new() {
|
||||
UserId = user.UserId,
|
||||
Email = user.Email,
|
||||
Username = user.Username
|
||||
};
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpPut]
|
||||
public IActionResult UpdateUser([FromBody] User user) {
|
||||
if (_context.UserId != user.UserId) return Forbid();
|
||||
if (!_users.UpdateUser(user)) return BadRequest();
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorized]
|
||||
[HttpDelete]
|
||||
public IActionResult DeleteUser() {
|
||||
_users.DeleteUser(_context.UserId);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user