Skip to content
This repository was archived by the owner on Nov 23, 2024. It is now read-only.

Commit c553a87

Browse files
committed
Add private membership verification API
1 parent 0613fb5 commit c553a87

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

src/Web/Web/Controllers/AdminController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ orderby r.Id ascending
9090
sb.AppendLine();
9191
}
9292

93-
Response.Headers.TryAdd(HeaderNames.ContentDisposition, new StringValues("attachment; filename=registrations.csv"));
93+
Response.Headers.TryAdd(HeaderNames.ContentDisposition, new StringValues("attachment; filename=members.csv"));
9494

9595
return Content(sb.ToString(), "text/csv");
9696
}
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using CodeMooc.Web.Data;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Primitives;
7+
using Microsoft.Net.Http.Headers;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
14+
namespace CodeMooc.Web.Controllers {
15+
16+
[Route("api")]
17+
[Authorize(Policy = Startup.LegacyBasicAdministratorsPolicyName)]
18+
public class ApiController : Controller {
19+
20+
protected DataContext Database { get; }
21+
protected ILogger<ApiController> Logger { get; }
22+
23+
public ApiController(
24+
DataContext database,
25+
ILogger<ApiController> logger
26+
) {
27+
Database = database;
28+
Logger = logger;
29+
}
30+
31+
[HttpPost("members/verify")]
32+
public IActionResult VerifyMembership([FromQuery] string email) {
33+
Logger.LogInformation(LoggingEvents.Api, "Member verification for mail {0}", email);
34+
35+
var entry = (from e in Database.Emails
36+
where e.Address == email.ToLowerInvariant().Trim()
37+
select e)
38+
.SingleOrDefault();
39+
if(entry == null) {
40+
return NotFound();
41+
}
42+
43+
var user = (from u in Database.Registrations
44+
where u.Id == entry.RegistrationId
45+
select u)
46+
.Include(u => u.Emails)
47+
.First();
48+
var emails = (from e in user.Emails
49+
orderby e.IsPrimary descending
50+
select e.Address).ToList();
51+
52+
var badges = (from b in Database.Badges
53+
where b.Type == BadgeType.Member
54+
where emails.Contains(b.Email)
55+
orderby b.Year ascending
56+
select b);
57+
58+
var memberships = badges.ToDictionary(
59+
b => b.Year.Year,
60+
b => new {
61+
IssuedOn = b.IssueTimestamp
62+
}
63+
);
64+
return Json(new {
65+
user.Id,
66+
PrimaryMail = emails[0],
67+
RegisteredOn = user.RegistrationTimestamp,
68+
IsMember = memberships.ContainsKey(DateTime.UtcNow.Year),
69+
Memberships = memberships
70+
});
71+
}
72+
73+
}
74+
75+
}

src/Web/Web/LoggingEvents.cs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public static class LoggingEvents {
1414

1515
public const int Badges = 4000;
1616

17+
public const int Api = 5000;
18+
1719
}
1820

1921
}

src/Web/Web/appsettings.Development.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"Default": "Trace",
77
"System": "Information",
88
"Microsoft": "Information",
9+
"Microsoft.AspNetCore": "Debug",
910
"Microsoft.EntityFrameworkCore.Database": "Information",
1011
"Microsoft.AspNetCore.StaticFiles": "Warning",
1112
"Microsoft.AspNetCore.Hosting": "Warning",

src/Web/Web/appsettings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
},
1717
"StaticUserPasswords": {
1818
"a.bogliolo": "$2y$12$jU9.9jJzc58TLq5Q1vYSTOuYR.4oLUL/vy006rCU69xP.wPeqqzN2",
19-
"codemooc.badges": "$2y$12$SV/SAqEpjehlwZfDO8yP9.GsUdlfM9VSzOi/SoflS8Jq.somCUII2"
19+
"codemooc.badges": "$2y$12$SV/SAqEpjehlwZfDO8yP9.GsUdlfM9VSzOi/SoflS8Jq.somCUII2",
20+
"api": "$2y$12$Uz3fRK7ihuJ8hr/cqwQgaOdb9oCmlSvGSC/BDHGs20ID2SV9EoDLO"
2021
},
2122
"Paths": {
2223
"Curricula": "/data/curricula",

0 commit comments

Comments
 (0)