Completed
Pull Request — 1.x (#627)
by Dylan
08:40
created

User::getChangePasswordForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ZfcUser\Service;
4
5
use Zend\Authentication\AuthenticationService;
6
use Zend\Form\Form;
7
use Zend\Hydrator\HydratorInterface;
8
use Zend\ServiceManager\ServiceManager;
9
use Zend\Crypt\Password\Bcrypt;
10
use Zend\Stdlib\Hydrator;
11
use ZfcBase\EventManager\EventProvider;
12
use ZfcUser\Mapper\UserInterface as UserMapperInterface;
13
use ZfcUser\Options\UserServiceOptionsInterface;
14
15
class User extends EventProvider
16
{
17
18
    /**
19
     * @var UserMapperInterface
20
     */
21
    protected $userMapper;
22
23
    /**
24
     * @var AuthenticationService
25
     */
26
    protected $authService;
27
28
    /**
29
     * @var Form
30
     */
31
    protected $loginForm;
32
33
    /**
34
     * @var Form
35
     */
36
    protected $registerForm;
37
38
    /**
39
     * @var Form
40
     */
41
    protected $changePasswordForm;
42
43
    /**
44
     * @var ServiceManager
45
     */
46
    protected $serviceManager;
47
48
    /**
49
     * @var UserServiceOptionsInterface
50
     */
51
    protected $options;
52
53
    /**
54
     * @var Hydrator\ClassMethods
55
     */
56
    protected $formHydrator;
57
58
    public function __construct(
59
        UserMapperInterface $userMapper,
60
        AuthenticationService $authService,
61
        Form $registerForm,
62
        UserServiceOptionsInterface $options,
63
        HydratorInterface $formHydrator
64
    ) {
65
        $this->userMapper = $userMapper;
66
        $this->authService = $authService;
67
        $this->registerForm = $registerForm;
68
        $this->options = $options;
69
        $this->formHydrator = $formHydrator;
0 ignored issues
show
Documentation Bug introduced by
$formHydrator is of type object<Zend\Hydrator\HydratorInterface>, but the property $formHydrator was declared to be of type object<Zend\Stdlib\Hydrator\ClassMethods>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
70
    }
71
72
    /**
73
     * createFromForm
74
     *
75
     * @param array $data
76
     * @return \ZfcUser\Entity\UserInterface
77
     * @throws Exception\InvalidArgumentException
78
     */
79
    public function register(array $data)
80
    {
81
        $class = $this->getOptions()->getUserEntityClass();
82
        $user  = new $class;
83
        $form  = $this->getRegisterForm();
84
        $form->setHydrator($this->getFormHydrator());
85
        $form->bind($user);
86
        $form->setData($data);
87
        if (!$form->isValid()) {
88
            return false;
89
        }
90
91
        $user = $form->getData();
92
        /* @var $user \ZfcUser\Entity\UserInterface */
93
94
        $bcrypt = new Bcrypt;
95
        $bcrypt->setCost($this->getOptions()->getPasswordCost());
96
        $user->setPassword($bcrypt->create($user->getPassword()));
97
98
        if ($this->getOptions()->getEnableUsername()) {
99
            $user->setUsername($data['username']);
100
        }
101
        if ($this->getOptions()->getEnableDisplayName()) {
102
            $user->setDisplayName($data['display_name']);
103
        }
104
105
        // If user state is enabled, set the default state value
106
        if ($this->getOptions()->getEnableUserState()) {
107
            $user->setState($this->getOptions()->getDefaultUserState());
108
        }
109
        $this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $user, 'form' => $form));
110
        $this->getUserMapper()->insert($user);
111
        $this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $user, 'form' => $form));
112
        return $user;
113
    }
114
115
    /**
116
     * change the current users password
117
     *
118
     * @param array $data
119
     * @return boolean
120
     */
121
    public function changePassword(array $data)
122
    {
123
        $currentUser = $this->getAuthService()->getIdentity();
124
125
        $oldPass = $data['credential'];
126
        $newPass = $data['newCredential'];
127
128
        $bcrypt = new Bcrypt;
129
        $bcrypt->setCost($this->getOptions()->getPasswordCost());
130
131
        if (!$bcrypt->verify($oldPass, $currentUser->getPassword())) {
132
            return false;
133
        }
134
135
        $pass = $bcrypt->create($newPass);
136
        $currentUser->setPassword($pass);
137
138
        $this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $currentUser, 'data' => $data));
139
        $this->getUserMapper()->update($currentUser);
140
        $this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $currentUser, 'data' => $data));
141
142
        return true;
143
    }
144
145
    public function changeEmail(array $data)
146
    {
147
        $currentUser = $this->getAuthService()->getIdentity();
148
149
        $bcrypt = new Bcrypt;
150
        $bcrypt->setCost($this->getOptions()->getPasswordCost());
151
152
        if (!$bcrypt->verify($data['credential'], $currentUser->getPassword())) {
153
            return false;
154
        }
155
156
        $currentUser->setEmail($data['newIdentity']);
157
158
        $this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $currentUser, 'data' => $data));
159
        $this->getUserMapper()->update($currentUser);
160
        $this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $currentUser, 'data' => $data));
161
162
        return true;
163
    }
164
165
    /**
166
     * getUserMapper
167
     *
168
     * @return UserMapperInterface
169
     */
170
    public function getUserMapper()
171
    {
172
        return $this->userMapper;
173
    }
174
175
    /**
176
     * getAuthService
177
     *
178
     * @return AuthenticationService
179
     */
180
    public function getAuthService()
181
    {
182
        return $this->authService;
183
    }
184
185
    /**
186
     * @return Form
187
     */
188
    public function getRegisterForm()
189
    {
190
        return $this->registerForm;
191
    }
192
193
    /**
194
     * @return Form
195
     */
196
    public function getChangePasswordForm()
197
    {
198
        return $this->changePasswordForm;
199
    }
200
201
    /**
202
     * get service options
203
     *
204
     * @return UserServiceOptionsInterface
205
     */
206
    public function getOptions()
207
    {
208
        return $this->options;
209
    }
210
211
    /**
212
     * Return the Form Hydrator
213
     *
214
     * @return \Zend\Stdlib\Hydrator\ClassMethods
215
     */
216
    public function getFormHydrator()
217
    {
218
        return $this->formHydrator;
219
    }
220
}
221