AshAdmin
repository·main·Indexed 41 days ago
https://github.com/ash-project/ash_adminA super-admin UI dashboard for Ash Framework applications, built with Phoenix LiveView. It provides a visual management interface for Ash domains and resources, allowing developers to configure action visibility, datatable displays, and form field appearances via DSL extensions.
What's inside AshAdmin
- AshAdmin is a super-admin UI dashboard designed for applications built with the Ash Framework. It is implemented using Phoenix LiveView and provides a visual interface for managing Ash resources and domains.
Configure the admin dashboard for a resource using `admin`
mainThe
adminDSL block is a resource extension used to alter how a specific Ash resource behaves and appears within theash_admininterface. You can use it to control which actions are visible, how columns are displayed in datatables, and how fields are formatted.Key configuration areas include:
- Action Visibility: Control which
read_actions,create_actions,update_actions,destroy_actions, orgeneric_actionsare available. - Datatable Display: Specify
table_columns,table_sortable_columns, andtable_filterable_columns. - Relationship Behavior: Use
label_fieldto define the display label for relationship selects, andrelationship_select_max_itemsto toggle between a select dropdown and a typeahead (default is 50). - Formatting: Use
format_fieldsto apply custom formatting (via MFA) to date/time fields in datatables and show/update pages.
- Action Visibility: Control which
Secure AshAdmin routes
mainAshAdmin does not have built-in security; it relies on your application's existing policies and authentication.
To secure the routes, pass authentication options (like
AshAuthentication.Phoenix.LiveSession.opts/1) directly to theash_adminmacro in your router. You can use anon_mountcallback to restrict access to specific user roles (e.g.,:admin).To ensure users act as themselves (triggering their own policies), implement the
AshAdmin.ActorPlugbehaviour and configure it via:actor_plugin your application config.# In your Router scope "/" do pipe_through [:browser] ash_admin "/admin", AshAuthentication.Phoenix.LiveSession.opts( on_mount: [{ExampleWeb.LiveUserAuth, :admin_only}] ) end # In your config config :ash_admin, :actor_plug, MyAppWeb.AshAdminActorPlug# Implementation of AshAdmin.ActorPlug defmodule ExampleWeb.AshAdminActorPlug do @behaviour AshAdmin.ActorPlug @impl true def actor_assigns(socket, _session) do dispatcher = socket.assigns[:current_user] [actor: dispatcher] end @impl true def set_actor_session(conn), do: conn endGenerate migrations for dev resources
mainIf you modify the development resources within the
devfolder, you must generate new migrations using the following command:mix generate_migrationsmix generate_migrationsManually set up AshAdmin in your Router
mainTo set up AshAdmin manually, import
AshAdmin.Routerinto your Phoenix router.Important Requirements:
- AshAdmin requires a Phoenix LiveView
:browserpipeline. If your application doesn't have one, use theadmin_browser_pipelinemacro to define one. - No Prefixes: Do not place the
ash_adminroute inside ascopethat has a module prefix (e.g.,scope "/", MyAppWeb). Instead, use an empty scopescope "/" do ... endto avoidUndefinedFunctionErrorissues.
defmodule MyAppWeb.Router do use Phoenix.Router import AshAdmin.Router # Define a browser pipeline if you don't have one admin_browser_pipeline :browser # Use an empty scope (no module prefix) for AshAdmin scope "/" do pipe_through [:browser] ash_admin "/admin" end end- AshAdmin requires a Phoenix LiveView
Recompile assets for AshAdmin
mainIf you make changes to CSS or JavaScript files, or if you update dependencies that include assets (such as Phoenix LiveView), you must recompile the assets using:
mix assets.deploymix assets.deployInstall AshAdmin
mainAdd
ash_adminto yourmix.exsdependencies.Alternatively, use Igniter for an automated installation process, which is the recommended method.
{:ash_admin, "~> 0.13.3"}mix igniter.install ash_adminSet up the AshAdmin development environment
mainAshAdmin includes a built-in development app located in the
devfolder, allowing you to develop without an external Phoenix application.To set up the environment, ensure you have PostgreSQL running locally, then execute the following commands in order:
- Create the database:
mix ash_postgres.create - Run migrations:
mix migrate - Migrate tenants:
mix migrate_tenants - Run the setup task:
mix setup
Once setup is complete, start the application server using:
mix devmix ash_postgres.create mix migrate mix migrate_tenants mix setup mix dev- Create the database:
Configure Content Security Policy (CSP) for AshAdmin
mainIf your application uses a strict Content Security Policy (CSP), AshAdmin's inline CSS and JavaScript may be blocked.
To fix this, you can:
- Add the default AshAdmin nonces to your
default-srcallowlist in your router. - Or, provide your own nonces by setting the
:csp_nonce_assign_keyoption in theash_adminroute macro.
# Option 1: Using default nonces in router plug :put_secure_browser_headers, %{"content-security-policy" => "default-src 'nonce-ash_admin-Ed55GFnX' 'self'"} # Option 2: Providing a custom nonce key in the route ash_admin "/admin", csp_nonce_assign_key: :csp_nonce_value- Add the default AshAdmin nonces to your
Configure the admin dashboard for a domain using AshAdmin.Domain
mainUse
AshAdmin.Domainas a domain extension to customize how a specific Ash domain is represented and behaved within the Ash Admin UI. You can control visibility, resource filtering, and naming via theadminconfiguration block.# Example usage within a domain definition # Note: The exact syntax depends on your domain definition structure defmodule MyDomain do use Ash.Domain admin do name "My Custom Domain" show? true show_resources [:user, :post] end endConfigure field appearance in admin forms using `admin.form.field`
mainUse the
admin.form.fieldDSL within theadmin.formblock to declare non-default behavior for specific attributes in the admin UI forms.This is useful for changing the input type (e.g., using a markdown editor) or setting constraints on file uploads.
# Example usage pattern admin do form do field :description do type :markdown end field :avatar do type :short_text max_file_size 1_000_000 accepted_extensions ".jpg", ".png" end end endConfigure Domains and Resources for AshAdmin
mainTo make your Ash data visible in the dashboard, you must perform two steps:
- Enable the Domain extension: Add
AshAdmin.Domainto yourAsh.Domainand setshow? truein theadminblock. - Identify the Actor (Optional): If your application uses an actor (e.g., a
Userresource) to drive policies, add theAshAdmin.Resourceextension to that resource and setactor? truein itsadminblock.
# In your Domain(s) use Ash.Domain, extensions: [AshAdmin.Domain] admin do show? true end # In your resource that acts as an actor (e.g. User) use Ash.Resource, domain: YourDomain, extensions: [AshAdmin.Resource] admin do actor? true end- Enable the Domain extension: Add