Ability to set user permissionId for registration
This commit is contained in:
parent
777fb1c4c9
commit
cb5c5b6b4f
@ -6,6 +6,12 @@
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:mysql://192.168.1.52:5618</jdbc-url>
|
||||
<jdbc-additional-properties>
|
||||
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.resource.type" value="Deployment" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||
</jdbc-additional-properties>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
<driver-properties>
|
||||
<property name="serverTimezone" value="UTC" />
|
||||
|
@ -1,7 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="file://$PROJECT_DIR$/Setup/Filler/Grants.sql" dialect="MySQL" />
|
||||
<file url="file://$PROJECT_DIR$/Setup/Filler/Permissions.sql" dialect="MySQL" />
|
||||
</component>
|
||||
</project>
|
@ -3,6 +3,7 @@ using API.DTO.Login;
|
||||
using API.Errors;
|
||||
using API.Services;
|
||||
using API.Services.Interfaces;
|
||||
using DAL.Models;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -48,16 +49,45 @@ namespace API.Controllers
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public ActionResult<UserDTO> register(UserRegisterDTO registerDTO)
|
||||
public ActionResult<UserDTO> register(UserRegisterDTO registerDTO, ulong? permissionId = null)
|
||||
{
|
||||
UserDTO? user = _userManager.registerUser(registerDTO);
|
||||
if (registerDTO.password == null)
|
||||
registerDTO.password = registerDTO.phoneNumber;
|
||||
|
||||
if (user == null)
|
||||
if (permissionId == null)
|
||||
{
|
||||
return Conflict(Strings.UserExists);
|
||||
User? user = getUser(User);
|
||||
if (user == null)
|
||||
return Unauthorized();
|
||||
|
||||
UserDTO? createdUser = _userManager.registerUser(registerDTO, user, permissionId);
|
||||
if (createdUser == null)
|
||||
return Conflict(Strings.UserExists);
|
||||
|
||||
return Ok(createdUser);
|
||||
}
|
||||
|
||||
return Ok(user);
|
||||
{
|
||||
UserDTO? user = _userManager.registerUser(registerDTO);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return Conflict(Strings.UserExists);
|
||||
}
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
public User? getUser(ClaimsPrincipal user)
|
||||
{
|
||||
Claim? idClaim = user.FindFirst(ClaimTypes.NameIdentifier);
|
||||
|
||||
if (idClaim == null)
|
||||
return null;
|
||||
|
||||
return _userService.getNoAuthentication(UInt64.Parse(idClaim.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,6 @@ namespace API.DTO.Login
|
||||
public PhoneNumber phoneNumber { get; set; } = null!;
|
||||
|
||||
[MaxLength(1000)]
|
||||
public string password { get; set; } = null!;
|
||||
public string? password { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
using API.DTO.Base;
|
||||
using API.DTO.Login;
|
||||
using DAL.Models;
|
||||
|
||||
namespace API.Services.Interfaces
|
||||
{
|
||||
@ -7,6 +8,6 @@ namespace API.Services.Interfaces
|
||||
{
|
||||
UserDTO? authenticateUser(UserLoginDTO loginDTO);
|
||||
|
||||
UserDTO? registerUser(UserRegisterDTO registerDTO);
|
||||
UserDTO? registerUser(UserRegisterDTO registerDTO, User? user = null, ulong? permissionId = null);
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ namespace API.Services
|
||||
return dto;
|
||||
}
|
||||
|
||||
public UserDTO? registerUser(UserRegisterDTO registerDTO)
|
||||
public UserDTO? registerUser(UserRegisterDTO registerDTO, User? user = null, ulong? permissionId = null)
|
||||
{
|
||||
if (_userService.getNoAuthentication(x =>
|
||||
x.phoneNumber.Equals(registerDTO.phoneNumber) ||
|
||||
@ -74,10 +74,13 @@ namespace API.Services
|
||||
byte[] salt;
|
||||
string hashedPassword = hashingAlgorithm.hash(registerDTO.password, out salt);
|
||||
|
||||
User user = _userService.add(registerDTO, hashedPassword, salt);
|
||||
User? createdUser = _userService.add(registerDTO, hashedPassword, salt, user, permissionId);
|
||||
|
||||
if (createdUser == null)
|
||||
return null;
|
||||
|
||||
UserDTO dto = new UserDTO();
|
||||
dto.adaptFromModel(user);
|
||||
dto.adaptFromModel(createdUser);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
@ -17,10 +17,9 @@ namespace API.Services
|
||||
_defaultUserPermission = defaultUserPermission;
|
||||
}
|
||||
|
||||
public User add(UserRegisterDTO registerDTO, string hashedPassword, byte[] salt)
|
||||
public User? add(UserRegisterDTO registerDTO, string hashedPassword, byte[] salt, User? user = null, ulong? permissionId = null)
|
||||
{
|
||||
Permission? defaultPermission = _permissionService.getNoAuthentication(_defaultUserPermission);
|
||||
|
||||
if (defaultPermission == null)
|
||||
throw new InvalidOperationException("defaultUserPermission doesn't exist.");
|
||||
|
||||
@ -33,11 +32,25 @@ namespace API.Services
|
||||
password = hashedPassword,
|
||||
salt = salt,
|
||||
|
||||
permissionId = defaultPermission.id,
|
||||
permissionId = permissionId ?? defaultPermission.id,
|
||||
|
||||
updated = DateTime.Now
|
||||
};
|
||||
|
||||
if (permissionId != null && user != null)
|
||||
{
|
||||
model.permissionId = permissionId.Value;
|
||||
model.updater = user.id;
|
||||
UserDTO userDTO = new UserDTO();
|
||||
userDTO.adaptFromModel(user);
|
||||
if (!_auth.canAdd(userDTO, user))
|
||||
return null;
|
||||
Context.Add(model);
|
||||
Context.SaveChanges();
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
Context.Add(model);
|
||||
Context.SaveChanges();
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADbContext_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F3bd4df5aff92cabbc4d630be64227073db1b8539b3a1e47786b4b189d7cdb7_003FDbContext_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=d51071ba_002D6946_002D464f_002Db1ff_002D8183035b48e5/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="Test1" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<TestAncestor>
|
||||
<TestId>NUnit3x::089ACCAD-8117-4C24-9429-56A394AC4734::net5.0::unitTests.Tests.Test1</TestId>
|
||||
|
Loading…
x
Reference in New Issue
Block a user