using ARC.GMP.Models; using ARC.GMP.Utils; using Newtonsoft.Json; using NLog; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using System.Web.Script.Serialization; namespace ARC.GMP.Types { public static class AccessPermissionManager { private static ILogger _logger = LogManager.GetCurrentClassLogger(); public static async Task IsAuthenticatedAsync(string url, string username) { AuthenticationDetail authDetail = null; string authenticationJson = ""; HttpCookie authenticationCookie = HttpContext.Current.Request.Cookies["Authentication"]; if (authenticationCookie == null) { //authenticationJson = InvokeWebService.GetWebClientRequest(url + username); authenticationJson = await Task.Run(() => InvokeWebService.GetHttpClientRequestAsync(url + username)); } else { authenticationJson = authenticationCookie.Value; } authDetail = JsonConvert.DeserializeObject(authenticationJson); var isAuthenticated = authDetail != null && authDetail.IsAuthenticated; _logger.Log(LogLevel.Trace, "IsAuthenticated: " + isAuthenticated); if (isAuthenticated && authenticationCookie == null) { HttpContext.Current.Response.Cookies.Add(new HttpCookie("Authentication", authenticationJson)); } return authDetail; } public static bool IsAuthenticated(string url, string username, out AuthenticationDetail authDetail) { string authenticationJson = ""; HttpCookie authenticationCookie = HttpContext.Current.Request.Cookies["Authentication"]; if (authenticationCookie == null) { authenticationJson = InvokeWebService.GetWebClientRequest(url + username); } else { authenticationJson = authenticationCookie.Value; } authDetail = JsonConvert.DeserializeObject(authenticationJson); var isAuthenticated = authDetail != null && authDetail.IsAuthenticated; _logger.Log(LogLevel.Trace, "IsAuthenticated: " + isAuthenticated); if (isAuthenticated && authenticationCookie == null) { HttpContext.Current.Response.Cookies.Add(new HttpCookie("Authentication", authenticationJson)); } return isAuthenticated; } public static async Task> IsAuthorizedAsync(string url, string token, AuthenticationDetail authDetail, string clientId) { string rolesJson = ""; HttpCookie authorizecookie = HttpContext.Current.Request.Cookies["UserRoles"]; _logger.Log(LogLevel.Trace, "Token: " + token); if (authorizecookie == null) { _logger.Log(LogLevel.Trace, "Get the details from Cookies"); string authorizationJson = ""; authorizationJson = await Task.Run(() => InvokeWebService.GetHttpClientRequestAsync(url, token, clientId)); _logger.Log(LogLevel.Trace, "authorizationJson Values: " + authorizationJson); rolesJson = GetRolesJsonString(authorizationJson, authDetail); _logger.Log(LogLevel.Trace, "Authorization Json: " + authorizationJson); } else { rolesJson = authorizecookie.Value; } _logger.Log(LogLevel.Trace, "Roles Json: " + rolesJson); var isAuthorized = false; isAuthorized = IsUserAuthorized(rolesJson); _logger.Log(LogLevel.Trace, "IsAuthorized: " + isAuthorized); if (isAuthorized) { if (authorizecookie == null) { HttpContext.Current.Response.Cookies.Add(new HttpCookie("UserRoles", rolesJson)); } } Tuple returnValue = new Tuple(isAuthorized, rolesJson); return await Task.Run(() => returnValue); } public static Tuple IsAuthorized(string url, string token, AuthenticationDetail authDetail, string clientId) { string rolesJson = ""; HttpCookie authorizecookie = HttpContext.Current.Request.Cookies["UserRoles"]; if (authorizecookie == null) { string authorizationJson = ""; authorizationJson = InvokeWebService.GetWebClientRequest(url, token, clientId); rolesJson = GetRolesJsonString(authorizationJson, authDetail); } else { rolesJson = authorizecookie.Value; } var isAuthorized = false; isAuthorized = IsUserAuthorized(rolesJson); _logger.Log(LogLevel.Trace, "IsAuthorized: " + isAuthorized); if (isAuthorized) { if (authorizecookie == null) { HttpContext.Current.Response.Cookies.Add(new HttpCookie("UserRoles", rolesJson)); } } Tuple returnValue = new Tuple(isAuthorized, rolesJson); return returnValue; } private static bool IsUserAuthorized(string rolesJson) { UserAuthorizeResponse authorizeDetail = JsonConvert.DeserializeObject(rolesJson); return authorizeDetail != null && authorizeDetail.IsAuthorized; } private static string GetRolesJsonString(string authorizationJson, AuthenticationDetail authDetail) { string rolesJson; AuthorizeUserByRoleModel roles = JsonConvert.DeserializeObject(authorizationJson); if (roles != null && roles.data != null) { _logger.Log(LogLevel.Trace, "Roles Values IsAuthorized: " + roles.data.IsAuthorized); _logger.Log(LogLevel.Trace, "Roles Values: " + roles.data.IsAuthorized); } if (authDetail != null) _logger.Log(LogLevel.Trace, "authDetail First Name: " + authDetail.FirstName); var user = new UserAuthorizeResponse(); user.FirstName = authDetail.FirstName; user.LastName = authDetail.LastName; user.UserName = authDetail.UserName; user.Email = authDetail.Email; user.IsAuthorized = roles.data.IsAuthorized; user.roleId = roles.data.RoleId.ToString(); user.Permissions = new List(); if (roles.data.UserAccesses != null) { var userAccesses = roles.data.UserAccesses.GroupBy(x => new { EntityName = x.EntityName }) .Select(x => new { entityAction = String.Join(",", x.Select(z => z.EntityAction)), entityName = x.Key.EntityName }); foreach (var access in userAccesses) { user.Permissions.Add(new UserAccessResponse { entityName = access.entityName, Create = (access.entityAction.Contains("Create")) ? "true" : "false", Update = (access.entityAction.Contains("Update")) ? "true" : "false", Read = (access.entityAction.Contains("Read")) ? "true" : "false", Delete = (access.entityAction.Contains("Delete")) ? "true" : "false", }); } } rolesJson = new JavaScriptSerializer().Serialize(user); return rolesJson; } public static bool HasPermissions(string rolesJson, string entity, List methodPermissions) { bool hasPermission = false; var userAccesses = JsonConvert.DeserializeObject(rolesJson); if (userAccesses != null && userAccesses.Permissions != null) { var userPermission = userAccesses.Permissions.FirstOrDefault(r => r.entityName.ToLowerInvariant() == entity.ToLowerInvariant()); if (userPermission != null) { hasPermission = true; var permissionString = userPermission.Create.ToLowerInvariant() == "true" ? "create." : "" + userPermission.Read.ToLowerInvariant() == "true" ? "read." : "" + userPermission.Update.ToLowerInvariant() == "true" ? "update." : "" + userPermission.Delete.ToLowerInvariant() == "true" ? "delete." : ""; foreach (string verb in methodPermissions) { if (permissionString.Contains(verb.ToLowerInvariant())) { hasPermission = hasPermission && permissionString.Contains(verb.ToLowerInvariant()); } } } } return hasPermission; } } }