Ver Fonte

Added security attributes for properties

Kenric Nugteren há 1 ano atrás
pai
commit
147d9a6d26

+ 2 - 0
InABox.Core/DatabaseSchema/DatabaseSchema.cs

@@ -185,6 +185,8 @@ namespace InABox.Core
                                 editor.Page = page;
                                 editor.Caption = caption;
                                 editor.EditorSequence = (int)(sequence ?? 999);
+
+                                editor.Security = prop.GetCustomAttributes<SecurityAttribute>().ToArray();
                             }
 
                             bool required = false;

+ 8 - 0
InABox.Core/Editors/BaseEditor.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Linq;
 
 namespace InABox.Core
 {
@@ -26,6 +27,8 @@ namespace InABox.Core
 
         Summary Summary { get; set; }
 
+        SecurityAttribute[] Security { get; set; }
+
         BaseEditor CloneEditor();
         public object Clone();
     }
@@ -63,6 +66,7 @@ namespace InABox.Core
             Caption = "";
             Summary = Summary.None;
             ToolTip = "";
+            Security = Array.Empty<SecurityAttribute>();
         }
 
         [EnumLookupEditor(typeof(Visible))]
@@ -98,6 +102,9 @@ namespace InABox.Core
         [NullEditor]
         public Summary Summary { get; set; }
 
+        [NullEditor]
+        public SecurityAttribute[] Security { get; set; }
+
         public BaseEditor CloneEditor()
         {
             var result = DoClone();
@@ -112,6 +119,7 @@ namespace InABox.Core
             result.Page = Page;
             result.Summary = Summary;
             result.ToolTip = ToolTip;
+            result.Security = Security.Select(x => x.Clone()).ToArray();
 
             return result;
         }

+ 53 - 0
InABox.Core/Editors/Utils/SecurityAttribute.cs

@@ -0,0 +1,53 @@
+using System;
+using System.Linq;
+
+namespace InABox.Core
+{
+    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
+    public class SecurityAttribute : Attribute
+    {
+        public Type SecurityDescriptor { get; set; }
+
+        private SecurityAttribute() { }
+
+        public SecurityAttribute(Type securityDescriptor)
+        {
+            if (!securityDescriptor.GetInterfaces().Contains(typeof(ISecurityDescriptor)))
+                throw new Exception(securityDescriptor.EntityName() + " is not a valid security descriptor!");
+            SecurityDescriptor = securityDescriptor;
+        }
+
+        public virtual SecurityAttribute Clone()
+        {
+            var result = new SecurityAttribute(SecurityDescriptor);
+            result.SecurityDescriptor = SecurityDescriptor;
+            return result;
+        }
+    }
+
+    public class CanViewAttribute : SecurityAttribute
+    {
+        public CanViewAttribute(Type TEntity): base(
+            typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanView<>).MakeGenericType(TEntity)))
+        {
+        }
+    }
+
+    public class CanEditAttribute : SecurityAttribute
+    {
+        public CanEditAttribute(Type TEntity) : base(
+            typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanEdit<>).MakeGenericType(TEntity)))
+        {
+
+        }
+    }
+
+    public class CanDeleteAttribute : SecurityAttribute
+    {
+        public CanDeleteAttribute(Type TEntity) : base(
+            typeof(AutoSecurityDescriptor<,>).MakeGenericType(TEntity, typeof(CanDelete<>).MakeGenericType(TEntity)))
+        {
+
+        }
+    }
+}

+ 13 - 0
inabox.wpf/DynamicGrid/DynamicEditorGrid.xaml.cs

@@ -591,6 +591,19 @@ namespace InABox.DynamicGrid
                     editor.Editable = Editable.Hidden;
                 }
 
+                if(editor is not null)
+                {
+                    foreach(var security in editor.Security)
+                    {
+                        if (!Security.IsAllowed(security.SecurityDescriptor))
+                        {
+                            editor.Visible = Visible.Hidden;
+                            editor.Editable = Editable.Hidden;
+                            break;
+                        }
+                    }
+                }
+
                 if(editor is not null)
                 {
                     OnGridCustomiseEditor?.Invoke(this, column, editor);