1
0
mirror of https://github.com/flarum/core.git synced 2025-07-23 01:31:40 +02:00

Update for composer branch

This commit is contained in:
Toby Zerner
2015-10-11 18:01:29 +10:30
parent 7fe52f2494
commit cdcf68cf5a
23 changed files with 535 additions and 253 deletions

View File

@@ -2,3 +2,5 @@
composer.phar
.DS_Store
Thumbs.db
bower_components
node_modules

View File

@@ -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);
};

View File

@@ -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"
}
}
}
}
}

View File

@@ -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"
}
}

View File

@@ -1,3 +0,0 @@
bower_components
node_modules
dist

View File

@@ -2,6 +2,6 @@ var gulp = require('flarum-gulp');
gulp({
modules: {
'suspend': 'src/**/*.js'
'flarum/suspend': 'src/**/*.js'
}
});

View File

@@ -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'
});
});
});
}
};
});

View File

@@ -2,6 +2,6 @@ var gulp = require('flarum-gulp');
gulp({
modules: {
'suspend': 'src/**/*.js'
'flarum/suspend': 'src/**/*.js'
}
});

View File

@@ -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);
}
};
});

View File

@@ -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'
}));

View File

@@ -1,2 +0,0 @@
suspend:
# hello_world: "Hello, world!"

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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

View File

@@ -0,0 +1,35 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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;
}
}
}

View File

@@ -1,24 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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');
}
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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');
}
}
}

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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;
}
}
}

View File

@@ -0,0 +1,39 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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];
}
}
}

View File

@@ -0,0 +1,47 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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);
}
}
}

View File

@@ -1,48 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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;
}
}
}

View File

@@ -1,54 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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'
]);
}
}

View File

@@ -1,61 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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];
}
}
}