-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/cli password reset #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
255d9d9
updates vanity links and 404
ljonesfl 08c8ad5
fixes 404 page.
ljonesfl 6818a25
adds user reset password command.
ljonesfl 495b3ac
updates
ljonesfl 5fee55e
updates
ljonesfl 1e2daa3
updates
ljonesfl 5e5dc98
updates
ljonesfl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,3 +36,7 @@ coverage* | |
| .env.*.local | ||
|
|
||
| var/ | ||
|
|
||
| # Runtime directories (logs, cache, temporary files) | ||
| storage/ | ||
| resources/storage/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
|
|
||
| system: | ||
| timezone: UTC | ||
| base_path: resources | ||
| base_path: . | ||
| routes_path: resources/config | ||
|
|
||
| logging: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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(); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 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() ); | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $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; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.