-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResetPasswordCommand.php
More file actions
252 lines (214 loc) · 6.09 KB
/
ResetPasswordCommand.php
File metadata and controls
252 lines (214 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace Neuron\Cms\Cli\Commands\User;
use Neuron\Core\Registry\RegistryKeys;
use Neuron\Cli\Commands\Command;
use Neuron\Cms\Repositories\DatabaseUserRepository;
use Neuron\Cms\Auth\PasswordHasher;
use Neuron\Patterns\Registry;
/**
* Reset user password
*/
class ResetPasswordCommand extends Command
{
/**
* @inheritDoc
*/
public function getName(): string
{
return 'cms:user:reset-password';
}
/**
* @inheritDoc
*/
public function getDescription(): string
{
return 'Reset a user\'s password';
}
/**
* Configure the command
*/
public function configure(): void
{
$this->addOption( 'username', 'u', true, 'Username of the user' );
$this->addOption( 'email', 'e', true, 'Email of the user' );
}
/**
* Execute the command
*/
public function execute( array $parameters = [] ): int
{
$this->output->writeln( "\n╔═══════════════════════════════════════╗" );
$this->output->writeln( "║ Neuron CMS - Reset User Password ║" );
$this->output->writeln( "╚═══════════════════════════════════════╝\n" );
// Load database configuration
$repository = $this->getUserRepository();
if( !$repository )
{
return 1;
}
$hasher = new PasswordHasher();
// Load password policy from configuration
try
{
$settings = Registry::getInstance()->get( RegistryKeys::SETTINGS );
if( $settings )
{
// Read password policy from auth.passwords section
$minLength = $settings->get( 'auth', 'passwords', 'min_length' );
$requireUppercase = $settings->get( 'auth', 'passwords', 'require_uppercase' );
$requireLowercase = $settings->get( 'auth', 'passwords', 'require_lowercase' );
$requireNumbers = $settings->get( 'auth', 'passwords', 'require_numbers' );
$requireSpecialChars = $settings->get( 'auth', 'passwords', 'require_special_chars' );
// Configure hasher with policy
if( $minLength !== null )
{
$hasher->setMinLength( (int)$minLength );
}
if( $requireUppercase !== null )
{
$hasher->setRequireUppercase( (bool)$requireUppercase );
}
if( $requireLowercase !== null )
{
$hasher->setRequireLowercase( (bool)$requireLowercase );
}
if( $requireNumbers !== null )
{
$hasher->setRequireNumbers( (bool)$requireNumbers );
}
if( $requireSpecialChars !== null )
{
$hasher->setRequireSpecialChars( (bool)$requireSpecialChars );
}
}
}
catch( \Exception $e )
{
// Fall back to defaults on any exception
}
// Get username or email from options or prompt
$username = $this->input->getOption( 'username' );
$email = $this->input->getOption( 'email' );
// If neither provided, prompt for identifier
if( !$username && !$email )
{
$identifier = $this->prompt( "Enter username or email: " );
$identifier = trim( $identifier );
if( empty( $identifier ) )
{
$this->output->error( "Username or email is required!" );
return 1;
}
// Determine if it's an email or username
if( filter_var( $identifier, FILTER_VALIDATE_EMAIL ) )
{
$email = $identifier;
}
else
{
$username = $identifier;
}
}
// Find the user
$user = null;
if( $username )
{
$user = $repository->findByUsername( $username );
if( !$user )
{
$this->output->error( "User '$username' not found!" );
return 1;
}
}
elseif( $email )
{
$user = $repository->findByEmail( $email );
if( !$user )
{
$this->output->error( "User with email '$email' not found!" );
return 1;
}
}
// Display user info
$this->output->writeln( "User found:" );
$this->output->writeln( " ID: " . $user->getId() );
$this->output->writeln( " Username: " . $user->getUsername() );
$this->output->writeln( " Email: " . $user->getEmail() );
$this->output->writeln( " Role: " . $user->getRole() );
$this->output->writeln( "" );
// Confirm action
$confirm = $this->prompt( "Reset password for this user? (yes/no) [no]: " );
if( strtolower( trim( $confirm ) ) !== 'yes' )
{
$this->output->warning( "Password reset cancelled." );
return 0;
}
// Get new password
$password = $this->secret( "\nEnter new password: " );
// Validate password against configured policy
if( !$hasher->meetsRequirements( $password ) )
{
$this->output->error( "Password does not meet requirements:" );
foreach( $hasher->getValidationErrors( $password ) as $error )
{
$this->output->writeln( " - $error" );
}
$this->output->writeln( "" );
return 1;
}
// Confirm password
$confirmPassword = $this->secret( "Confirm new password: " );
if( $password !== $confirmPassword )
{
$this->output->error( "Passwords do not match!" );
return 1;
}
// Update password
try
{
$user->setPasswordHash( $hasher->hash( $password ) );
$user->setUpdatedAt( new \DateTimeImmutable() );
// Clear any lockout
$user->setFailedLoginAttempts( 0 );
$user->setLockedUntil( null );
$success = $repository->update( $user );
if( !$success )
{
$this->output->error( "Failed to update password in database" );
return 1;
}
$this->output->success( "Password reset successfully for user: " . $user->getUsername() );
$this->output->writeln( "" );
return 0;
}
catch( \Exception $e )
{
$this->output->error( "Error resetting password: " . $e->getMessage() );
return 1;
}
}
/**
* Get user repository
*
* Protected to allow mocking in tests
*/
protected function getUserRepository(): ?DatabaseUserRepository
{
try
{
$settings = Registry::getInstance()->get( RegistryKeys::SETTINGS );
if( !$settings )
{
$this->output->error( "Application not initialized: Settings not found in Registry" );
$this->output->writeln( "This is a configuration error - the application should load settings into the Registry" );
return null;
}
return new DatabaseUserRepository( $settings );
}
catch( \Exception $e )
{
$this->output->error( "Database connection failed: " . $e->getMessage() );
return null;
}
}
}