Skip to content

Commit d57dece

Browse files
authored
Merge branch 'master' into 729-panel-performance
2 parents 497f3f4 + 64e4f5f commit d57dece

8 files changed

Lines changed: 112 additions & 67 deletions

File tree

config/module.config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace ProspectOne\UserModule;
33

4+
use ProspectOne\UserModule\Entity\Role;
45
use ProspectOne\UserModule\Entity\User;
56
use ProspectOne\UserModule\Factory\BcryptFactory;
67
use ProspectOne\UserModule\Factory\HydratorFactory;
@@ -170,6 +171,7 @@
170171
'header_name' => "xxx-user-module-auth",
171172
],
172173
'userEntity' => User::class,
174+
'roleEntity' => Role::class,
173175
'sessionsEnabled' => true,
174176
],
175177
'console' => [

src/Controller/Factory/AuthControllerFactory.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,40 @@
1414
*/
1515
class AuthControllerFactory implements FactoryInterface
1616
{
17+
/**
18+
* @param ContainerInterface $container
19+
* @param string $requestedName
20+
* @param array|null $options
21+
* @return object|AuthController
22+
*/
1723
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
1824
{
25+
$params = $this->getParams($container);
26+
27+
return $this->createService($params);
28+
}
29+
30+
/**
31+
* @param ContainerInterface $container
32+
* @return array
33+
* @throws \Psr\Container\ContainerExceptionInterface
34+
* @throws \Psr\Container\NotFoundExceptionInterface
35+
*/
36+
public function getParams(ContainerInterface $container)
37+
{
1938
$entityManager = $container->get('doctrine.entitymanager.orm_default');
2039
$authManager = $container->get(AuthManager::class);
2140
$authService = $container->get(AuthenticationService::class);
2241
$userManager = $container->get(UserManager::class);
23-
24-
return new AuthController($entityManager, $authManager, $authService, $userManager);
42+
return [$entityManager, $authManager, $authService, $userManager];
43+
}
44+
45+
/**
46+
* @param $params
47+
* @return AuthController
48+
*/
49+
public function createService($params)
50+
{
51+
return new AuthController(...$params);
2552
}
2653
}

src/Controller/UserController.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class UserController extends AbstractActionController
2929
*/
3030
public $userEntityClassName;
3131

32+
/**
33+
* @var string
34+
*/
35+
public $roleEntityClassName;
36+
3237
/**
3338
* Entity manager.
3439
* @var \Doctrine\ORM\EntityManager
@@ -83,6 +88,14 @@ public function getUserRoleId(): int
8388
return $this->userRoleId;
8489
}
8590

91+
/**
92+
* @return string
93+
*/
94+
public function getRoleEntityClassName(): string
95+
{
96+
return $this->roleEntityClassName;
97+
}
98+
8699
/**
87100
* Constructor.
88101
* @param EntityManager $entityManager
@@ -97,6 +110,7 @@ public function __construct(EntityManager $entityManager, UserManager $userManag
97110
$this->container = $container;
98111
$config = $this->container->get("Config");
99112
$this->userEntityClassName = $config['ProspectOne\UserModule']['userEntity'];
113+
$this->roleEntityClassName = $config['ProspectOne\UserModule']['roleEntity'];
100114
$this->userRoleId = $userRoleId;
101115
}
102116

@@ -417,7 +431,7 @@ public function setPasswordAction()
417431
*/
418432
public function getRolesSelector()
419433
{
420-
$roles = $this->entityManager->getRepository(Role::class)->findAll();
434+
$roles = $this->entityManager->getRepository($this->getRoleEntityClassName())->findAll();
421435
$hydrator = new ClassMethods();
422436
$rolesselector = [];
423437
foreach ($roles as $role) {

src/Entity/Role.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace ProspectOne\UserModule\Entity;
44

55
use Doctrine\ORM\Mapping as ORM;
6+
use ProspectOne\UserModule\Interfaces\RoleInterface;
67

78
/**
89
* @ORM\Entity
910
* @ORM\Table(name="user_role")
1011
*/
11-
class Role
12+
class Role implements RoleInterface
1213
{
1314
/**
1415
* @ORM\Id

src/Interfaces/RoleInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ProspectOne\UserModule\Interfaces;
4+
5+
/**
6+
* Interface RoleInterface
7+
* @package ProspectOne\UserModule\Interfaces
8+
*/
9+
interface RoleInterface
10+
{
11+
public function getRoleId();
12+
public function setRoleId($roleId);
13+
public function getRoleName();
14+
public function setRoleName($roleName);
15+
}

src/Service/Factory/AuthAdapterFactory.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/Service/Factory/UserManagerFactory.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,35 @@ class UserManagerFactory
2121
*/
2222
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
2323
{
24+
$params = $this->getParams($container);
25+
26+
return $this->createService($params, UserManager::class);
27+
}
28+
29+
/**
30+
* @param ContainerInterface $container
31+
* @return array
32+
*/
33+
protected function getParams(ContainerInterface $container)
34+
{
2435
$entityManager = $container->get('doctrine.entitymanager.orm_default');
2536
/** @var Bcrypt $bcrypt */
2637
$bcrypt = $container->get('ProspectOne\UserModule\Bcrypt');
2738

2839
$config = $container->get("Config");
2940
$userEntityClassName = $config['ProspectOne\UserModule']['userEntity'];
30-
31-
return new UserManager($entityManager, $bcrypt, $userEntityClassName);
41+
$roleEntityClassName = $config['ProspectOne\UserModule']['roleEntity'];
42+
43+
return [$entityManager, $bcrypt, $userEntityClassName, $roleEntityClassName];
44+
}
45+
46+
/**
47+
* @param array $params
48+
* @param string $requestedName
49+
* @return UserManager
50+
*/
51+
protected function createService(array $params, $requestedName)
52+
{
53+
return new $requestedName(...$params);
3254
}
3355
}

src/Service/UserManager.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace ProspectOne\UserModule\Service;
33

4-
use ProspectOne\UserModule\Entity\Role;
4+
use ProspectOne\UserModule\Interfaces\RoleInterface;
55
use ProspectOne\UserModule\Interfaces\UserInterface;
66
use ProspectOne\UserModule\Model\UserModel;
77
use Zend\Crypt\Password\Bcrypt;
@@ -14,7 +14,7 @@
1414
*/
1515
class UserManager
1616
{
17-
const ADMIN_ROLE_ID = 2;
17+
const ADMIN_ROLE_ID = 1;
1818
const ADMIN_EMAIL = 'admin@example.com';
1919
const ADMIN_NAME = 'Admin';
2020
const ADMIN_PASSWORD = 'Secur1ty';
@@ -25,6 +25,11 @@ class UserManager
2525
*/
2626
public $userEntityClassName;
2727

28+
/**
29+
* @var string
30+
*/
31+
public $roleEntityClassName;
32+
2833
/**
2934
* Doctrine entity manager.
3035
* @var EntityManager
@@ -64,20 +69,30 @@ public function getUserModel(): UserModel
6469
{
6570
return $this->userModel;
6671
}
72+
73+
/**
74+
* @return string
75+
*/
76+
public function getRoleEntityClassName()
77+
{
78+
return $this->roleEntityClassName;
79+
}
6780

6881
/**
6982
* UserManager constructor.
7083
* @param EntityManager $entityManager
7184
* @param Bcrypt $bcrypt
7285
* @param string $userEntityClassName
86+
* @param string $roleEntityClassName
7387
* @param UserModel $userModel
7488
*/
75-
public function __construct(EntityManager $entityManager, Bcrypt $bcrypt, $userEntityClassName, UserModel $userModel)
89+
public function __construct(EntityManager $entityManager, Bcrypt $bcrypt, $userEntityClassName, $roleEntityClassName, UserModel $userModel)
7690
{
7791
$this->entityManager = $entityManager;
7892
$this->bcrypt = $bcrypt;
7993
$this->userEntityClassName = $userEntityClassName;
8094
$this->userModel = $userModel;
95+
$this->roleEntityClassName = $roleEntityClassName;
8196
}
8297

8398
/**
@@ -100,8 +115,8 @@ public function addUser($data)
100115
$user->setFullName($data['full_name']);
101116

102117
// Get role object based on role Id from form
103-
/** @var Role $role */
104-
$role = $this->entityManager->find(Role::class, $data['role']);
118+
/** @var RoleInterface $role */
119+
$role = $this->entityManager->find($this->getRoleEntityClassName(), $data['role']);
105120
// Set role to user
106121
$user->addRole($role);
107122

@@ -146,8 +161,8 @@ public function updateUser(UserInterface $user, $data)
146161
$user->setStatus($data['status']);
147162

148163
// Get role object based on role Id from form
149-
/** @var Role $role */
150-
$role = $this->entityManager->find(Role::class, $data['role']);
164+
/** @var RoleInterface $role */
165+
$role = $this->entityManager->find($this->getRoleEntityClassName(), $data['role']);
151166
// Set role to user
152167
$user->addRole($role);
153168

@@ -168,14 +183,14 @@ public function createAdminUserIfNotExists()
168183
/** @var UserInterface $user */
169184
$user = new $this->userEntityClassName();
170185
$user->setEmail(self::ADMIN_EMAIL);
171-
$user->setFullName(self::ADMIN_NAME);
186+
$user->setFullName(static::ADMIN_NAME);
172187
$passwordHash = $this->bcrypt->create(self::ADMIN_PASSWORD);
173188
$user->setPassword($passwordHash);
174189
$user->setStatus($user->getStatusActive());
175190
$user->setDateCreated(date('Y-m-d H:i:s'));
176191
// Get role object based on role Id from form
177-
/** @var Role $role */
178-
$role = $this->entityManager->find(Role::class, self::ADMIN_ROLE_ID);
192+
/** @var RoleInterface $role */
193+
$role = $this->entityManager->find($this->getRoleEntityClassName(), self::ADMIN_ROLE_ID);
179194
// Set role to user
180195
$user->addRole($role);
181196
$this->entityManager->persist($user);

0 commit comments

Comments
 (0)