Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed all references to the student #152

Merged
merged 4 commits into from
Jun 2, 2020
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
4 changes: 2 additions & 2 deletions API/Controllers/EmbedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public async Task<IActionResult> CreateEmbeddedProject(EmbeddedProjectResource e
return BadRequest(problem);
}

string identity = HttpContext.User.GetStudentId(HttpContext);
string identity = HttpContext.User.GetIdentityId(HttpContext);
bool isAllowed = userService.UserHasScope(identity, nameof(Defaults.Scopes.EmbedWrite));
User user = await userService.GetUserByIdentityIdAsync(identity);

Expand Down Expand Up @@ -209,7 +209,7 @@ public async Task<IActionResult> DeleteEmbeddedProject(string guid)
}


string identity = HttpContext.User.GetStudentId(HttpContext);
string identity = HttpContext.User.GetIdentityId(HttpContext);
bool isAllowed = userService.UserHasScope(identity, nameof(Defaults.Scopes.EmbedWrite));

if(!(embeddedProject.User.IdentityId == identity || isAllowed))
Expand Down
8 changes: 4 additions & 4 deletions API/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public UserController(IUserService userService, IMapper mapper)
[Authorize]
public async Task<IActionResult> GetCurrentUser()
{
string studentId = HttpContext.User.GetStudentId(HttpContext);
User user = await userService.GetUserByIdentityIdAsync(studentId);
string identityId = HttpContext.User.GetIdentityId(HttpContext);
User user = await userService.GetUserByIdentityIdAsync(identityId);
if(user == null)
{
ProblemDetails problem = new ProblemDetails
Expand Down Expand Up @@ -168,7 +168,7 @@ public async Task<IActionResult> UpdateAccount(int userId, [FromBody] UserResour
}

/// <summary>
/// Gets the student information.
/// Delete the user account.
/// </summary>
/// <returns></returns>
[HttpDelete("{userId}")]
Expand All @@ -180,7 +180,7 @@ public async Task<IActionResult> DeleteAccount(int userId)
ProblemDetails problem = new ProblemDetails
{
Title = "Failed getting the user account.",
Detail = "The database does not contain a user with this student id.",
Detail = "The database does not contain a user with this user id.",
Instance = "C4C62149-FF9A-4E4C-8C9F-6BBF518BA085"
};
return NotFound(problem);
Expand Down
6 changes: 3 additions & 3 deletions API/Extensions/ScopeRequirement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public ScopeRequirementHandler(IHttpContextAccessor httpContextAccessor, IUserSe
/// <returns></returns>
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, ScopeRequirement requirement)
{
string studentId = httpContextAccessor.HttpContext.User.GetStudentId(httpContextAccessor.HttpContext);
if(string.IsNullOrEmpty(studentId))
string identityId = httpContextAccessor.HttpContext.User.GetIdentityId(httpContextAccessor.HttpContext);
if(string.IsNullOrEmpty(identityId))
{
return Task.CompletedTask;
}

if(userService.UserHasScope(studentId, requirement.RequiredScope))
if(userService.UserHasScope(identityId, requirement.RequiredScope))
{
context.Succeed(requirement);
}
Expand Down
23 changes: 13 additions & 10 deletions API/Extensions/UsersExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ namespace API.Extensions
internal static class UsersExtensions
{
/// <summary>
/// Gets the student identifier asynchronous.
/// Gets the identity identifier.
/// </summary>
/// <param name="claimsPrincipal">The claims principal.</param>
/// <param name="actionContext">The action context.</param>
/// <returns></returns>
/// <exception cref="Exception">The back-end header isn't added!</exception>
/// <exception cref="Exception">
/// User is not authenticated!
/// or
/// The back-end header isn't added!
/// </exception>
/// <exception cref="NotSupportedException">The jwt doesn't have a sub</exception>
/// <exception cref="System.Exception">The back-end header isn't added!</exception>
public static string GetStudentId(this ClaimsPrincipal claimsPrincipal, HttpContext actionContext)
public static string GetIdentityId(this ClaimsPrincipal claimsPrincipal, HttpContext actionContext)
{
string studentId;
string identityId;

if(claimsPrincipal.Identities.Any(i => !i.IsAuthenticated))
{
Expand All @@ -57,16 +60,16 @@ public static string GetStudentId(this ClaimsPrincipal claimsPrincipal, HttpCont

if(claimsPrincipal.IsInRole(Defaults.Roles.BackendApplication))
{
string studentIdHeader = actionContext.Request.Headers.SingleOrDefault(h => h.Key == "StudentId")
string identityIdHeader = actionContext.Request.Headers.SingleOrDefault(h => h.Key == "IdentityId")
.Value
.FirstOrDefault();

if(string.IsNullOrWhiteSpace(studentIdHeader))
if(string.IsNullOrWhiteSpace(identityIdHeader))
{
throw new Exception("The back-end header isn't added!");
}

studentId = studentIdHeader;
identityId = identityIdHeader;
} else
{
string sub = claimsPrincipal.Claims.FirstOrDefault(c => c.Type.Equals("sub"))
Expand All @@ -79,7 +82,7 @@ public static string GetStudentId(this ClaimsPrincipal claimsPrincipal, HttpCont
return sub;
}

return studentId;
return identityId;
}

/// <summary>
Expand All @@ -90,7 +93,7 @@ public static string GetStudentId(this ClaimsPrincipal claimsPrincipal, HttpCont
/// <returns></returns>
public static async Task<User> GetContextUser(this HttpContext actionContext, IUserService userService)
{
string identityProverId = actionContext.User.GetStudentId(actionContext);
string identityProverId = actionContext.User.GetIdentityId(actionContext);
return await userService.GetUserByIdentityIdAsync(identityProverId);
}

Expand Down
8 changes: 4 additions & 4 deletions API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseAuthentication();
app.UseAuthorization();

//StudentInfo
//UserInfo
app.UseWhen(context =>
context.User.Identities.Any(i => i.IsAuthenticated), appBuilder =>
{
Expand All @@ -232,8 +232,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
DbContext dbContext = context.RequestServices.GetService<DbContext>();
IUserService userService =
context.RequestServices.GetService<IUserService>();
string studentId = context.User.GetStudentId(context);
if(await userService.GetUserByIdentityIdAsync(studentId).ConfigureAwait(false) == null)
string identityId = context.User.GetIdentityId(context);
if(await userService.GetUserByIdentityIdAsync(identityId).ConfigureAwait(false) == null)
{
User newUser = context.GetUserInformation(Config);
if(newUser == null)
Expand All @@ -243,7 +243,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
Name = "Developer",
Email = "Developer@DEX.com",
IdentityId = studentId
IdentityId = identityId
};
userService.Add(newUser);
} else
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Return Unauthorized instead of Bad Request when not allowed to perform action in controller - [#132](https://github.com/DigitalExcellence/dex-backend/issues/132)
- Changed the migrations and seeding of the data - [#134](https://github.com/DigitalExcellence/dex-backend/issues/134)
- Get user from the session & add current user to project. - [#139](https://github.com/DigitalExcellence/dex-backend/issues/139)
- Changed Student reference to be named identity. - [#145](https://github.com/DigitalExcellence/dex-backend/issues/145)

### Deprecated

Expand Down