Initial commit
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebDesktopBackend.Contract.Logic;
|
||||
using WebDesktopBackend.Entitys.Files;
|
||||
using WebDesktopBackend.LogicResults;
|
||||
using WebDesktopBackend.Security.Authorization;
|
||||
using FileShare = WebDesktopBackend.Entitys.Files.FileShare;
|
||||
|
||||
namespace WebDesktopBackend.Controller {
|
||||
[ApiController]
|
||||
[Route("files")]
|
||||
public class FileController : ControllerBase {
|
||||
private readonly IFileLogic _fileLogic;
|
||||
|
||||
public FileController(IFileLogic fileLogic) {
|
||||
_fileLogic = fileLogic;
|
||||
}
|
||||
|
||||
[HttpPost("upload/directory")]
|
||||
[Authorized]
|
||||
public ActionResult CreateDirectory([FromQuery] string directory, [FromQuery] string name) {
|
||||
return this.FromLogicResult(_fileLogic.CreateDirectory(directory, name));
|
||||
}
|
||||
|
||||
[HttpPost("upload/file")]
|
||||
[Authorized]
|
||||
[DisableRequestSizeLimit]
|
||||
public async Task<ActionResult> UploadFile() {
|
||||
try {
|
||||
return this.FromLogicResult(await _fileLogic.UploadFile(Request.Form));
|
||||
} catch (Exception) {
|
||||
return StatusCode((int)HttpStatusCode.BadRequest, "File upload Interupted");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("upload/json")]
|
||||
[Authorized]
|
||||
[DisableRequestSizeLimit]
|
||||
public async Task<ActionResult> UploadJson([FromQuery] string directory, [FromQuery] string name) {
|
||||
using var reader = new StreamReader(Request.Body, Encoding.UTF8);
|
||||
string content = await reader.ReadToEndAsync();
|
||||
return this.FromLogicResult(await _fileLogic.UploadJson(directory, name, content));
|
||||
}
|
||||
|
||||
[HttpGet("download/file")]
|
||||
[Authorized]
|
||||
public IActionResult DownloadFile([FromQuery] string directory, [FromQuery] string file) {
|
||||
var result = _fileLogic.DownloadFile(directory, file);
|
||||
if (!result.IsSuccessful)
|
||||
return this.FromLogicResult(result);
|
||||
|
||||
return File(result.Data, "APPLICATION/octet-stream", file);
|
||||
}
|
||||
|
||||
[HttpGet("download/json")]
|
||||
[Authorized]
|
||||
public async Task<ActionResult<string>> DownloadJson([FromQuery] string file) {
|
||||
return this.FromLogicResult(await _fileLogic.DownloadJson(file));
|
||||
}
|
||||
|
||||
[HttpGet("content")]
|
||||
[Authorized]
|
||||
public ActionResult<DirectoryContent> GetDirectoryContent([FromQuery] string directory) {
|
||||
return this.FromLogicResult(_fileLogic.GetDirectory(directory));
|
||||
}
|
||||
|
||||
[HttpGet("info/directory")]
|
||||
[Authorized]
|
||||
public ActionResult<DirectoryInformation> GetDirectoryInformation([FromQuery] string directory) {
|
||||
return this.FromLogicResult(_fileLogic.GetDirectoryInformation(directory));
|
||||
}
|
||||
|
||||
[HttpGet("info/file")]
|
||||
[Authorized]
|
||||
public ActionResult<DirectoryInformation> GetFileInformation([FromQuery] string directory, [FromQuery] string file) {
|
||||
return this.FromLogicResult(_fileLogic.GetFileInformation(directory, file));
|
||||
}
|
||||
|
||||
[HttpPut("move/directory")]
|
||||
[Authorized]
|
||||
public ActionResult MoveDirectory([FromQuery] string directory, [FromQuery] string name, [FromQuery] string to) {
|
||||
return this.FromLogicResult(_fileLogic.MoveDirectory(directory, name, to));
|
||||
}
|
||||
|
||||
[HttpPut("move/file")]
|
||||
[Authorized]
|
||||
public ActionResult MoveFile([FromQuery] string directory, [FromQuery] string file, [FromQuery] string to) {
|
||||
return this.FromLogicResult(_fileLogic.MoveFile(directory, file, to));
|
||||
}
|
||||
|
||||
[HttpDelete("delete")]
|
||||
[Authorized]
|
||||
public ActionResult DeleteFile([FromQuery] string url) {
|
||||
return this.FromLogicResult(_fileLogic.Delete(url));
|
||||
}
|
||||
|
||||
[HttpGet("share")]
|
||||
[Authorized]
|
||||
public ActionResult<FileShare> ShareFile([FromQuery] string url) {
|
||||
return this.FromLogicResult(_fileLogic.Share(url));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebDesktopBackend.Extentions;
|
||||
using WebDesktopBackend.Security.Authorization;
|
||||
|
||||
namespace WebDesktopBackend.Controller {
|
||||
|
||||
[ApiController]
|
||||
[Route("update")]
|
||||
public class UpdateController : ControllerBase {
|
||||
|
||||
[HttpGet("test")]
|
||||
public ActionResult Test() {
|
||||
return Ok("Authorized");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorized("group.admin")]
|
||||
public async Task Update() {
|
||||
if (HttpContext.WebSockets.IsWebSocketRequest) {
|
||||
using var socket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
||||
using var target = await new ClientWebSocket().ConnectAsync(new Uri("ws://213.136.89.237:4042"));
|
||||
|
||||
var t1 = socket.AddMessageEventHandler(msg => {
|
||||
target.SendMessage(msg);
|
||||
});
|
||||
var t2 = target.AddMessageEventHandler(msg => {
|
||||
socket.SendMessage(msg);
|
||||
});
|
||||
|
||||
while (!socket.CloseStatus.HasValue) {
|
||||
await Task.Delay(500);
|
||||
}
|
||||
|
||||
t1.Cancel();
|
||||
t2.Cancel();
|
||||
await target.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
|
||||
} else {
|
||||
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebDesktopBackend.Contract;
|
||||
using WebDesktopBackend.Contract.Logic;
|
||||
using WebDesktopBackend.Entitys.Tokens;
|
||||
using WebDesktopBackend.Entitys.User;
|
||||
using WebDesktopBackend.LogicResults;
|
||||
using WebDesktopBackend.Security;
|
||||
using WebDesktopBackend.Security.Authorization;
|
||||
|
||||
namespace WebDesktopBackend.Controller {
|
||||
[ApiController]
|
||||
[Route("users")]
|
||||
public class UserController : ControllerBase {
|
||||
private readonly IUserLogic _logic;
|
||||
private readonly ITokenContext _context;
|
||||
|
||||
public UserController(IUserLogic logic, ITokenContext context) {
|
||||
_logic = logic;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
[HttpPut("login")]
|
||||
public ActionResult<AccessTokenResponse> Login([FromBody] UserLogin login) {
|
||||
ILogicResult<Tokens> result = _logic.Login(login);
|
||||
if (result.State == LogicResultState.Ok) SetRefreshToken(result.Data.refreshToken);
|
||||
return this.FromLogicResult(new LogicResult<AccessTokenResponse> {State = result.State, Data = new AccessTokenResponse {Id = result.Data?.accessToken.Id}});
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public ActionResult<AccessTokenResponse> Register([FromBody] UserEditor editor) {
|
||||
ILogicResult<Tokens> result = _logic.Register(editor);
|
||||
SetRefreshToken(result.Data.refreshToken);
|
||||
return this.FromLogicResult(new LogicResult<AccessTokenResponse> {State = result.State, Data = new AccessTokenResponse {Id = result.Data.accessToken.Id}});
|
||||
}
|
||||
|
||||
[HttpDelete("logout")]
|
||||
[Authorized]
|
||||
public ActionResult Logout() {
|
||||
DeleteRefreshToken();
|
||||
return this.FromLogicResult(_logic.Logout());
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[Authorized(Permissions.EditUsers)]
|
||||
public ActionResult EditUser(string id, [FromBody] UserEditor editor) {
|
||||
return this.FromLogicResult(_logic.EditUser(id, editor));
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorized(Permissions.DeleteUsers)]
|
||||
public ActionResult DeleteUser(string id) {
|
||||
return this.FromLogicResult(_logic.DeleteUser(id));
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[Authorized(Permissions.ShowUsers)]
|
||||
public ActionResult<User> GetUser(string id) {
|
||||
return this.FromLogicResult(_logic.GetUser(id));
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorized(Permissions.ShowUsers)]
|
||||
public ActionResult<User[]> GetUsers() {
|
||||
return this.FromLogicResult(_logic.GetUsers());
|
||||
}
|
||||
|
||||
[HttpGet("validate")]
|
||||
[Authorized]
|
||||
public ActionResult Validate() {
|
||||
return this.FromLogicResult(_logic.Valdiate());
|
||||
}
|
||||
|
||||
[HttpGet("token")]
|
||||
public ActionResult<AccessTokenResponse> GetToken() {
|
||||
return this.FromLogicResult(_logic.GetToken(GetRefreshToken()));
|
||||
}
|
||||
|
||||
[HttpGet("ownuser")]
|
||||
[Authorized]
|
||||
public ActionResult<User> GetOwnUser() {
|
||||
return this.FromLogicResult(_logic.GetOwnUser());
|
||||
}
|
||||
|
||||
[HttpPut("ownuser")]
|
||||
[Authorized]
|
||||
public ActionResult<User> EditOwnUser([FromBody] UserEditor editor) {
|
||||
return this.FromLogicResult(_logic.EditUser(_context.UserId, editor));
|
||||
}
|
||||
|
||||
[HttpDelete("ownuser")]
|
||||
[Authorized]
|
||||
public ActionResult<User> DeleteOwnUser() {
|
||||
Logout();
|
||||
return this.FromLogicResult(_logic.DeleteUser(_context.UserId));
|
||||
}
|
||||
|
||||
[HttpGet("{id}/permissions")]
|
||||
[Authorized(Permissions.EditUserPermissions)]
|
||||
public ActionResult<string[]> GetPermissions(string id) {
|
||||
return this.FromLogicResult(_logic.GetPermissions(id));
|
||||
}
|
||||
|
||||
[HttpGet("{id}/permissions/raw")]
|
||||
[Authorized(Permissions.EditUserPermissions)]
|
||||
public ActionResult<string[]> GetRawPermissions(string id) {
|
||||
return this.FromLogicResult(_logic.GetRawPermissions(id));
|
||||
}
|
||||
|
||||
[HttpGet("permissions")]
|
||||
[Authorized]
|
||||
public ActionResult<string[]> GetPermissions() {
|
||||
return this.FromLogicResult(_logic.GetPermissions(HttpContext.User.GetUserId()));
|
||||
}
|
||||
|
||||
[HttpPost("{id}/permissions/{permission}")]
|
||||
[Authorized(Permissions.EditUserPermissions)]
|
||||
public ActionResult AddPermission(string id, string permission) {
|
||||
return this.FromLogicResult(_logic.AddPermission(id, permission));
|
||||
}
|
||||
|
||||
[HttpDelete("{id}/permissions/{permission}")]
|
||||
[Authorized(Permissions.EditUserPermissions)]
|
||||
public ActionResult DeletePermission(string id, string permission) {
|
||||
return this.FromLogicResult(_logic.DeletePermission(id, permission));
|
||||
}
|
||||
|
||||
private void DeleteRefreshToken()
|
||||
{
|
||||
HttpContext.Response.Cookies.Delete("refresh_token");
|
||||
}
|
||||
private void SetRefreshToken(RefreshToken token)
|
||||
{
|
||||
HttpContext.Response.Cookies.Append("refresh_token", token.Id, new CookieOptions()
|
||||
{
|
||||
MaxAge = token.ExpirationDate - DateTime.Now,
|
||||
HttpOnly = true,
|
||||
Secure = true
|
||||
});
|
||||
}
|
||||
|
||||
private string GetRefreshToken() {
|
||||
return HttpContext.Request.Cookies["refresh_token"];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user