using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using System;
namespace Oqtane.Models
{
///
/// Permission information for anything in Oqtane.
/// Things in Oqtane are identified as Entities, so anything that can be identified can be described here.
///
public class Permission : ModelBase
{
///
/// Internal ID storing this information.
///
public int PermissionId { get; set; }
///
/// Reference to the which contains both the target Entity and permissions.
///
public int SiteId { get; set; }
///
/// Name of the Entity these permissions apply to (ie. Module )
///
public string EntityName { get; set; }
///
/// ID of the Entity these permissions apply to (ie. a ModuleId). A value of -1 indicates the permission applies to all EntityNames regardless of ID (ie. API permissions)
///
public int EntityId { get; set; }
///
/// Name of the permission (ie. View)
///
public string PermissionName { get; set; }
///
/// this permission applies to. If null then this is a permission.
///
public int? RoleId { get; set; }
///
/// The role name associated to the RoleId.
///
[NotMapped]
public string RoleName { get; set; }
///
/// this permission applies to. If null then this is a permission.
///
public int? UserId { get; set; }
///
/// The type of permission (ie. grant = true, deny = false)
///
public bool IsAuthorized { get; set; }
public Permission()
{
}
public Permission(string permissionName, string roleName, bool isAuthorized)
{
Initialize(-1, "", -1, permissionName, roleName, null, isAuthorized);
}
public Permission(string permissionName, int userId, bool isAuthorized)
{
Initialize(-1, "", -1, permissionName, "", userId, isAuthorized);
}
public Permission(int siteId, string entityName, string permissionName, string roleName, int? userId, bool isAuthorized)
{
Initialize(siteId, entityName, -1, permissionName, roleName, userId, isAuthorized);
}
public Permission(int siteId, string entityName, int entityId, string permissionName, string roleName, int? userId, bool isAuthorized)
{
Initialize(siteId, entityName, entityId, permissionName, roleName, userId, isAuthorized);
}
private void Initialize(int siteId, string entityName, int entityId, string permissionName, string roleName, int? userId, bool isAuthorized)
{
SiteId = siteId;
EntityName = entityName;
EntityId = entityId;
PermissionName = permissionName;
if (!string.IsNullOrEmpty(roleName))
{
RoleId = null;
RoleName = roleName;
UserId = null;
}
else
{
RoleId = null;
RoleName = null;
UserId = userId;
}
IsAuthorized = isAuthorized;
}
public Permission Clone()
{
return new Permission
{
SiteId = SiteId,
EntityName = EntityName,
EntityId = EntityId,
PermissionName = PermissionName,
RoleName = RoleName,
RoleId = RoleId,
UserId = UserId,
IsAuthorized = IsAuthorized,
CreatedBy = CreatedBy,
CreatedOn = CreatedOn,
ModifiedBy = ModifiedBy,
ModifiedOn = ModifiedOn
};
}
[Obsolete("The Role property is deprecated", false)]
[NotMapped]
[JsonIgnore] // exclude from API payload
public Role Role { get; set; }
}
}