Finish signup process, including state restoration

This commit is contained in:
Toby Zerner 2015-02-25 15:34:02 +10:30
parent 327719c5a3
commit 13a4ced63d
15 changed files with 62 additions and 29 deletions

View File

@ -28,6 +28,7 @@ export default Ember.Controller.extend(ModalController, {
return user.save().then(function(user) { return user.save().then(function(user) {
controller.set('welcomeUser', user); controller.set('welcomeUser', user);
controller.set('loading', false); controller.set('loading', false);
controller.send('saveState');
}, function(reason) { }, function(reason) {
controller.set('loading', false); controller.set('loading', false);
}); });

View File

@ -7,6 +7,13 @@ export default Ember.Route.extend(ApplicationRouteMixin, {
activate: function() { activate: function() {
if (!Ember.isEmpty(FLARUM_ALERT)) { if (!Ember.isEmpty(FLARUM_ALERT)) {
this.controllerFor('alerts').send('alert', AlertMessage.create(FLARUM_ALERT)); this.controllerFor('alerts').send('alert', AlertMessage.create(FLARUM_ALERT));
FLARUM_ALERT = null;
}
var restoreUrl = localStorage.getItem('restoreUrl');
if (restoreUrl && this.get('session.isAuthenticated')) {
this.transitionTo(restoreUrl);
localStorage.removeItem('restoreUrl');
} }
}, },
@ -42,6 +49,10 @@ export default Ember.Route.extend(ApplicationRouteMixin, {
sessionChanged: function() { sessionChanged: function() {
this.refresh(); this.refresh();
},
saveState: function() {
localStorage.setItem('restoreUrl', this.router.get('url'));
} }
} }
}); });

View File

