2012-04-23 50 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.prefix线app/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