Skip to content

Commit

Permalink
$php supersake security:resetpassword email-or-id
Browse files Browse the repository at this point in the history
  • Loading branch information
axyr committed Apr 18, 2016
1 parent d2af9d6 commit ff4bf40
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
1 change: 0 additions & 1 deletion code/Commands/Security/CreateMemberCommand.php
@@ -1,6 +1,5 @@
<?php


use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

Expand Down
70 changes: 69 additions & 1 deletion code/Commands/Security/ResetPasswordCommand.php
@@ -1,5 +1,7 @@
<?php

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class ResetPasswordCommand extends SilverstripeCommand
{
Expand All @@ -15,6 +17,72 @@ class ResetPasswordCommand extends SilverstripeCommand

public function fire()
{
$this->warn('Not implemented yet!');
$member = $this->getMemberByEmailOrID();

if(!(bool)$member) {
$this->error('Member not found');
} else {
$link = $this->sendResetPasswordEmail($member);
$this->info('Email sent to '.$member->getName().'<'.$member->Email.'>');
$this->line($link);
}
}

/**
* @return Member|null
*/
protected function getMemberByEmailOrID()
{
$emailorid = $this->argument('emailorid');

$member = null;

if(Str::contains($emailorid, '@')) {
$member = Member::get()->where("Email = '".Convert::raw2sql($emailorid)."'")->first();
}else{
$member = Member::get()->byID($emailorid);
}

return $member;
}

/**
* Send the reset password email and return the generated link.
*
* @param Member $member
* @return string
*/
protected function sendResetPasswordEmail(Member $member)
{
// hack ?
global $_FILE_TO_URL_MAPPING;

if($_FILE_TO_URL_MAPPING[BASE_PATH]) {
$_SERVER['REQUEST_URI'] = $_FILE_TO_URL_MAPPING[BASE_PATH];
}

$token = $member->generateAutologinTokenAndStoreHash();
$link = Security::getPasswordResetLink($member, $token);

/* @var Member_ForgotPasswordEmail $email */
$email = Member_ForgotPasswordEmail::create();
$email->populateTemplate($member);
$email->populateTemplate(array(
'PasswordResetLink' => $link
));
$email->setTo($member->Email);
$email->send();

return $link;
}

/**
* @return array
*/
protected function getArguments()
{
return [
['emailorid', InputArgument::REQUIRED, 'The emailaddress or ID of the Member']
];
}
}
12 changes: 12 additions & 0 deletions docs/en/MemberOperations.md
@@ -1,5 +1,7 @@
#Member Operations

##Adding a new Member to the database

You can simply create new Member with:

```
Expand All @@ -15,3 +17,13 @@ The FirstName will be set with whatever you set before the @ sign.
--firstname / -f : Optional FirstName. Default to name@ part of the emailaddress.
--surname / -s : Optional Surname.
--lastname / -l : Alias for Surname.

##Sending a reset password email

You can send a password reset email with:

```
$php supersake security:resetpassword some@email.com
```

This will send an email with the reset link.

0 comments on commit ff4bf40

Please sign in to comment.