Skip to content

Commit 9fd4716

Browse files
authored
Merge pull request #752 from PHOENIXCONTACT/fix/remove-automapper
Removed AutoMapper from Moryx.Identity.AccessManagement
2 parents 1c98a1c + 12b1fc7 commit 9fd4716

File tree

7 files changed

+106
-89
lines changed

7 files changed

+106
-89
lines changed

src/Moryx.Identity.AccessManagement/Controllers/API/AuthController.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Security.Claims;
55
using System.Text.Json;
6-
using AutoMapper;
76
using Microsoft.AspNetCore.Authorization;
87
using Microsoft.AspNetCore.Http;
98
using Microsoft.AspNetCore.Mvc;
@@ -28,7 +27,6 @@ public class AuthController : ControllerBase
2827
private readonly MoryxRoleManager _roleManager;
2928
private readonly IPermissionManager _permissionManager;
3029
private readonly ITokenService _tokenService;
31-
private readonly IMapper _mapper;
3230
private readonly IConfiguration _configuration;
3331

3432
/// <summary>
@@ -40,14 +38,12 @@ public class AuthController : ControllerBase
4038
/// <param name="permissionManager">The permission manager used by the AccessManagement</param>
4139
/// <param name="tokenService">A token service for handling the JWTs</param>
4240
/// <param name="configuration">Configuration settings mainly for the cookies' domain</param>
43-
public AuthController(IMapper mapper,
44-
MoryxUserManager userManager,
41+
public AuthController(MoryxUserManager userManager,
4542
MoryxRoleManager roleManager,
4643
IPermissionManager permissionManager,
4744
ITokenService tokenService,
4845
IConfiguration configuration)
4946
{
50-
_mapper = mapper;
5147
_userManager = userManager;
5248
_roleManager = roleManager;
5349
_permissionManager = permissionManager;
@@ -75,7 +71,7 @@ public async Task<IActionResult> SignUp(MoryxUserModel userModel)
7571
});
7672
}
7773

78-
var user = _mapper.Map<MoryxUserModel, MoryxUser>(userModel);
74+
var user = ModelConverter.GetUserFromModel(userModel);
7975
var userCreateResult = await _userManager.CreateAsync(user, userModel.Password);
8076

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

136-
var userModel = _mapper.Map<MoryxUser, MoryxUserModel>(user);
132+
var userModel = ModelConverter.GetUserModelFromUser(user);
137133
return Ok(userModel);
138134
}
139135

@@ -182,8 +178,7 @@ public async Task<IActionResult> SignIn(UserLoginModel userLoginModel)
182178
[Route("RefreshToken")]
183179
public async Task<IActionResult> RefreshToken()
184180
{
185-
186-
TokenRequest tokenRequest = new TokenRequest()
181+
var tokenRequest = new TokenRequest
187182
{
188183
RefreshToken = Request.Cookies[MoryxIdentityDefaults.REFRESH_TOKEN_COOKIE_NAME],
189184
Token = Request.Cookies[MoryxIdentityDefaults.JWT_COOKIE_NAME]
@@ -207,7 +202,7 @@ public async Task<IActionResult> GetUser()
207202
if (user is null)
208203
return NotFound("User not found");
209204

210-
var userModel = _mapper.Map<MoryxUser, MoryxUserModel>(user);
205+
var userModel = ModelConverter.GetUserModelFromUser(user);
211206
return Ok(userModel);
212207
}
213208

@@ -245,7 +240,7 @@ public async Task<ActionResult<string[]>> GetUserPermissions([FromQuery] string
245240
/// <summary>
246241
/// Verifies whether the given token is valid
247242
/// </summary>
248-
/// <param name="token">The token to be verified.</param>
243+
/// <param name="token">The token to be verified.</param>
249244
[AllowAnonymous]
250245
[HttpPost("verifyToken")]
251246
public IActionResult VerifyToken([FromBody] string token)
@@ -451,7 +446,7 @@ public async Task<IActionResult> DeleteRole(string roleName)
451446
}
452447

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

470465
return Ok(permissions);
471466
}
@@ -536,4 +531,3 @@ private static bool IsSuperAdmin(string roleName)
536531
}
537532
}
538533
}
539-

src/Moryx.Identity.AccessManagement/Controllers/MVC/LoginController.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
// Licensed under the Apache License, Version 2.0
33

44
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
5-
using AutoMapper;
6-
using Microsoft.AspNetCore.Http;
75
using Microsoft.AspNetCore.Mvc;
86
using Microsoft.Extensions.Configuration;
9-
using Moryx.Identity.AccessManagement.Data;
107
using Moryx.Identity.AccessManagement.Identity;
118
using Moryx.Identity.AccessManagement.Models;
129

