using System; using System.Collections.Generic; using System.Security.Principal; using System.Text; using System.Linq; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Authorization; using GreenTree.Maschinenbestellungen.Domain.Model; using System.Security.Claims; namespace GreenTree.Maschinenbestellungen.Services.Authorization { public class CookieAuthorizationService : IAuthorizationService { #region Implementation /// /// Check wether the user has a specific policy /// /// The user identity. /// The policy to be checked. public bool UserHasPolicy(IIdentity identity, string policy) { var claimsIdentity = identity as ClaimsIdentity; if (claimsIdentity == null) return false; return claimsIdentity.Claims .Any(c => c.Type == "Policy" && c.Value == policy); } /// /// Check wether the user has any policy regarding base data management /// /// The user identity. public bool UserHasBasedataPolicy(IIdentity identity) { var claimsIdentity = identity as ClaimsIdentity; if (claimsIdentity == null) return false; return claimsIdentity.Claims .Any(c => c.Type == "Policy" && (c.Value.StartsWith("Customer") || c.Value.StartsWith("Employee") || c.Value.StartsWith("Material") || c.Value.StartsWith("Supplier"))); } /// /// Check wether the user has any policy in a specific category /// /// The user identity. /// The category (e.g. 'Customer'). public bool UserHasCategoryPolicy(IIdentity identity, string category) { var claimsIdentity = identity as ClaimsIdentity; if (claimsIdentity == null) return false; return claimsIdentity.Claims .Any(c => c.Type == "Policy" && c.Value.StartsWith(category)); } #endregion } }