【问题标题】:Only one user can open admin.ctp file只有一个用户可以打开 admin.ctp 文件
【发布时间】:2015-01-06 08:48:17
【问题描述】:

我想做一个页面,这个页面只能打开一个用户。我想做这样的事情:如果用户 id = 1 则打开,否则抛出错误。

我已经试过了:

if (in_array($this->action, array('controller' => 'users', 'action' => 'admin'))) {
        $postId = (int) $this->request->params['pass'][0];
        if ($this->User->isOwnedBy($postId, $users['id'] = 1)) {

            return true;
        }else{echo "You are not admin!";}

    }

然后我想,也许这更容易一些?

public function admin($id = null) {

            $this->User->id = $id;
                if ($id == 1) {
                    echo 'You are admin';
                }
                else {
                    throw new NotFoundException(__('You are not admin !'));
                }
        }

但它不起作用,我如何让这个用户 ID 进入这个 if。第二个解决方案只抛出这个错误,但我不想要它,如果用户 ID 为 1,我想要访问。

这是用户图片

感谢您提供任何线索或解决方案。

【问题讨论】:

  • 您遇到什么错误?将 id 与会话数据匹配。你在使用身份验证组件吗?
  • 我收到此错误:else {throw new NotFoundException(__('You are not admin !'));}
  • 你在使用auth组件登录吗?
  • 你是说这个? :if ($this->Auth->login()) { return $this->redirect($this->Auth->redirectUrl()); }
  • 添加了答案。您可以检查已登录用户。

标签: php cakephp


【解决方案1】:

试试这个-

public function admin($id = null) {
   $currentUserId = $this->Auth->user(id);
   //$isAdmin       = $this->User->hasAny(
      //array('User.id' => $currentUserId)
   //);
   //if ($isAdmin) {
   if ($currentUserId == 1)
       echo 'You are admin';
   } else {
       throw new NotFoundException(__('You are not admin !'));
   }
}

对于已登录的用户。

【讨论】:

  • 我需要id为1的用户,atm所有登录的用户都可以打开这个文件,但我不想要这个。
  • 哦……忘了……更新答案。
  • 很好,它的工作,但得到一个通知:Notice (8): Use of undefined constant id - assumed 'id' [APP\Controller\UsersController.php, line 88]
【解决方案2】:

获取登录用户数据的推荐方法是通过 AuthComponent 本身:

// in any controller
$userId = $this->Auth->user('id');

请参阅CakePHP Book 的 Auth 部分中的Accessing the logged in user访问登录用户。

【讨论】:

  • 是的,我有这个,如果用户登录,那么他可以打开这个。但我想,只有 id 为 1 的用户才能打开这个
【解决方案3】:

更好的选择:-

为管理员使用角色 ID,这将帮助您处理多个管理员。并向管理员提供否定 ID:- 比如在用户表中添加role_id 列并为-1 赋值

public function admin($id = null) {
   $role = $this->Session->read('Auth.User.role_id');
   if ($role < 0)
       echo 'You are admin';
   } else {
       throw new NotFoundException(__('You are not admin !'));
   }
}

【讨论】:

    【解决方案4】:

    所以你想检查用户 ID 是否为 1 ??

    //Fetch the user informations from database and stock it into $user
    
    if(is_admin($user[id])){
        echo 'You are admin';
    }
    else{
        echo 'You are not admin';
    }
    
    function is_admin($id = NULL){
        if($id == 1){
            return true;
        }
        else{
            return false;
        }
    }
    

    如果我理解错了告诉我

    编辑: 根据我们的聊天记录,试试这个:

    //Fetch the ID of the user and stock it in $user_id
    
    if($user_id == 1){
        echo 'You are admin';
    }
    else{
        echo 'You are not admin';
    }
    

    【讨论】:

    • 你是如何获得$user的?
    • 通过从数据库中获取它。我不放它的细节,因为这不是主题。他想知道如何验证用户是否是管理员,而不是如何从数据库中获取数据...imo...
    • 它不工作,出现错误:Error: Call to undefined function is_admin()
    • 您是否将所有这些行放在同一个文件中?如果没有,您是否将文件包含在函数中?
    • 新编辑,新错误:Error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION)
    猜你喜欢
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 2016-09-15
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    相关资源
    最近更新 更多