giving back user language on login

This commit is contained in:
Kim 2023-03-09 17:21:31 +01:00
parent 2703dd1e11
commit 55e0ad22a3
3 changed files with 31 additions and 30 deletions

View File

@ -1,5 +1,6 @@
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Web.Http;
using InnovEnergy.App.Backend.Database; using InnovEnergy.App.Backend.Database;
using InnovEnergy.App.Backend.Model; using InnovEnergy.App.Backend.Model;
using InnovEnergy.App.Backend.Model.Relations; using InnovEnergy.App.Backend.Model.Relations;
@ -10,39 +11,38 @@ using HttpContextAccessor = Microsoft.AspNetCore.Http.HttpContextAccessor;
namespace InnovEnergy.App.Backend.Controllers; namespace InnovEnergy.App.Backend.Controllers;
[ApiController] [ApiController]
[Route("api/")] [Microsoft.AspNetCore.Mvc.Route("api/")]
public class Controller public class Controller
{ {
[Returns<String>] [Returns<String>]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[Returns(HttpStatusCode.BadRequest)] [Returns(HttpStatusCode.BadRequest)]
[HttpPost($"{nameof(Login)}")] [Microsoft.AspNetCore.Mvc.HttpPost($"{nameof(Login)}")]
public Object Login(Credentials credentials) public Object Login(Credentials credentials)
{ {
if (String.IsNullOrWhiteSpace(credentials.Username) || if (String.IsNullOrWhiteSpace(credentials.Username) ||
String.IsNullOrWhiteSpace(credentials.Password)) String.IsNullOrWhiteSpace(credentials.Password))
return new HttpResponseMessage(HttpStatusCode.BadRequest); return new HttpResponseException(HttpStatusCode.BadRequest);
using var db = Db.Connect(); using var db = Db.Connect();
var user = db.GetUserByEmail(credentials.Username); var user = db.GetUserByEmail(credentials.Username);
if (user is null)
return new HttpResponseMessage(HttpStatusCode.Unauthorized);
#if !DEBUG
if (user is null)
return new HttpResponseException(HttpStatusCode.BadRequest);
if (!VerifyPassword(credentials.Password, user)) if (!VerifyPassword(credentials.Password, user))
return new HttpResponseMessage(HttpStatusCode.Unauthorized); return new HttpResponseException(HttpStatusCode.Unauthorized);
#endif
var ses = new Session(user); var ses = new Session(user);
db.NewSession(ses); db.NewSession(ses);
return ses.Token; return new {ses.Token, user.Language};
} }
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpPost($"{nameof(Logout)}")] [Microsoft.AspNetCore.Mvc.HttpPost($"{nameof(Logout)}")]
public Object Logout() public Object Logout()
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -57,7 +57,7 @@ public class Controller
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpGet($"{nameof(GetInstallationS3Key)}")] [Microsoft.AspNetCore.Mvc.HttpGet($"{nameof(GetInstallationS3Key)}")]
public Object GetInstallationS3Key(Int64 installationId) public Object GetInstallationS3Key(Int64 installationId)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -82,7 +82,7 @@ public class Controller
[Returns<User>] [Returns<User>]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpGet($"{nameof(GetUserById)}")] [Microsoft.AspNetCore.Mvc.HttpGet($"{nameof(GetUserById)}")]
public Object GetUserById(Int64 id) public Object GetUserById(Int64 id)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -101,7 +101,7 @@ public class Controller
[Returns<Installation>] [Returns<Installation>]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpGet($"{nameof(GetInstallationById)}")] [Microsoft.AspNetCore.Mvc.HttpGet($"{nameof(GetInstallationById)}")]
public Object GetInstallationById(Int64 id) public Object GetInstallationById(Int64 id)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -120,7 +120,7 @@ public class Controller
[Returns<Folder>] [Returns<Folder>]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpGet($"{nameof(GetFolderById)}")] [Microsoft.AspNetCore.Mvc.HttpGet($"{nameof(GetFolderById)}")]
public Object GetFolderById(Int64 id) public Object GetFolderById(Int64 id)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -139,7 +139,7 @@ public class Controller
[Returns<Installation[]>] // assuming swagger knows about arrays but not lists (JSON) [Returns<Installation[]>] // assuming swagger knows about arrays but not lists (JSON)
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpGet($"{nameof(GetAllInstallations)}/")] [Microsoft.AspNetCore.Mvc.HttpGet($"{nameof(GetAllInstallations)}/")]
public Object GetAllInstallations() public Object GetAllInstallations()
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -156,7 +156,7 @@ public class Controller
[Returns<Folder[]>] // assuming swagger knows about arrays but not lists (JSON) [Returns<Folder[]>] // assuming swagger knows about arrays but not lists (JSON)
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpGet($"{nameof(GetAllFolders)}/")] [Microsoft.AspNetCore.Mvc.HttpGet($"{nameof(GetAllFolders)}/")]
public Object GetAllFolders() public Object GetAllFolders()
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -171,7 +171,7 @@ public class Controller
[Returns<TreeNode[]>] // assuming swagger knows about arrays but not lists (JSON) [Returns<TreeNode[]>] // assuming swagger knows about arrays but not lists (JSON)
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpGet($"{nameof(GetTree)}/")] [Microsoft.AspNetCore.Mvc.HttpGet($"{nameof(GetTree)}/")]
public Object GetTree() public Object GetTree()
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -193,7 +193,7 @@ public class Controller
[Returns<TreeNode[]>] // assuming swagger knows about arrays but not lists (JSON) [Returns<TreeNode[]>] // assuming swagger knows about arrays but not lists (JSON)
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpGet($"{nameof(GetAllFoldersAndInstallations)}/")] [Microsoft.AspNetCore.Mvc.HttpGet($"{nameof(GetAllFoldersAndInstallations)}/")]
public Object GetAllFoldersAndInstallations() public Object GetAllFoldersAndInstallations()
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -229,7 +229,7 @@ public class Controller
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpPost($"{nameof(CreateUser)}/")] [Microsoft.AspNetCore.Mvc.HttpPost($"{nameof(CreateUser)}/")]
public Object CreateUser(User newUser) public Object CreateUser(User newUser)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -244,7 +244,7 @@ public class Controller
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpPost($"{nameof(CreateInstallation)}/")] [Microsoft.AspNetCore.Mvc.HttpPost($"{nameof(CreateInstallation)}/")]
public Object CreateInstallation(Installation installation) public Object CreateInstallation(Installation installation)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -260,7 +260,7 @@ public class Controller
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpPost($"{nameof(CreateFolder)}/")] [Microsoft.AspNetCore.Mvc.HttpPost($"{nameof(CreateFolder)}/")]
public Object CreateFolder(Folder folder) public Object CreateFolder(Folder folder)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -275,7 +275,7 @@ public class Controller
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpPut($"{nameof(UpdateUser)}/")] [Microsoft.AspNetCore.Mvc.HttpPut($"{nameof(UpdateUser)}/")]
public Object UpdateUser(User updatedUser) public Object UpdateUser(User updatedUser)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -289,7 +289,7 @@ public class Controller
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpPut($"{nameof(UpdateInstallation)}/")] [Microsoft.AspNetCore.Mvc.HttpPut($"{nameof(UpdateInstallation)}/")]
public Object UpdateInstallation(Installation installation) public Object UpdateInstallation(Installation installation)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -321,7 +321,7 @@ public class Controller
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpPut($"{nameof(UpdateFolder)}/")] [Microsoft.AspNetCore.Mvc.HttpPut($"{nameof(UpdateFolder)}/")]
public Object UpdateFolder(Folder folder) public Object UpdateFolder(Folder folder)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -353,7 +353,7 @@ public class Controller
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpDelete($"{nameof(DeleteUser)}/")] [Microsoft.AspNetCore.Mvc.HttpDelete($"{nameof(DeleteUser)}/")]
public Object DeleteUser(Int64 userId) public Object DeleteUser(Int64 userId)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -375,7 +375,7 @@ public class Controller
[Returns(HttpStatusCode.OK)] [Returns(HttpStatusCode.OK)]
[Returns(HttpStatusCode.Unauthorized)] [Returns(HttpStatusCode.Unauthorized)]
[HttpDelete($"{nameof(DeleteInstallation)}/")] [Microsoft.AspNetCore.Mvc.HttpDelete($"{nameof(DeleteInstallation)}/")]
public Object DeleteInstallation(Int64 installationId) public Object DeleteInstallation(Int64 installationId)
{ {
var caller = GetCaller(); var caller = GetCaller();
@ -398,7 +398,7 @@ public class Controller
[ProducesResponseType(200)] [ProducesResponseType(200)]
[ProducesResponseType(401)] [ProducesResponseType(401)]
[HttpDelete($"{nameof(DeleteFolder)}/")] [Microsoft.AspNetCore.Mvc.HttpDelete($"{nameof(DeleteFolder)}/")]
public Object DeleteFolder(Int64 folderId) public Object DeleteFolder(Int64 folderId)
{ {
var caller = GetCaller(); var caller = GetCaller();

View File

@ -8,6 +8,7 @@ public class User : TreeNode
public String Email { get; set; } = null!; public String Email { get; set; } = null!;
public Boolean HasWriteAccess { get; set; } = false; public Boolean HasWriteAccess { get; set; } = false;
public String Salt { get; set; } = null!; public String Salt { get; set; } = null!;
public String Language { get; set; } = null!;
public String Password { get; set; } = null!; public String Password { get; set; } = null!;
// TODO: must reset pwd // TODO: must reset pwd

Binary file not shown.