77
88use Magento \Authorization \Model \Acl \Role \Group as RoleGroup ;
99use Magento \Authorization \Model \Acl \Role \User as RoleUser ;
10- use Magento \Framework \App \ResourceConnection ;
10+ use Magento \Framework \App \ObjectManager ;
11+ use Magento \Framework \Serialize \Serializer \Json ;
1112
1213class Role implements \Magento \Framework \Acl \LoaderInterface
1314{
15+ /**
16+ * Cache key for ACL roles cache
17+ */
18+ const ACL_ROLES_CACHE_KEY = 'authorization_role_cached_data ' ;
19+
1420 /**
1521 * @var \Magento\Framework\App\ResourceConnection
1622 */
@@ -26,19 +32,45 @@ class Role implements \Magento\Framework\Acl\LoaderInterface
2632 */
2733 protected $ _roleFactory ;
2834
35+ /**
36+ * @var \Magento\Framework\Acl\Data\CacheInterface
37+ */
38+ private $ aclDataCache ;
39+
40+ /**
41+ * @var Json
42+ */
43+ private $ serializer ;
44+
45+ /**
46+ * @var string
47+ */
48+ private $ cacheKey ;
49+
2950 /**
3051 * @param \Magento\Authorization\Model\Acl\Role\GroupFactory $groupFactory
3152 * @param \Magento\Authorization\Model\Acl\Role\UserFactory $roleFactory
3253 * @param \Magento\Framework\App\ResourceConnection $resource
54+ * @param \Magento\Framework\Acl\Data\CacheInterface $aclDataCache
55+ * @param Json $serializer
56+ * @param string $cacheKey
3357 */
3458 public function __construct (
3559 \Magento \Authorization \Model \Acl \Role \GroupFactory $ groupFactory ,
3660 \Magento \Authorization \Model \Acl \Role \UserFactory $ roleFactory ,
37- \Magento \Framework \App \ResourceConnection $ resource
61+ \Magento \Framework \App \ResourceConnection $ resource ,
62+ \Magento \Framework \Acl \Data \CacheInterface $ aclDataCache = null ,
63+ Json $ serializer = null ,
64+ $ cacheKey = self ::ACL_ROLES_CACHE_KEY
3865 ) {
3966 $ this ->_resource = $ resource ;
4067 $ this ->_groupFactory = $ groupFactory ;
4168 $ this ->_roleFactory = $ roleFactory ;
69+ $ this ->aclDataCache = $ aclDataCache ?: ObjectManager::getInstance ()->get (
70+ \Magento \Framework \Acl \Data \CacheInterface::class
71+ );
72+ $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
73+ $ this ->cacheKey = $ cacheKey ;
4274 }
4375
4476 /**
@@ -49,12 +81,7 @@ public function __construct(
4981 */
5082 public function populateAcl (\Magento \Framework \Acl $ acl )
5183 {
52- $ roleTableName = $ this ->_resource ->getTableName ('authorization_role ' );
53- $ connection = $ this ->_resource ->getConnection ();
54-
55- $ select = $ connection ->select ()->from ($ roleTableName )->order ('tree_level ' );
56-
57- foreach ($ connection ->fetchAll ($ select ) as $ role ) {
84+ foreach ($ this ->getRolesArray () as $ role ) {
5885 $ parent = $ role ['parent_id ' ] > 0 ? $ role ['parent_id ' ] : null ;
5986 switch ($ role ['role_type ' ]) {
6087 case RoleGroup::ROLE_TYPE :
@@ -71,4 +98,28 @@ public function populateAcl(\Magento\Framework\Acl $acl)
7198 }
7299 }
73100 }
101+
102+ /**
103+ * Get application ACL roles array
104+ *
105+ * @return array
106+ */
107+ private function getRolesArray ()
108+ {
109+ $ rolesCachedData = $ this ->aclDataCache ->load ($ this ->cacheKey );
110+ if ($ rolesCachedData ) {
111+ return $ this ->serializer ->unserialize ($ rolesCachedData );
112+ }
113+
114+ $ roleTableName = $ this ->_resource ->getTableName ('authorization_role ' );
115+ $ connection = $ this ->_resource ->getConnection ();
116+
117+ $ select = $ connection ->select ()
118+ ->from ($ roleTableName )
119+ ->order ('tree_level ' );
120+
121+ $ rolesArray = $ connection ->fetchAll ($ select );
122+ $ this ->aclDataCache ->save ($ this ->serializer ->serialize ($ rolesArray ), $ this ->cacheKey );
123+ return $ rolesArray ;
124+ }
74125}
0 commit comments