From cdcf68cf5a4edf9c0bd8c2d18a5732f509c67443 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 11 Oct 2015 18:01:29 +1030 Subject: [PATCH] Update for composer branch --- extensions/suspend/.gitignore | 2 + extensions/suspend/bootstrap.php | 13 +- extensions/suspend/composer.json | 30 ++- extensions/suspend/flarum.json | 25 --- extensions/suspend/js/.gitignore | 3 - extensions/suspend/js/admin/Gulpfile.js | 2 +- extensions/suspend/js/admin/dist/extension.js | 26 +++ extensions/suspend/js/forum/Gulpfile.js | 2 +- extensions/suspend/js/forum/dist/extension.js | 206 ++++++++++++++++++ extensions/suspend/js/forum/src/main.js | 6 +- extensions/suspend/locale/en.yml | 2 - ...000_add_suspended_until_to_users_table.php | 16 +- ...4_000000_rename_suspended_until_column.php | 16 +- extensions/suspend/scripts/compile.sh | 27 +++ extensions/suspend/src/Access/UserPolicy.php | 35 +++ extensions/suspend/src/Extension.php | 24 -- .../suspend/src/Listener/AddClientAssets.php | 49 +++++ .../src/Listener/AddUserSuspendAttributes.php | 55 +++++ .../RevokeAccessFromSuspendedUsers.php | 39 ++++ .../src/Listener/SaveSuspensionToDatabase.php | 47 ++++ .../src/Listeners/AddApiAttributes.php | 48 ---- .../suspend/src/Listeners/AddClientAssets.php | 54 ----- .../suspend/src/Listeners/PersistData.php | 61 ------ 23 files changed, 535 insertions(+), 253 deletions(-) delete mode 100644 extensions/suspend/flarum.json delete mode 100644 extensions/suspend/js/.gitignore create mode 100644 extensions/suspend/js/admin/dist/extension.js create mode 100644 extensions/suspend/js/forum/dist/extension.js delete mode 100644 extensions/suspend/locale/en.yml create mode 100755 extensions/suspend/scripts/compile.sh create mode 100644 extensions/suspend/src/Access/UserPolicy.php delete mode 100644 extensions/suspend/src/Extension.php create mode 100644 extensions/suspend/src/Listener/AddClientAssets.php create mode 100755 extensions/suspend/src/Listener/AddUserSuspendAttributes.php create mode 100755 extensions/suspend/src/Listener/RevokeAccessFromSuspendedUsers.php create mode 100755 extensions/suspend/src/Listener/SaveSuspensionToDatabase.php delete mode 100755 extensions/suspend/src/Listeners/AddApiAttributes.php delete mode 100644 extensions/suspend/src/Listeners/AddClientAssets.php delete mode 100755 extensions/suspend/src/Listeners/PersistData.php diff --git a/extensions/suspend/.gitignore b/extensions/suspend/.gitignore index a4f3b125e..0f98a63d8 100644 --- a/extensions/suspend/.gitignore +++ b/extensions/suspend/.gitignore @@ -2,3 +2,5 @@ composer.phar .DS_Store Thumbs.db +bower_components +node_modules diff --git a/extensions/suspend/bootstrap.php b/extensions/suspend/bootstrap.php index bbc8ed046..3e86cbfc2 100644 --- a/extensions/suspend/bootstrap.php +++ b/extensions/suspend/bootstrap.php @@ -9,6 +9,15 @@ * file that was distributed with this source code. */ -require __DIR__.'/vendor/autoload.php'; +use Flarum\Suspend\Access; +use Flarum\Suspend\Listener; +use Illuminate\Contracts\Events\Dispatcher; -return 'Flarum\Suspend\Extension'; +return function (Dispatcher $events) { + $events->subscribe(Listener\AddClientAssets::class); + $events->subscribe(Listener\AddUserSuspendAttributes::class); + $events->subscribe(Listener\RevokeAccessFromSuspendedUsers::class); + $events->subscribe(Listener\SaveSuspensionToDatabase::class); + + $events->subscribe(Access\UserPolicy::class); +}; \ No newline at end of file diff --git a/extensions/suspend/composer.json b/extensions/suspend/composer.json index be703e313..b3d596255 100644 --- a/extensions/suspend/composer.json +++ b/extensions/suspend/composer.json @@ -1,10 +1,34 @@ { + "name": "flarum/suspend", + "description": "Suspend users so they can't post.", + "type": "flarum-extension", + "license": "MIT", + "authors": [ + { + "name": "Toby Zerner", + "email": "toby.zerner@gmail.com" + } + ], + "support": { + "issues": "https://github.com/flarum/core/issues", + "source": "https://github.com/flarum/suspend" + }, + "require": { + "flarum/core": "^0.1.0-beta.3" + }, "autoload": { "psr-4": { "Flarum\\Suspend\\": "src/" } }, - "scripts": { - "style": "phpcs --standard=PSR2 -np src" + "extra": { + "flarum-extension": { + "title": "Suspend", + "icon": { + "name": "ban", + "backgroundColor": "#ddd", + "color": "#666" + } + } } -} +} \ No newline at end of file diff --git a/extensions/suspend/flarum.json b/extensions/suspend/flarum.json deleted file mode 100644 index b54c4841a..000000000 --- a/extensions/suspend/flarum.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "suspend", - "title": "Suspend", - "description": "Suspend users so they can't post.", - "keywords": [], - "version": "0.1.0-beta.2", - "author": { - "name": "Toby Zerner", - "email": "toby@flarum.org", - "homepage": "http://tobyzerner.com" - }, - "license": "MIT", - "require": { - "flarum": ">=0.1.0-beta.2" - }, - "support": { - "source": "https://github.com/flarum/suspend", - "issues": "https://github.com/flarum/core/issues" - }, - "icon": { - "name": "ban", - "backgroundColor": "#ddd", - "color": "#666" - } -} diff --git a/extensions/suspend/js/.gitignore b/extensions/suspend/js/.gitignore deleted file mode 100644 index 372e20a51..000000000 --- a/extensions/suspend/js/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -bower_components -node_modules -dist diff --git a/extensions/suspend/js/admin/Gulpfile.js b/extensions/suspend/js/admin/Gulpfile.js index c36cae0a2..c672877ab 100644 --- a/extensions/suspend/js/admin/Gulpfile.js +++ b/extensions/suspend/js/admin/Gulpfile.js @@ -2,6 +2,6 @@ var gulp = require('flarum-gulp'); gulp({ modules: { - 'suspend': 'src/**/*.js' + 'flarum/suspend': 'src/**/*.js' } }); diff --git a/extensions/suspend/js/admin/dist/extension.js b/extensions/suspend/js/admin/dist/extension.js new file mode 100644 index 000000000..e97a289ee --- /dev/null +++ b/extensions/suspend/js/admin/dist/extension.js @@ -0,0 +1,26 @@ +System.register('flarum/suspend/main', ['flarum/extend', 'flarum/app', 'flarum/components/PermissionGrid'], function (_export) { + 'use strict'; + + var extend, app, PermissionGrid; + return { + setters: [function (_flarumExtend) { + extend = _flarumExtend.extend; + }, function (_flarumApp) { + app = _flarumApp['default']; + }, function (_flarumComponentsPermissionGrid) { + PermissionGrid = _flarumComponentsPermissionGrid['default']; + }], + execute: function () { + + app.initializers.add('suspend', function () { + extend(PermissionGrid.prototype, 'moderateItems', function (items) { + items.add('suspendUsers', { + icon: 'ban', + label: 'Suspend users', + permission: 'user.suspend' + }); + }); + }); + } + }; +}); \ No newline at end of file diff --git a/extensions/suspend/js/forum/Gulpfile.js b/extensions/suspend/js/forum/Gulpfile.js index c36cae0a2..c672877ab 100644 --- a/extensions/suspend/js/forum/Gulpfile.js +++ b/extensions/suspend/js/forum/Gulpfile.js @@ -2,6 +2,6 @@ var gulp = require('flarum-gulp'); gulp({ modules: { - 'suspend': 'src/**/*.js' + 'flarum/suspend': 'src/**/*.js' } }); diff --git a/extensions/suspend/js/forum/dist/extension.js b/extensions/suspend/js/forum/dist/extension.js new file mode 100644 index 000000000..969eef812 --- /dev/null +++ b/extensions/suspend/js/forum/dist/extension.js @@ -0,0 +1,206 @@ +System.register('flarum/suspend/main', ['flarum/extend', 'flarum/app', 'flarum/utils/UserControls', 'flarum/components/Button', 'flarum/components/Badge', 'flarum/Model', 'flarum/models/User', 'flarum/suspend/components/SuspendUserModal'], function (_export) { + 'use strict'; + + var extend, app, UserControls, Button, Badge, Model, User, SuspendUserModal; + return { + setters: [function (_flarumExtend) { + extend = _flarumExtend.extend; + }, function (_flarumApp) { + app = _flarumApp['default']; + }, function (_flarumUtilsUserControls) { + UserControls = _flarumUtilsUserControls['default']; + }, function (_flarumComponentsButton) { + Button = _flarumComponentsButton['default']; + }, function (_flarumComponentsBadge) { + Badge = _flarumComponentsBadge['default']; + }, function (_flarumModel) { + Model = _flarumModel['default']; + }, function (_flarumModelsUser) { + User = _flarumModelsUser['default']; + }, function (_flarumSuspendComponentsSuspendUserModal) { + SuspendUserModal = _flarumSuspendComponentsSuspendUserModal['default']; + }], + execute: function () { + + app.initializers.add('flarum-suspend', function () { + User.prototype.canSuspend = Model.attribute('canSuspend'); + User.prototype.suspendUntil = Model.attribute('suspendUntil', Model.transformDate); + + extend(UserControls, 'moderationControls', function (items, user) { + if (user.canSuspend()) { + items.add('suspend', Button.component({ + children: 'Suspend', + icon: 'ban', + onclick: function onclick() { + return app.modal.show(new SuspendUserModal({ user: user })); + } + })); + } + }); + + extend(User.prototype, 'badges', function (items) { + var until = this.suspendUntil(); + + if (new Date() < until) { + items.add('suspended', Badge.component({ + icon: 'ban', + type: 'suspended', + label: 'Suspended' + })); + } + }); + }); + } + }; +});;System.register('flarum/suspend/components/SuspendUserModal', ['flarum/components/Modal', 'flarum/components/Button'], function (_export) { + 'use strict'; + + var Modal, Button, SuspendUserModal; + return { + setters: [function (_flarumComponentsModal) { + Modal = _flarumComponentsModal['default']; + }, function (_flarumComponentsButton) { + Button = _flarumComponentsButton['default']; + }], + execute: function () { + SuspendUserModal = (function (_Modal) { + babelHelpers.inherits(SuspendUserModal, _Modal); + + function SuspendUserModal() { + babelHelpers.classCallCheck(this, SuspendUserModal); + + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + babelHelpers.get(Object.getPrototypeOf(SuspendUserModal.prototype), 'constructor', this).apply(this, args); + + var until = this.props.user.suspendUntil(); + var status = null; + + if (new Date() > until) until = null; + + if (until) { + if (until.getFullYear() === 9999) status = 'indefinitely';else status = 'limited'; + } + + this.status = m.prop(status); + this.daysRemaining = m.prop(status === 'limited' && -moment().diff(until, 'days') + 1); + } + + babelHelpers.createClass(SuspendUserModal, [{ + key: 'className', + value: function className() { + return 'SuspendUserModal Modal--small'; + } + }, { + key: 'title', + value: function title() { + return 'Suspend ' + this.props.user.username(); + } + }, { + key: 'content', + value: function content() { + var _this = this; + + return m( + 'div', + { className: 'Modal-body' }, + m( + 'div', + { className: 'Form' }, + m( + 'div', + { className: 'Form-group' }, + m( + 'label', + null, + 'Suspension Status' + ), + m( + 'div', + null, + m( + 'label', + { className: 'checkbox' }, + m('input', { type: 'radio', name: 'status', checked: !this.status(), onclick: m.withAttr('value', this.status) }), + 'Not suspended' + ), + m( + 'label', + { className: 'checkbox' }, + m('input', { type: 'radio', name: 'status', checked: this.status() === 'indefinitely', value: 'indefinitely', onclick: m.withAttr('value', this.status) }), + 'Suspended indefinitely' + ), + m( + 'label', + { className: 'checkbox SuspendUserModal-days' }, + m('input', { type: 'radio', name: 'status', checked: this.status() === 'limited', value: 'limited', onclick: function (e) { + _this.status(e.target.value); + m.redraw(true); + _this.$('.SuspendUserModal-days-input input').select(); + m.redraw.strategy('none'); + } }), + 'Suspended for a limited time...', + this.status() === 'limited' ? m( + 'div', + { className: 'SuspendUserModal-days-input' }, + m('input', { type: 'number', + value: this.daysRemaining(), + oninput: m.withAttr('value', this.daysRemaining), + className: 'FormControl' }), + ' days' + ) : '' + ) + ) + ), + m( + 'div', + { className: 'Form-group' }, + m( + Button, + { className: 'Button Button--primary', loading: this.loading, type: 'submit' }, + 'Save Changes' + ) + ) + ) + ); + } + }, { + key: 'onsubmit', + value: function onsubmit(e) { + var _this2 = this; + + e.preventDefault(); + + this.loading = true; + + var suspendUntil = null; + switch (this.status()) { + case 'indefinitely': + suspendUntil = new Date('9999-12-31'); + break; + + case 'limited': + suspendUntil = moment().add(this.daysRemaining(), 'days').toDate(); + break; + + default: + // no default + } + + this.props.user.save({ suspendUntil: suspendUntil }).then(function () { + return _this2.hide(); + }, function () { + _this2.loading = false; + m.redraw(); + }); + } + }]); + return SuspendUserModal; + })(Modal); + + _export('default', SuspendUserModal); + } + }; +}); \ No newline at end of file diff --git a/extensions/suspend/js/forum/src/main.js b/extensions/suspend/js/forum/src/main.js index fc0431897..eaef11608 100644 --- a/extensions/suspend/js/forum/src/main.js +++ b/extensions/suspend/js/forum/src/main.js @@ -6,9 +6,9 @@ import Badge from 'flarum/components/Badge'; import Model from 'flarum/Model'; import User from 'flarum/models/User'; -import SuspendUserModal from 'suspend/components/SuspendUserModal'; +import SuspendUserModal from 'flarum/suspend/components/SuspendUserModal'; -app.initializers.add('suspend', () => { +app.initializers.add('flarum-suspend', () => { User.prototype.canSuspend = Model.attribute('canSuspend'); User.prototype.suspendUntil = Model.attribute('suspendUntil', Model.transformDate); @@ -27,7 +27,7 @@ app.initializers.add('suspend', () => { if (new Date() < until) { items.add('suspended', Badge.component({ - icon: 'times', + icon: 'ban', type: 'suspended', label: 'Suspended' })); diff --git a/extensions/suspend/locale/en.yml b/extensions/suspend/locale/en.yml deleted file mode 100644 index 028189c95..000000000 --- a/extensions/suspend/locale/en.yml +++ /dev/null @@ -1,2 +0,0 @@ -suspend: - # hello_world: "Hello, world!" diff --git a/extensions/suspend/migrations/2015_05_11_000000_add_suspended_until_to_users_table.php b/extensions/suspend/migrations/2015_05_11_000000_add_suspended_until_to_users_table.php index bf44be09e..ab59a0e7f 100644 --- a/extensions/suspend/migrations/2015_05_11_000000_add_suspended_until_to_users_table.php +++ b/extensions/suspend/migrations/2015_05_11_000000_add_suspended_until_to_users_table.php @@ -8,18 +8,13 @@ * file that was distributed with this source code. */ -namespace Flarum\Migrations\Suspend; +namespace Flarum\Suspend\Migration; +use Flarum\Database\AbstractMigration; use Illuminate\Database\Schema\Blueprint; -use Flarum\Migrations\Migration; -class AddSuspendedUntilToUsersTable extends Migration +class AddSuspendedUntilToUsersTable extends AbstractMigration { - /** - * Run the migrations. - * - * @return void - */ public function up() { $this->schema->table('users', function (Blueprint $table) { @@ -27,11 +22,6 @@ class AddSuspendedUntilToUsersTable extends Migration }); } - /** - * Reverse the migrations. - * - * @return void - */ public function down() { $this->schema->table('users', function (Blueprint $table) { diff --git a/extensions/suspend/migrations/2015_09_14_000000_rename_suspended_until_column.php b/extensions/suspend/migrations/2015_09_14_000000_rename_suspended_until_column.php index 5c6a6ced6..f97b6f29a 100644 --- a/extensions/suspend/migrations/2015_09_14_000000_rename_suspended_until_column.php +++ b/extensions/suspend/migrations/2015_09_14_000000_rename_suspended_until_column.php @@ -8,18 +8,13 @@ * file that was distributed with this source code. */ -namespace Flarum\Migrations\Suspend; +namespace Flarum\Suspend\Migration; +use Flarum\Database\AbstractMigration; use Illuminate\Database\Schema\Blueprint; -use Flarum\Migrations\Migration; -class RenameSuspendedUntilColumn extends Migration +class RenameSuspendedUntilColumn extends AbstractMigration { - /** - * Run the migrations. - * - * @return void - */ public function up() { $this->schema->table('users', function (Blueprint $table) { @@ -27,11 +22,6 @@ class RenameSuspendedUntilColumn extends Migration }); } - /** - * Reverse the migrations. - * - * @return void - */ public function down() { $this->schema->table('users', function (Blueprint $table) { diff --git a/extensions/suspend/scripts/compile.sh b/extensions/suspend/scripts/compile.sh new file mode 100755 index 000000000..b0d8e8bd3 --- /dev/null +++ b/extensions/suspend/scripts/compile.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# This script compiles the extension so that it can be used in a Flarum +# installation. It should be run from the root directory of the extension. + +base=$PWD + +cd "${base}/js" + +if [ -f bower.json ]; then + bower install +fi + +for app in forum admin; do + cd "${base}/js" + + if [ -d $app ]; then + cd $app + + if [ -f bower.json ]; then + bower install + fi + + npm install + gulp --production + fi +done diff --git a/extensions/suspend/src/Access/UserPolicy.php b/extensions/suspend/src/Access/UserPolicy.php new file mode 100644 index 000000000..be9cc8ec8 --- /dev/null +++ b/extensions/suspend/src/Access/UserPolicy.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Suspend\Access; + +use Flarum\Core\Access\AbstractPolicy; +use Flarum\Core\User; + +class UserPolicy extends AbstractPolicy +{ + /** + * {@inheritdoc} + */ + protected $model = User::class; + + /** + * @param User $actor + * @param User $user + * @return bool|null + */ + public function suspend(User $actor, User $user) + { + if ($user->isAdmin() || $user->id === $actor->id) { + return false; + } + } +} + diff --git a/extensions/suspend/src/Extension.php b/extensions/suspend/src/Extension.php deleted file mode 100644 index 9eb8b261a..000000000 --- a/extensions/suspend/src/Extension.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Suspend; - -use Flarum\Support\Extension as BaseExtension; -use Illuminate\Events\Dispatcher; - -class Extension extends BaseExtension -{ - public function listen(Dispatcher $events) - { - $events->subscribe('Flarum\Suspend\Listeners\AddClientAssets'); - $events->subscribe('Flarum\Suspend\Listeners\AddApiAttributes'); - $events->subscribe('Flarum\Suspend\Listeners\PersistData'); - } -} diff --git a/extensions/suspend/src/Listener/AddClientAssets.php b/extensions/suspend/src/Listener/AddClientAssets.php new file mode 100644 index 000000000..5c6bf7b1b --- /dev/null +++ b/extensions/suspend/src/Listener/AddClientAssets.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Suspend\Listener; + +use Flarum\Event\ConfigureClientView; +use Illuminate\Contracts\Events\Dispatcher; + +class AddClientAssets +{ + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(ConfigureClientView::class, [$this, 'addAssets']); + } + + /** + * @param ConfigureClientView $event + */ + public function addAssets(ConfigureClientView $event) + { + if ($event->isForum()) { + $event->addAssets([ + __DIR__.'/../../js/forum/dist/extension.js', + __DIR__.'/../../less/forum/extension.less' + ]); + $event->addBootstrapper('flarum/suspend/main'); + $event->addTranslations('flarum-suspend.forum'); + } + + if ($event->isAdmin()) { + $event->addAssets([ + __DIR__.'/../../js/admin/dist/extension.js', + __DIR__.'/../../less/admin/extension.less' + ]); + $event->addBootstrapper('flarum/suspend/main'); + $event->addTranslations('flarum-suspend.admin'); + } + } +} diff --git a/extensions/suspend/src/Listener/AddUserSuspendAttributes.php b/extensions/suspend/src/Listener/AddUserSuspendAttributes.php new file mode 100755 index 000000000..991da3917 --- /dev/null +++ b/extensions/suspend/src/Listener/AddUserSuspendAttributes.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Suspend\Listener; + +use Flarum\Api\Serializer\UserSerializer; +use Flarum\Core\User; +use Flarum\Event\ConfigureModelDates; +use Flarum\Event\PrepareApiAttributes; +use Illuminate\Contracts\Events\Dispatcher; + +class AddUserSuspendAttributes +{ + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(ConfigureModelDates::class, [$this, 'addDates']); + $events->listen(PrepareApiAttributes::class, [$this, 'addAttributes']); + } + + /** + * @param ConfigureModelDates $event + */ + public function addDates(ConfigureModelDates $event) + { + if ($event->isModel(User::class)) { + $event->dates[] = 'suspend_until'; + } + } + + /** + * @param PrepareApiAttributes $event + */ + public function addAttributes(PrepareApiAttributes $event) + { + if ($event->isSerializer(UserSerializer::class)) { + $canSuspend = $event->actor->can('suspend', $event->model); + + if ($canSuspend) { + $event->attributes['suspendUntil'] = $event->formatDate($event->model->suspend_until); + } + + $event->attributes['canSuspend'] = $canSuspend; + } + } +} diff --git a/extensions/suspend/src/Listener/RevokeAccessFromSuspendedUsers.php b/extensions/suspend/src/Listener/RevokeAccessFromSuspendedUsers.php new file mode 100755 index 000000000..7c4ff8889 --- /dev/null +++ b/extensions/suspend/src/Listener/RevokeAccessFromSuspendedUsers.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Suspend\Listener; + +use Carbon\Carbon; +use Flarum\Core\Group; +use Flarum\Event\PrepareUserGroups; +use Illuminate\Contracts\Events\Dispatcher; + +class RevokeAccessFromSuspendedUsers +{ + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(PrepareUserGroups::class, [$this, 'prepareUserGroups']); + } + + /** + * @param PrepareUserGroups $event + */ + public function prepareUserGroups(PrepareUserGroups $event) + { + $suspendUntil = $event->user->suspend_until; + + if ($suspendUntil && $suspendUntil->gt(Carbon::now())) { + $event->groupIds = [Group::GUEST_ID]; + } + } +} diff --git a/extensions/suspend/src/Listener/SaveSuspensionToDatabase.php b/extensions/suspend/src/Listener/SaveSuspensionToDatabase.php new file mode 100755 index 000000000..cb5e04c3d --- /dev/null +++ b/extensions/suspend/src/Listener/SaveSuspensionToDatabase.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Suspend\Listener; + +use Carbon\Carbon; +use Flarum\Core\Access\AssertPermissionTrait; +use Flarum\Event\UserWillBeSaved; +use Illuminate\Contracts\Events\Dispatcher; + +class SaveSuspensionToDatabase +{ + use AssertPermissionTrait; + + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + $events->listen(UserWillBeSaved::class, [$this, 'whenUserWillBeSaved']); + } + + /** + * @param UserWillBeSaved $event + */ + public function whenUserWillBeSaved(UserWillBeSaved $event) + { + $attributes = array_get($event->data, 'attributes', []); + + if (array_key_exists('suspendUntil', $attributes)) { + $suspendUntil = $attributes['suspendUntil']; + $user = $event->user; + $actor = $event->actor; + + $this->assertCan($actor, 'suspend', $user); + + $user->suspend_until = new Carbon($suspendUntil); + } + } +} diff --git a/extensions/suspend/src/Listeners/AddApiAttributes.php b/extensions/suspend/src/Listeners/AddApiAttributes.php deleted file mode 100755 index 8a8a59f70..000000000 --- a/extensions/suspend/src/Listeners/AddApiAttributes.php +++ /dev/null @@ -1,48 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Suspend\Listeners; - -use Flarum\Events\ModelDates; -use Flarum\Events\ApiAttributes; -use Flarum\Core\Users\User; -use Illuminate\Contracts\Events\Dispatcher; -use Flarum\Api\Serializers\UserSerializer; - -class AddApiAttributes -{ - public function subscribe(Dispatcher $events) - { - $events->listen(ModelDates::class, [$this, 'addDates']); - $events->listen(ApiAttributes::class, [$this, 'addAttributes']); - } - - public function addDates(ModelDates $event) - { - if ($event->model instanceof User) { - $event->dates[] = 'suspend_until'; - } - } - - public function addAttributes(ApiAttributes $event) - { - if ($event->serializer instanceof UserSerializer) { - $canSuspend = $event->model->can($event->actor, 'suspend'); - - if ($canSuspend) { - $suspendUntil = $event->model->suspend_until; - - $event->attributes['suspendUntil'] = $suspendUntil ? $suspendUntil->toRFC3339String() : null; - } - - $event->attributes['canSuspend'] = $canSuspend; - } - } -} diff --git a/extensions/suspend/src/Listeners/AddClientAssets.php b/extensions/suspend/src/Listeners/AddClientAssets.php deleted file mode 100644 index 4f95575b0..000000000 --- a/extensions/suspend/src/Listeners/AddClientAssets.php +++ /dev/null @@ -1,54 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Suspend\Listeners; - -use Flarum\Events\RegisterLocales; -use Flarum\Events\BuildClientView; -use Illuminate\Contracts\Events\Dispatcher; - -class AddClientAssets -{ - public function subscribe(Dispatcher $events) - { - $events->listen(RegisterLocales::class, [$this, 'addLocale']); - $events->listen(BuildClientView::class, [$this, 'addAssets']); - } - - public function addLocale(RegisterLocales $event) - { - $event->addTranslations('en', __DIR__.'/../../locale/en.yml'); - } - - public function addAssets(BuildClientView $event) - { - $event->forumAssets([ - __DIR__.'/../../js/forum/dist/extension.js', - __DIR__.'/../../less/forum/extension.less' - ]); - - $event->forumBootstrapper('suspend/main'); - - $event->forumTranslations([ - // 'suspend.hello_world' - ]); - - $event->adminAssets([ - __DIR__.'/../../js/admin/dist/extension.js', - __DIR__.'/../../less/admin/extension.less' - ]); - - $event->adminBootstrapper('suspend/main'); - - $event->adminTranslations([ - // 'suspend.hello_world' - ]); - } -} diff --git a/extensions/suspend/src/Listeners/PersistData.php b/extensions/suspend/src/Listeners/PersistData.php deleted file mode 100755 index 26c2dc63d..000000000 --- a/extensions/suspend/src/Listeners/PersistData.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Suspend\Listeners; - -use Flarum\Events\UserWillBeSaved; -use Flarum\Events\ModelAllow; -use Flarum\Events\GetUserGroups; -use Flarum\Core\Users\User; -use Flarum\Core\Groups\Group; -use Carbon\Carbon; - -class PersistData -{ - public function subscribe($events) - { - $events->listen(UserWillBeSaved::class, [$this, 'whenUserWillBeSaved']); - $events->listen(ModelAllow::class, [$this, 'disallowAdminSuspension']); - $events->listen(GetUserGroups::class, [$this, 'revokePermissions']); - } - - public function whenUserWillBeSaved(UserWillBeSaved $event) - { - $attributes = array_get($event->data, 'attributes', []); - - if (array_key_exists('suspendUntil', $attributes)) { - $suspendUntil = $attributes['suspendUntil']; - $user = $event->user; - $actor = $event->actor; - - $user->assertCan($actor, 'suspend'); - - $user->suspend_until = new Carbon($suspendUntil); - } - } - - public function disallowAdminSuspension(ModelAllow $event) - { - if ($event->model instanceof User && $event->action === 'suspend') { - if ($event->model->isAdmin() || $event->model->id === $event->actor->id) { - return false; - } - } - } - - public function revokePermissions(GetUserGroups $event) - { - $suspendUntil = $event->user->suspend_until; - - if ($suspendUntil && $suspendUntil->gt(Carbon::now())) { - $event->groupIds = [Group::GUEST_ID]; - } - } -}