C
C#2mo ago
Yarden

✅ How should I organize user roles (User, Manager, Admin) in a single User entity with ASP.NET Core?

Hello I want to create a website where a user can search for coffee stores. Now, I've created a controller where a User can register, but this gives him automatically "user role". Now, I want also to create manager and admin roles, but I don't think for example admin needs so many attributes when registers, right? Or maybe he needs even more? How am I suppose to logically think about it? This is the User entity:
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using FindMyCoffee.Domain.Enums;
using System.ComponentModel.DataAnnotations.Schema;

namespace FindMyCoffee.Models
{
[Index(nameof(UserName), IsUnique = true)]
[Index(nameof(Email), IsUnique = true)]
public class UserEntity
{

[Timestamp]
public byte[] RowVersion { get; set; } //I don't understand this fully yet.. I'll leave it this way for now
public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
public DateTime ModifiedAtUtc { get; set; } = DateTime.UtcNow;

[Key] public int Id { get; set; }

public string UserName { get; set; } = default!;

[Required, MaxLength(254), EmailAddress]
public string Email { get; set; } = default!;

public DateOnly? DOB { get; set; } = default!;

public Role Role { get; set; } = Role.User;

public string FirstName { get; set; } = default!;

public string LastName { get; set; } = default!;

public Gender Gender { get; set; } = default!;

[Required, MaxLength(100)]
public string PasswordHash { get; set; } = default!;

}
}
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using FindMyCoffee.Domain.Enums;
using System.ComponentModel.DataAnnotations.Schema;

namespace FindMyCoffee.Models
{
[Index(nameof(UserName), IsUnique = true)]
[Index(nameof(Email), IsUnique = true)]
public class UserEntity
{

[Timestamp]
public byte[] RowVersion { get; set; } //I don't understand this fully yet.. I'll leave it this way for now
public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
public DateTime ModifiedAtUtc { get; set; } = DateTime.UtcNow;

[Key] public int Id { get; set; }

public string UserName { get; set; } = default!;

[Required, MaxLength(254), EmailAddress]
public string Email { get; set; } = default!;

public DateOnly? DOB { get; set; } = default!;

public Role Role { get; set; } = Role.User;

public string FirstName { get; set; } = default!;

public string LastName { get; set; } = default!;

public Gender Gender { get; set; } = default!;

[Required, MaxLength(100)]
public string PasswordHash { get; set; } = default!;

}
}
cs Every role suppose to have the same attributes? I also have Role enum where you can choose the Role as you can see, it's in a different class made just for the enum. I hope my explanation was well explained, thanks!
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?