128 lines
4.1 KiB
C#
128 lines
4.1 KiB
C#
using MCVIngenieros.Transactional.Abstractions.Interfaces;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
namespace back.DataModels;
|
|
|
|
public class RoleDto
|
|
{
|
|
public string Id { get; set; } = null!;
|
|
public List<PermissionDto> Permissions { get; set; } = [];
|
|
}
|
|
|
|
[Table("Roles")]
|
|
public partial class Role : IEntity<Role>
|
|
{
|
|
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
|
public string Id { get; set; } = null!;
|
|
[Required, MaxLength(100)]
|
|
public string Name { get; set; } = null!;
|
|
[MaxLength(250)]
|
|
public string? Description { get; set; }
|
|
public string? BaseRoleModelId { get; set; }
|
|
public virtual Role? BaseRoleModel { get; set; }
|
|
public virtual ICollection<Role> InverseBaseRoleModel { get; set; } = [];
|
|
public virtual ICollection<Permission> Permissions { get; set; } = new HashSet<Permission>();
|
|
public virtual ICollection<User> Users { get; set; } = [];
|
|
|
|
public bool IsAdmin() => BaseRoleModel != null ? BaseRoleModel.IsAdmin() : Id == AdminRole.Id;
|
|
public bool IsContentManager() => BaseRoleModel != null ? BaseRoleModel.IsContentManager() : Id == ContentManagerRole.Id;
|
|
public bool IsUser() => BaseRoleModel != null ? BaseRoleModel.IsUser() : Id == UserRole.Id;
|
|
|
|
public bool HasPermission(Permission permission)
|
|
{
|
|
var baseRoleHasPermission = BaseRoleModel != null && BaseRoleModel.HasPermission(permission);
|
|
return baseRoleHasPermission || Permissions.Any(p => p.Id == permission.Id);
|
|
}
|
|
|
|
public Role() { }
|
|
|
|
public Role(string id, string name, string description, List<Permission>? permissions = null, Role? baseRoleModel = null)
|
|
{
|
|
Id = id;
|
|
Name = name;
|
|
Description = description;
|
|
Permissions = permissions ?? [];
|
|
if (baseRoleModel != null)
|
|
{
|
|
BaseRoleModel = baseRoleModel;
|
|
BaseRoleModelId = baseRoleModel.Id;
|
|
foreach (var permission in baseRoleModel.Permissions)
|
|
{
|
|
if (!Permissions.Any(p => p.Id == permission.Id))
|
|
{
|
|
Permissions.Add(permission);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public override int GetHashCode() => HashCode.Combine(Id, Name);
|
|
|
|
public override bool Equals(object? obj) => obj is Role otherEvent && Equals(otherEvent);
|
|
|
|
public bool Equals(Role? other)
|
|
{
|
|
if (other is null) return false;
|
|
if (ReferenceEquals(this, other)) return true;
|
|
return Id == other.Id || GetHashCode() == other.GetHashCode();
|
|
}
|
|
|
|
public bool IsNull => this is null;
|
|
|
|
public object Clone() => (Role)MemberwiseClone();
|
|
|
|
public int CompareTo(object? obj)
|
|
{
|
|
if (obj is null) return 1;
|
|
if (obj is not Role other) throw new ArgumentException("Object is not a Person");
|
|
return CompareTo(other);
|
|
}
|
|
|
|
public int CompareTo(Role? other)
|
|
{
|
|
if (other is null) return 1;
|
|
if (ReferenceEquals(this, other)) return 0;
|
|
return string.Compare(Id, other.Id, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
public RoleDto ToDto()
|
|
{
|
|
return new RoleDto
|
|
{
|
|
Id = Id,
|
|
Permissions = [.. Permissions.Select(p => p.ToDto())]
|
|
};
|
|
}
|
|
|
|
public static readonly Role UserRole = new(
|
|
"1", "User", "Role for regular users",
|
|
[
|
|
Permission.ViewContentPermission,
|
|
Permission.LikeContentPermission
|
|
]
|
|
);
|
|
|
|
public static readonly Role ContentManagerRole = new(
|
|
"2", "Content Manager", "Role for managing content",
|
|
[
|
|
Permission.CreateContentPermission,
|
|
Permission.EditContentPermission,
|
|
Permission.DeleteContentPermission
|
|
],
|
|
UserRole
|
|
);
|
|
|
|
public static readonly Role AdminRole = new(
|
|
"3", "Admin", "Administrator role with full permissions",
|
|
[
|
|
Permission.CreateUserPermission,
|
|
Permission.DisableUserPermission,
|
|
Permission.EditUserPermission,
|
|
Permission.DeleteUserPermission,
|
|
Permission.EditWebConfigPermission
|
|
],
|
|
ContentManagerRole
|
|
);
|
|
}
|