Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Security.Claims;
using System.Text.Json;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -28,7 +27,6 @@ public class AuthController : ControllerBase
private readonly MoryxRoleManager _roleManager;
private readonly IPermissionManager _permissionManager;
private readonly ITokenService _tokenService;
private readonly IMapper _mapper;
private readonly IConfiguration _configuration;

/// <summary>
Expand All @@ -40,14 +38,12 @@ public class AuthController : ControllerBase
/// <param name="permissionManager">The permission manager used by the AccessManagement</param>
/// <param name="tokenService">A token service for handling the JWTs</param>
/// <param name="configuration">Configuration settings mainly for the cookies' domain</param>
public AuthController(IMapper mapper,
MoryxUserManager userManager,
public AuthController(MoryxUserManager userManager,
MoryxRoleManager roleManager,
IPermissionManager permissionManager,
ITokenService tokenService,
IConfiguration configuration)
{
_mapper = mapper;
_userManager = userManager;
_roleManager = roleManager;
_permissionManager = permissionManager;
Expand Down Expand Up @@ -75,7 +71,7 @@ public async Task<IActionResult> SignUp(MoryxUserModel userModel)
});
}

var user = _mapper.Map<MoryxUserModel, MoryxUser>(userModel);
var user = ModelConverter.GetUserFromModel(userModel);
var userCreateResult = await _userManager.CreateAsync(user, userModel.Password);

if (userCreateResult.Succeeded)
Expand Down Expand Up @@ -133,7 +129,7 @@ public async Task<IActionResult> SignIn(UserLoginModel userLoginModel)
var jwtToken = await _tokenService.GenerateToken(user);
HttpContext.Response.Cookies.SetJwtCookie(jwtToken, user);

var userModel = _mapper.Map<MoryxUser, MoryxUserModel>(user);
var userModel = ModelConverter.GetUserModelFromUser(user);
return Ok(userModel);
}

Expand Down Expand Up @@ -182,8 +178,7 @@ public async Task<IActionResult> SignIn(UserLoginModel userLoginModel)
[Route("RefreshToken")]
public async Task<IActionResult> RefreshToken()
{

TokenRequest tokenRequest = new TokenRequest()
var tokenRequest = new TokenRequest
{
RefreshToken = Request.Cookies[MoryxIdentityDefaults.REFRESH_TOKEN_COOKIE_NAME],
Token = Request.Cookies[MoryxIdentityDefaults.JWT_COOKIE_NAME]
Expand All @@ -207,7 +202,7 @@ public async Task<IActionResult> GetUser()
if (user is null)
return NotFound("User not found");

var userModel = _mapper.Map<MoryxUser, MoryxUserModel>(user);
var userModel = ModelConverter.GetUserModelFromUser(user);
return Ok(userModel);
}

Expand Down Expand Up @@ -245,7 +240,7 @@ public async Task<ActionResult<string[]>> GetUserPermissions([FromQuery] string
/// <summary>
/// Verifies whether the given token is valid
/// </summary>
/// <param name="token">The token to be verified.</param>
/// <param name="token">The token to be verified.</param>
[AllowAnonymous]
[HttpPost("verifyToken")]
public IActionResult VerifyToken([FromBody] string token)
Expand Down Expand Up @@ -451,7 +446,7 @@ public async Task<IActionResult> DeleteRole(string roleName)
}

/// <summary>
/// Returns a list of permissions available in the system. Includes all permissions that start with
/// Returns a list of permissions available in the system. Includes all permissions that start with
/// the provided <paramref name="filter"/>.
/// </summary>
/// <param name="filter">A filter on the returned list. </param>
Expand All @@ -465,7 +460,7 @@ public IActionResult Permissions([FromQuery] string filter = "")
var permissions = _permissionManager.Permissions
.Include(p => p.Roles).ToArray()
.Where(p => p.Name.StartsWith(filter))
.Select(permission => _mapper.Map<Permission, PermissionModel>(permission)).ToArray();
.Select(ModelConverter.GetPermissionModelFromPermission).ToArray();

return Ok(permissions);
}
Expand Down Expand Up @@ -536,4 +531,3 @@ private static bool IsSuperAdmin(string roleName)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
// Licensed under the Apache License, Version 2.0

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Moryx.Identity.AccessManagement.Data;
using Moryx.Identity.AccessManagement.Identity;
using Moryx.Identity.AccessManagement.Models;

