Improved permissions
This commit is contained in:
@@ -4,25 +4,22 @@ using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using gswi.Interface.Services;
|
||||
using gswi.Model.DTOs;
|
||||
using gswi.Share.Common;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace gswi.Controllers
|
||||
{
|
||||
namespace gswi.Controllers {
|
||||
[Route("api/auth")]
|
||||
[ApiController]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
public class AuthController : ControllerBase {
|
||||
private readonly IAuthService _authService;
|
||||
|
||||
public AuthController(
|
||||
IAuthService authServce
|
||||
)
|
||||
{
|
||||
) {
|
||||
_authService = authServce;
|
||||
}
|
||||
|
||||
@@ -30,118 +27,102 @@ namespace gswi.Controllers
|
||||
// Get /api/auth/users
|
||||
[HttpGet("users")]
|
||||
[Authorize]
|
||||
public async Task<List<AuthUserDTO>> GetAllAuthUsers()
|
||||
{
|
||||
public async Task<List<AuthUserDTO>> GetAllAuthUsers() {
|
||||
return await _authService.GetAllAuthUsersAsync();
|
||||
}
|
||||
|
||||
|
||||
// POST /api/auth/users/get/filtered
|
||||
[HttpPost("users/get/filtered")]
|
||||
[Authorize]
|
||||
public async Task<GetFilteredAuthUsersResultDTO> GetFilteredAuthUsers(AuthUserSelectCriterion selectCriterion)
|
||||
{
|
||||
public async Task<GetFilteredAuthUsersResultDTO> GetFilteredAuthUsers(AuthUserSelectCriterion selectCriterion) {
|
||||
return await _authService.GetFilteredAuthUsersAsync(selectCriterion);
|
||||
}
|
||||
|
||||
// Get /api/auth/users/get/<mail>
|
||||
[HttpGet("users/get/{email}")]
|
||||
[Authorize]
|
||||
public async Task<AuthUserDTO> GetUserFromEMail(string email)
|
||||
{
|
||||
public async Task<AuthUserDTO> GetUserFromEMail(string email) {
|
||||
return await _authService.GetAuthUserByEMailAsync(email);
|
||||
}
|
||||
|
||||
// Get /api/auth/users/find/<mail>
|
||||
[HttpGet("users/find/{email}")]
|
||||
[Authorize]
|
||||
public async Task<AuthUserDTO> FindUserFromEMail(string email)
|
||||
{
|
||||
public async Task<AuthUserDTO> FindUserFromEMail(string email) {
|
||||
return await _authService.FindAuthUserByEMailAsync(email);
|
||||
}
|
||||
|
||||
/* Auth requests */
|
||||
// POST /api/auth/register
|
||||
[HttpPost("register")]
|
||||
public async Task Register(AuthUserDTO userDTO)
|
||||
{
|
||||
public async Task Register(AuthUserDTO userDTO) {
|
||||
await _authService.AddAuthUserAsync(userDTO);
|
||||
}
|
||||
|
||||
// POST /api/auth/register/<id>
|
||||
[HttpPost("register/{id}")]
|
||||
public async Task<bool> ConfirmEMail(string id)
|
||||
{
|
||||
public async Task<bool> ConfirmEMail(string id) {
|
||||
return await _authService.ConfirmEMail(id);
|
||||
}
|
||||
|
||||
// POST /api/auth/login
|
||||
[HttpPost("login")]
|
||||
public async Task<TokenDTO> Login(AuthUserDTO userDTO)
|
||||
{
|
||||
public async Task<TokenDTO> Login(AuthUserDTO userDTO) {
|
||||
return await _authService.Login(userDTO);
|
||||
}
|
||||
|
||||
// POST /api/auth/forgot-password
|
||||
[HttpPost("forgot-password")]
|
||||
public async Task ForgotPassword([FromBody] string email)
|
||||
{
|
||||
public async Task ForgotPassword([FromBody] string email) {
|
||||
await _authService.ForgotPassword(email);
|
||||
}
|
||||
|
||||
// POST /api/auth/confirm-forgot-password
|
||||
[HttpPost("confirm-forgot-password")]
|
||||
public async Task<EMailStringDTO> ConfirmForgotPassword([FromBody] string id)
|
||||
{
|
||||
public async Task<EMailStringDTO> ConfirmForgotPassword([FromBody] string id) {
|
||||
return await _authService.ConfirmForgotPassword(id);
|
||||
}
|
||||
|
||||
// POST /api/auth/reset-password
|
||||
[HttpPost("reset-password")]
|
||||
public async Task ResetPassword(ResetPasswordDTO rpDTO)
|
||||
{
|
||||
public async Task ResetPassword(ResetPasswordDTO rpDTO) {
|
||||
await _authService.ResetPassword(rpDTO);
|
||||
}
|
||||
|
||||
// POST /api/auth/update-user
|
||||
[HttpPost("update-user")]
|
||||
public async Task UpdateUser(UpdateUserDTO updateUserDTO)
|
||||
{
|
||||
public async Task UpdateUser(UpdateUserDTO updateUserDTO) {
|
||||
await _authService.UpdateUser(updateUserDTO);
|
||||
}
|
||||
|
||||
// POST /api/auth/update-user-as-admin
|
||||
[HttpPost("update-user-as-admin")]
|
||||
[Authorize]
|
||||
public async Task UpdateUserAsAdmin(AdminUpdateUserDTO updateUserDTO)
|
||||
{
|
||||
public async Task UpdateUserAsAdmin(AdminUpdateUserDTO updateUserDTO) {
|
||||
await _authService.UpdateUserAsAdmin(updateUserDTO);
|
||||
}
|
||||
|
||||
// POST /api/auth/refresh
|
||||
[HttpPost("refresh")]
|
||||
public async Task<TokenDTO> Refresh(TokenDTO tokenDTO)
|
||||
{
|
||||
public async Task<TokenDTO> Refresh(TokenDTO tokenDTO) {
|
||||
return await _authService.Refresh(tokenDTO);
|
||||
}
|
||||
|
||||
// POST /api/auth/revoke
|
||||
[HttpPost("revoke")]
|
||||
public async Task Revoke(TokenDTO tokenDTO)
|
||||
{
|
||||
public async Task Revoke(TokenDTO tokenDTO) {
|
||||
await _authService.Revoke(tokenDTO);
|
||||
}
|
||||
|
||||
// POST /api/auth/delete-user
|
||||
[HttpPost("delete-user")]
|
||||
public async Task DeleteAuthUserAsync(AuthUserDTO userDTO)
|
||||
{
|
||||
public async Task DeleteAuthUserAsync(AuthUserDTO userDTO) {
|
||||
await _authService.DeleteAuthUserAsync(userDTO);
|
||||
}
|
||||
|
||||
// POST /api/auth/delete-user
|
||||
[HttpPost("delete-user-by-mail/{mail}")]
|
||||
public async Task DeleteAuthUserByEMailAsync(string mail)
|
||||
{
|
||||
public async Task DeleteAuthUserByEMailAsync(string mail) {
|
||||
await _authService.DeleteAuthUserByEMailAsync(mail);
|
||||
}
|
||||
}
|
||||
|
@@ -1,73 +1,56 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using gswi.Data;
|
||||
using gswi.Model;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace gswi
|
||||
{
|
||||
public class DataSeeder
|
||||
{
|
||||
namespace gswi {
|
||||
public class DataSeeder {
|
||||
private readonly DatabaseContext _databaseContext;
|
||||
|
||||
public DataSeeder(DatabaseContext databaseContext)
|
||||
{
|
||||
public DataSeeder(DatabaseContext databaseContext) {
|
||||
_databaseContext = databaseContext;
|
||||
}
|
||||
|
||||
public void SeedData()
|
||||
{
|
||||
public void SeedData() {
|
||||
_databaseContext.Database.EnsureCreated();
|
||||
|
||||
if (!_databaseContext.AuthUsers.Any())
|
||||
{
|
||||
|
||||
var admin = new AuthUser()
|
||||
{
|
||||
if (!_databaseContext.AuthUsers.Any()) {
|
||||
var admin = new AuthUser() {
|
||||
FirstName = "Admin",
|
||||
LastName = "Administator",
|
||||
EMail = "admin@localhost",
|
||||
Password = ComputeHash("Administator", new SHA256CryptoServiceProvider()),
|
||||
LastName = "Admin",
|
||||
EMail = "admin@localhost.local",
|
||||
Password = ComputeHash("localhost.local", new SHA256CryptoServiceProvider()),
|
||||
AuthRole = AuthRoles.Admin
|
||||
};
|
||||
|
||||
var authUser = new AuthUser()
|
||||
{
|
||||
FirstName = "Max",
|
||||
LastName = "Mustermann",
|
||||
EMail = "max.mustermann@gmail.com",
|
||||
Password = ComputeHash("test1234", new SHA256CryptoServiceProvider()),
|
||||
var support = new AuthUser() {
|
||||
FirstName = "Support",
|
||||
LastName = "Supporter",
|
||||
EMail = "support@localhost.local",
|
||||
Password = ComputeHash("localhost.local", new SHA256CryptoServiceProvider()),
|
||||
AuthRole = AuthRoles.Supporter
|
||||
};
|
||||
|
||||
var authUser1 = new AuthUser()
|
||||
{
|
||||
FirstName = "Max",
|
||||
LastName = "Tester",
|
||||
EMail = "max.mustermann@mustermail.com",
|
||||
Password = ComputeHash("test1234", new SHA256CryptoServiceProvider()),
|
||||
};
|
||||
|
||||
var authUser2 = new AuthUser()
|
||||
{
|
||||
FirstName = "Max",
|
||||
LastName = "Muster",
|
||||
EMail = "max.mustermann@yahoo.com",
|
||||
Password = ComputeHash("test1234", new SHA256CryptoServiceProvider()),
|
||||
var user = new AuthUser() {
|
||||
FirstName = "User",
|
||||
LastName = "User",
|
||||
EMail = "user@localhost.local",
|
||||
Password = ComputeHash("localhost.local", new SHA256CryptoServiceProvider()),
|
||||
AuthRole = AuthRoles.User
|
||||
};
|
||||
|
||||
_databaseContext.AuthUsers.Add(admin);
|
||||
_databaseContext.AuthUsers.Add(authUser);
|
||||
_databaseContext.AuthUsers.Add(authUser1);
|
||||
_databaseContext.AuthUsers.Add(authUser2);
|
||||
_databaseContext.AuthUsers.Add(support);
|
||||
_databaseContext.AuthUsers.Add(user);
|
||||
}
|
||||
_databaseContext.SaveChanges();
|
||||
}
|
||||
|
||||
public string ComputeHash(string input, HashAlgorithm algorithm)
|
||||
{
|
||||
public string ComputeHash(string input, HashAlgorithm algorithm) {
|
||||
Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
|
||||
Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
|
||||
return BitConverter.ToString(hashedBytes);
|
||||
|
Reference in New Issue
Block a user