Skip to content

Commit 42987e3

Browse files
Consumer Api Poc: Public Relationship Template References (#941)
* feat: Add method to IUserContext to get the client id * feat: Add method to IUserContext to get client id * feat: Add endpoint, controller and bruno files for querying the public relationship template references * chore: Add example references to appsettings override * chore: Make public relationship template references optional in appsettings --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 2d129ee commit 42987e3

File tree

10 files changed

+131
-0
lines changed

10 files changed

+131
-0
lines changed

Applications/AdminApi/src/AdminApi/Authentication/AnonymousUserContext.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ public string GetUsername()
4444
{
4545
throw new NotSupportedException();
4646
}
47+
48+
public string GetClientId()
49+
{
50+
throw new NotSupportedException();
51+
}
52+
53+
public string? GetClientIdOrNull()
54+
{
55+
return null;
56+
}
4757
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
meta {
2+
name: List Public Relationship Template References
3+
type: http
4+
seq: 1
5+
}
6+
7+
get {
8+
url: {{pocUrl}}/PublicRelationshipTemplateReferences
9+
body: none
10+
auth: inherit
11+
}

Applications/ConsumerApi/src/http/environments/Local.bru

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
vars {
22
baseUrl: http://localhost:8081/api/v1
3+
pocUrl: http://localhost:8081/api/poc
34
auth.url: http://localhost:8081/connect/token
45
auth.username: USRa
56
auth.password: Aaaaaaaa1!

BuildingBlocks/src/BuildingBlocks.Application.Abstractions/Infrastructure/UserContext/IUserContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ public interface IUserContext
1515

1616
string GetUsername();
1717
string? GetUsernameOrNull();
18+
19+
string GetClientId();
20+
string? GetClientIdOrNull();
1821
}

Infrastructure/UserContext/AspNetCoreUserContext.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class AspNetCoreUserContext : IUserContext
1212
private const string DEVICE_ID_CLAIM = "device_id";
1313
private const string USER_ID_CLAIM = "sub";
1414
private const string SUBSCRIPTION_PLAN_CLAIM = "subscription_plan";
15+
private const string CLIENT_ID_CLAIM = "client_id";
1516
private readonly IHttpContextAccessor _context;
1617

1718
public AspNetCoreUserContext(IHttpContextAccessor context)
@@ -66,6 +67,17 @@ public string GetUsername()
6667
return username;
6768
}
6869

70+
public string GetClientId()
71+
{
72+
return GetClientIdOrNull() ?? throw new NotFoundException();
73+
}
74+
75+
public string? GetClientIdOrNull()
76+
{
77+
var clientId = GetHttpContext().User.FindFirstValue(CLIENT_ID_CLAIM);
78+
return clientId;
79+
}
80+
6981
private HttpContext GetHttpContext()
7082
{
7183
return _context.HttpContext ?? throw new Exception("HttpContext is null");

Modules/Challenges/src/Challenges.Jobs.Cleanup/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,14 @@ public string GetUsernameOrNull()
8282
{
8383
throw new NotSupportedException();
8484
}
85+
86+
public string GetClientId()
87+
{
88+
throw new NotSupportedException();
89+
}
90+
91+
public string? GetClientIdOrNull()
92+
{
93+
throw new NotSupportedException();
94+
}
8595
}

Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/UserContextStub.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ public string GetUsernameOrNull()
4444
{
4545
throw new NotSupportedException();
4646
}
47+
48+
public string GetClientId()
49+
{
50+
throw new NotSupportedException();
51+
}
52+
53+
public string? GetClientIdOrNull()
54+
{
55+
throw new NotSupportedException();
56+
}
4757
}

Modules/Relationships/src/Relationships.ConsumerApi/Configuration.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class Configuration
1111
[Required]
1212
public InfrastructureConfiguration Infrastructure { get; set; } = new();
1313

14+
public Dictionary<string, IEnumerable<PublicRelationshipTemplateReferenceDefinition>> PublicRelationshipTemplateReferences { get; set; } = [];
15+
1416
public class InfrastructureConfiguration
1517
{
1618
[Required]
@@ -31,4 +33,16 @@ public class SqlDatabaseConfiguration
3133
public bool EnableHealthCheck { get; set; } = true;
3234
}
3335
}
36+
37+
public class PublicRelationshipTemplateReferenceDefinition
38+
{
39+
[Required]
40+
public string Title { get; set; } = string.Empty;
41+
42+
[Required]
43+
public string Description { get; set; } = string.Empty;
44+
45+
[Required]
46+
public string TruncatedReference { get; set; } = string.Empty;
47+
}
3448
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Backbone.BuildingBlocks.API.Mvc;
2+
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.UserContext;
3+
using MediatR;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Options;
7+
8+
namespace Backbone.Modules.Relationships.ConsumerApi.Controllers;
9+
10+
[Route("api/poc/[controller]")]
11+
[Authorize("OpenIddict.Validation.AspNetCore")]
12+
public class PublicRelationshipTemplateReferencesController : ApiControllerBase
13+
{
14+
private readonly Configuration _options;
15+
16+
public PublicRelationshipTemplateReferencesController(IMediator mediator, IOptions<Configuration> options) : base(mediator)
17+
{
18+
_options = options.Value;
19+
}
20+
21+
[HttpGet]
22+
public IActionResult ListPublicRelationshipTemplateReferences(IUserContext userContext)
23+
{
24+
var clientId = userContext.GetClientId();
25+
var response = _options.PublicRelationshipTemplateReferences.GetValueOrDefault(clientId) ?? [];
26+
27+
return Ok(response);
28+
}
29+
}

appsettings.override.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,37 @@
111111
"ConnectionString": "User ID=postgres;Password=admin;Server=localhost;Port=5432;Database=enmeshed;" // postgres
112112
// "ConnectionString": "Server=localhost;Database=enmeshed;User Id=sa;Password=Passw0rd;TrustServerCertificate=True" // sqlserver
113113
}
114+
},
115+
"PublicRelationshipTemplateReferences": {
116+
"test": [
117+
{
118+
"title": "a",
119+
"description": "Description",
120+
"truncatedReference": "Reference"
121+
},
122+
{
123+
"title": "b",
124+
"description": "Description",
125+
"truncatedReference": "Reference"
126+
},
127+
{
128+
"title": "c",
129+
"description": "Description",
130+
"truncatedReference": "Reference"
131+
}
132+
],
133+
"client2": [
134+
{
135+
"title": "d",
136+
"description": "Description",
137+
"truncatedReference": "Reference"
138+
},
139+
{
140+
"title": "e",
141+
"description": "Description",
142+
"truncatedReference": "Reference"
143+
}
144+
]
114145
}
115146
},
116147
"Synchronization": {

0 commit comments

Comments
 (0)