使用cakephp 3.4将URL链接从用户标识更改为用户名后,无法编辑我的配置文件 [英] Cannot edit my profile after changing the url link from user id to username using cakephp 3.4

查看:70
本文介绍了使用cakephp 3.4将URL链接从用户标识更改为用户名后,无法编辑我的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种方法在url http:// localhost / sample / users / profile / john 代替 http :// localhost / sample / users / view / 1

I have this method to display a user profile like this one in url http://localhost/sample/users/profile/john instead of http://localhost/sample/users/view/1

public function profile($username)
{
    $user = $this->Users->find()->where(['username' => $username])->first(); 
    $accountUsername  =  $user->username;
    $this->set('profileUserName', $accountUsername);
    $this->set('users', $user);
    $this->set('_serialize', ['user']);
}

当我尝试编辑个人资料时,它将始终转到您不是允许这样做。

When I try to edit my profile It will always go to "You are not allowed to do this."

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->getData());


        if ($this->Users->save($user)) {
            $this->Flash->success(__('User profile successfuly  updated.'));
            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.'));
    return $this->redirect(['action' => 'index']);
}
}

我尝试在编辑方法上添加

I tried to add this on edit method

$logged_user_id=$this->Auth->user('id');
$logged_user_name=$this->Auth->user('username');

  if(($logged_user_id==$id)&&($logged_user_name == $username)){
      $user = $this->Users->get($id, [
        'contain' => []
    ]);

profile.ctp

profile.ctp

<div class="paginator">
    <ul>       
        <li><?= $this->Html->link(__('Edit User'), ['action' => 'edit', $users->id]) ?> </li>
        <li><?= $this->Form->postLink(__('Delete User'), ['action' => 'delete', $users->id], ['confirm' => __('Are you sure you want to delete # {0}?', $users->id)]) ?> </li>
        <li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?> </li>

        <li><?= $this->Html->link(__('Logout'), ['action' => 'logout']) ?> </li>
    </ul>   
  </div>

也许是因为$ id的获取导致了问题?

Maybe because of the get by $id causing the problems?

推荐答案

public function beforeFilter(\Cake\Event\Event $event)
{
  $user = $this->request->session()->read('Auth.User');
  $this->set('user_id', $user['id']); 
}

只需编辑您的profile.ctp并将$ users-> id更改为$ user_id

just edit your profile.ctp and change $users->id to $user_id

<div class="paginator">
    <ul>       
        <li><?= $this->Html->link(__('Edit User'), ['action' => 'edit', $user_id]) ?> </li>
        <li><?= $this->Form->postLink(__('Delete User'), ['action' => 'delete', $users->id], ['confirm' => __('Are you sure you want to delete # {0}?', $users->id)]) ?> </li>
        <li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?> </li>

        <li><?= $this->Html->link(__('Logout'), ['action' => 'logout']) ?> </li>
    </ul>   
  </div>

您总是直接说您不被允许这样做。因此,在配置文件方法中

Explanation you always directly goes to "You are not allowed to do this." because of this in profile method

$user = $this->Users->find()->where(['username' => $username])->first();

由于在用户表的数据库中有重复的用户名,因此系统混淆了要编辑的配置文件并抛出错误不允许您执行此操作。找到具有相同用户名值的第一行数据后

the system is confused what profile to edit since you have duplicate username in the database for users table, so it throws and error "You are not allowed to do this." after finding the first row of data with the same "username value"

将此代码添加到UsersTable.php中,以防止重复的用户名

add this code to UsersTable.php to prevent duplicate username

$validator
        ->requirePresence('username')
        ->notBlank('username', 'A username is required')
        ->add('username', 'unique', [
                    'rule' => 'validateUnique',
                    'provider' => 'table',
                    'message' => 'Username is already used'
             ]);

这篇关于使用cakephp 3.4将URL链接从用户标识更改为用户名后,无法编辑我的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