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

Добавление ролей пользователя без конечных точек #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

namaksimow
Copy link
Contributor

No description provided.

@namaksimow namaksimow linked an issue Oct 24, 2024 that may be closed by this pull request
backend/WebApi/Domain/Role.cs Outdated Show resolved Hide resolved
b.Property<string>("Username")
.IsRequired()
.HasMaxLength(255)
.HasMaxLength(300)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Это зря добавилось

@@ -6,7 +6,7 @@ public class User
{
public int Id { get; init; }

[MaxLength(255)]
[MaxLength(300)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Тоже зря закоммитил это

Copy link
Collaborator

Choose a reason for hiding this comment

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

Нужно вернуть на 255

backend/WebApi/Domain/User.cs Outdated Show resolved Hide resolved
backend/WebApi/Program.cs Outdated Show resolved Hide resolved
backend/WebApi/RegisterRequest.cs Outdated Show resolved Hide resolved
backend/WebApi/Program.cs Outdated Show resolved Hide resolved
@@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Лишний юзинг

backend/WebApi/Program.cs Outdated Show resolved Hide resolved
@@ -126,6 +130,7 @@
Username = registration.Username,
UsernameNormalized = usernameNormalized,
PasswordHash = registration.Password,
Role = (Role)Enum.Parse(typeof(Role), registration.Role.ToString()),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Переводить в строку, чтоб потом из строки обратно?)
Можно же просто Role = registration.Role,

Comment on lines +86 to +87
builder.Services.AddAuthorizationBuilder().AddPolicy("admin_greetings", policy =>
policy.RequireRole(Role.Admin.ToString()));
Copy link
Collaborator

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",
Copy link
Collaborator

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");
Copy link
Collaborator

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());

Copy link
Collaborator

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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Вместо EnumRole

@@ -6,7 +6,7 @@ public class User
{
public int Id { get; init; }

[MaxLength(255)]
[MaxLength(300)]
Copy link
Collaborator

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();

Copy link
Collaborator

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()),

@AleksanderNekr AleksanderNekr added the wontfix This will not be worked on label Oct 26, 2024
@AleksanderNekr AleksanderNekr added cifra-feature Хакатон "Цифра" and removed wontfix This will not be worked on labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cifra-feature Хакатон "Цифра"
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Роли пользователей
2 participants