【问题标题】:Cake PHP 1.2 Admin revision to application. Prefix/Admin Routing?Cake PHP 1.2 对应用程序的管理修订。前缀/管理员路由?
【发布时间】:2012-04-23 17:10:01
【问题描述】:

目前,Auth 组件已用于锁定任何未登录的人对应用程序的访问。但是 users_controller 的添加操作已通过路由提供,以便他们可以“注册”。 (重点是任何人都可以注册以访问应用程序的功能,但我需要实现一个唯一的管理员用户

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

因此,在views/pages/home.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 将 users_controller 的编辑操作和删除操作分开,这样只有管理员才能编辑和删除用户。

我的 users 表的数据库结构需要唯一的用户名 (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');
        }

    }

【问题讨论】:

    标签: cakephp admin cakephp-1.2


    【解决方案1】:

    请注意,我只会在较小/私人应用程序中推荐以下解决方案。较大的应用程序/企业应用程序应该真正在 ACL 上运行。

    话虽如此,您可以做的是使用管理员路由和小的数据库调整。

    1. 通过取消注释app/config/core.php 中的Routing.prefix 行来启用管理员路由前缀
    2. 调整您的 users 表以保存一个名为 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
    }
    

    【讨论】:

    • 感谢您的宝贵时间和帮助 Oldskool。我已经在我的 users_controller 上使用修改后的编辑和删除操作/方法更新了帖子。我需要完成的最后一件事是检查,以便只有管理员用户在导航上显示一个显示“用户”的选项卡。此选项卡会将他们路由到 users_controller.php 的索引操作,他们将在其中管理用户,并且他们可以使用安全的编辑和删除操作。这个检查会在 home.ctp 上执行吗?头文件.ctp?或者可能两者兼而有之?
    • 我不确定语法对于我上面发布的更新的编辑和删除功能是否正确。它没有导致应用程序崩溃或引发错误。
    • @BenAidley 是的,您可以在视图中添加相同的检查,以使其显示在导航菜单中。喜欢:if($this-&gt;Session-&gt;read('Auth.User.admin') == 1) { echo $this-&gt;Html-&gt;link('Users', array('admin' =&gt; true, 'controller' =&gt; 'users', 'action' =&gt; 'index')); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 2013-01-05
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多