Files
HopFrame/docs/blazor/admin.md

4.3 KiB

HopFrame Admin Pages

Admin pages can be defined through a AdminContext similar to how a DbContext is defined. They generate administration pages like /administration/users simply by reading the structure of the provided model and optionally some additional configuration.

Fun fact: The already existing pages /administration/users and /administration/groups are also generated using an internal AdminContext.

Usage

  1. Create a class that inherits the AdminPagesContext base class

    public class AdminContext : AdminPagesContext {
    
    }
    
  2. Add your admin pages as properties to the class

    public class AdminContext : AdminPagesContext {
    
        public AdminPage<Address> Addresses { get; set; }
    
        public AdminPage<Employee> Employees { get; set; }
    
    }
    
  3. Optionally you can further configure your pages in the OnModelCreating method

    public class AdminContext : AdminPagesContext {
    
        public AdminPage<Address> Addresses { get; set; }
        public AdminPage<Employee> Employees { get; set; }
    
        public override void OnModelCreating(IAdminContextGenerator generator) {
            base.OnModelCreating(generator);
    
            generator.Page<Employee>()
                .Property(e => e.Address)
                .IsSelector();
    
            generator.Page<Address>()
                .Property(a => a.Employee)
                .Ignore();
    
            generator.Page<Address>()
                .Property(a => a.AddressId)
                .IsSelector<Employee>()
                .Parser<Employee>((model, e) => model.AddressId = e.EmployeeId);
    
            generator.Page<Employee>()
                .ConfigureRepository<EmployeeProvider>()
                .ListingProperty(e => e.Name);
    
            generator.Page<Address>()
                .ConfigureRepository<AddressProvider>()
                .ListingProperty(a => a.City);
        }
    }
    
  4. Optionally you can also add some of the following attributes to your classes / properties to further configure the admin pages:

    Attributes for classes:

    [AttributeUsage(AttributeTargets.Class)]
    public sealed class AdminButtonConfigAttribute(bool showCreateButton = true, bool showDeleteButton = true, bool showUpdateButton = true) : Attribute {
        public bool ShowCreateButton { get; set; } = showCreateButton;
        public bool ShowDeleteButton { get; set; } = showDeleteButton;
        public bool ShowUpdateButton { get; set; } = showUpdateButton;
    }
    
    [AttributeUsage(AttributeTargets.Class)]
    public sealed class AdminPermissionsAttribute(string view = null, string create = null, string update = null, string delete = null) : Attribute {
        public AdminPagePermissions Permissions { get; set; } = new() {
            Create = create,
            Update = update,
            Delete = delete,
            View = view
        };
    }
    
    [AttributeUsage(AttributeTargets.Class)]
    public class AdminUrlAttribute(string url) : Attribute {
        public string Url { get; set; } = url;
    }
    

    Attributes for properties:

    [AttributeUsage(AttributeTargets.Property)]
    public sealed class AdminHideValueAttribute : Attribute;
    
    [AttributeUsage(AttributeTargets.Property)]
    public sealed class AdminIgnoreAttribute(bool onlyForListing = false) : Attribute {
        public bool OnlyForListing { get; set; } = onlyForListing;
    }
    
    [AttributeUsage(AttributeTargets.Property)]
    public sealed class AdminPrefixAttribute(string prefix) : Attribute {
        public string Prefix { get; set; } = prefix;
    }
    
    [AttributeUsage(AttributeTargets.Property)]
    public sealed class AdminUneditableAttribute : Attribute;
    
    [AttributeUsage(AttributeTargets.Property)]
    public class AdminUniqueAttribute : Attribute;
    
    [AttributeUsage(AttributeTargets.Property)]
    public sealed class AdminUnsortableAttribute : Attribute;
    
    [AttributeUsage(AttributeTargets.Property)]
    public sealed class ListingPropertyAttribute : Attribute;