2b9f74a1b2
* Added autocreate database if not exists
79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Security.Principal;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNet.SignalR;
|
|
using Microsoft.AspNet.SignalR.Hubs;
|
|
using Step.Config;
|
|
using Step.Database.Controllers;
|
|
using Step.Model;
|
|
using static Step.Utils.Constants;
|
|
using Step.Utils;
|
|
|
|
namespace Step.Attributes
|
|
{
|
|
class SignalRAuthorizeAttribute : AuthorizeAttribute
|
|
{
|
|
public string FunctionAccess;
|
|
public ACTIONS Action;
|
|
|
|
protected override bool UserAuthorized(IPrincipal user)
|
|
{
|
|
if (!base.UserAuthorized(user))
|
|
return false;
|
|
|
|
// Get user level stored in the bearer token
|
|
ClaimsIdentity identity = user.Identity as ClaimsIdentity;
|
|
var userRoleLevel = identity.Claims.Where(c => c.Type == ROLE_LEVEL_KEY).SingleOrDefault();
|
|
// User data not found -> not authorized
|
|
if (userRoleLevel == null)
|
|
return false;
|
|
|
|
// check authorization
|
|
if (!CheckAuthorization(Convert.ToInt32(userRoleLevel.Value), FunctionAccess))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
private bool CheckAuthorization(int userLevel, string functionName)
|
|
{
|
|
try
|
|
{
|
|
using (FunctionAccessController acController = new FunctionAccessController())
|
|
{
|
|
|
|
// Read from db category levels
|
|
FunctionAccessModel functionAccess = acController.FindEnabledFunctionByName(functionName);
|
|
if (functionAccess != null && StartupConfigController.CheckAreaStatus(functionAccess.Area))
|
|
{
|
|
if (Action == ACTIONS.READ)
|
|
{ // Check read permissions
|
|
if (functionAccess.ReadLevelMin > userLevel)
|
|
return false; // Not authorized
|
|
}
|
|
else
|
|
{ // Check write permissions
|
|
if (functionAccess.WriteLevelMin > userLevel)
|
|
return false; // Not authorized
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ExceptionManager.Manage(ex);
|
|
}
|
|
// Authorized
|
|
return true;
|
|
}
|
|
}
|
|
}
|