2016-03-02 172 views
3

我正在使用sonatadmin進行symfony 2項目。有時管理員用戶可能會意外刪除他自己的帳戶。如何防止管理員用戶刪除他自己的帳戶?謝謝!SonataAdmin:防止管理員刪除自己的帳戶

+0

這可能幫助https://sonata-project.org/bundles/admin/2-3/doc/reference/batch_actions.html您可以ovveride模板,如果去掉複選框行中的用戶是admin –

+0

我嘗試了類似的東西。但有一點是用戶仍然可以在帳戶編輯頁面中刪除他的帳戶。所以我可能需要修改一些代碼 – fallcool

回答

3

爲了防止管理員通過以下ADVANCED CONFIGURATION

admin:     # Admin Classes 
    user: 
     class:   Sonata\UserBundle\Admin\Entity\UserAdmin 
     controller:  YourUserBundle:CRUD 
     translation: SonataUserBundle 

,然後在你的控制器覆蓋刪除自己的帳戶,你需要定義自己的CRUDController索納塔用戶在這些功能batchActionDelete() & deleteAction()功能檢查,如果請求包含管理對象/ id然後在這裏限制。對於batchActionDelete()功能

0

我與FOSUserBundle一起使用SonataUserBundle和

public function deleteAction($id) 
    { 
     $id  = $this->get('request')->get($this->admin->getIdParameter()); 
     $object = $this->admin->getObject($id); 

     if (!$object) { 
      throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id)); 
     } 
     $userid = $this->getUser()->getId() // get id of logged in user 
     if($userid == $id){ 
       $this->addFlash(
        'sonata_flash_error', 
        'Error you cannot delete your own account' 
       ); 
      return $this->redirectTo($object); 
     } 
    // other code from base class 

    } 

同樣的邏輯我結束了以下解決方案。

config.yml:

parameters: 
    sonata.user.admin.user.controller: AppBundle:CRUD\CRUD 

的appbundle \控制器\ CRUD \ CRUDController:

<?php 

namespace AppBundle\Controller\CRUD; 

use Sonata\AdminBundle\Controller\CRUDController as Controller; 
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; 
use Symfony\Component\HttpFoundation\RedirectResponse; 

class CRUDController extends Controller 
{ 
    public function deleteAction($id) 
    { 
     $request = $this->getRequest(); 
     $id  = $request->get($this->admin->getIdParameter()); 
     $object = $this->admin->getObject($id); 

     if (!$object) { 
      throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id)); 
     } 

     $currentUserId = $this->getUser()->getId(); // ID of the current user 
     if ($currentUserId == $id) { 
      $this->addFlash(
       'sonata_flash_error', 
       'You cannot delete your own account.' 
      ); 

      return $this->redirectTo($object); 
     } 

     return parent::deleteAction($id); 
    } 

    public function batchActionDelete(ProxyQueryInterface $query) 
    { 
     $request  = $this->getRequest(); 
     $currentUserId = $this->getUser()->getId(); // ID of the current user 
     $selectedUsers = $query->execute(); 

     foreach ($selectedUsers as $selectedUser) { 
      if ($selectedUser->getId() == $currentUserId) { 
       $this->addFlash(
        'sonata_flash_error', 
        'You cannot delete your own account.' 
       ); 

       return new RedirectResponse(
        $this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())) 
       ); 
      } 
     } 

     return parent::batchActionDelete($query); 
    } 
} 

參考文獻:

相關問題