@ -28,7 +28,7 @@
{{ui/loading-indicator classNameBindings=":modal-loading loading:active"}} {{ui/loading-indicator classNameBindings=":modal-loading loading:active"}}
{{#if welcomeUser}} {{#if welcomeUser}}
<div {{bind-attr class=":signup-welcome :fade" style=welcomeStyle}}> <div {{bind-attr class=":signup-welcome" style=welcomeStyle}}>
{{user-avatar welcomeUser}} {{user-avatar welcomeUser}}
<h3>Welcome, {{welcomeUser.username}}!</h3> <h3>Welcome, {{welcomeUser.username}}!</h3>

View File

@ -9,10 +9,10 @@ export default Ember.View.extend(ModalView, {
didInsertElement: function() { didInsertElement: function() {
}, },
welcomeUserDidChange: Ember.observer('welcomeUser', function() { welcomeUserDidChange: Ember.observer('controller.welcomeUser', function() {
if (this.get('welcomeUser')) { if (this.get('controller.welcomeUser')) {
Ember.run.scheduleOnce('afterRender', this, function() { Ember.run.scheduleOnce('afterRender', this, function() {
this.$('.signup-welcome').addClass('in'); this.$('.signup-welcome').hide().fadeIn();
}); });
} }
}) })

View File

@ -44,7 +44,7 @@ abstract class BaseAction extends Action
} }
} }
protected function dispatch($command, $params) protected function dispatch($command, $params = [])
{ {
$this->event(new CommandWillBeDispatched($command, $params)); $this->event(new CommandWillBeDispatched($command, $params));
return $this->bus->dispatch($command); return $this->bus->dispatch($command);

View File

@ -22,8 +22,8 @@ class CreateAction extends BaseAction
$email = $params->get('users.email'); $email = $params->get('users.email');
$password = $params->get('users.password'); $password = $params->get('users.password');
$command = new RegisterUserCommand($username, $email, $password, $this->actor->getUser()); $command = new RegisterUserCommand($username, $email, $password, $this->actor->getUser(), app('flarum.forum'));
$this->dispatch($command, $params); $user = $this->dispatch($command, $params);
// Presumably, the user was created successfully. (The command handler // Presumably, the user was created successfully. (The command handler
// would have thrown an exception if not.) We set this post as our // would have thrown an exception if not.) We set this post as our

View File

@ -21,7 +21,7 @@ class CoreServiceProvider extends ServiceProvider
*/ */
public function boot(Dispatcher $events, Bus $bus) public function boot(Dispatcher $events, Bus $bus)
{ {
$this->loadViewsFrom(__DIR__.'../../views', 'flarum'); $this->loadViewsFrom(__DIR__.'/../../views', 'flarum');
$this->registerEventHandlers($events); $this->registerEventHandlers($events);
$this->registerPostTypes(); $this->registerPostTypes();

View File

@ -2,7 +2,7 @@
use Flarum\Core\Models\User; use Flarum\Core\Models\User;
class EmailWasChanged class UserEmailWasChanged
{ {
public $user; public $user;

View File

@ -2,7 +2,7 @@
use Flarum\Core\Models\User; use Flarum\Core\Models\User;
class EmailWasConfirmed class UserEmailWasConfirmed
{ {
public $user; public $user;

View File

@ -2,7 +2,7 @@
use Flarum\Core\Models\User; use Flarum\Core\Models\User;
class PasswordWasChanged class UserPasswordWasChanged
{ {
public $user; public $user;

View File

@ -1,5 +1,7 @@
<?php namespace Flarum\Core\Handlers\Commands; <?php namespace Flarum\Core\Handlers\Commands;
use Flarum\Core\Models\User;
use Flarum\Core\Events\UserWillBeSaved;
use Flarum\Core\Support\DispatchesEvents; use Flarum\Core\Support\DispatchesEvents;
class RegisterUserCommandHandler class RegisterUserCommandHandler

View File

@ -3,6 +3,7 @@
use Illuminate\Mail\Mailer; use Illuminate\Mail\Mailer;
use Flarum\Core\Events\UserWasRegistered; use Flarum\Core\Events\UserWasRegistered;
use Flarum\Core\Events\EmailWasChanged; use Flarum\Core\Events\EmailWasChanged;
use Config;
class EmailConfirmationMailer class EmailConfirmationMailer
{ {
@ -29,7 +30,7 @@ class EmailConfirmationMailer
{ {
$user = $event->user; $user = $event->user;
$forumTitle = Config::get('flarum::forum_tite'); $forumTitle = Config::get('flarum::forum_title');
$data = [ $data = [
'username' => $user->username, 'username' => $user->username,
@ -37,8 +38,10 @@ class EmailConfirmationMailer
'url' => route('flarum.confirm', ['id' => $user->id, 'token' => $user->confirmation_token]) 'url' => route('flarum.confirm', ['id' => $user->id, 'token' => $user->confirmation_token])
]; ];
$this->mailer->send(['text' => 'flarum::emails.confirm'], $data, function ($message) use ($user) { $this->mailer->send(['text' => 'flarum::emails.confirm'], $data, function ($message) use ($user, $forumTitle) {
$message->to($user->email)->subject('['.$forumTitle.'] Email Address Confirmation'); $message->to($user->email);
$message->subject('['.$forumTitle.'] Email Address Confirmation');
$message->from('noreply@localhost', $forumTitle);
}); });
} }

View File

@ -6,10 +6,10 @@ use Flarum\Core\Exceptions\InvalidConfirmationTokenException;
use Flarum\Core\Events\UserWasDeleted; use Flarum\Core\Events\UserWasDeleted;
use Flarum\Core\Events\UserWasRegistered; use Flarum\Core\Events\UserWasRegistered;
use Flarum\Core\Events\UserWasRenamed; use Flarum\Core\Events\UserWasRenamed;
use Flarum\Core\Events\EmailWasChanged; use Flarum\Core\Events\UserEmailWasChanged;
use Flarum\Core\Events\PasswordWasChanged; use Flarum\Core\Events\UserPasswordWasChanged;
use Flarum\Core\Events\UserWasActivated; use Flarum\Core\Events\UserWasActivated;
use Flarum\Core\Events\EmailWasConfirmed; use Flarum\Core\Events\UserEmailWasConfirmed;
class User extends Model class User extends Model
{ {
@ -115,7 +115,7 @@ class User extends Model
{ {
if ($email !== $this->email) { if ($email !== $this->email) {
$this->email = $email; $this->email = $email;
$this->raise(new EmailWasChanged($this)); $this->raise(new UserEmailWasChanged($this));
} }
return $this; return $this;
@ -130,7 +130,7 @@ class User extends Model
public function changePassword($password) public function changePassword($password)
{ {
$this->password = $password ? static::$hasher->make($password) : null; $this->password = $password ? static::$hasher->make($password) : null;
$this->raise(new PasswordWasChanged($this)); $this->raise(new UserPasswordWasChanged($this));
return $this; return $this;
} }
@ -211,7 +211,7 @@ class User extends Model
$this->is_confirmed = true; $this->is_confirmed = true;
$this->confirmation_token = null; $this->confirmation_token = null;
$this->raise(new EmailWasConfirmed($this)); $this->raise(new UserEmailWasConfirmed($this));
return $this; return $this;
} }
@ -302,6 +302,16 @@ class User extends Model
return Permission::whereIn('grantee', $this->getGrantees()); return Permission::whereIn('grantee', $this->getGrantees());
} }
/**
* Define the relationship with the user's access tokens.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function accessTokens()
{
return $this->hasMany('Flarum\Core\Models\AccessToken');
}
/** /**
* Set the hasher with which to hash passwords. * Set the hasher with which to hash passwords.
* *

View File

@ -1,6 +1,7 @@
<?php namespace Flarum\Web\Actions; <?php namespace Flarum\Web\Actions;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Contracts\Bus\Dispatcher;
use Flarum\Web\Events\CommandWillBeDispatched; use Flarum\Web\Events\CommandWillBeDispatched;
use Flarum\Core\Support\Actor; use Flarum\Core\Support\Actor;
@ -8,9 +9,10 @@ abstract class Action
{ {
abstract public function handle(Request $request, $routeParams = []); abstract public function handle(Request $request, $routeParams = []);
public function __construct(Actor $actor) public function __construct(Actor $actor, Dispatcher $bus)
{ {
$this->actor = $actor; $this->actor = $actor;
$this->bus = $bus;
} }
protected function callAction($class, $params = []) protected function callAction($class, $params = [])
@ -19,9 +21,9 @@ abstract class Action
return $action->call($params); return $action->call($params);
} }
protected function dispatch($command, $params) protected function dispatch($command, $params = [])
{ {
$this->event(new CommandWillBeDispatched($command, $params)); event(new CommandWillBeDispatched($command, $params));
return $this->bus->dispatch($command); return $this->bus->dispatch($command);
} }
} }

View File

@ -1,25 +1,29 @@
<?php namespace Flarum\Web\Actions; <?php namespace Flarum\Web\Actions;
use Flarum\Core\Users\Commands\ConfirmEmailCommand; use Illuminate\Http\Request;
use Cookie; use Flarum\Core\Commands\ConfirmEmailCommand;
use Flarum\Core\Commands\GenerateAccessTokenCommand;
use Flarum\Core\Exceptions\InvalidConfirmationTokenException;
class ConfirmAction extends Action class ConfirmAction extends Action
{ {
use MakesRememberCookie; use MakesRememberCookie;
public function respond(Request $request, $params = []) public function handle(Request $request, $routeParams = [])
{ {
try { try {
$userId = array_get($routeParams, 'id');
$token = array_get($routeParams, 'token');
$command = new ConfirmEmailCommand($userId, $token); $command = new ConfirmEmailCommand($userId, $token);
$user = $this->dispatch($command); $user = $this->dispatch($command);
} catch (InvalidConfirmationTokenException $e) { } catch (InvalidConfirmationTokenException $e) {
return 'Invalid confirmation token'; return 'Invalid confirmation token';
} }
$token = AccessToken::generate($user->id); $command = new GenerateAccessTokenCommand($user->id);
$token->save(); $token = $this->dispatch($command);
return Redirect::to('/') return redirect('/')
->withCookie($this->makeRememberCookie($token->id)) ->withCookie($this->makeRememberCookie($token->id))
->with('alert', ['type' => 'success', 'message' => 'Thanks for confirming!']); ->with('alert', ['type' => 'success', 'message' => 'Thanks for confirming!']);
} }