| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- 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<TAction> : 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<T> : AutoSecurityDescriptor<CanView>
- {
- 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<AutoEntity>() == null;
- }
- public class CanEdit<T> : AutoSecurityDescriptor<CanEdit>
- {
- 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<AutoEntity>() == null && TEntity.HasInterface<IImportable>();
- }
- public class CanImport<T> : AutoSecurityDescriptor<CanImport>
- {
- 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<T> : AutoSecurityDescriptor<CanExport>
- {
- 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<AutoEntity>() == null;
- }
- public class CanMerge<T> : AutoSecurityDescriptor<CanMerge>
- {
- 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<T> : AutoSecurityDescriptor<CanPost>
- {
- 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<T> : AutoSecurityDescriptor<CanConfigurePost>
- {
- 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<AutoEntity>() == null) &&
- (TEntity.GetInterfaces().Any(x => x.IsConstructedGenericType && x.GetGenericTypeDefinition() == typeof(IManyToMany<,>)));
- }
- public class CanDelete<T> : AutoSecurityDescriptor<CanDelete>
- {
- 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<AutoEntity>() == null);
- }
- public class CanManageIssues<T> : AutoSecurityDescriptor<CanManageIssues>
- {
- 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<AutoEntity>() == null);
- }
- public class CanManageProblems<T> : AutoSecurityDescriptor<CanManageProblems>
- {
- protected override Type TEntity => typeof(T);
- }
- }
|