-
Notifications
You must be signed in to change notification settings - Fork 0
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
Добавление ролей пользователя без конечных точек #3
base: main
Are you sure you want to change the base?
Conversation
b.Property<string>("Username") | ||
.IsRequired() | ||
.HasMaxLength(255) | ||
.HasMaxLength(300) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это зря добавилось
backend/WebApi/Database/Migrations/20241023183751_Add property Role to User.cs
Outdated
Show resolved
Hide resolved
@@ -6,7 +6,7 @@ public class User | |||
{ | |||
public int Id { get; init; } | |||
|
|||
[MaxLength(255)] | |||
[MaxLength(300)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тоже зря закоммитил это
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нужно вернуть на 255
@@ -9,6 +9,7 @@ | |||
using Microsoft.AspNetCore.Identity; | |||
using Microsoft.AspNetCore.Mvc; | |||
using Microsoft.EntityFrameworkCore; | |||
using Microsoft.EntityFrameworkCore.Metadata; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лишний юзинг
f16efce
to
fa07d41
Compare
@@ -126,6 +130,7 @@ | |||
Username = registration.Username, | |||
UsernameNormalized = usernameNormalized, | |||
PasswordHash = registration.Password, | |||
Role = (Role)Enum.Parse(typeof(Role), registration.Role.ToString()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Переводить в строку, чтоб потом из строки обратно?)
Можно же просто Role = registration.Role,
builder.Services.AddAuthorizationBuilder().AddPolicy("admin_greetings", policy => | ||
policy.RequireRole(Role.Admin.ToString())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Так лучше
builder.Services.AddAuthorizationBuilder()
.AddPolicy(Role.Admin.ToString(), policy => policy.RequireRole(Role.Admin.ToString()));
}); | ||
}) | ||
.RequireAuthorization(); | ||
|
||
app.MapGet("/ping", () => "pong") | ||
.WithTags("Health"); | ||
|
||
app.MapGet( | ||
"/role", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
roles – мн. число
Items = Enum.GetValues<Role>(), | ||
})); | ||
|
||
app.MapGet("/admin", () => "admin is you").RequireAuthorization("admin_greetings"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
app.MapGet("/admin", () => "admin is you")
.RequireAuthorization(Role.Admin.ToString());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Рекомендация: если методы с длинными названиями, то лучше переносить их вызовы на новую строку
public sealed record RegisterRequest([property: EmailAddress] string Email, string Username, string Password, Enum Role); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вместо Enum
→ Role
@@ -6,7 +6,7 @@ public class User | |||
{ | |||
public int Id { get; init; } | |||
|
|||
[MaxLength(255)] | |||
[MaxLength(300)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нужно вернуть на 255
@@ -82,6 +83,9 @@ | |||
}); | |||
builder.Services.AddAuthorization(); | |||
|
|||
builder.Services.AddAuthorizationBuilder().AddPolicy("admin_greetings", policy => | |||
policy.RequireRole(Role.Admin.ToString())); | |||
|
|||
var app = builder.Build(); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В эндпоинтах входа и регистрации в клеймы добавь
new(ClaimTypes.Role, user.Role.ToString()),
No description provided.