【问题标题】:how to prevent other users to edit my profile in cakephp3如何防止其他用户在 cakephp3 中编辑我的个人资料
【发布时间】:2017-08-16 03:39:39
【问题描述】:

我有一个使用 cakephp3 的简单程序,当我尝试将其直接放入浏览器时:

http://localhost/sample/users/edit/82

它直接进入登录页面。然后登录后,即使该配置文件不是当前用户登录,我的代码仍然可以编辑配置文件。

下面是我的编辑代码

public function edit($id = null)
{
    $user = $this->Users->get($id, [
        'contain' => []
    ]);
    if ($this->request->is(['patch', 'post', 'put'])) {
        $user = $this->Users->patchEntity($user, $this->request->data);


                if ($this->Users->save($user)) {
                    $this->Flash->success(__('The user has been saved.'));
                    return $this->redirect(['action' => 'index']);
                } else {
                    $this->Flash->error(__('The user could not be saved. Please, try again.'));
                }

    }
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
}


edit.ctp

<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
 <ul class="side-nav">
    <li><?= $this->Form->postLink(
            __('Delete'),
            ['action' => 'delete', $user->id],
            ['confirm' => __('Are you sure you want to delete # {0}?', 
  $user->id)]
        )
    ?></li>
    <li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?>
  </li>
</ul>

    <div class="users form large-10 medium-9 columns">
     <?= $this->Form->create($user) ?>
   <fieldset>
      <legend><?= __('Edit User') ?></legend>
      <?php
          echo $this->Form->input('username');
          echo $this->Form->input('password');
      ?>
    </fieldset>
<?= $this->Form->button(__('Submit')) ?>
  <?= $this->Form->end() ?>
</div>

【问题讨论】:

    标签: php html css cakephp-3.0


    【解决方案1】:

    您必须检查现有用户是否正在尝试更新他/她的个人资料。你可以这样做。

    所有这些都在您的编辑方法之上

    public function edit($id = null)
    {
      $logged_user_id=$this->Auth->user('id');
    
      if($logged_user_id==$id){
      $user = $this->Users->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->data);
    
    
                    if ($this->Users->save($user)) {
                        $this->Flash->success(__('The user has been saved.'));
                        return $this->redirect(['action' => 'index']);
                    } else {
                        $this->Flash->error(__('The user could not be saved. Please, try again.'));
                    }
    
        }
        $this->set(compact('user'));
        $this->set('_serialize', ['user']);
       } else {
                        $this->Flash->error(__('You are not allowed to do this.'));
         }
    }
    

    【讨论】:

    • @bikash.biz 但添加该代码后我无法编辑自己的个人资料?有什么要补充的吗?即使我正在编辑我的 id,它也总是“你不能这样做。”
    • @JeromeManatad 你必须阅读你的会话以了解当前的使用 ID。此变量 ($logged_user_id) 存储当前记录的用户 ID。我不确定您的表结构,所以这是我的猜测。
    • @JeromeManatad 仍然如果您做不到,请发布您的会话数组。您在编辑操作/方法的顶部编写了下面的代码,它将打印一个数组,发布该数组。 pr($this->request->session()->read());die;
    • @bikash.bilz 鉴于用于身份验证的存储是可配置的,您最好使用$this-&gt;Auth-&gt;user() 而不是直接读取会话。另外,请尝试更轻松地格式化您的代码,这在涉及控制结构时尤其重要 - 谢谢!
    • 注意(8):未定义变量:用户 [APP/Template\Users\edit.ctp,第 6 行] 注意(8):试图获取非对象的属性 [APP/Template\Users \edit.ctp,第 6 行] 注意 (8):未定义变量:用户 [APP/Template\Users\edit.ctp,第 7 行] 注意 (8):尝试获取非对象的属性 [APP/Template\Users \edit.ctp, line 7] 我在上面的视图中添加了编辑代码
    【解决方案2】:

    就我而言,就像 ndm 所说,我不使用会话,这就是我所做的(希望它有所帮助):

    public function edit($id = null)
    {
    
        if(!is_null($this->Auth->user())): // if the user is logged
          if(is_null($id)) { $id = $this->Auth->user('id'); }
          if($this->Auth->user()['group_id']<>1): // in my case group 1 is for the administrator group, i let them edit profile
            $id = $this->Auth->user('id'); // in this case, if the user is not an administrator, id will always be his "user id"
          endif;      
        endif;
    
        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));
    
                return $this->redirect(['action' => 'edit', $id]);
            }
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
    
        $this->set(compact('user'));
        $this->set('_serialize', ['user']);
    }
    

    【讨论】:

    • 在你的情况下?他们将是用户表中的 group_id 字段吗?
    • 是的,“groups”表是针对角色的,所以 group_id 告诉我用户属于哪个角色
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2020-11-27
    • 1970-01-01
    • 2012-03-16
    • 2020-10-29
    • 2017-11-10
    • 2015-11-23
    相关资源
    最近更新 更多