Expand All @@ -16,16 +13,14 @@ public class LoginController : Controller
{
private readonly MoryxUserManager _userManager;
private readonly ITokenService _tokenService;
private readonly IMapper _mapper;
private readonly IConfiguration _configuration;
private readonly IPasswordResetService _pwResetService;

public LoginController(MoryxUserManager userManager,
ITokenService tokenService, IMapper mapper, IConfiguration configuration, IPasswordResetService passwordResetService)
ITokenService tokenService, IConfiguration configuration, IPasswordResetService passwordResetService)
{
_userManager = userManager;
_tokenService = tokenService;
_mapper = mapper;
_configuration = configuration;
_pwResetService = passwordResetService;
}
Expand Down Expand Up @@ -80,7 +75,7 @@ public IActionResult Register()

public async Task<IActionResult> RegisterExecute(MoryxUserRegisterModel userModel)
{
var user = _mapper.Map<MoryxUserRegisterModel, MoryxUser>(userModel);
var user = ModelConverter.GetUserFromUserRegisterModel(userModel);;

var userCreateResult = await _userManager.CreateAsync(user, userModel.Password);
if (userCreateResult.Succeeded)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@
// Licensed under the Apache License, Version 2.0

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Moryx.Identity.AccessManagement.Data;
using Moryx.Identity.AccessManagement.Identity;
using Moryx.Identity.AccessManagement.Models;

namespace Moryx.Identity.AccessManagement.Controllers
{

[Authorize(Roles = Roles.SuperAdmin)]
public class UsersController : Controller
{
private readonly IMapper _mapper;
private readonly MoryxUserManager _userManager;
private readonly IConfiguration _configuration;
private readonly IPasswordResetService _pwResetService;

public UsersController(IMapper mapper, MoryxUserManager userManager, IConfiguration configuration, IPasswordResetService passwordResetService)
public UsersController(MoryxUserManager userManager, IConfiguration configuration, IPasswordResetService passwordResetService)
{
_mapper = mapper;
_userManager = userManager;
_configuration = configuration;
_pwResetService = passwordResetService;
Expand All @@ -38,7 +35,7 @@ public async Task<IActionResult> Index()
public async Task<IActionResult> Edit(string userId)
{
var user = await _userManager.FindByIdAsync(userId);
var userModel = _mapper.Map<MoryxUserUpdateModel>(user);
var userModel = ModelConverter.GetUserUpdateModelFromUser(user);
var pwReset = await _pwResetService.GetPasswordReset(userId);
if (pwReset != null)
userModel.PasswordResetToken = pwReset.ResetToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public static class ServiceCollectionExtensions
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="jwtConfigurationSection">The section of the configuration containing the <see cref="JwtSettings"/>.</param>
/// <param name="connectionString">The connection string for the PostgreSql database used by the MORYX AccessManagement.</param>
/// <param name="corsOptionsAction">Action providing CORS options used for a call to
/// <param name="corsOptionsAction">Action providing CORS options used for a call to
/// <see cref="CorsServiceCollectionExtensions.AddCors(IServiceCollection, Action{CorsOptions})"/>.</param>
/// <remarks>
/// This method configures the <see cref="IServiceCollection"/> to use the MORYX AccessManagement with a PostgreSql
/// database provider.
/// It combines MORYX identity specific service registrations with the effects of
/// This method configures the <see cref="IServiceCollection"/> to use the MORYX AccessManagement with a PostgreSql
/// database provider.
/// It combines MORYX identity specific service registrations with the effects of
/// <see cref="EntityFrameworkServiceCollectionExtensions.AddDbContext{MoryxIdentitiesDbContext}(IServiceCollection, Action{DbContextOptionsBuilder}?, ServiceLifetime, ServiceLifetime)"/>
/// <see cref="IdentityServiceCollectionExtensions.AddIdentity{MoryxUser, MoryxRole}(IServiceCollection, Action{IdentityOptions})"/>
/// using the <see cref="MoryxUserManager"/>, the <see cref="MoryxRoleManager"/> and the <see cref="MoryxIdentitiesDbContext"/>,
Expand Down Expand Up @@ -61,12 +61,12 @@ public static IServiceCollection AddMoryxAccessManagement(this IServiceCollectio
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="jwtConfigurationSection">The section of the configuration containing the <see cref="JwtSettings"/>.</param>
/// <param name="dbOptionsAction">A <see cref="Action{DbOptionsContextBuilder}"/> tp use a custom database provider.</param>
/// <param name="corsOptionsAction">Action providing CORS options used for a call to
/// <param name="corsOptionsAction">Action providing CORS options used for a call to
/// <see cref="CorsServiceCollectionExtensions.AddCors(IServiceCollection, Action{CorsOptions})"/>.</param>
/// <remarks>
/// This method configures the <see cref="IServiceCollection"/> to use the MORYX AccessManagement with a custom
/// database provider.
/// It combines MORYX identity specific service registrations with the effects of
/// database provider.
/// It combines MORYX identity specific service registrations with the effects of
/// <see cref="EntityFrameworkServiceCollectionExtensions.AddDbContext{MoryxIdentitiesDbContext}(IServiceCollection, Action{DbContextOptionsBuilder}?, ServiceLifetime, ServiceLifetime)"/>
/// <see cref="IdentityServiceCollectionExtensions.AddIdentity{MoryxUser, MoryxRole}(IServiceCollection, Action{IdentityOptions}?)"/>
/// using the <see cref="MoryxUserManager"/>, the <see cref="MoryxRoleManager"/> and the <see cref="MoryxIdentitiesDbContext"/>,
Expand All @@ -90,9 +90,6 @@ public static IServiceCollection AddMoryxAccessManagement(this IServiceCollectio
services.AddSingleton(resolver =>
resolver.GetRequiredService<IOptions<JwtSettings>>().Value);

// Register AutoMapper
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

// Register Identity
services.AddDbContext<MoryxIdentitiesDbContext>(dbOptionsAction);

Expand Down
36 changes: 0 additions & 36 deletions src/Moryx.Identity.AccessManagement/Mappings/MappingProfile.cs

This file was deleted.

68 changes: 68 additions & 0 deletions src/Moryx.Identity.AccessManagement/Models/ModelConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2025, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using Moryx.Identity.AccessManagement.Data;

namespace Moryx.Identity.AccessManagement.Models;

internal static class ModelConverter
{
public static MoryxUserUpdateModel GetUserUpdateModelFromUser(MoryxUser user)
{
var model = new MoryxUserUpdateModel
{
UserName = user.UserName,
Email = user.Email,
FirstName = user.Firstname,
LastName = user.LastName
};

return model;
}

public static MoryxUser GetUserFromUserRegisterModel(MoryxUserRegisterModel userModel)
{
var user = new MoryxUser
{
UserName = userModel.UserName,
Email = userModel.Email,
Firstname = userModel.FirstName,
LastName = userModel.LastName
};
return user;
}

public static MoryxUser GetUserFromModel(MoryxUserModel userModel)
{
var user = new MoryxUser
{
UserName = userModel.UserName,
Email = userModel.Email,
Firstname = userModel.FirstName,
LastName = userModel.LastName
};
return user;
}

public static MoryxUserModel GetUserModelFromUser(MoryxUser user)
{
var model = new MoryxUserModel
{
UserName = user.UserName,
Email = user.Email,
FirstName = user.Firstname,
LastName = user.LastName
};
return model;
}

public static PermissionModel GetPermissionModelFromPermission(Permission permission)
{
var model = new PermissionModel
{
Name = permission.Name,
Roles = permission.Roles.Select(r => r.Name).ToArray()
};
return model;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Description>MORYX Identity and Access Management (IAM) Module</Description>
<IsPackable>true</IsPackable>

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Description>MORYX Identity and Access Management (IAM) Module</Description>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<!-- Asp.Net Core dependencies -->
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
<PackageReference Include="Microsoft.Identity.Web" />
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" />
<PackageReference Include="AutoMapper" />
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL"/>
<PackageReference Include="System.IdentityModel.Tokens.Jwt"/>
<PackageReference Include="Microsoft.Identity.Web"/>
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Moryx.Identity\Moryx.Identity.csproj" />
<ProjectReference Include="..\Moryx.Identity\Moryx.Identity.csproj"/>
</ItemGroup>
</Project>
</Project>
Loading