Discuss your project
Partitech Sonata Extra Bundle

Sonata Extra Bundle: validation workflow

Overview The Sonata Extra validation workflow records the actions performed in the administration, offering a complete overview of activities, involved resources, descriptions, and users. This feature ensures that actions are recorded but not applied immed...

Contrôles de compatibilité Symfony et Sonata

Overview

The Sonata Extra validation workflow records the actions performed in the administration, offering a complete overview of activities, involved resources, descriptions, and users. This feature ensures that actions are recorded but not applied immediately. They instead require validation by a user with the ROLE_APPROVE permission.

Features

  • Action Logging: Records each action but does not apply the changes immediately.
  • Role-based approval: Actions require the approval of a user with ROLE_APPROVE.
  • Pending Changes Alert: A red notification badge in the administration interface alerts administrators of changes awaiting approval.
  • Detailed action view: Displays the action type, the resource, the description, the user, and the date in the list view.
  • Approval and Detail Buttons: Allows administrators to approve changes or view detailed information.
  • Purge feature: An option to purge pending changes.
  • Detailed view of changes: Displays the fields and values affected for each action.

Screens

  • Publisher's action: publisher_action_approval.png

  • Administrator Notification: notification_approval_admin.png

  • List view: admin_approval_list.png

  • Detailed view: detail_admin_approval.png

Setup steps

Add event listeners

    sonata-extra.doctrine_activity_listener:
        class: Partitech\SonataExtra\EventListener\DoctrineActivityListener
        tags:
            - { name: doctrine.event_listener, event: prePersist }
            - { name: doctrine.event_listener, event: preUpdate }
            - { name: doctrine.event_listener, event: preRemove }
            - { name: doctrine.event_listener, event: onFlush }
            - { name: kernel.event_listener, event: sonata.admin.event.batch_action.pre_batch_action, method: onPreBatchAction }



    sonata-extra.configure_menu_listener:
        class: Partitech\SonataExtra\EventListener\ConfigureMenuListener
        tags:
            - { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: onMenuConfigure }
        arguments:
            - "@service_container"
            - "@doctrine.orm.entity_manager"
            - "@request_stack"
            - "@security.authorization_checker"

Configure Entity Exclusion

You can exclude any entity from the workflow. By default, you should exclude your SonataMediaMedia and User entities.

parameters:

  sonata_approve_excluded_entities:
    - 'App\Entity\BackofficeUser'
    - 'App\Entity\SonataMediaMedia'
    - 'Partitech\SonataExtra\Entity\Slider'

Configure the menu

By default, the approval menu is dynamically inserted at the root of the menu with a red visual alert. To avoid double display when the menu group is open, you can list the menu items that will hide the root alert.

parameters:
  sonata_approve_menu:
    - 'admin_app_approval'
    - 'admin_app_adminactivitylog'
    - 'admin_app_sonatamediamedia'
    - 'admin_app_backofficeuser'
    - 'admin_app_sonatapagesite'

Configure roles

All users who do not have the ROLE_APPROVE role will be in the workflow rule.
If you want to get the administrative activity log without the approval workflow, simply add ROLE_APPROVE to your default administrator user.

security:
  role_hierarchy:
        ROLE_EDITOR:
            - ROLE_USER
            - ROLE_SONATA_ADMIN
            - ROLE_ADMIN_USERADMIN_ALL

            - ROLE_ADMIN_ADMIN_ACTIVITY_LOG_ALL
            - ROLE_ADMIN_ADMIN_APPROVAL_LOG_ALL
            - ROLE_ADMIN_USERADMIN_ALL
            - ROLE_ADMIN_REDIRECTION_ALL
            - ROLE_SONATA_EXTRA_ADMIN_SLIDER_ALL
            - ROLE_SONATA_EXTRA_ADMIN_SLIDER_SLIDES_ALL
            - ROLE_SONATA_EXTRA_ADMIN_FAQ_CATEGORY_ALL
            - ROLE_SONATA_EXTRA_ADMIN_FAQ_QUESTION_ALL

            - ROLE_SONATA_USER_ADMIN_USER_ALL

            - ROLE_SONATA_MEDIA_ADMIN_MEDIA_ALL
            - ROLE_SONATA_MEDIA_ADMIN_GALLERY_ALL
            - ROLE_SONATA_MEDIA_ADMIN_GALLERY_ITEM_ALL

            - ROLE_SONATA_PAGE_ADMIN_PAGE_ALL
            - ROLE_SONATA_PAGE_ADMIN_BLOCK_ALL
            - ROLE_SONATA_PAGE_ADMIN_SHARED_BLOCK_ALL
            - ROLE_SONATA_PAGE_ADMIN_SNAPSHOT_ALL
            - ROLE_SONATA_PAGE_ADMIN_SITE_ALL

            - ROLE_PRODIGIOUS_SONATA_MENU_ADMIN_MENU_ALL
            - ROLE_PRODIGIOUS_SONATA_MENU_ADMIN_MENU_ITEM_ALL

        ROLE_ADMIN:
            - ROLE_USER
            - ROLE_SONATA_ADMIN
            - ROLE_APPROVE
            - ROLE_EDITOR
            - ROLE_ADMIN_ADMIN_ACTIVITY_LOG_ALL
            - ROLE_ADMIN_ADMIN_APPROVAL_LOG_ALL
            -
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

Usage and workflow

  • Unapproved users: Users without ROLE_APPROVE can perform actions, but these will be recorded and pending approval.
  • Approval required: A notification in the 'Pending Changes' tool alerts administrators of actions requiring approval.
  • Approval process: Administrators with the APPROVER_ROLE can view the details and either approve or reject the changes.
  • Purge option: Administrators can use the purge button to clear pending changes.

Conclusion

The SonataExtra approval workflow strengthens control and security by ensuring that all changes are recorded and require the approval of authorized personnel. This feature is crucial for maintaining integrity and accountability within the administrative interface.

Adapt the example to the installed version

The Symfony and Sonata examples remain tied to the versions indicated in the article. Symfony 4.x and Symfony 6.2 are no longer maintained: for a current project, use a maintained branch and the documentation that exactly matches your dependencies.

php bin/console about
composer show symfony/framework-bundle sonata-project/admin-bundle

Before resuming the code, check the signatures, services, routes, and templates concerned. Then add a functional test covering permissions, HTTP code, and the visible result.

Reference: maintained Symfony versions.

Share this article