“cakephp/authentication:^3.0” How to modify user identity data for authentication?

  1. database.
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(255) NOT NULL,
    password VARCHAR(255) NOT NULL,
    created DATETIME,
    modified DATETIME,
    is_use_two_step_auth int DEFAULT NULL
);

2.After successfully logging in with email and password.

You can get the authenticated user identity data using the authentication component.

$user = $this->Authentication->getIdentity();
/** 
 *  [
 * id => '1',
 * email => 'lis@xxx.com',
 * password => 'xxxxxxxxx',
 * created => '2014-09-14 15:00:00',
 * modified => '2014-09-14 15:00:00',
 * is_use_two_step_auth  => '0',
 * ]
 */

// Modify user information 
// This sentence will print a warning.(debug.log: notice: Creation of dynamic property Authentication\Identity::$loginMethod is deprecated
)
$user->loginMethod = 1;
$user->blockId = 1;
$this->Authentication->setIdentity($user);

3.How to modify user identity data for authentication?
Next time, we will gather user intelligence

$user = $this->Authentication->getIdentity();
/** 
 *  [
 * id => '1',
 * email => 'lis@xxx.com',
 * password => 'xxxxxxxxx',
 * created => '2014-09-14 15:00:00',
 * modified => '2014-09-14 15:00:00',
 * is_use_two_step_auth  => '0',
 * blockId => 1,
 * loginMethod => 1
 * ]
 */

// The business will also modify the user ->blockId
//  (It seems that this modification is not recommended)
$user->blockId = 1;
$this->Authentication->setIdentity($user);

Is there a question here?

Thank you for your valuable time

$user = $this->Authentication->getIdentity();
$user->blockId = 1;//*********************** This line of code

debug.log

$user->blockId = 1;
Is it correct to print the log below?

2024-09-18 10:51:29 notice: Creation of dynamic property Authentication\Identity::$blockId  is deprecated
Request URL: /m-block/set-admin-block
Referer URL: http://localhost:8765/
Trace:
App\Controller\MBlockController->setAdminBlock() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Controller\Controller.php, line 498
Cake\Controller\Controller->invokeAction() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Controller\ControllerFactory.php, line 139
Cake\Controller\ControllerFactory->handle() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Controller\ControllerFactory.php, line 114
Cake\Controller\ControllerFactory->invoke() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\BaseApplication.php, line 332
Cake\Http\BaseApplication->handle() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\Runner.php, line 86
Cake\Http\Runner->handle() D:\A\cakephp-demo\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php, line 107
Authentication\Middleware\AuthenticationMiddleware->process() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\Runner.php, line 82
Cake\Http\Runner->handle() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\Middleware\BodyParserMiddleware.php, line 162
Cake\Http\Middleware\BodyParserMiddleware->process() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\Runner.php, line 82
Cake\Http\Runner->handle() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php, line 118
Cake\Routing\Middleware\RoutingMiddleware->process() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\Runner.php, line 82
Cake\Http\Runner->handle() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php, line 69
Cake\Routing\Middleware\AssetMiddleware->process() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\Runner.php, line 82
Cake\Http\Runner->handle() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php, line 115
Cake\Error\Middleware\ErrorHandlerMiddleware->process() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\Runner.php, line 82
Cake\Http\Runner->handle() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\Runner.php, line 60
Cake\Http\Runner->run() D:\A\cakephp-demo\vendor\cakephp\cakephp\src\Http\Server.php, line 103
Cake\Http\Server->run() D:\A\cakephp-demo\webroot\index.php, line 38
[main]

See the very first section of PHP: Deprecated Features - Manual for why this is happening. This is a feature of PHP8.2. That document tells you how to get around it, but since the Identity class is in third-party code, you don’t have access to change the code. Looking at it, there’s functions for getting information from it, but setting it is expressly not allowed (i.e. it’s an immutable class).

Looks what what you could do would instead be (untested, but should at least be very close)

$user = new \Authentication\Identity(
    array_merge($user->getOriginalData, [
        'loginMethod' => 1,
        'blockId' => 1,
    ]),
    $user->getConfig()
);

This will create an entirely new identity object, keeping existing data and configuration but adding or replacing those two values.

1 Like

Thank you for your answer.

Upgrade from version 3.x to version 5.x

// cakephp3.x(AuthComponent)
$loginUser = $this->Auth->user();
$loginUser['block_id '] = xxx;
$this->Auth->setUser($loginUser);

// cakephp5.x(cakephp/authentication:^3.0)
$loginUser = $this->Authentication->getIdentity();
$loginUser['block_id '] = xxx;
$this->Authentication->setIdentity($loginUser);

Can the current user switch block_id in version 3. x, but is this the case when implementing this feature in version 5. x?

Is this working for you now? Or still giving problems? It doesn’t look like what I had suggested for you, so not clear whether you got a different variant to work, or you didn’t know how to use what I said.

1 Like

Thank you for your response. Sorry, I am unable to input images and my English is not very good.
Below, I have organized my needs and questions.

Upgrade old project 3. x to 5. x.

There is a business in the old project that is as follows:

1.Account and password login successful.

login page

username: 11101
password: xxxxx
button => Login

index page

Welcome 11101--------------------------button => Logout
==========================================
select => blockList //** You can switch blocks here
change => changeBlockId() //** Switching events

cakephp3.x changeBlockId()

public function changeBlockId() {

  $blockId = $this->request->getData('block_id ');

  $loginUser = $this->Auth->user();
  $loginUser['block_id '] = $blockId;
  $this->Auth->setUser($loginUser);

}

Other methods, such as user lists
cakephp3.x

UsersController.php
public function index() {
    // You can obtain the "Block" for switching to the homepage here
    $block_id = $this->Auth->user('block_id ')
    // ...Other code logic
}

"users/index "
You can obtain the “Block” for switching to the homepage in the middle
Cake PHP5. x wants to achieve the same functionality

Because this project is upgrading from 3. x to 5. x, I want to retain the previous business

At present, this is also written incorrectly and cannot achieve the desired effect
cakephp5.x changeBlockId()

public function changeBlockId() {

  $blockId = $this->request->getData('block_id ');

  $loginUser = $this->Authentication->getIdentity();
  $loginUser['block_id '] = $blockId;
  $this->Authentication->setIdentity($loginUser);

}

Try:

public function changeBlockId() {
    $blockId = $this->request->getData('block_id ');

    $loginUser = $this->Authentication->getIdentity();
    $loginUser = new \Authentication\Identity(
        array_merge($loginUser->getOriginalData, [
            'blockId' => $blockId,
        ]),
        $loginUser->getConfig()
    );

    $this->Authentication->setIdentity($loginUser);
}
1 Like

Can achieve the desired effect, thank you very much.
感谢大佬。