Skip to content

Commit 74b7eb8

Browse files
committed
Updated constants class. Minor refactor of DI registrations
1 parent c328b1f commit 74b7eb8

15 files changed

Lines changed: 93 additions & 102 deletions

src/Host/InMemory/InMemoryIdentityManagerService.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public IdentityManagerMetadata GetMetadata()
2929
{
3030
var createprops = new List<PropertyMetadata>()
3131
{
32-
PropertyMetadata.FromProperty<InMemoryUser>(x => x.Username, name:Constants.ClaimTypes.Username, required:true),
32+
PropertyMetadata.FromProperty<InMemoryUser>(x => x.Username, name:IdentityManagerConstants.ClaimTypes.Username, required:true),
3333
};
3434

3535
var updateprops = new List<PropertyMetadata>();
3636
updateprops.AddRange(new PropertyMetadata[]{
37-
PropertyMetadata.FromProperty<InMemoryUser>(x => x.Username, name:Constants.ClaimTypes.Username, required:true),
38-
PropertyMetadata.FromPropertyName<InMemoryUser>("Password", name:Constants.ClaimTypes.Password, required:true),
39-
PropertyMetadata.FromFunctions<InMemoryUser, string>(Constants.ClaimTypes.Name, u => GetName(u), SetName, displayName:"DisplayName", required:true),
37+
PropertyMetadata.FromProperty<InMemoryUser>(x => x.Username, name:IdentityManagerConstants.ClaimTypes.Username, required:true),
38+
PropertyMetadata.FromPropertyName<InMemoryUser>("Password", name:IdentityManagerConstants.ClaimTypes.Password, required:true),
39+
PropertyMetadata.FromFunctions<InMemoryUser, string>(IdentityManagerConstants.ClaimTypes.Name, u => GetName(u), SetName, displayName:"DisplayName", required:true),
4040
});
4141
updateprops.AddRange(PropertyMetadata.FromType<InMemoryUser>());
4242
updateprops.AddRange(new PropertyMetadata[]{
@@ -76,7 +76,7 @@ public IdentityManagerMetadata GetMetadata()
7676
},
7777
RoleMetadata = new RoleMetadata
7878
{
79-
RoleClaimType = Constants.ClaimTypes.Role,
79+
RoleClaimType = IdentityManagerConstants.ClaimTypes.Role,
8080
SupportsCreate = true,
8181
SupportsDelete = true,
8282
CreateProperties = roleCreateProps,
@@ -91,15 +91,15 @@ private string GetName(InMemoryUser user)
9191
{
9292
if(user == null) throw new ArgumentNullException();
9393

94-
return user.Claims.GetValue(Constants.ClaimTypes.Name);
94+
return user.Claims.GetValue(IdentityManagerConstants.ClaimTypes.Name);
9595
}
9696

9797
private IdentityManagerResult SetName(InMemoryUser user, string value)
9898
{
9999
if (user == null) throw new ArgumentNullException("SetName::" + string.Format(ExceptionMessages.IsNotAssigned, user));
100100
if (value == null) throw new ArgumentNullException("SetName::" + string.Format(ExceptionMessages.IsNotAssigned, value));
101101

102-
user.Claims.SetValue(Constants.ClaimTypes.Name, value);
102+
user.Claims.SetValue(IdentityManagerConstants.ClaimTypes.Name, value);
103103
return IdentityManagerResult.Success;
104104
}
105105

@@ -152,7 +152,7 @@ from u in users
152152
filter = filter.ToLower();
153153
query =
154154
from u in query
155-
let names = (from c in u.Claims where c.Type == Constants.ClaimTypes.Name select c.Value.ToLower())
155+
let names = (from c in u.Claims where c.Type == IdentityManagerConstants.ClaimTypes.Name select c.Value.ToLower())
156156
where
157157
u.Username.ToLower().Contains(filter) ||
158158
names.Contains(filter)
@@ -165,7 +165,7 @@ from u in query.Distinct()
165165
{
166166
Subject = u.Subject,
167167
Username = u.Username,
168-
Name = u.Claims.Where(x => x.Type == Constants.ClaimTypes.Name).Select(x => x.Value).FirstOrDefault(),
168+
Name = u.Claims.Where(x => x.Type == IdentityManagerConstants.ClaimTypes.Name).Select(x => x.Value).FirstOrDefault(),
169169
};
170170
var total = items.Count();
171171

@@ -200,7 +200,7 @@ public async Task<IdentityManagerResult<UserDetail>> GetUserAsync(string subject
200200
{
201201
Subject = user.Subject,
202202
Username = user.Username,
203-
Name = user.Claims.GetValue(Constants.ClaimTypes.Name),
203+
Name = user.Claims.GetValue(IdentityManagerConstants.ClaimTypes.Name),
204204
Properties = props,
205205
Claims = user.Claims.Select(x => new ClaimValue { Type = x.Type, Value = x.Value })
206206
});
@@ -245,7 +245,7 @@ private async Task<string> GetUserProperty(PropertyMetadata property, InMemoryUs
245245
switch (property.Type)
246246
{
247247
case "role.admin":
248-
return user.Claims.HasValue(Constants.ClaimTypes.Role, "admin").ToString().ToLower();
248+
return user.Claims.HasValue(IdentityManagerConstants.ClaimTypes.Role, "admin").ToString().ToLower();
249249
case "gravatar":
250250
return user.Claims.GetValue("gravatar");
251251
}
@@ -267,8 +267,8 @@ private Task<IdentityManagerResult> SetUserProperty(IEnumerable<PropertyMetadata
267267
case "role.admin":
268268
{
269269
var val = bool.Parse(value);
270-
if (val) user.Claims.AddClaim(Constants.ClaimTypes.Role, "admin");
271-
else user.Claims.RemoveClaim(Constants.ClaimTypes.Role, "admin");
270+
if (val) user.Claims.AddClaim(IdentityManagerConstants.ClaimTypes.Role, "admin");
271+
else user.Claims.RemoveClaim(IdentityManagerConstants.ClaimTypes.Role, "admin");
272272
}
273273
break;
274274
case "gravatar":
@@ -292,12 +292,12 @@ private IEnumerable<string> ValidateUserProperty(string type, string value)
292292
{
293293
switch (type)
294294
{
295-
case Constants.ClaimTypes.Username:
295+
case IdentityManagerConstants.ClaimTypes.Username:
296296
{
297297
if (users.Any(x => x.Username == value)) return new[] { "That Username is already in use" };
298298
}
299299
break;
300-
case Constants.ClaimTypes.Password:
300+
case IdentityManagerConstants.ClaimTypes.Password:
301301
{
302302
if (value.Length < 3) return new[] { "Password must have at least 3 characters" };
303303
}

src/Host/InMemory/Users.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Security.Claims;
4-
using IdentityManager2.Core;
4+
using IdentityManager2;
55

66
namespace Host.InMemory
77
{
@@ -18,10 +18,10 @@ public static ICollection<InMemoryUser> Get(int random = 0)
1818
Email = "alice@email.com",
1919
Mobile = "123",
2020
Claims = new HashSet<Claim>{
21-
new Claim(Constants.ClaimTypes.Name, "Alice Smith"),
22-
new Claim(Constants.ClaimTypes.Role, "admin"),
23-
new Claim(Constants.ClaimTypes.Role, "employee"),
24-
new Claim(Constants.ClaimTypes.Role, "manager"),
21+
new Claim(IdentityManagerConstants.ClaimTypes.Name, "Alice Smith"),
22+
new Claim(IdentityManagerConstants.ClaimTypes.Role, "admin"),
23+
new Claim(IdentityManagerConstants.ClaimTypes.Role, "employee"),
24+
new Claim(IdentityManagerConstants.ClaimTypes.Role, "manager"),
2525
new Claim("department", "sales"),
2626
}
2727
},
@@ -31,9 +31,9 @@ public static ICollection<InMemoryUser> Get(int random = 0)
3131
Password = "bob",
3232
Email = "bob@email.com",
3333
Claims = new HashSet<Claim>{
34-
new Claim(Constants.ClaimTypes.Name, "Bob Smith"),
35-
new Claim(Constants.ClaimTypes.Role, "employee"),
36-
new Claim(Constants.ClaimTypes.Role, "developer"),
34+
new Claim(IdentityManagerConstants.ClaimTypes.Name, "Bob Smith"),
35+
new Claim(IdentityManagerConstants.ClaimTypes.Role, "employee"),
36+
new Claim(IdentityManagerConstants.ClaimTypes.Role, "developer"),
3737
new Claim("department", "IT"),
3838
}
3939
},
@@ -43,9 +43,9 @@ public static ICollection<InMemoryUser> Get(int random = 0)
4343
Password = "test",
4444
Email = "test@email.com",
4545
Claims = new HashSet<Claim>{
46-
new Claim(Constants.ClaimTypes.Name, "Test"),
47-
new Claim(Constants.ClaimTypes.Role, "employee"),
48-
new Claim(Constants.ClaimTypes.Role, "developer"),
46+
new Claim(IdentityManagerConstants.ClaimTypes.Name, "Test"),
47+
new Claim(IdentityManagerConstants.ClaimTypes.Role, "employee"),
48+
new Claim(IdentityManagerConstants.ClaimTypes.Role, "developer"),
4949
new Claim("department", "IT"),
5050
}
5151
},

src/IdentityManager2/Api/Controllers/MetaController.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using IdentityManager2.Api.Models;
5-
using IdentityManager2.Core;
65
using IdentityManager2.Core.Metadata;
76
using Microsoft.AspNetCore.Authorization;
87
using Microsoft.AspNetCore.Mvc;
98

109
namespace IdentityManager2.Api.Controllers
1110
{
12-
[Route(Constants.MetadataRoutePrefix)]
13-
[Authorize(Constants.IdMgrAuthPolicy)]
11+
[Route(IdentityManagerConstants.MetadataRoutePrefix)]
12+
[Authorize(IdentityManagerConstants.IdMgrAuthPolicy)]
1413
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
1514
public class MetaController : Controller
1615
{

src/IdentityManager2/Api/Controllers/PageController.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Threading.Tasks;
33
using IdentityManager2.Assets;
44
using IdentityManager2.Configuration;
5-
using IdentityManager2.Core;
65
using Microsoft.AspNetCore.Authentication;
76
using Microsoft.AspNetCore.Authorization;
87
using Microsoft.AspNetCore.Mvc;
@@ -20,8 +19,8 @@ public PageController(IdentityManagerOptions config)
2019
}
2120

2221
[HttpGet]
23-
[Authorize(Constants.IdMgrAuthPolicy)]
24-
[Route("", Name = Constants.RouteNames.Home)]
22+
[Authorize(IdentityManagerConstants.IdMgrAuthPolicy)]
23+
[Route("", Name = IdentityManagerConstants.RouteNames.Home)]
2524
public IActionResult Index()
2625
{
2726
return new EmbeddedHtmlResult(
@@ -32,13 +31,13 @@ public IActionResult Index()
3231

3332
[HttpGet]
3433
[AllowAnonymous]
35-
[Route("api/login", Name = Constants.RouteNames.Login)]
34+
[Route("api/login", Name = IdentityManagerConstants.RouteNames.Login)]
3635
public async Task<IActionResult> Login(string returnUrl)
3736
{
3837
var result = await HttpContext.AuthenticateAsync(config.SecurityConfiguration.HostAuthenticationType);
3938
if (result.Succeeded)
4039
{
41-
await HttpContext.SignInAsync(Constants.LocalApiScheme, result.Principal);
40+
await HttpContext.SignInAsync(IdentityManagerConstants.LocalApiScheme, result.Principal);
4241
return Redirect(returnUrl);
4342
}
4443

@@ -47,11 +46,11 @@ public async Task<IActionResult> Login(string returnUrl)
4746

4847
[HttpGet]
4948
[AllowAnonymous]
50-
[Route("logout", Name = Constants.RouteNames.Logout)]
49+
[Route("logout", Name = IdentityManagerConstants.RouteNames.Logout)]
5150
public async Task<IActionResult> Logout()
5251
{
5352
await config.SecurityConfiguration.SignOut(HttpContext);
54-
return RedirectToRoute(Constants.RouteNames.Home, null);
53+
return RedirectToRoute(IdentityManagerConstants.RouteNames.Home, null);
5554
}
5655
}
5756
}

src/IdentityManager2/Api/Controllers/RolesController.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace IdentityManager2.Api.Controllers
1515
{
16-
[Route(Constants.RoleRoutePrefix)]
17-
[Authorize(Constants.IdMgrAuthPolicy)]
16+
[Route(IdentityManagerConstants.RoleRoutePrefix)]
17+
[Authorize(IdentityManagerConstants.IdMgrAuthPolicy)]
1818
[ResponseCache(NoStore=true, Location=ResponseCacheLocation.None)]
1919
public class RolesController : Controller
2020
{
@@ -45,7 +45,7 @@ public async Task<IdentityManagerMetadata> GetMetadataAsync()
4545
}
4646

4747
// GET api/roles
48-
[HttpGet, Route("", Name = Constants.RouteNames.GetRoles)]
48+
[HttpGet, Route("", Name = IdentityManagerConstants.RouteNames.GetRoles)]
4949
public async Task<IActionResult> GetRolesAsync(string filter = null, int start = 0, int count = 100)
5050
{
5151
var meta = await GetMetadataAsync();
@@ -71,7 +71,7 @@ public async Task<IActionResult> GetRolesAsync(string filter = null, int start =
7171
}
7272

7373
// POST
74-
[HttpPost, Route("", Name = Constants.RouteNames.CreateRole)]
74+
[HttpPost, Route("", Name = IdentityManagerConstants.RouteNames.CreateRole)]
7575
public async Task<IActionResult> CreateRoleAsync([FromBody]PropertyValue[] properties)
7676
{
7777
var meta = await GetMetadataAsync();
@@ -92,7 +92,7 @@ public async Task<IActionResult> CreateRoleAsync([FromBody]PropertyValue[] prope
9292
var result = await service.CreateRoleAsync(properties);
9393
if (result.IsSuccess)
9494
{
95-
var url = Url.Link(Constants.RouteNames.GetRole, new { subject = result.Result.Subject });
95+
var url = Url.Link(IdentityManagerConstants.RouteNames.GetRole, new { subject = result.Result.Subject });
9696

9797
var resource = new
9898
{
@@ -108,7 +108,7 @@ public async Task<IActionResult> CreateRoleAsync([FromBody]PropertyValue[] prope
108108
return BadRequest(ModelState.ToError());
109109
}
110110

111-
[HttpGet("{subject}", Name = Constants.RouteNames.GetRole)]
111+
[HttpGet("{subject}", Name = IdentityManagerConstants.RouteNames.GetRole)]
112112
public async Task<IActionResult> GetRoleAsync(string subject)
113113
{
114114
if (IsNullOrWhiteSpace(subject))
@@ -145,7 +145,7 @@ public async Task<IActionResult> GetRoleAsync(string subject)
145145
return BadRequest(result.ToError());
146146
}
147147

148-
[HttpDelete, Route("{subject}", Name = Constants.RouteNames.DeleteRole)]
148+
[HttpDelete, Route("{subject}", Name = IdentityManagerConstants.RouteNames.DeleteRole)]
149149
public async Task<IActionResult> DeleteRoleAsync(string subject)
150150
{
151151
var meta = await GetMetadataAsync();
@@ -168,7 +168,7 @@ public async Task<IActionResult> DeleteRoleAsync(string subject)
168168
return BadRequest(result.ToError());
169169
}
170170

171-
[HttpPut, Route("{subject}/properties/{type}", Name = Constants.RouteNames.UpdateRoleProperty)]
171+
[HttpPut, Route("{subject}/properties/{type}", Name = IdentityManagerConstants.RouteNames.UpdateRoleProperty)]
172172
public async Task<IActionResult> SetPropertyAsync(string subject, string type)
173173
{
174174
if (IsNullOrWhiteSpace(subject))

0 commit comments

Comments
 (0)