Skip to content
Open
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
6 changes: 4 additions & 2 deletions Identity/Identity.Core/UsersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ public async Task MakeAdminAsync(string email, string password)
await _repository.ChangeUserRoleToAdminAsync(email);
}

public async Task<Roles> GetUserRole(string email)
public async Task<IEnumerable<string>> GetUserRolesAsync(string email)
{
var role = await _repository.GetUserRole(email);
return role;

return new List<string> { role.ToString() };
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Identity.Data.Migrations
{
/// <inheritdoc />
public partial class AddedRolesForUsers : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsAdmin",
table: "Users");

migrationBuilder.AddColumn<int>(
name: "Role",
table: "Users",
type: "integer",
nullable: false,
defaultValue: 0);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Role",
table: "Users");

migrationBuilder.AddColumn<bool>(
name: "IsAdmin",
table: "Users",
type: "boolean",
nullable: false,
defaultValue: false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.IsRequired()
.HasColumnType("text");

b.Property<bool>("IsAdmin")
.HasColumnType("boolean");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");

b.Property<int>("Role")
.HasColumnType("integer");

b.HasKey("Id");

b.ToTable("Users");
Expand Down
3 changes: 3 additions & 0 deletions Identity/Identity/EndpointHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Identity;
Expand Down Expand Up @@ -35,6 +36,8 @@ public static async Task<object> AddUser([FromServices] UsersService service, Us
return new {Message = "User added successfully."};
}


[Authorize(Roles = "Admin")]
public static async Task<object> MakeAdmin([FromServices] UsersService service, string email, string password)
{
if (string.IsNullOrEmpty(email))
Expand Down
2 changes: 2 additions & 0 deletions Identity/Identity/Identity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="7.0.13" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.*-*" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
<ProjectReference Include="..\..\AcademicHub.Common\CustomExceptions\CustomExceptions.csproj" />
<ProjectReference Include="..\Identity.Core\Identity.Core.csproj" />
</ItemGroup>
Expand Down
30 changes: 28 additions & 2 deletions Identity/Identity/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Security.Claims;
using CustomExceptions;
using Identity;
using Microsoft.AspNetCore.Authentication.Google;
using Identity.Core;
using Identity.Data;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -49,11 +51,35 @@
})
.AddGoogle(googleOptions =>
{
googleOptions.ClientId = builder.Configuration["Auth:Google:ClientID"]!;
googleOptions.ClientSecret = builder.Configuration["Auth:Google:ClientSecret"]!;
googleOptions.ClientId = builder.Configuration["Auth:Google:ClientID"];
googleOptions.ClientSecret = builder.Configuration["Auth:Google:ClientSecret"];
googleOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
googleOptions.Events = new OAuthEvents
{
OnCreatingTicket = async context =>
{
// Extract the email from the Principal
var emailClaim = context.Principal.FindFirst(ClaimTypes.Email);
if (emailClaim == null)
{
throw new Exception("Email claim not found");
}
var email = emailClaim.Value;

// Rest of your code
var userService = context.HttpContext.RequestServices.GetRequiredService<UsersService>();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here will be runtime error as at this place not in the DB. Check other callbacks maybe there postAuth or something like that appears

var roles = await userService.GetUserRolesAsync(email);

var claimsIdentity = context.Principal.Identity as ClaimsIdentity;
foreach (var role in roles)
{
claimsIdentity?.AddClaim(new Claim(ClaimTypes.Role, role));
}
}
};
});


// Register authorization services
builder.Services.AddAuthorization();

Expand Down
2 changes: 2 additions & 0 deletions Identity/Identity/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
"ConnectionStrings": {
"DefaultConnection": "Host=postgres; Database=Identity; User Id=identityuser; Password=identityuser; Port=5432"
}


}