Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HamedFathi/9dbe220464e2e943db09baee1fb055e2 to your computer and use it in GitHub Desktop.
Save HamedFathi/9dbe220464e2e943db09baee1fb055e2 to your computer and use it in GitHub Desktop.
Domain Entities for Dynamic Permissions Management
public abstract class Entity
{
public long Id { get; set; }
[SuppressMessage("ReSharper", "BaseObjectGetHashCodeCallInGetHashCode")]
[SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")]
public override int GetHashCode()
{
if (IsTransient())
return base.GetHashCode();
unchecked
{
var hash = GetType().GetHashCode(); //todo: this.GetRealType().GetHashCode();
return (hash * 31) ^ Id.GetHashCode();
}
}
public bool IsTransient()
{
return Id == 0;
}
public override bool Equals(object obj)
{
if (!(obj is Entity other)) return false;
if (ReferenceEquals(this, other)) return true;
var typeOfThis = GetType(); //todo: this.GetRealType();
var typeOfOther = obj.GetType(); //todo: other.GetRealType();
if (typeOfThis != typeOfOther) return false;
if (IsTransient() || other.IsTransient()) return false;
return Id.Equals(other.Id);
}
public override string ToString()
{
return $"[{GetType().Name/*todo: this.GetRealType().Name*/} : {Id}]";
}
public static bool operator ==(Entity left, Entity right)
{
return Equals(left, right);
}
public static bool operator !=(Entity left, Entity right)
{
return !(left == right);
}
}
/// <summary>
/// for TPH inheritance style
/// </summary>
public abstract class PermissionSetting : Entity
{
/// <summary>
/// Unique Name of the Permission
/// </summary>
public string Name { get; set; }
/// <summary>
/// Indicate This Permission Is Granted With Role/User or Not
/// </summary>
public bool IsGranted { get; set; }
}
public class RolePermissionSetting : PermissionSetting
{
public Role Role { get; set; }
public long? RoleId { get; set; }
}
public class Role : Entity
{
public string Name { get; set; }
public bool IsDefault { get; set; }
public bool IsSystemEntry { get; set; }
public byte[] RowVersion { get; set; }
public ICollection<UserRole> Users { get; set; } = new HashSet<UserRole>();
public ICollection<RolePermissionSetting> Permissions { get; set; } = new HashSet<RolePermissionSetting>();
}
public class UserPermissionSetting : PermissionSetting
{
public User User { get; set; }
public long? UserId { get; set; }
}
public class User : Entity
{
public string FriendlyName { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public byte[] PhotoFile { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public DateTimeOffset? LockoutEndDateTime { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
public bool IsActive { get; set; } = true;
public bool IsSystemEntry { get; set; }
public byte[] RowVersion { get; set; }
public ICollection<UserRole> Roles { get; set; } = new HashSet<UserRole>();
public ICollection<UserPermissionSetting> Permissions { get; set; } = new HashSet<UserPermissionSetting>();
}
public class UserRole : Entity
{
public Role Role { get; set; }
public long RoleId { get; set; }
public User User { get; set; }
public long UserId { get; set; }
}
public static class PermissionNames
{
public const string Pages = nameof(Pages);
public const string Pages_Administration = nameof(Pages_Administration);
public const string Pages_Administration_Roles = nameof(Pages_Administration_Roles);
public const string Pages_Administration_Roles_Create = nameof(Pages_Administration_Roles_Create);
public const string Pages_Administration_Roles_Edit = nameof(Pages_Administration_Roles_Create);
public const string Pages_Administration_Roles_Delete = nameof(Pages_Administration_Roles_Create);
public const string Pages_Administration_Users = nameof(Pages_Administration_Users);
public const string Pages_Administration_Users_Create = nameof(Pages_Administration_Users_Create);
public const string Pages_Administration_Users_Edit = nameof(Pages_Administration_Users_Create);
public const string Pages_Administration_Users_Delete = nameof(Pages_Administration_Users_Create);
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment