|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Auth\Factory\Form; |
|
11
|
|
|
|
|
12
|
|
|
use Interop\Container\ContainerInterface; |
|
13
|
|
|
use Zend\ServiceManager\FactoryInterface; |
|
14
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
15
|
|
|
use Zend\Form\Element\Select; |
|
16
|
|
|
use Auth\Entity\User; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class RoleSelectFactory |
|
20
|
|
|
* |
|
21
|
|
|
* Creates the select box of roles used temporary on the users profiles page. Box is removed from the |
|
22
|
|
|
* Users profiles Page now. But maybe we can reuse the code for the admin user |
|
23
|
|
|
* |
|
24
|
|
|
* @package Auth\Factory\Form |
|
25
|
|
|
*/ |
|
26
|
|
|
class RoleSelectFactory implements FactoryInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Create a Select Element |
|
30
|
|
|
* |
|
31
|
|
|
* @param ContainerInterface $container |
|
32
|
|
|
* @param string $requestedName |
|
33
|
|
|
* @param null|array $options |
|
34
|
|
|
* |
|
35
|
|
|
* @return Select |
|
36
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
|
37
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
38
|
|
|
* creating a service. |
|
39
|
|
|
* @throws ContainerException if any other error occurs |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$config = $container->get('Config'); |
|
44
|
|
|
$translator = $container->get('translator'); |
|
45
|
|
|
|
|
46
|
|
|
$publicRoles = isset($config['acl']['public_roles']) |
|
47
|
|
|
&& is_array($config['acl']['public_roles']) |
|
48
|
|
|
&& !empty($config['acl']['public_roles']) |
|
49
|
|
|
? $config['acl']['public_roles'] |
|
50
|
|
|
: (in_array(User::ROLE_USER, $config['acl']['roles']) |
|
51
|
|
|
|| array_key_exists('user', $config['acl']['roles']) |
|
52
|
|
|
? array(User::ROLE_USER) |
|
53
|
|
|
: array('none') |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$valueOptions = array(); |
|
57
|
|
|
foreach ($publicRoles as $role) { |
|
58
|
|
|
$valueOptions[$role] = $translator->translate($role); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$select = new Select('role'); |
|
62
|
|
|
$select->setValueOptions($valueOptions); |
|
63
|
|
|
|
|
64
|
|
|
return $select; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
|
68
|
|
|
{ |
|
69
|
|
|
return $this($serviceLocator->getServiceLocator(), Select::class); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: