Innovenergy_trunk/csharp/App/Backend/Controllers/Controller.cs

338 lines
9.3 KiB
C#
Raw Normal View History

2023-03-08 12:20:33 +00:00
using InnovEnergy.App.Backend.Database;
2023-03-15 13:38:06 +00:00
using InnovEnergy.App.Backend.DataTypes;
using InnovEnergy.App.Backend.DataTypes.Methods;
using InnovEnergy.App.Backend.Relations;
2023-03-20 07:33:44 +00:00
using InnovEnergy.Lib.Utils;
2023-02-16 12:57:06 +00:00
using Microsoft.AspNetCore.Mvc;
2023-03-08 12:20:33 +00:00
namespace InnovEnergy.App.Backend.Controllers;
2023-02-16 12:57:06 +00:00
[ApiController]
2023-03-15 13:38:06 +00:00
[Route("api/")]
2023-03-20 07:33:44 +00:00
public class Controller : ControllerBase
2023-02-16 12:57:06 +00:00
{
2023-03-20 07:33:44 +00:00
[HttpPost(nameof(Login))]
public ActionResult<Session> Login(Credentials credentials)
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var session = credentials.Login();
2023-03-09 16:21:31 +00:00
2023-03-15 13:38:06 +00:00
return session is null
2023-03-20 07:33:44 +00:00
? Unauthorized()
: session;
2023-02-16 12:57:06 +00:00
}
2023-03-20 07:33:44 +00:00
[HttpPost(nameof(Logout))]
public ActionResult Logout()
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-15 13:38:06 +00:00
return session.Logout()
2023-03-20 07:33:44 +00:00
? Ok()
: Unauthorized();
}
2023-03-09 11:50:21 +00:00
2023-03-20 07:33:44 +00:00
[HttpGet(nameof(GetUserById))]
public ActionResult<User> GetUserById(Int64 id)
{
var caller = GetSession()?.User;
if (caller == null)
2023-03-20 07:33:44 +00:00
return Unauthorized();
var user = Db.GetUserById(id);
if (user is null || !caller.HasAccessTo(user))
2023-03-20 07:33:44 +00:00
return Unauthorized();
2023-03-16 11:49:25 +00:00
user.Password = "";
return user;
}
2023-03-15 13:38:06 +00:00
2023-03-20 07:33:44 +00:00
[HttpGet(nameof(GetInstallationById))]
public ActionResult<Installation> GetInstallationById(Int64 id)
{
var user = GetSession()?.User;
if (user == null)
2023-03-20 07:33:44 +00:00
return Unauthorized();
var installation = Db.GetInstallationById(id);
if (installation is null || !user.HasAccessTo(installation))
2023-03-20 07:33:44 +00:00
return Unauthorized();
return installation;
}
2023-03-20 07:33:44 +00:00
[HttpGet(nameof(GetUsersWithAccessToInstallation))]
public ActionResult<IEnumerable<Object>> GetUsersWithAccessToInstallation(Int64 id)
{
var user = GetSession()?.User;
if (user == null)
2023-03-20 07:33:44 +00:00
return Unauthorized();
var installation = Db.GetInstallationById(id);
if (installation is null || !user.HasAccessTo(installation))
2023-03-20 07:33:44 +00:00
return Unauthorized();
2023-03-09 11:50:21 +00:00
var directAccess = installation
.UsersWithDirectAccess()
.Where(u => u.IsDescendantOf(user));
var inheritedAccess = installation
.Ancestors()
.SelectMany(f => f.UsersWithDirectAccess()
.Where(u => u.IsDescendantOf(user))
.Select(u => new { folderId = f.Id, user = u }));
return directAccess
2023-03-20 07:33:44 +00:00
.Concat<Object>(inheritedAccess)
.Apply(Ok); // TODO: typing
}
2023-03-20 07:33:44 +00:00
[HttpGet(nameof(GetUsersWithAccessToFolder))]
public ActionResult<IEnumerable<Object>> GetUsersWithAccessToFolder(Int64 id)
{
var user = GetSession()?.User;
if (user == null)
2023-03-20 07:33:44 +00:00
return Unauthorized();
var folder = Db.GetFolderById(id);
if (folder is null || !user.HasAccessTo(folder))
2023-03-20 07:33:44 +00:00
return Unauthorized();
return folder
.Ancestors()
.Prepend(folder)
.SelectMany(f => f.UsersWithDirectAccess()
.Where(u => u.IsDescendantOf(user))
2023-03-20 07:33:44 +00:00
.Select(u => new { folderId = f.Id, user = u }))
.ToList();
}
2023-02-22 13:46:36 +00:00
2023-03-20 07:33:44 +00:00
[HttpGet(nameof(GetFolderById))]
public ActionResult<Folder> GetFolderById(Int64 id)
{
var user = GetSession()?.User;
if (user == null)
2023-03-20 07:33:44 +00:00
return Unauthorized();
var folder = Db.GetFolderById(id);
if (folder is null || !user.HasAccessTo(folder))
2023-03-20 07:33:44 +00:00
return Unauthorized();
return folder;
}
2023-02-16 14:08:50 +00:00
2023-02-24 11:58:47 +00:00
2023-03-20 07:33:44 +00:00
[HttpGet(nameof(GetAllInstallations))]
public ActionResult<IEnumerable<Installation>> GetAllInstallations()
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var user = GetSession()?.User;
2023-03-20 07:33:44 +00:00
if (user is null)
return Unauthorized();
2023-02-16 12:57:06 +00:00
2023-03-20 07:33:44 +00:00
return user.AccessibleInstallations().ToList();
2023-02-16 12:57:06 +00:00
}
2023-02-24 11:58:47 +00:00
2023-03-20 07:33:44 +00:00
[HttpGet(nameof(GetAllFolders))]
public ActionResult<IEnumerable<Folder>> GetAllFolders()
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var user = GetSession()?.User;
2023-03-20 07:33:44 +00:00
if (user is null)
return Unauthorized();
2023-02-24 12:59:56 +00:00
2023-03-20 07:33:44 +00:00
return new(user.AccessibleFolders());
2023-02-24 12:59:56 +00:00
}
2023-03-15 13:38:06 +00:00
2023-03-20 07:33:44 +00:00
[HttpGet(nameof(GetAllFoldersAndInstallations))]
public ActionResult<IEnumerable<TreeNode>> GetAllFoldersAndInstallations()
{
2023-03-15 13:38:06 +00:00
var user = GetSession()?.User;
2023-03-20 07:33:44 +00:00
if (user is null)
return Unauthorized();
2023-03-20 07:33:44 +00:00
return new (user.AccessibleFoldersAndInstallations());
}
2023-03-09 15:33:14 +00:00
2023-03-15 13:38:06 +00:00
2023-03-20 07:33:44 +00:00
[HttpPost(nameof(CreateUser))]
public ActionResult<User> CreateUser(User newUser)
2023-03-09 15:33:14 +00:00
{
2023-03-20 07:33:44 +00:00
return GetSession().Create(newUser)
2023-03-15 13:38:06 +00:00
? newUser
2023-03-20 07:33:44 +00:00
: Unauthorized() ;
2023-03-09 15:33:14 +00:00
}
2023-03-20 07:33:44 +00:00
[HttpPost(nameof(CreateInstallation))]
public async Task<ActionResult<Installation>> CreateInstallation(Installation installation)
2023-03-09 15:33:14 +00:00
{
2023-03-20 07:33:44 +00:00
if (!await GetSession().Create(installation))
return Unauthorized();
return installation;
2023-03-09 15:33:14 +00:00
}
2023-03-20 07:33:44 +00:00
[HttpPost(nameof(CreateFolder))]
public ActionResult<Folder> CreateFolder(Folder folder)
2023-03-09 15:33:14 +00:00
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-09 15:33:14 +00:00
2023-03-20 07:33:44 +00:00
if (!session.Create(folder))
return Unauthorized();
return folder;
2023-03-09 15:33:14 +00:00
}
2023-03-20 07:33:44 +00:00
[HttpPost(nameof(GrantUserAccessToFolder))]
public ActionResult GrantUserAccessToFolder(FolderAccess folderAccess)
{
var session = GetSession();
// TODO: automatic BadRequest when properties are null during deserialization
var folder = Db.GetFolderById(folderAccess.FolderId);
var user = Db.GetUserById(folderAccess.UserId);
return session.GrantUserAccessTo(user, folder)
2023-03-20 07:33:44 +00:00
? Ok()
: Unauthorized();
}
2023-03-20 07:33:44 +00:00
[HttpPost(nameof(RevokeUserAccessToFolder))]
public ActionResult RevokeUserAccessToFolder(FolderAccess folderAccess)
{
var session = GetSession();
// TODO: automatic BadRequest when properties are null during deserialization
var folder = Db.GetFolderById(folderAccess.FolderId);
var user = Db.GetUserById(folderAccess.UserId);
return session.RevokeUserAccessTo(user, folder)
2023-03-20 07:33:44 +00:00
? Ok()
: Unauthorized();
}
2023-03-20 07:33:44 +00:00
[HttpPost(nameof(GrantUserAccessToInstallation))]
public ActionResult GrantUserAccessToInstallation(InstallationAccess installationAccess)
{
var session = GetSession();
// TODO: automatic BadRequest when properties are null during deserialization
var installation = Db.GetFolderById(installationAccess.InstallationId);
var user = Db.GetUserById(installationAccess.UserId);
return session.GrantUserAccessTo(user, installation)
2023-03-20 07:33:44 +00:00
? Ok()
: Unauthorized();
}
2023-03-20 07:33:44 +00:00
[HttpPost(nameof(RevokeUserAccessToInstallation))]
public ActionResult RevokeUserAccessToInstallation(InstallationAccess installationAccess)
{
var session = GetSession();
// TODO: automatic BadRequest when properties are null during deserialization
var installation = Db.GetFolderById(installationAccess.InstallationId);
var user = Db.GetUserById(installationAccess.UserId);
return session.RevokeUserAccessTo(user, installation)
2023-03-20 07:33:44 +00:00
? Ok()
: Unauthorized();
}
2023-03-20 07:33:44 +00:00
[HttpPut(nameof(UpdateUser))]
public ActionResult<User> UpdateUser(User updatedUser)
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-20 07:33:44 +00:00
if (!session.Update(updatedUser))
return Unauthorized();
updatedUser.Password = ""; // TODO: generic sanitize return values
2023-03-16 11:49:25 +00:00
return updatedUser;
2023-02-16 12:57:06 +00:00
}
2023-02-24 11:58:47 +00:00
2023-03-20 07:33:44 +00:00
[HttpPut(nameof(UpdateInstallation))]
public ActionResult<Installation> UpdateInstallation(Installation installation)
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-20 07:33:44 +00:00
if (!session.Update(installation))
return Unauthorized();
2023-02-16 12:57:06 +00:00
2023-03-20 07:33:44 +00:00
return installation;
2023-02-16 12:57:06 +00:00
}
2023-03-20 07:33:44 +00:00
[HttpPut(nameof(UpdateFolder))]
public ActionResult<Folder> UpdateFolder(Folder folder)
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-20 07:33:44 +00:00
if (!session.Update(folder))
return Unauthorized();
2023-02-16 12:57:06 +00:00
2023-03-20 07:33:44 +00:00
return folder;
2023-02-16 12:57:06 +00:00
}
2023-03-20 07:33:44 +00:00
[HttpDelete(nameof(DeleteUser))]
public ActionResult DeleteUser(Int64 userId)
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
var user = Db.GetUserById(userId);
2023-02-24 11:58:47 +00:00
2023-03-15 13:38:06 +00:00
return session.Delete(user)
2023-03-20 07:33:44 +00:00
? Ok()
: Unauthorized();
2023-02-16 12:57:06 +00:00
}
2023-03-20 07:33:44 +00:00
[HttpDelete(nameof(DeleteInstallation))]
public ActionResult DeleteInstallation(Int64 installationId)
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
var installation = Db.GetInstallationById(installationId);
2023-03-15 13:38:06 +00:00
return session.Delete(installation)
2023-03-20 07:33:44 +00:00
? Ok()
: Unauthorized();
2023-02-16 12:57:06 +00:00
}
2023-03-20 07:33:44 +00:00
[HttpDelete(nameof(DeleteFolder))]
public ActionResult DeleteFolder(Int64 folderId)
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-15 13:38:06 +00:00
var folder = Db.GetFolderById(folderId);
return session.Delete(folder)
2023-03-20 07:33:44 +00:00
? Ok()
: Unauthorized();
2023-02-16 12:57:06 +00:00
}
2023-03-15 13:38:06 +00:00
private static Session? GetSession()
2023-02-24 11:58:47 +00:00
{
var ctxAccessor = new HttpContextAccessor();
2023-03-15 13:38:06 +00:00
return ctxAccessor.HttpContext?.Items["Session"] as Session;
2023-02-24 11:58:47 +00:00
}
2023-02-16 12:57:06 +00:00
}
2023-02-24 11:58:47 +00:00