doubinei1457 2015-01-06 08:48
浏览 74
已采纳

只有一个用户可以打开admin.ctp文件

I want to do a page, and this page can open only one user. I want to do something like this: If user id = 1 then its opening else its throwing error.

I already tried this :

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!";}

    }

Then I thinked, maybe this is a little bit easier ?

public function admin($id = null) {

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

But its not working, How I get this user id in to this if. This second solution throwing only this error, but I dont want it, I want access if user id is 1.

Here is users picture

enter image description here

Thank you for any clue or solution.

  • 写回答

4条回答 默认 最新

  • dssu33392 2015-01-06 09:13
    关注

    Try this-

    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 !'));
       }
    }
    

    For logged in users.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动