using EnumsNET;
using System.Collections.Generic;
using AyaNova.Biz;
namespace AyaNova.Api.ControllerHelpers
{
//AUTHORIZATION ROLES: NOTE - this is only 'stage1' of generally checking rights, individual objects can also have business rules that affect access exactly as these roles do
//Most objects won't need more than this but some specialized ones will have further checks depending on biz rules
internal static class Authorized
{
///
/// User has any role limited or full
///
///
///
///
internal static bool HasAnyRole(IDictionary HttpContextItems, AuthorizationRoles CheckRoles)
{
AuthorizationRoles currentUserRoles = UserRolesFromContext.Roles(HttpContextItems);
return HasAnyRole(currentUserRoles, CheckRoles);
}
///
/// User has any role limited or full
///
///
///
///
internal static bool HasAnyRole(AuthorizationRoles currentUserRoles, AuthorizationRoles CheckRoles)
{
if (currentUserRoles.HasAnyFlags(CheckRoles))
return true;
return false;
}
///
/// any access at all?
///
///
///
///
internal static bool HasAnyRole(IDictionary HttpContextItems, AyaType objectType)
{
AuthorizationRoles currentUserRoles = UserRolesFromContext.Roles(HttpContextItems);
return HasAnyRole(currentUserRoles, objectType);
}
///
/// User has any access at all to this object?
///
///
///
///
internal static bool HasAnyRole(AuthorizationRoles currentUserRoles, AyaType objectType)
{
var RoleSet = BizRoles.GetRoleSet(objectType);
var AllowedRoles = RoleSet.ReadFullRecord | RoleSet.Change | RoleSet.Select;
return currentUserRoles.HasAnyFlags(AllowedRoles);
}
///
/// READ FULL RECORD (not just name and id)
///
///
///
///
internal static bool HasSelectRole(IDictionary HttpContextItems, AyaType objectType)
{
AuthorizationRoles currentUserRoles = UserRolesFromContext.Roles(HttpContextItems);
return HasSelectRole(currentUserRoles, objectType);
}
///
/// SELECT BY NAME
///
///
///
///
internal static bool HasSelectRole(AuthorizationRoles currentUserRoles, AyaType objectType)
{
//NOTE: this assumes that if you can change you can read
if (currentUserRoles.HasAnyFlags(BizRoles.GetRoleSet(objectType).Change))
return true;
if (currentUserRoles.HasAnyFlags(BizRoles.GetRoleSet(objectType).ReadFullRecord))
return true;
if (currentUserRoles.HasAnyFlags(BizRoles.GetRoleSet(objectType).Select))
return true;
return false;
}
///
/// READ FULL RECORD (not just name and id)
///
///
///
///
internal static bool HasReadFullRole(IDictionary HttpContextItems, AyaType objectType)
{
AuthorizationRoles currentUserRoles = UserRolesFromContext.Roles(HttpContextItems);
return HasReadFullRole(currentUserRoles, objectType);
}
///
/// READ FULL RECORD (not just name and id)
///
///
///
///
internal static bool HasReadFullRole(AuthorizationRoles currentUserRoles, AyaType objectType)
{
//NOTE: this assumes that if you can change you can read
var RoleSet = BizRoles.GetRoleSet(objectType);
var AllowedRoles = RoleSet.ReadFullRecord | RoleSet.Change;
return currentUserRoles.HasAnyFlags(AllowedRoles);
// if (currentUserRoles.HasAnyFlags(BizRoles.GetRoleSet(objectType).Change))
// return true;
// if (currentUserRoles.HasAnyFlags(BizRoles.GetRoleSet(objectType).ReadFullRecord))
// return true;
//return false;
}
///
/// CREATE
///
///
///
///
internal static bool HasCreateRole(IDictionary HttpContextItems, AyaType objectType)
{
AuthorizationRoles currentUserRoles = UserRolesFromContext.Roles(HttpContextItems);
return HasCreateRole(currentUserRoles, objectType);
}
///
/// CREATE
///
///
///
///
internal static bool HasCreateRole(AuthorizationRoles currentUserRoles, AyaType objectType)
{
if (currentUserRoles.HasAnyFlags(BizRoles.GetRoleSet(objectType).Change))
return true;
return false;
}
///
/// MODIFY
///
///
///
///
internal static bool HasModifyRole(IDictionary HttpContextItems, AyaType objectType)
{
AuthorizationRoles currentUserRoles = UserRolesFromContext.Roles(HttpContextItems);
return HasModifyRole(currentUserRoles, objectType);
}
///
/// MODIFY
///
///
///
///
internal static bool HasModifyRole(AuthorizationRoles currentUserRoles, AyaType objectType)
{
if (currentUserRoles.HasAnyFlags(BizRoles.GetRoleSet(objectType).Change))
return true;
return false;
}
///
/// DELETE
///
///
///
///
//For now just going to treat as a modify, but for maximum flexibility keeping this as a separate method in case we change our minds in future
internal static bool HasDeleteRole(IDictionary HttpContextItems, AyaType objectType)
{
AuthorizationRoles currentUserRoles = UserRolesFromContext.Roles(HttpContextItems);
long currentUserId = UserIdFromContext.Id(HttpContextItems);
return HasDeleteRole(currentUserRoles, objectType);
}
///
/// DELETE
///
///
///
///
//For now just going to treat as a modify, but for maximum flexibility keeping this as a separate method in case we change our minds in future
internal static bool HasDeleteRole(AuthorizationRoles currentUserRoles, AyaType objectType)
{
if (currentUserRoles.HasAnyFlags(BizRoles.GetRoleSet(objectType).Change))
return true;
return false;
}
}
}//eons