2012-04-23 52 views
1

目前,Auth組件已被用於鎖定未登錄的任何人對應用程序的訪問。但是,users_controller的添加操作已在路由中可用,以便他們可以「註冊」。 (整個的一點是,任何人都可以註冊訪問應用程序的功能,但我需要實現一個獨特的管理員用戶蛋糕PHP 1.2管理員修改到應用程序。前綴/管理員路由?

Router::connect('/register', array('controller' => 'users', 'action' => 'add')); 

因此一個「註冊」鏈接已被放在意見/頁/家。與一個登錄表單沿着CTP:

<p> 
    Only takes about 1 minute to <?php echo $html->link('register', '/register'); ?> and its free. 
    </p> 

    <div id="login"> 

    <h2>Login</h2> 

    <?php 
    if ($session->check('Message.auth')) $session->flash('auth'); 
    echo $form->create('User', array('action' => 'login')); 
    echo $form->input('username', array('value' => 'billy')); 
    echo $form->input('password', array('value' => 'billy')); 
    echo $form->end('Login'); 
    ?> 

    </div> 

視圖/元件/ header.ctp:

<?php if($html->loggedIn()): ?> 
     <ul class="right"> 
      <li><?php echo $html->link('Log Out', '/users/logout'); ?></li> 
       </ul> 
    </div> 
     <?php endif; ?> 
    <!-- end nav --> 

我的大問題是我需要找到一種簡單的方法,最好不使用ACL來分隔user_controller的編輯操作和刪除操作,以便只有管理員才能編輯和刪除用戶。

爲用戶表我的數據庫結構需要一個唯一的用戶名(VARCHAR),獨特的ID(INT)

是否有我實現登錄表單說,如果字符串輸入在支票上沒有簡單的方法用戶名是'admin'還是'id'的值等於1,那麼只允許這個唯一的'admin'用戶訪問用戶控制器的編輯和刪除操作?我想象一些路由可能會涉及到。

或者正在覈心中激活cake_admin用戶一個可行的選項?

我可以根據要求提供更多我的代碼。請記住我與版本1.2的工作

users_controller.php中:

function edit($id = null) 
    { 
    if(!$this->Session->read('Auth.User.admin') == 1) { 
     // Not an admin user, go back where we came from 
     $this->redirect($this->referer()); 
    } 
    $this->idEmpty($id, 'index'); 

     if (!empty($this->data)) 
     { 
      if ($this->User->save($this->data)) 
      { 
     $this->flashSuccess('The User has been saved', 'index'); 
      } 
      else 
      { 
     $this->flashWarning('The User could not be saved. Please, try again.'); 
      } 
     } 
     if (empty($this->data)) 
     { 
      $this->data = $this->User->read(null, $id); 
     } 

    } 


    function delete($id = null) 
    { 
if(!$this->Session->read('Auth.User.admin') == 1) { 
     // Not an admin user, go back where we came from 
     $this->redirect($this->referer()); 
    } 
      $this->idEmpty($id, 'index'); 

     if ($this->User->del($id)) 
     { 
     $this->flashSuccess('User Deleted', 'index'); 
     } 

    } 

回答

0

請注意,我會建議以下解決方案僅在一個較小的/私有應用程序。更大的應用程序/企業應用程序應該真正在ACL上運行。

這就是說,你可以做的是使用管理路由和次要的數據庫調整。

  1. 通過取消註釋Routing.prefixapp/config/core.php
  2. 調整你的用戶表來保存稱爲admin額外BOOLEAN類型字段(默認值爲0)啓用管理路由前綴。
  3. 爲您的管理員用戶設置admin字段值爲1。

然後創建您的編輯/刪除功能是這樣的:

function admin_edit($id = null) { 
    if(!$this->Session->read('Auth.User.admin') == 1) { 
     // Not an admin user, go back where we came from 
     $this->redirect($this->referer()); 
    } 

    // Rest of your logic goes here 
} 
+0

感謝您的時間和幫助OLDSKOOL。我已更新了我的users_controller上修改的編輯和刪除操作/方法的帖子。我需要做的最後一件事是檢查,以便只有管理員用戶在導航中顯示「用戶」標籤。此選項卡會將它們路由到users_controller.php的索引操作,它們將管理用戶,並且受保護的編輯和刪除操作可供他們使用。這個檢查將在home.ctp上執行嗎? header.ctp?或者可能兩個? – 2012-04-24 11:52:58

+0

我不確定該語法對於我上面發佈的更新後的編輯和刪除功能是否正確。它沒有導致應用程序崩潰或拋出錯誤。 – 2012-04-24 12:14:28

+0

@ BenAidley是的,您可以將相同的支票添加到您的視圖中,以使其顯示在導航菜單中。如:($ this-> Session-> read('Auth.User.admin')== 1){echo $ this-> Html-> link('Users',array('admin'=> true, 'controller'=>'users','action'=>'index')); }' – Oldskool 2012-04-26 17:31:01