@@ -16,16 +13,14 @@ public class LoginController : Controller
1613
{
1714
private readonly MoryxUserManager _userManager;
1815
private readonly ITokenService _tokenService;
19-
private readonly IMapper _mapper;
2016
private readonly IConfiguration _configuration;
2117
private readonly IPasswordResetService _pwResetService;
2218

2319
public LoginController(MoryxUserManager userManager,
24-
ITokenService tokenService, IMapper mapper, IConfiguration configuration, IPasswordResetService passwordResetService)
20+
ITokenService tokenService, IConfiguration configuration, IPasswordResetService passwordResetService)
2521
{
2622
_userManager = userManager;
2723
_tokenService = tokenService;
28-
_mapper = mapper;
2924
_configuration = configuration;
3025
_pwResetService = passwordResetService;
3126
}
@@ -80,7 +75,7 @@ public IActionResult Register()
8075

8176
public async Task<IActionResult> RegisterExecute(MoryxUserRegisterModel userModel)
8277
{
83-
var user = _mapper.Map<MoryxUserRegisterModel, MoryxUser>(userModel);
78+
var user = ModelConverter.GetUserFromUserRegisterModel(userModel);;
8479

8580
var userCreateResult = await _userManager.CreateAsync(user, userModel.Password);
8681
if (userCreateResult.Succeeded)

src/Moryx.Identity.AccessManagement/Controllers/MVC/UsersController.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22
// Licensed under the Apache License, Version 2.0
33

44
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
5-
using AutoMapper;
65
using Microsoft.AspNetCore.Authorization;
76
using Microsoft.AspNetCore.Mvc;
87
using Microsoft.EntityFrameworkCore;
98
using Microsoft.Extensions.Configuration;
9+
using Moryx.Identity.AccessManagement.Data;
1010
using Moryx.Identity.AccessManagement.Identity;
1111
using Moryx.Identity.AccessManagement.Models;
1212

1313
namespace Moryx.Identity.AccessManagement.Controllers
1414
{
15-
1615
[Authorize(Roles = Roles.SuperAdmin)]
1716
public class UsersController : Controller
1817
{
19-
private readonly IMapper _mapper;
2018
private readonly MoryxUserManager _userManager;
2119
private readonly IConfiguration _configuration;
2220
private readonly IPasswordResetService _pwResetService;
2321

24-
public UsersController(IMapper mapper, MoryxUserManager userManager, IConfiguration configuration, IPasswordResetService passwordResetService)
22+
public UsersController(MoryxUserManager userManager, IConfiguration configuration, IPasswordResetService passwordResetService)
2523
{
26-
_mapper = mapper;
2724
_userManager = userManager;
2825
_configuration = configuration;
2926
_pwResetService = passwordResetService;
@@ -38,7 +35,7 @@ public async Task<IActionResult> Index()
3835
public async Task<IActionResult> Edit(string userId)
3936
{
4037
var user = await _userManager.FindByIdAsync(userId);
41-
var userModel = _mapper.Map<MoryxUserUpdateModel>(user);
38+
var userModel = ModelConverter.GetUserUpdateModelFromUser(user);
4239
var pwReset = await _pwResetService.GetPasswordReset(userId);
4340
if (pwReset != null)
4441
userModel.PasswordResetToken = pwReset.ResetToken;

src/Moryx.Identity.AccessManagement/Extensions/ServiceCollectionExtensions.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public static class ServiceCollectionExtensions
2828
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
2929
/// <param name="jwtConfigurationSection">The section of the configuration containing the <see cref="JwtSettings"/>.</param>
3030
/// <param name="connectionString">The connection string for the PostgreSql database used by the MORYX AccessManagement.</param>
31-
/// <param name="corsOptionsAction">Action providing CORS options used for a call to
31+
/// <param name="corsOptionsAction">Action providing CORS options used for a call to
3232
/// <see cref="CorsServiceCollectionExtensions.AddCors(IServiceCollection, Action{CorsOptions})"/>.</param>
3333
/// <remarks>
34-
/// This method configures the <see cref="IServiceCollection"/> to use the MORYX AccessManagement with a PostgreSql
35-
/// database provider.
36-
/// It combines MORYX identity specific service registrations with the effects of
34+
/// This method configures the <see cref="IServiceCollection"/> to use the MORYX AccessManagement with a PostgreSql
35+
/// database provider.
36+
/// It combines MORYX identity specific service registrations with the effects of
3737
/// <see cref="EntityFrameworkServiceCollectionExtensions.AddDbContext{MoryxIdentitiesDbContext}(IServiceCollection, Action{DbContextOptionsBuilder}?, ServiceLifetime, ServiceLifetime)"/>
3838
/// <see cref="IdentityServiceCollectionExtensions.AddIdentity{MoryxUser, MoryxRole}(IServiceCollection, Action{IdentityOptions})"/>
3939
/// using the <see cref="MoryxUserManager"/>, the <see cref="MoryxRoleManager"/> and the <see cref="MoryxIdentitiesDbContext"/>,
@@ -61,12 +61,12 @@ public static IServiceCollection AddMoryxAccessManagement(this IServiceCollectio
6161
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
6262
/// <param name="jwtConfigurationSection">The section of the configuration containing the <see cref="JwtSettings"/>.</param>
6363
/// <param name="dbOptionsAction">A <see cref="Action{DbOptionsContextBuilder}"/> tp use a custom database provider.</param>
64-
/// <param name="corsOptionsAction">Action providing CORS options used for a call to
64+
/// <param name="corsOptionsAction">Action providing CORS options used for a call to
6565
/// <see cref="CorsServiceCollectionExtensions.AddCors(IServiceCollection, Action{CorsOptions})"/>.</param>
6666
/// <remarks>
6767
/// This method configures the <see cref="IServiceCollection"/> to use the MORYX AccessManagement with a custom
68-
/// database provider.
69-
/// It combines MORYX identity specific service registrations with the effects of
68+
/// database provider.
69+
/// It combines MORYX identity specific service registrations with the effects of
7070
/// <see cref="EntityFrameworkServiceCollectionExtensions.AddDbContext{MoryxIdentitiesDbContext}(IServiceCollection, Action{DbContextOptionsBuilder}?, ServiceLifetime, ServiceLifetime)"/>
7171
/// <see cref="IdentityServiceCollectionExtensions.AddIdentity{MoryxUser, MoryxRole}(IServiceCollection, Action{IdentityOptions}?)"/>
7272
/// using the <see cref="MoryxUserManager"/>, the <see cref="MoryxRoleManager"/> and the <see cref="MoryxIdentitiesDbContext"/>,
@@ -90,9 +90,6 @@ public static IServiceCollection AddMoryxAccessManagement(this IServiceCollectio
9090
services.AddSingleton(resolver =>
9191
resolver.GetRequiredService<IOptions<JwtSettings>>().Value);
9292

93-
// Register AutoMapper
94-
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
95-
9693
// Register Identity
9794
services.AddDbContext<MoryxIdentitiesDbContext>(dbOptionsAction);
9895

src/Moryx.Identity.AccessManagement/Mappings/MappingProfile.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2025, Phoenix Contact GmbH & Co. KG
2+
// Licensed under the Apache License, Version 2.0
3+
4+
using Moryx.Identity.AccessManagement.Data;
5+
6+
namespace Moryx.Identity.AccessManagement.Models;
7+
8+
internal static class ModelConverter
9+
{
10+
public static MoryxUserUpdateModel GetUserUpdateModelFromUser(MoryxUser user)
11+
{
12+
var model = new MoryxUserUpdateModel
13+
{
14+
UserName = user.UserName,
15+
Email = user.Email,
16+
FirstName = user.Firstname,
17+
LastName = user.LastName
18+
};
19+
20+
return model;
21+
}
22+
23+
public static MoryxUser GetUserFromUserRegisterModel(MoryxUserRegisterModel userModel)
24+
{
25+
var user = new MoryxUser
26+
{
27+
UserName = userModel.UserName,
28+
Email = userModel.Email,
29+
Firstname = userModel.FirstName,
30+
LastName = userModel.LastName
31+
};
32+
return user;
33+
}
34+
35+
public static MoryxUser GetUserFromModel(MoryxUserModel userModel)
36+
{
37+
var user = new MoryxUser
38+
{
39+
UserName = userModel.UserName,
40+
Email = userModel.Email,
41+
Firstname = userModel.FirstName,
42+
LastName = userModel.LastName
43+
};
44+
return user;
45+
}
46+
47+
public static MoryxUserModel GetUserModelFromUser(MoryxUser user)
48+
{
49+
var model = new MoryxUserModel
50+
{
51+
UserName = user.UserName,
52+
Email = user.Email,
53+
FirstName = user.Firstname,
54+
LastName = user.LastName
55+
};
56+
return model;
57+
}
58+
59+
public static PermissionModel GetPermissionModelFromPermission(Permission permission)
60+
{
61+
var model = new PermissionModel
62+
{
63+
Name = permission.Name,
64+
Roles = permission.Roles.Select(r => r.Name).ToArray()
65+
};
66+
return model;
67+
}
68+
}
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
2-
<PropertyGroup>
3-
<TargetFramework>net8.0</TargetFramework>
4-
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
5-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6-
<Description>MORYX Identity and Access Management (IAM) Module</Description>
7-
<IsPackable>true</IsPackable>
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
6+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
7+
<Description>MORYX Identity and Access Management (IAM) Module</Description>
8+
<IsPackable>true</IsPackable>
89
</PropertyGroup>
10+
911
<ItemGroup>
1012
<!-- Asp.Net Core dependencies -->
11-
<FrameworkReference Include="Microsoft.AspNetCore.App" />
12-
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
13-
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
14-
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
15-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
16-
<PackageReference Include="Microsoft.Identity.Web" />
17-
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" />
18-
<PackageReference Include="AutoMapper" />
13+
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
14+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore"/>
15+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer"/>
16+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL"/>
17+
<PackageReference Include="System.IdentityModel.Tokens.Jwt"/>
18+
<PackageReference Include="Microsoft.Identity.Web"/>
19+
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi"/>
1920
</ItemGroup>
21+
2022
<ItemGroup>
21-
<ProjectReference Include="..\Moryx.Identity\Moryx.Identity.csproj" />
23+
<ProjectReference Include="..\Moryx.Identity\Moryx.Identity.csproj"/>
2224
</ItemGroup>
23-
</Project>
25+
</Project>

0 commit comments

Comments
 (0)