using System; using System.Globalization; using System.Linq; using System.Reflection; namespace InABox.Core { public enum AutoSecurityDescriptorActionType { View, Edit, Delete, Import, Export, Merge, Post, ConfigurePost, ManageIssues, ManageProblems } public interface IAutoSecurityDescriptor : ISecurityDescriptor { AutoSecurityDescriptorActionType ActionType { get; } Type EntityType { get; } } public abstract class AutoSecurityDescriptor : IAutoSecurityDescriptor where TAction : IAutoSecurityAction, new() { protected abstract Type TEntity { get; } private TAction _action = new TAction(); public AutoSecurityDescriptorActionType ActionType => _action.Type; Type IAutoSecurityDescriptor.EntityType => TEntity; public virtual bool Visible => true; public string Category => new Inflector.Inflector(new CultureInfo("en")) .Pluralize( TEntity.EntityName().Split('.').Last().SplitCamelCase() ); public string Type => GetLicenseCaption(); public virtual string Code => GetCode(); public virtual string Description { get { var caption = GetType().GetCaption(false); return string.IsNullOrWhiteSpace(caption) ? GetCode().SplitCamelCase() : caption; } } public virtual bool Value => _action.GetValue(TEntity); public virtual bool HasScope(SecurityDescriptorScope scope) { return true; } private string GetLicenseCaption() { var license = TEntity .GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ILicense<>)) ?.GenericTypeArguments.FirstOrDefault(); return license != null ? license.GetCaption() : typeof(CoreLicense).GetCaption(); } private string GetCode() { return string.Format( "Can{0}{1}{2}", _action.Prefix, string.IsNullOrWhiteSpace(_action.Postfix) ? new Inflector.Inflector(new CultureInfo("en")).Pluralize(TEntity.EntityName().Split('.').Last()) : TEntity.EntityName().Split('.').Last(), string.IsNullOrWhiteSpace(_action.Postfix) ? "" : new Inflector.Inflector(new CultureInfo("en")).Pluralize(_action.Postfix) ); } } public interface IAutoSecurityAction { AutoSecurityDescriptorActionType Type { get; } string Prefix { get; } string Postfix { get; } bool GetValue(Type TEntity); } public class CanView : IAutoSecurityAction { public string Prefix => "View"; public string Postfix => ""; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.View; public bool GetValue(Type TEntity) => true; } public class CanView : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } public class CanEdit : IAutoSecurityAction { public string Prefix => "Edit"; public string Postfix => ""; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Edit; public bool GetValue(Type TEntity) => TEntity.GetCustomAttribute() == null; } public class CanEdit : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } public class CanImport : IAutoSecurityAction { public string Prefix => "Import"; public string Postfix => ""; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Import; public bool GetValue(Type TEntity) => TEntity.GetCustomAttribute() == null && TEntity.HasInterface(); } public class CanImport : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } public class CanExport : IAutoSecurityAction { public string Prefix => "Export"; public string Postfix => ""; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Export; public bool GetValue(Type TEntity) => false; } public class CanExport : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } public class CanMerge : IAutoSecurityAction { public string Prefix => "Merge"; public string Postfix => ""; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Merge; public bool GetValue(Type TEntity) => TEntity.GetCustomAttribute() == null; } public class CanMerge : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } public class CanPost : IAutoSecurityAction { public string Prefix => "Post"; public string Postfix => ""; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Post; public bool GetValue(Type TEntity) => true; } public class CanPost : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } public class CanConfigurePost : IAutoSecurityAction { public string Prefix => "Configure"; public string Postfix => "Post Settings"; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.ConfigurePost; public bool GetValue(Type TEntity) => true; } public class CanConfigurePost : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } public class CanDelete : IAutoSecurityAction { public string Prefix => "Delete"; public string Postfix => ""; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.Delete; public bool GetValue(Type TEntity) => (TEntity.GetCustomAttribute() == null) && (TEntity.GetInterfaces().Any(x => x.IsConstructedGenericType && x.GetGenericTypeDefinition() == typeof(IManyToMany<,>))); } public class CanDelete : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } public class CanManageIssues : IAutoSecurityAction { public string Prefix => "Manage"; public string Postfix => "Issue"; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.ManageIssues; public bool GetValue(Type TEntity) => (TEntity.GetCustomAttribute() == null); } public class CanManageIssues : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } public class CanManageProblems : IAutoSecurityAction { public string Prefix => "Manage"; public string Postfix => "Problem"; public AutoSecurityDescriptorActionType Type => AutoSecurityDescriptorActionType.ManageProblems; public bool GetValue(Type TEntity) => (TEntity.GetCustomAttribute() == null); } public class CanManageProblems : AutoSecurityDescriptor { protected override Type TEntity => typeof(T); } }