Page MenuHomeDevCentral

D26.id52.diff
No OneTemporary

D26.id52.diff

diff --git a/app/Console/Commands/AccountReset.php b/app/Console/Commands/AccountReset.php
new file mode 100644
--- /dev/null
+++ b/app/Console/Commands/AccountReset.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace AuthGrove\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Contracts\Auth\PasswordBroker;
+use Illuminate\Support\Facades\Password;
+
+
+use AuthGrove\User;
+use AuthGrove\Console\Services\AccountHelpers as AccountHelpers;
+
+class AccountReset extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'account:reset {user} {--subject=} {--format=human}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Gets a reset password link for an account.';
+
+ /**
+ * The user e-mail
+ *
+ * @var string
+ */
+ private $email;
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Gets e-mail subject from command line option or default localiztion
+ *
+ * @return string the e-mail subject
+ */
+ public function getEmailSubject () {
+ $subject = $this->option('subject');
+ if ($subject === null) {
+ return trans('emails.reset-password-subject');
+ }
+ return $subject;
+ }
+
+ /**
+ * Sends a reset passsword e-mail
+ *
+ * @return bool true if a mail has been sent, false if the user is invalid
+ */
+ public function sendResetMail () {
+ //Information we need for this mail
+ $subject = $this->getEmailSubject();
+ $credentials = [
+ 'email' => $this->email
+ ];
+
+ //Tries to send the mail
+ $response = Password::sendResetLink($credentials, function($m) use ($subject)
+ {
+ $m->subject($subject);
+ });
+
+ //Handles password broker response, returning true on success
+ switch ($response)
+ {
+ case PasswordBroker::RESET_LINK_SENT:
+ return true;
+
+ case PasswordBroker::INVALID_USER:
+ return false;
+
+ default:
+ throw new Exception("Unhandled password broker response: " . $response);
+ }
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ //Gets the information
+ $user = AccountHelpers::findUser($this->argument('user'));
+ if ($user === null) {
+ $this->error("User not found.");
+ return;
+ }
+
+ $this->email = $user->getInformation()['email'];
+
+ //Operation
+ $success = $this->sendResetMail();
+ if (!$success) {
+ $this->error("The user has been found, but the password broker considers this user is invalid.");
+ exit;
+ }
+
+ //Regular output
+ $format = $this->option('format');
+ switch ($format) {
+ case "human":
+ $this->info("A reset link mail has been sent to $this->email.");
+ break;
+
+ case "json":
+ echo json_encode([
+ "result" => "ok",
+ "email" => $this->email
+ ]), "\n";
+ break;
+
+ default:
+ $this->error("Unknown format: $format");
+ }
+ }
+}
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -13,6 +13,7 @@
protected $commands = [
'AuthGrove\Console\Commands\AccountInfo',
'AuthGrove\Console\Commands\AccountDump',
+ 'AuthGrove\Console\Commands\AccountReset',
'AuthGrove\Console\Commands\Inspire',
];

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 17, 17:53 (21 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2249688
Default Alt Text
D26.id52.diff (3 KB)

Event Timeline