Skip to content

Commit 19024f9

Browse files
authored
Merge pull request #27 from ProspectOne/449-login-error-on-localhost-http-adminperfopsint-login-if-not-exist-any-user-in-db
449 login error on localhost http adminperfopsint login if not exist any user in db
2 parents 3220a8c + 8ff6378 commit 19024f9

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;
@@ -167,6 +168,7 @@
167168
'header_name' => "xxx-user-module-auth",
168169
],
169170
'userEntity' => User::class,
171+
'roleEntity' => Role::class,
170172
'sessionsEnabled' => true,
171173
],
172174
'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 Zend\Crypt\Password\Bcrypt;
77
use Zend\Math\Rand;
@@ -13,7 +13,7 @@
1313
*/
1414
class UserManager
1515
{
16-
const ADMIN_ROLE_ID = 2;
16+
const ADMIN_ROLE_ID = 1;
1717
const ADMIN_EMAIL = 'admin@example.com';
1818
const ADMIN_NAME = 'Admin';
1919
const ADMIN_PASSWORD = 'Secur1ty';
@@ -24,6 +24,11 @@ class UserManager
2424
*/
2525
public $userEntityClassName;
2626

27+
/**
28+
* @var string
29+
*/
30+
public $roleEntityClassName;
31+
2732
/**
2833
* Doctrine entity manager.
2934
* @var EntityManager
@@ -51,17 +56,27 @@ public function getBcrypt(): Bcrypt
5156
return $this->bcrypt;
5257
}
5358

59+
/**
60+
* @return string
61+
*/
62+
public function getRoleEntityClassName()
63+
{
64+
return $this->roleEntityClassName;
65+
}
66+
5467
/**
5568
* UserManager constructor.
5669
* @param EntityManager $entityManager
5770
* @param Bcrypt $bcrypt
5871
* @param string $userEntityClassName
72+
* @param string $roleEntityClassName
5973
*/
60-
public function __construct(EntityManager $entityManager, Bcrypt $bcrypt, $userEntityClassName)
74+
public function __construct(EntityManager $entityManager, Bcrypt $bcrypt, $userEntityClassName, $roleEntityClassName)
6175
{
6276
$this->entityManager = $entityManager;
6377
$this->bcrypt = $bcrypt;
6478
$this->userEntityClassName = $userEntityClassName;
79+
$this->roleEntityClassName = $roleEntityClassName;
6580
}
6681

6782
/**
@@ -84,8 +99,8 @@ public function addUser($data)
8499
$user->setFullName($data['full_name']);
85100

86101
// Get role object based on role Id from form
87-
/** @var Role $role */
88-
$role = $this->entityManager->find(Role::class, $data['role']);
102+
/** @var RoleInterface $role */
103+
$role = $this->entityManager->find($this->getRoleEntityClassName(), $data['role']);
89104
// Set role to user
90105
$user->addRole($role);
91106

@@ -130,8 +145,8 @@ public function updateUser(UserInterface $user, $data)
130145
$user->setStatus($data['status']);
131146

132147
// Get role object based on role Id from form
133-
/** @var Role $role */
134-
$role = $this->entityManager->find(Role::class, $data['role']);
148+
/** @var RoleInterface $role */
149+
$role = $this->entityManager->find($this->getRoleEntityClassName(), $data['role']);
135150
// Set role to user
136151
$user->addRole($role);
137152

@@ -152,14 +167,14 @@ public function createAdminUserIfNotExists()
152167
/** @var UserInterface $user */
153168
$user = new $this->userEntityClassName();
154169
$user->setEmail(self::ADMIN_EMAIL);
155-
$user->setFullName(self::ADMIN_NAME);
170+
$user->setFullName(static::ADMIN_NAME);
156171
$passwordHash = $this->bcrypt->create(self::ADMIN_PASSWORD);
157172
$user->setPassword($passwordHash);
158173
$user->setStatus($user->getStatusActive());
159174
$user->setDateCreated(date('Y-m-d H:i:s'));
160175
// Get role object based on role Id from form
161-
/** @var Role $role */
162-
$role = $this->entityManager->find(Role::class, self::ADMIN_ROLE_ID);
176+
/** @var RoleInterface $role */
177+
$role = $this->entityManager->find($this->getRoleEntityClassName(), self::ADMIN_ROLE_ID);
163178
// Set role to user
164179
$user->addRole($role);
165180
$this->entityManager->persist($user);

0 commit comments

Comments
 (0)