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

373 lines
10 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-02-16 12:57:06 +00:00
using Microsoft.AspNetCore.Mvc;
2023-03-15 13:38:06 +00:00
using static System.Net.HttpStatusCode;
using Folder = InnovEnergy.App.Backend.DataTypes.Folder;
using Installation = InnovEnergy.App.Backend.DataTypes.Installation;
using Object = System.Object;
using User = InnovEnergy.App.Backend.DataTypes.User;
2023-02-16 12:57:06 +00:00
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-02-16 12:57:06 +00:00
public class Controller
{
2023-03-15 13:38:06 +00:00
private static readonly HttpResponseMessage _Unauthorized = new HttpResponseMessage(Unauthorized);
private static readonly HttpResponseMessage _Ok = new HttpResponseMessage(OK);
private static readonly HttpResponseMessage _BadRequest = new HttpResponseMessage(BadRequest);
2023-02-24 11:58:47 +00:00
[Returns<String>]
2023-03-15 13:38:06 +00:00
[Returns(Unauthorized)]
[Returns(BadRequest)]
[HttpPost($"{nameof(Login)}")]
public Object 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
? _Unauthorized
: session;
2023-02-16 12:57:06 +00:00
}
2023-03-15 13:38:06 +00:00
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPost($"{nameof(Logout)}")]
public Object Logout()
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-15 13:38:06 +00:00
return session.Logout()
? _Ok
: _Unauthorized;
}
2023-03-09 11:50:21 +00:00
[Returns<User>]
[Returns(Unauthorized)]
[HttpGet($"{nameof(GetUserById)}")]
public Object GetUserById(Int64 id)
{
var caller = GetSession()?.User;
if (caller == null)
return _Unauthorized;
var user = Db.GetUserById(id);
if (user is null || !caller.HasAccessTo(user))
return _Unauthorized;
2023-03-16 11:49:25 +00:00
user.Password = "";
return user;
}
2023-03-15 13:38:06 +00:00
[Returns<Installation>]
[Returns(Unauthorized)]
[HttpGet($"{nameof(GetInstallationById)}")]
public Object GetInstallationById(Int64 id)
{
var user = GetSession()?.User;
if (user == null)
return _Unauthorized;
var installation = Db.GetInstallationById(id);
if (installation is null || !user.HasAccessTo(installation))
return _Unauthorized;
return installation;
}
[Returns<Installation>]
[Returns(Unauthorized)]
[HttpGet($"{nameof(GetUsersWithAccessToInstallation)}")]
public Object GetUsersWithAccessToInstallation(Int64 id)
{
var user = GetSession()?.User;
if (user == null)
return _Unauthorized;
var installation = Db.GetInstallationById(id);
if (installation is null || !user.HasAccessTo(installation))
return _Unauthorized;
2023-03-09 11:50:21 +00:00
var usersWithInheritedAccess = installation
.Ancestors()
.SelectMany(f => f.UsersWithDirectAccess()
.Where(u => u.IsDescendantOf(user))
.Select(u => new { folderId = f.Id, user = u }))
.OfType<Object>();
var usersWithDirectAccess = installation.UsersWithDirectAccess()
.Where(u => u.IsDescendantOf(user))
.Select(u => new { installationId = installation.Id, user = u })
.OfType<Object>();
return usersWithInheritedAccess.Concat(usersWithDirectAccess);
}
[Returns<Installation>]
[Returns(Unauthorized)]
[HttpGet($"{nameof(GetUsersWithAccessToFolder)}")]
public Object GetUsersWithAccessToFolder(Int64 id)
{
var user = GetSession()?.User;
if (user == null)
return _Unauthorized;
var folder = Db.GetFolderById(id);
if (folder is null || !user.HasAccessTo(folder))
return _Unauthorized;
return folder
.Ancestors()
.Append(folder)
.SelectMany(f => f.UsersWithDirectAccess()
.Where(u => u.IsDescendantOf(user))
.Select(u => new { folderId = f.Id, user = u }));
}
2023-02-22 13:46:36 +00:00
[Returns<Folder>]
[Returns(Unauthorized)]
[HttpGet($"{nameof(GetFolderById)}")]
public Object GetFolderById(Int64 id)
{
var user = GetSession()?.User;
if (user == null)
return _Unauthorized;
var folder = Db.GetFolderById(id);
if (folder is null || !user.HasAccessTo(folder))
return _Unauthorized;
return folder;
}
2023-02-16 14:08:50 +00:00
2023-02-24 11:58:47 +00:00
[Returns<Installation[]>] // assuming swagger knows about arrays but not lists (JSON)
2023-03-15 13:38:06 +00:00
[Returns(Unauthorized)]
[HttpGet($"{nameof(GetAllInstallations)}/")]
2023-02-16 12:57:06 +00:00
public Object GetAllInstallations()
{
2023-03-15 13:38:06 +00:00
var user = GetSession()?.User;
2023-02-16 12:57:06 +00:00
2023-03-15 13:38:06 +00:00
return user is null
? _Unauthorized
: user.AccessibleInstallations();
2023-02-16 12:57:06 +00:00
}
2023-02-24 11:58:47 +00:00
[Returns<Folder[]>] // assuming swagger knows about arrays but not lists (JSON)
2023-03-15 13:38:06 +00:00
[Returns(Unauthorized)]
[HttpGet($"{nameof(GetAllFolders)}/")]
2023-02-16 12:57:06 +00:00
public Object GetAllFolders()
{
2023-03-15 13:38:06 +00:00
var user = GetSession()?.User;
2023-02-24 12:59:56 +00:00
2023-03-15 13:38:06 +00:00
return user is null
? _Unauthorized
: user.AccessibleFolders();
2023-02-24 12:59:56 +00:00
}
2023-03-15 13:38:06 +00:00
[Returns<TreeNode[]>] // assuming swagger knows about arrays but not lists (JSON)
2023-03-15 13:38:06 +00:00
[Returns(Unauthorized)]
[HttpGet($"{nameof(GetAllFoldersAndInstallations)}/")]
public Object GetAllFoldersAndInstallations()
{
2023-03-15 13:38:06 +00:00
var user = GetSession()?.User;
2023-03-15 13:38:06 +00:00
return user is null
? _Unauthorized
: user.AccessibleFoldersAndInstallations();
}
2023-02-24 12:59:56 +00:00
2023-03-09 15:33:14 +00:00
2023-03-15 13:38:06 +00:00
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPost($"{nameof(CreateUser)}/")]
2023-03-09 15:33:14 +00:00
public Object CreateUser(User newUser)
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-02-24 12:59:56 +00:00
2023-03-15 13:38:06 +00:00
return session.Create(newUser)
? newUser
: _Unauthorized ;
2023-03-09 15:33:14 +00:00
}
2023-03-15 13:38:06 +00:00
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPost($"{nameof(CreateInstallation)}/")]
2023-03-09 15:33:14 +00:00
public Object CreateInstallation(Installation installation)
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-09 15:33:14 +00:00
2023-03-15 13:38:06 +00:00
return session.Create(installation)
? installation
: _Unauthorized;
2023-03-09 15:33:14 +00:00
}
2023-03-15 13:38:06 +00:00
[Returns(OK)]
[Returns(Unauthorized)]
[Returns(InternalServerError)]
[HttpPost($"{nameof(CreateFolder)}/")]
2023-03-09 15:33:14 +00:00
public Object CreateFolder(Folder folder)
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-09 15:33:14 +00:00
2023-03-15 13:38:06 +00:00
return session.Create(folder)
? folder
: _Unauthorized;
2023-03-09 15:33:14 +00:00
}
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPost($"{nameof(GrantUserAccessToFolder)}/")]
public Object GrantUserAccessToFolder([FromQuery] Int64 folderId, [FromQuery] Int64? id)
{
var session = GetSession();
var user = id is not null ? Db.GetUserById(id) : session?.User;
return session.GrantUserAccessTo(user, Db.GetFolderById(folderId))
? _Ok
: _Unauthorized;
}
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPost($"{nameof(GrantUserAccessToInstallation)}/")]
public Object GrantUserAccessToInstallation([FromQuery] Int64 installationId, [FromQuery] Int64? id)
{
var session = GetSession();
var user = id is not null ? Db.GetUserById(id) : session?.User;
return session.GrantUserAccessTo(user, Db.GetInstallationById(installationId))
? _Ok
: _Unauthorized;
}
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPost($"{nameof(RevokeUserAccessToInstallation)}/")]
public Object RevokeUserAccessToInstallation([FromQuery] Int64 installationId, [FromQuery] Int64? id)
{
var session = GetSession();
var user = id is not null ? Db.GetUserById(id) : session?.User;
return session.RevokeUserAccessTo(user, Db.GetInstallationById(installationId))
? _Ok
: _Unauthorized;
}
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPost($"{nameof(RevokeUserAccessToFolder)}/")]
public Object RevokeUserAccessToFolder([FromQuery] Int64 folderId, [FromQuery] Int64? id)
{
var session = GetSession();
var user = id is not null ? Db.GetUserById(id) : session?.User;
2023-02-24 12:59:56 +00:00
return session.RevokeUserAccessTo(user, Db.GetFolderById(folderId))
? _Ok
: _Unauthorized;
}
2023-03-15 13:38:06 +00:00
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPut($"{nameof(UpdateUser)}/")]
2023-02-16 12:57:06 +00:00
public Object UpdateUser(User updatedUser)
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-03-16 11:49:25 +00:00
if (!session.Update(updatedUser)) return _Unauthorized;
updatedUser.Password = "";
return updatedUser;
2023-02-16 12:57:06 +00:00
}
2023-02-24 11:58:47 +00:00
2023-03-15 13:38:06 +00:00
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPut($"{nameof(UpdateInstallation)}/")]
2023-02-24 12:59:56 +00:00
public Object UpdateInstallation(Installation installation)
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-02-16 12:57:06 +00:00
2023-03-15 13:38:06 +00:00
return session.Update(installation)
? installation
: _Unauthorized;
2023-02-16 12:57:06 +00:00
}
2023-03-15 13:38:06 +00:00
[Returns(OK)]
[Returns(Unauthorized)]
[HttpPut($"{nameof(UpdateFolder)}/")]
2023-02-24 11:58:47 +00:00
public Object UpdateFolder(Folder folder)
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var session = GetSession();
2023-02-16 12:57:06 +00:00
2023-03-15 13:38:06 +00:00
return session.Update(folder)
? folder
: _Unauthorized;
2023-02-16 12:57:06 +00:00
}
2023-03-15 13:38:06 +00:00
[Returns(OK)]
[Returns(Unauthorized)]
[HttpDelete($"{nameof(DeleteUser)}/")]
2023-02-16 12:57:06 +00:00
public Object DeleteUser(Int64 userId)
{
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)
? _Ok
: _Unauthorized;
2023-02-16 12:57:06 +00:00
}
2023-03-15 13:38:06 +00:00
[Returns(OK)]
[Returns(Unauthorized)]
[HttpDelete($"{nameof(DeleteInstallation)}/")]
2023-02-24 11:58:47 +00:00
public Object 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)
? _Ok
: _Unauthorized;
2023-02-16 12:57:06 +00:00
}
[ProducesResponseType(200)]
[ProducesResponseType(401)]
2023-03-15 13:38:06 +00:00
[HttpDelete($"{nameof(DeleteFolder)}/")]
2023-02-16 12:57:06 +00:00
public Object DeleteFolder(Int64 folderId)
{
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)
? _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