Skip to content

Commit bf6b166

Browse files
Merge pull request #4820 from magento-qwerty/MAGETWO-96975
[qwerty] MAGETWO-96975: Remove __sleep and __wakeup from code
2 parents c6cf182 + 726b4c5 commit bf6b166

File tree

47 files changed

+707
-753
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+707
-753
lines changed

app/code/Magento/Authorization/Model/Role.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public function __construct( //phpcs:ignore Generic.CodeAnalysis.UselessOverridi
5252

5353
/**
5454
* @inheritDoc
55+
*
56+
* @SuppressWarnings(PHPMD.SerializationAware)
57+
* @deprecated Do not use PHP serialization.
5558
*/
5659
public function __sleep()
5760
{
@@ -61,6 +64,9 @@ public function __sleep()
6164

6265
/**
6366
* @inheritDoc
67+
*
68+
* @SuppressWarnings(PHPMD.SerializationAware)
69+
* @deprecated Do not use PHP serialization.
6470
*/
6571
public function __wakeup()
6672
{

app/code/Magento/Backend/Model/Auth/Session.php

Lines changed: 198 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
*/
66
namespace Magento\Backend\Model\Auth;
77

8+
use Magento\Framework\Acl;
9+
use Magento\Framework\AclFactory;
10+
use Magento\Framework\App\ObjectManager;
811
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
912
use Magento\Framework\Stdlib\CookieManagerInterface;
13+
use Magento\Backend\Spi\SessionUserHydratorInterface;
14+
use Magento\Backend\Spi\SessionAclHydratorInterface;
15+
use Magento\User\Model\User;
16+
use Magento\User\Model\UserFactory;
1017

1118
/**
1219
* Backend Auth session model
1320
*
1421
* @api
15-
* @method \Magento\User\Model\User|null getUser()
16-
* @method \Magento\Backend\Model\Auth\Session setUser(\Magento\User\Model\User $value)
17-
* @method \Magento\Framework\Acl|null getAcl()
18-
* @method \Magento\Backend\Model\Auth\Session setAcl(\Magento\Framework\Acl $value)
1922
* @method int getUpdatedAt()
2023
* @method \Magento\Backend\Model\Auth\Session setUpdatedAt(int $value)
2124
*
@@ -56,6 +59,36 @@ class Session extends \Magento\Framework\Session\SessionManager implements \Mage
5659
*/
5760
protected $_config;
5861

62+
/**
63+
* @var SessionUserHydratorInterface
64+
*/
65+
private $userHydrator;
66+
67+
/**
68+
* @var SessionAclHydratorInterface
69+
*/
70+
private $aclHydrator;
71+
72+
/**
73+
* @var UserFactory
74+
*/
75+
private $userFactory;
76+
77+
/**
78+
* @var AclFactory
79+
*/
80+
private $aclFactory;
81+
82+
/**
83+
* @var User|null
84+
*/
85+
private $user;
86+
87+
/**
88+
* @var Acl|null
89+
*/
90+
private $acl;
91+
5992
/**
6093
* @param \Magento\Framework\App\Request\Http $request
6194
* @param \Magento\Framework\Session\SidResolverInterface $sidResolver
@@ -70,6 +103,10 @@ class Session extends \Magento\Framework\Session\SessionManager implements \Mage
70103
* @param \Magento\Backend\Model\UrlInterface $backendUrl
71104
* @param \Magento\Backend\App\ConfigInterface $config
72105
* @throws \Magento\Framework\Exception\SessionException
106+
* @param SessionUserHydratorInterface|null $userHydrator
107+
* @param SessionAclHydratorInterface|null $aclHydrator
108+
* @param UserFactory|null $userFactory
109+
* @param AclFactory|null $aclFactory
73110
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
74111
*/
75112
public function __construct(
@@ -84,11 +121,19 @@ public function __construct(
84121
\Magento\Framework\App\State $appState,
85122
\Magento\Framework\Acl\Builder $aclBuilder,
86123
\Magento\Backend\Model\UrlInterface $backendUrl,
87-
\Magento\Backend\App\ConfigInterface $config
124+
\Magento\Backend\App\ConfigInterface $config,
125+
?SessionUserHydratorInterface $userHydrator = null,
126+
?SessionAclHydratorInterface $aclHydrator = null,
127+
?UserFactory $userFactory = null,
128+
?AclFactory $aclFactory = null
88129
) {
89130
$this->_config = $config;
90131
$this->_aclBuilder = $aclBuilder;
91132
$this->_backendUrl = $backendUrl;
133+
$this->userHydrator = $userHydrator ?? ObjectManager::getInstance()->get(SessionUserHydratorInterface::class);
134+
$this->aclHydrator = $aclHydrator ?? ObjectManager::getInstance()->get(SessionAclHydratorInterface::class);
135+
$this->userFactory = $userFactory ?? ObjectManager::getInstance()->get(UserFactory::class);
136+
$this->aclFactory = $aclFactory ?? ObjectManager::getInstance()->get(AclFactory::class);
92137
parent::__construct(
93138
$request,
94139
$sidResolver,
@@ -232,6 +277,16 @@ public function processLogin()
232277
return $this;
233278
}
234279

280+
/**
281+
* @inheritDoc
282+
*/
283+
public function destroy(array $options = null)
284+
{
285+
$this->user = null;
286+
$this->acl = null;
287+
parent::destroy($options);
288+
}
289+
235290
/**
236291
* Process of configuring of current auth storage when logout was performed
237292
*
@@ -255,4 +310,142 @@ public function isValidForPath($path)
255310
{
256311
return true;
257312
}
313+
314+
/**
315+
* Logged-in user.
316+
*
317+
* @return User|null
318+
*/
319+
public function getUser()
320+
{
321+
if (!$this->user) {
322+
$userData = $this->getUserData();
323+
if ($userData) {
324+
/** @var User $user */
325+
$user = $this->userFactory->create();
326+
$this->userHydrator->hydrate($user, $userData);
327+
$this->user = $user;
328+
} elseif ($user = parent::getUser()) {
329+
$this->setUser($user);
330+
}
331+
}
332+
333+
return $this->user;
334+
}
335+
336+
/**
337+
* Set logged-in user instance.
338+
*
339+
* @param User|null $user
340+
* @return Session
341+
*/
342+
public function setUser($user)
343+
{
344+
$this->setUserData(null);
345+
if ($user) {
346+
$this->setUserData($this->userHydrator->extract($user));
347+
}
348+
$this->user = $user;
349+
350+
return $this;
351+
}
352+
353+
/**
354+
* Is user logged in?
355+
*
356+
* @return bool
357+
*/
358+
public function hasUser()
359+
{
360+
return (bool)$this->getUser();
361+
}
362+
363+
/**
364+
* Remove logged-in user.
365+
*
366+
* @return Session
367+
*/
368+
public function unsUser()
369+
{
370+
$this->user = null;
371+
parent::unsUser();
372+
return $this->unsUserData();
373+
}
374+
375+
/**
376+
* Logged-in user's ACL data.
377+
*
378+
* @return Acl|null
379+
*/
380+
public function getAcl()
381+
{
382+
if (!$this->acl) {
383+
$aclData = $this->getUserAclData();
384+
if ($aclData) {
385+
/** @var Acl $acl */
386+
$acl = $this->aclFactory->create();
387+
$this->aclHydrator->hydrate($acl, $aclData);
388+
$this->acl = $acl;
389+
} elseif ($acl = parent::getAcl()) {
390+
$this->setAcl($acl);
391+
}
392+
}
393+
394+
return $this->acl;
395+
}
396+
397+
/**
398+
* Set logged-in user's ACL data instance.
399+
*
400+
* @param Acl|null $acl
401+
* @return Session
402+
*/
403+
public function setAcl($acl)
404+
{
405+
$this->setUserAclData(null);
406+
if ($acl) {
407+
$this->setUserAclData($this->aclHydrator->extract($acl));
408+
}
409+
$this->acl = $acl;
410+
411+
return $this;
412+
}
413+
414+
/**
415+
* Whether ACL data is present.
416+
*
417+
* @return bool
418+
*/
419+
public function hasAcl()
420+
{
421+
return (bool)$this->getAcl();
422+
}
423+
424+
/**
425+
* Remove ACL data.
426+
*
427+
* @return Session
428+
*/
429+
public function unsAcl()
430+
{
431+
$this->acl = null;
432+
parent::unsAcl();
433+
return $this->unsUserAclData();
434+
}
435+
436+
/**
437+
* @inheritDoc
438+
*/
439+
public function writeClose()
440+
{
441+
//Updating data in session in case these objects has been changed.
442+
if ($this->user) {
443+
$this->setUser($this->user);
444+
}
445+
if ($this->acl) {
446+
$this->setAcl($this->acl);
447+
}
448+
449+
parent::writeClose();
450+
}
258451
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Backend\Model\Auth;
10+
11+
use Magento\Backend\Spi\SessionAclHydratorInterface;
12+
use Magento\Framework\Acl;
13+
14+
/**
15+
* @inheritDoc
16+
*/
17+
class SessionAclHydrator extends Acl implements SessionAclHydratorInterface
18+
{
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function extract(Acl $acl): array
23+
{
24+
return ['rules' => $acl->_rules, 'resources' => $acl->_resources, 'roles' => $acl->_roleRegistry];
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function hydrate(Acl $target, array $data): void
31+
{
32+
$target->_rules = $data['rules'];
33+
$target->_resources = $data['resources'];
34+
$target->_roleRegistry = $data['roles'];
35+
}
36+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Backend\Model\Auth;
10+
11+
use Magento\Backend\Spi\SessionUserHydratorInterface;
12+
use Magento\User\Model\User;
13+
use Magento\Authorization\Model\Role;
14+
use Magento\Authorization\Model\RoleFactory;
15+
16+
/**
17+
* @inheritDoc
18+
*/
19+
class SessionUserHydrator implements SessionUserHydratorInterface
20+
{
21+
/**
22+
* @var RoleFactory
23+
*/
24+
private $roleFactory;
25+
26+
/**
27+
* @param RoleFactory $roleFactory
28+
*/
29+
public function __construct(RoleFactory $roleFactory)
30+
{
31+
$this->roleFactory = $roleFactory;
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function extract(User $user): array
38+
{
39+
return ['data' => $user->getData(), 'role_data' => $user->getRole()->getData()];
40+
}
41+
42+
/**
43+
* @inheritDoc
44+
*/
45+
public function hydrate(User $target, array $data): void
46+
{
47+
$target->setData($data['data']);
48+
/** @var Role $role */
49+
$role = $this->roleFactory->create();
50+
$role->setData($data['role_data']);
51+
$target->setData('extracted_role', $role);
52+
$target->getRole();
53+
}
54+
}

0 commit comments

Comments
 (0)