From b6be2cddab8919bcd9b845c7694008f452da056b Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 26 May 2015 18:03:02 +0930 Subject: [PATCH] Roughly implement change password/email, delete account modals --- .../src/components/change-email-modal.js | 44 +++++++++++++++++++ .../src/components/change-password-modal.js | 30 +++++++++++++ .../src/components/delete-account-modal.js | 34 ++++++++++++++ .../js/forum/src/components/settings-page.js | 12 +++-- .../core/src/Core/CoreServiceProvider.php | 2 +- .../Events/EmailConfirmationMailer.php | 4 +- .../src/Forum/Middleware/LoginWithCookie.php | 5 ++- 7 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 framework/core/js/forum/src/components/change-email-modal.js create mode 100644 framework/core/js/forum/src/components/change-password-modal.js create mode 100644 framework/core/js/forum/src/components/delete-account-modal.js diff --git a/framework/core/js/forum/src/components/change-email-modal.js b/framework/core/js/forum/src/components/change-email-modal.js new file mode 100644 index 000000000..8cd89d073 --- /dev/null +++ b/framework/core/js/forum/src/components/change-email-modal.js @@ -0,0 +1,44 @@ +import FormModal from 'flarum/components/form-modal'; +import Alert from 'flarum/components/alert'; + +export default class ChangeEmailModal extends FormModal { + constructor(props) { + super(props); + + this.email = m.prop(app.session.user().email()); + } + + view() { + return super.view({ + className: 'modal-sm change-email-modal', + title: 'Change Email', + body: [ + m('div.form-group', [ + m('input.form-control[type=email][name=email][placeholder=Email]', {value: this.email(), onchange: m.withAttr('value', this.email)}) + ]), + m('div.form-group', [ + m('button.btn.btn-primary.btn-block[type=submit]', 'Save Changes') + ]) + ] + }); + } + + onsubmit(e) { + e.preventDefault(); + + if (this.email() === app.session.user().email()) { + this.hide(); + return; + } + + this.loading(true); + app.session.user().save({ email: this.email() }).then(() => { + this.hide(); + }, response => { + this.loading(false); + this.alert = new Alert({ type: 'warning', message: response.errors.map((error, k) => [error.detail, k < response.errors.length - 1 ? m('br') : '']) }); + m.redraw(); + this.$('[name='+response.errors[0].path+']').select(); + }); + } +} diff --git a/framework/core/js/forum/src/components/change-password-modal.js b/framework/core/js/forum/src/components/change-password-modal.js new file mode 100644 index 000000000..b77591dd8 --- /dev/null +++ b/framework/core/js/forum/src/components/change-password-modal.js @@ -0,0 +1,30 @@ +import FormModal from 'flarum/components/form-modal'; + +export default class ChangePasswordModal extends FormModal { + view() { + return super.view({ + className: 'modal-sm change-password-modal', + title: 'Change Password', + body: [ + m('p.help-text', 'Click the button below and check your email for a link to change your password.'), + m('div.form-group', [ + m('button.btn.btn-primary.btn-block[type=submit]', 'Send Password Reset Email') + ]) + ] + }); + } + + onsubmit(e) { + e.preventDefault(); + this.loading(true); + + m.request({ + method: 'POST', + url: app.config['api_url']+'/forgot', + data: {email: app.session.user().email()}, + background: true + }).then(response => { + this.hide(); + }); + } +} diff --git a/framework/core/js/forum/src/components/delete-account-modal.js b/framework/core/js/forum/src/components/delete-account-modal.js new file mode 100644 index 000000000..14300a746 --- /dev/null +++ b/framework/core/js/forum/src/components/delete-account-modal.js @@ -0,0 +1,34 @@ +import FormModal from 'flarum/components/form-modal'; + +export default class DeleteAccountModal extends FormModal { + constructor(props) { + super(props); + + this.confirmation = m.prop(); + } + + view() { + return super.view({ + className: 'modal-sm change-password-modal', + title: 'Delete Account', + body: [ + m('p.help-text', 'Hold up there skippy! If you delete your account, there\'s no going back. All of your posts will be kept, but no longer associated with your account.'), + m('div.form-group', [ + m('input.form-control[name=confirm][placeholder=Type "DELETE" to proceed]', {oninput: m.withAttr('value', this.confirmation)}) + ]), + m('div.form-group', [ + m('button.btn.btn-primary.btn-block[type=submit]', {disabled: this.loading() || this.confirmation() != 'DELETE'}, 'Delete Account') + ]) + ] + }); + } + + onsubmit(e) { + e.preventDefault(); + + if (this.confirmation() !== 'DELETE') return; + + this.loading(true); + app.session.user().delete().then(() => app.session.logout()); + } +} diff --git a/framework/core/js/forum/src/components/settings-page.js b/framework/core/js/forum/src/components/settings-page.js index 3245c2f40..06e962d32 100644 --- a/framework/core/js/forum/src/components/settings-page.js +++ b/framework/core/js/forum/src/components/settings-page.js @@ -4,6 +4,9 @@ import SwitchInput from 'flarum/components/switch-input'; import ActionButton from 'flarum/components/action-button'; import FieldSet from 'flarum/components/field-set'; import NotificationGrid from 'flarum/components/notification-grid'; +import ChangePasswordModal from 'flarum/components/change-password-modal'; +import ChangeEmailModal from 'flarum/components/change-email-modal'; +import DeleteAccountModal from 'flarum/components/delete-account-modal'; import listItems from 'flarum/helpers/list-items'; import icon from 'flarum/helpers/icon'; @@ -62,21 +65,24 @@ export default class SettingsPage extends UserPage { items.add('changePassword', ActionButton.component({ label: 'Change Password', - className: 'btn btn-default' + className: 'btn btn-default', + onclick: () => app.modal.show(new ChangePasswordModal()) }) ); items.add('changeEmail', ActionButton.component({ label: 'Change Email', - className: 'btn btn-default' + className: 'btn btn-default', + onclick: () => app.modal.show(new ChangeEmailModal()) }) ); items.add('deleteAccount', ActionButton.component({ label: 'Delete Account', - className: 'btn btn-default btn-danger' + className: 'btn btn-default btn-danger', + onclick: () => app.modal.show(new DeleteAccountModal()) }) ); diff --git a/framework/core/src/Core/CoreServiceProvider.php b/framework/core/src/Core/CoreServiceProvider.php index 8dac4d129..55833f4f1 100644 --- a/framework/core/src/Core/CoreServiceProvider.php +++ b/framework/core/src/Core/CoreServiceProvider.php @@ -214,7 +214,7 @@ class CoreServiceProvider extends ServiceProvider }); // Allow a user to edit their own account. - User::grantPermission('edit', function ($grant, $user) { + User::grantPermission(['edit', 'delete'], function ($grant, $user) { $grant->where('id', $user->id); }); diff --git a/framework/core/src/Core/Handlers/Events/EmailConfirmationMailer.php b/framework/core/src/Core/Handlers/Events/EmailConfirmationMailer.php index 4b2b7747e..cab962ef5 100755 --- a/framework/core/src/Core/Handlers/Events/EmailConfirmationMailer.php +++ b/framework/core/src/Core/Handlers/Events/EmailConfirmationMailer.php @@ -38,9 +38,9 @@ class EmailConfirmationMailer 'url' => route('flarum.forum.confirm', ['id' => $user->id, 'token' => $user->confirmation_token]) ]; - $this->mailer->send(['text' => 'flarum::emails.confirm'], $data, function ($message) use ($user, $forumTitle) { + $this->mailer->send(['text' => 'flarum::emails.confirm'], $data, function ($message) use ($user) { $message->to($user->email); - $message->subject('['.$forumTitle.'] Email Address Confirmation'); + $message->subject('Email Address Confirmation'); }); } diff --git a/framework/core/src/Forum/Middleware/LoginWithCookie.php b/framework/core/src/Forum/Middleware/LoginWithCookie.php index 0eaa71aae..34b512f3a 100644 --- a/framework/core/src/Forum/Middleware/LoginWithCookie.php +++ b/framework/core/src/Forum/Middleware/LoginWithCookie.php @@ -17,8 +17,9 @@ class LoginWithCookie public function handle($request, Closure $next) { if (($token = $request->cookie('flarum_remember')) && - ($accessToken = AccessToken::where('id', $token)->first())) { - $this->actor->setUser($user = $accessToken->user); + ($accessToken = AccessToken::where('id', $token)->first()) && + ($user = $accessToken->user)) { + $this->actor->setUser($user); $user->updateLastSeen()->save(); }