|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vivait\AuthBundle\Features\Context; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Behat\Context\BehatContext; |
|
6
|
|
|
use Behat\Gherkin\Node\TableNode; |
|
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
8
|
|
|
use Doctrine\ORM\EntityRepository; |
|
9
|
|
|
use Vivait\AuthBundle\Entity\Tenant; |
|
10
|
|
|
use Vivait\AuthBundle\Entity\User; |
|
11
|
|
|
use Vivait\AuthBundle\Entity\UserRepository; |
|
12
|
|
|
use Behat\Symfony2Extension\Context\KernelDictionary; |
|
13
|
|
|
use Behat\Behat\Context\Step; |
|
14
|
|
|
use Vivait\BehatAliceLoader\AliceContext; |
|
15
|
|
|
|
|
16
|
|
|
class AuthContext extends BehatContext { |
|
17
|
|
|
use KernelDictionary; |
|
18
|
|
|
|
|
19
|
|
|
protected $current_user; |
|
20
|
|
|
protected $users = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Gets current_user |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getCurrentUser() { |
|
27
|
|
|
return $this->current_user; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Gets users |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getUsers() { |
|
35
|
|
|
return $this->users; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Sets users |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $users |
|
42
|
|
|
* |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setUsers( $users ) { |
|
46
|
|
|
$this->users = $users; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return ObjectManager|object |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function getManager() |
|
55
|
|
|
{ |
|
56
|
|
|
$container = $this->getContainer(); |
|
57
|
|
|
$em = $container->get('doctrine')->getManager(); |
|
58
|
|
|
return $em; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $username |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getUserPassword($username) { |
|
|
|
|
|
|
65
|
|
|
if (!isset($this->users[$username]['password'])) { |
|
66
|
|
|
throw new \OutOfBoundsException('Invalid user '. $username); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->users[ $username ]['password']; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @Given /^I am authenticated as "([^"]*)"$/ |
|
74
|
|
|
* @Given /^I am authenticated as "([^"]*)" using "([^"]*)"$/ |
|
75
|
|
|
*/ |
|
76
|
|
|
public function iAmAuthenticatedAs($username, $password = null) { |
|
77
|
|
|
if ($password === null) { |
|
78
|
|
|
$password = $this->getUserPassword($username); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$container = $this->getContainer(); |
|
82
|
|
|
$em = $container->get('doctrine')->getManager(); |
|
83
|
|
|
$this->current_user = $em |
|
84
|
|
|
->getRepository('VivaitAuthBundle:User') |
|
85
|
|
|
->findOneBy([ |
|
86
|
|
|
'username' => $username |
|
87
|
|
|
]); |
|
88
|
|
|
|
|
89
|
|
|
return array( |
|
90
|
|
|
new Step\Given('I am on "/login"'), |
|
91
|
|
|
new Step\When('I fill in "_username" with "'. $username .'"'), |
|
92
|
|
|
new Step\When('I fill in "_password" with "'. $password .'"'), |
|
93
|
|
|
new Step\When('I press "Sign in"'), |
|
94
|
|
|
new Step\Then('I should not see "Login Failed"'), |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @Given /^there are the following users:$/ |
|
100
|
|
|
*/ |
|
101
|
|
|
public function thereAreTheFollowingUsers(TableNode $table) { |
|
102
|
|
|
foreach ($table->getHash() as $row) { |
|
103
|
|
|
$this->users[$row['username']] = $row; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/* @var $alice AliceContext */ |
|
107
|
|
|
$alice = $this->getMainContext()->getSubcontextByClassName('Vivait\BehatAliceLoader\AliceContext'); |
|
108
|
|
|
|
|
109
|
|
|
if ($alice) { |
|
110
|
|
|
/* @var $users User[] */ |
|
111
|
|
|
$users = $alice->thereAreTheFollowing('VivaitAuthBundle:User', $table); |
|
112
|
|
|
|
|
113
|
|
|
foreach ($users as $user) { |
|
114
|
|
|
$user->hashPassword($this->getContainer()->get('security.encoder_factory')); |
|
115
|
|
|
|
|
116
|
|
|
if (!$user->getTenants()->count()) { |
|
117
|
|
|
$tenant = $this->getManager()->getRepository('VivaitAuthBundle:Tenant')->findDefaultTenant(); |
|
118
|
|
|
$tenant->addUser( $user ); |
|
119
|
|
|
$this->getManager()->persist( $tenant ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->getManager()->persist( $user ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->getManager()->flush(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return UserRepository |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function getUserRepository() |
|
133
|
|
|
{ |
|
134
|
|
|
$em = $this->getManager(); |
|
135
|
|
|
return $em->getRepository('VivaitAuthBundle:User'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return EntityRepository |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function getGroupRepository() |
|
142
|
|
|
{ |
|
143
|
|
|
$em = $this->getManager(); |
|
144
|
|
|
return $em->getRepository('VivaitAuthBundle:Group'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return EntityRepository |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function getTenantRepository() |
|
151
|
|
|
{ |
|
152
|
|
|
$em = $this->getManager(); |
|
153
|
|
|
return $em->getRepository('VivaitAuthBundle:Tenant'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.