Add group management actions to API

This commit is contained in:
Toby Zerner 2015-07-31 20:10:49 +09:30
parent 9dd5a742e5
commit 1d5586165c
21 changed files with 684 additions and 58 deletions

View File

@ -0,0 +1,40 @@
<?php namespace Flarum\Api\Actions\Groups;
use Flarum\Core\Groups\Commands\CreateGroup;
use Flarum\Api\Actions\CreateAction as BaseCreateAction;
use Flarum\Api\JsonApiRequest;
use Illuminate\Contracts\Bus\Dispatcher;
class CreateAction extends BaseCreateAction
{
/**
* @var Dispatcher
*/
protected $bus;
/**
* @inheritdoc
*/
public $serializer = 'Flarum\Api\Serializers\GroupSerializer';
/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}
/**
* Create a group according to input from the API request.
*
* @param JsonApiRequest $request
* @return \Flarum\Core\Groups\Group
*/
protected function create(JsonApiRequest $request)
{
return $this->bus->dispatch(
new CreateGroup($request->actor, $request->get('data'))
);
}
}

View File

@ -0,0 +1,34 @@
<?php namespace Flarum\Api\Actions\Groups;
use Flarum\Core\Groups\Commands\DeleteGroup;
use Flarum\Api\Actions\DeleteAction as BaseDeleteAction;
use Flarum\Api\Request;
use Illuminate\Contracts\Bus\Dispatcher;
class DeleteAction extends BaseDeleteAction
{
/**
* @var Dispatcher
*/
protected $bus;
/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}
/**
* Delete a group.
*
* @param Request $request
*/
protected function delete(Request $request)
{
$this->bus->dispatch(
new DeleteGroup($request->get('id'), $request->actor)
);
}
}

View File

@ -12,36 +12,6 @@ class IndexAction extends SerializeCollectionAction
*/
public $serializer = 'Flarum\Api\Serializers\GroupSerializer';
/**
* @inheritdoc
*/
public $include = [];
/**
* @inheritdoc
*/
public $link = [];
/**
* @inheritdoc
*/
public $limitMax = 50;
/**
* @inheritdoc
*/
public $limit = 20;
/**
* @inheritdoc
*/
public $sortFields = [];
/**
* @inheritdoc
*/
public $sort;
/**
* Get the groups, ready to be serialized and assigned to the document
* response.

View File

@ -0,0 +1,43 @@
<?php namespace Flarum\Api\Actions\Groups;
use Flarum\Core\Groups\Commands\EditGroup;
use Flarum\Api\Actions\SerializeResourceAction;
use Flarum\Api\JsonApiRequest;
use Illuminate\Contracts\Bus\Dispatcher;
use Tobscure\JsonApi\Document;
class UpdateAction extends SerializeResourceAction
{
/**
* @var Dispatcher
*/
protected $bus;
/**
* @inheritdoc
*/
public $serializer = 'Flarum\Api\Serializers\GroupSerializer';
/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}
/**
* Update a group according to input from the API request, and return it
* ready to be serialized and assigned to the JsonApi response.
*
* @param JsonApiRequest $request
* @param Document $document
* @return \Flarum\Core\Groups\Group
*/
protected function data(JsonApiRequest $request, Document $document)
{
return $this->bus->dispatch(
new EditGroup($request->get('id'), $request->actor, $request->get('data'))
);
}
}

View File

@ -299,13 +299,6 @@ class ApiServiceProvider extends ServiceProvider
$this->action('Flarum\Api\Actions\Groups\CreateAction')
);
// Show a single group
$routes->get(
'/groups/{id}',
'flarum.api.groups.show',
$this->action('Flarum\Api\Actions\Groups\ShowAction')
);
// Edit a group
$routes->patch(
'/groups/{id}',

View File

@ -39,11 +39,12 @@ class CoreServiceProvider extends ServiceProvider
{
$this->app->singleton('flarum.forum', 'Flarum\Core\Forum');
// TODO: probably use Illuminate's AggregateServiceProvider
// FIXME: probably use Illuminate's AggregateServiceProvider
// functionality, because it includes the 'provides' stuff.
$this->app->register('Flarum\Core\Activity\ActivityServiceProvider');
$this->app->register('Flarum\Core\Discussions\DiscussionsServiceProvider');
$this->app->register('Flarum\Core\Formatter\FormatterServiceProvider');
$this->app->register('Flarum\Core\Groups\GroupsServiceProvider');
$this->app->register('Flarum\Core\Notifications\NotificationsServiceProvider');
$this->app->register('Flarum\Core\Posts\PostsServiceProvider');
$this->app->register('Flarum\Core\Users\UsersServiceProvider');

View File

@ -0,0 +1,30 @@
<?php namespace Flarum\Core\Groups\Commands;
use Flarum\Core\Users\User;
class CreateGroup
{
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes of the new group.
*
* @var array
*/
public $data;
/**
* @param User $actor The user performing the action.
* @param array $data The attributes of the new group.
*/
public function __construct(User $actor, array $data)
{
$this->actor = $actor;
$this->data = $data;
}
}

View File

@ -0,0 +1,50 @@
<?php namespace Flarum\Core\Groups\Commands;
use Flarum\Core\Groups\Group;
use Flarum\Core\Forum;
use Flarum\Events\GroupWillBeSaved;
use Flarum\Core\Support\DispatchesEvents;
class CreateGroupHandler
{
use DispatchesEvents;
/**
* @var Forum
*/
protected $forum;
/**
* @param Forum $forum
*/
public function __construct(Forum $forum)
{
$this->forum = $forum;
}
/**
* @param CreateGroup $command
* @return Group
*/
public function handle(CreateGroup $command)
{
$actor = $command->actor;
$data = $command->data;
$this->forum->assertCan($actor, 'createGroup');
$group = Group::build(
array_get($data, 'attributes.nameSingular'),
array_get($data, 'attributes.namePlural'),
array_get($data, 'attributes.color'),
array_get($data, 'attributes.icon')
);
event(new GroupWillBeSaved($group, $actor, $data));
$group->save();
$this->dispatchEventsFor($group);
return $group;
}
}

View File

@ -0,0 +1,42 @@
<?php namespace Flarum\Core\Groups\Commands;
use Flarum\Core\Groups\Group;
use Flarum\Core\Users\User;
class DeleteGroup
{
/**
* The ID of the group to delete.
*
* @var int
*/
public $groupId;
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* Any other group input associated with the action. This is unused by
* default, but may be used by extensions.
*
* @var array
*/
public $data;
/**
* @param int $groupId The ID of the group to delete.
* @param User $actor The user performing the action.
* @param array $data Any other group input associated with the action. This
* is unused by default, but may be used by extensions.
*/
public function __construct($groupId, User $actor, array $data = [])
{
$this->groupId = $groupId;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@ -0,0 +1,45 @@
<?php namespace Flarum\Core\Groups\Commands;
use Flarum\Core\Groups\Group;
use Flarum\Core\Groups\GroupRepository;
use Flarum\Events\GroupWillBeDeleted;
use Flarum\Core\Support\DispatchesEvents;
class DeleteGroupHandler
{
use DispatchesEvents;
/**
* @var GroupRepository
*/
protected $groups;
/**
* @param GroupRepository $groups
*/
public function __construct(GroupRepository $groups)
{
$this->groups = $groups;
}
/**
* @param DeleteGroup $command
* @return Group
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
*/
public function handle(DeleteGroup $command)
{
$actor = $command->actor;
$group = $this->groups->findOrFail($command->groupId, $actor);
$group->assertCan($actor, 'delete');
event(new GroupWillBeDeleted($group, $actor, $command->data));
$group->delete();
$this->dispatchEventsFor($group);
return $group;
}
}

View File

@ -0,0 +1,40 @@
<?php namespace Flarum\Core\Groups\Commands;
use Flarum\Core\Groups\Group;
use Flarum\Core\Users\User;
class EditGroup
{
/**
* The ID of the group to edit.
*
* @var int
*/
public $groupId;
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes to update on the post.
*
* @var array
*/
public $data;
/**
* @param int $groupId The ID of the group to edit.
* @param User $actor The user performing the action.
* @param array $data The attributes to update on the post.
*/
public function __construct($groupId, User $actor, array $data)
{
$this->groupId = $groupId;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@ -0,0 +1,60 @@
<?php namespace Flarum\Core\Groups\Commands;
use Flarum\Core\Groups\Group;
use Flarum\Core\Groups\GroupRepository;
use Flarum\Events\GroupWillBeSaved;
use Flarum\Core\Support\DispatchesEvents;
class EditGroupHandler
{
use DispatchesEvents;
/**
* @var GroupRepository
*/
protected $groups;
/**
* @param GroupRepository $groups
*/
public function __construct(GroupRepository $groups)
{
$this->groups = $groups;
}
/**
* @param EditGroup $command
* @return Group
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
*/
public function handle(EditGroup $command)
{
$actor = $command->actor;
$data = $command->data;
$group = $this->groups->findOrFail($command->groupId, $actor);
$group->assertCan($actor, 'edit');
$attributes = array_get($data, 'attributes', []);
if (isset($attributes['nameSingular']) && isset($attributes['namePlural'])) {
$group->rename($attributes['nameSingular'], $attributes['namePlural']);
}
if (isset($attributes['color'])) {
$group->color = $attributes['color'];
}
if (isset($attributes['icon'])) {
$group->icon = $attributes['icon'];
}
event(new GroupWillBeSaved($group, $actor, $data));
$group->save();
$this->dispatchEventsFor($group);
return $group;
}
}

View File

@ -1,17 +1,39 @@
<?php namespace Flarum\Core\Groups;
use Flarum\Core\Model;
use Flarum\Core\Support\Locked;
use Flarum\Core\Support\VisibleScope;
use Flarum\Core\Support\EventGenerator;
use Flarum\Core\Support\ValidatesBeforeSave;
use Flarum\Events\GroupWasDeleted;
use Flarum\Events\GroupWasCreated;
use Flarum\Events\GroupWasRenamed;
/**
* @todo document database columns with @property
*/
class Group extends Model
{
use ValidatesBeforeSave;
use EventGenerator;
use Locked;
use VisibleScope;
/**
* {@inheritdoc}
*/
protected $table = 'groups';
/**
* The validation rules for this model.
*
* @var array
*/
protected $rules = [
'name_singular' => 'required',
'name_plural' => 'required'
];
/**
* The ID of the administrator group.
*/
@ -27,6 +49,60 @@ class Group extends Model
*/
const MEMBER_ID = 3;
/**
* Boot the model.
*
* @return void
*/
public static function boot()
{
parent::boot();
static::deleted(function ($group) {
$group->raise(new GroupWasDeleted($group));
$group->permissions()->delete();
});
}
/**
* Create a new group.
*
* @param string $nameSingular
* @param string $namePlural
* @param string $color
* @param string $icon
* @return static
*/
public static function build($nameSingular, $namePlural, $color, $icon)
{
$group = new static;
$group->name_singular = $nameSingular;
$group->name_plural = $namePlural;
$group->color = $color;
$group->icon = $icon;
$group->raise(new GroupWasCreated($group));
return $group;
}
/**
* Rename the group.
*
* @param string $nameSingular
* @param string $namePlural
* @return $this
*/
public function rename($nameSingular, $namePlural)
{
$this->name_singular = $nameSingular;
$this->name_plural = $namePlural;
return $this;
}
/**
* Define the relationship with the group's users.
*
@ -40,7 +116,7 @@ class Group extends Model
/**
* Define the relationship with the group's permissions.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function permissions()
{

View File

@ -0,0 +1,50 @@
<?php namespace Flarum\Core\Groups;
use Flarum\Core\Users\User;
use Illuminate\Database\Eloquent\Builder;
class GroupRepository
{
/**
* Get a new query builder for the groups table.
*
* @return Builder
*/
public function query()
{
return User::query();
}
/**
* Find a user by ID, optionally making sure it is visible to a certain
* user, or throw an exception.
*
* @param int $id
* @param User $actor
* @return Group
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function findOrFail($id, User $actor = null)
{
$query = Group::where('id', $id);
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
}
/**
* Scope a query to only include records that are visible to a user.
*
* @param Builder $query
* @param User $actor
* @return Builder
*/
protected function scopeVisibleTo(Builder $query, User $actor = null)
{
if ($actor !== null) {
$query->whereVisibleTo($actor);
}
return $query;
}
}

View File

@ -0,0 +1,28 @@
<?php namespace Flarum\Core\Groups;
use Flarum\Events\ModelAllow;
use Flarum\Support\ServiceProvider;
use Illuminate\Contracts\Container\Container;
class GroupsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
Group::setValidator($this->app->make('validator'));
$events = $this->app->make('events');
$events->listen(ModelAllow::class, function (ModelAllow $event) {
if ($event->model instanceof Group) {
if ($event->actor->hasPermission('group.'.$event->action)) {
return true;
}
}
});
}
}

View File

@ -0,0 +1,21 @@
<?php namespace Flarum\Events;
use Flarum\Core\Groups\Group;
class GroupWasCreated
{
/**
* The group that was created.
*
* @var Group
*/
public $group;
/**
* @param Group $group The group that was created.
*/
public function __construct(Group $group)
{
$this->group = $group;
}
}

View File

@ -0,0 +1,21 @@
<?php namespace Flarum\Events;
use Flarum\Core\Groups\Group;
class GroupWasDeleted
{
/**
* The group that was deleted.
*
* @var Group
*/
public $group;
/**
* @param Group $group The group that was deleted.
*/
public function __construct(Group $group)
{
$this->group = $group;
}
}

View File

@ -0,0 +1,21 @@
<?php namespace Flarum\Events;
use Flarum\Core\Groups\Group;
class GroupWasRenamed
{
/**
* The group that was renamed.
*
* @var Group
*/
public $group;
/**
* @param Group $group The group that was renamed.
*/
public function __construct(Group $group)
{
$this->group = $group;
}
}

View File

@ -0,0 +1,40 @@
<?php namespace Flarum\Events;
use Flarum\Core\Groups\Group;
use Flarum\Core\Users\User;
class GroupWillBeDeleted
{
/**
* The group who will be deleted.
*
* @var Group
*/
public $group;
/**
* The user who is performing the action.
*
* @var User
*/
public $actor;
/**
* Any group input associated with the command.
*
* @var array
*/
public $data;
/**
* @param Group $group The group who will be deleted.
* @param User $actor The user performing the action.
* @param array $data Any group input associated with the command.
*/
public function __construct(Group $group, User $actor, array $data)
{
$this->group = $group;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@ -0,0 +1,40 @@
<?php namespace Flarum\Events;
use Flarum\Core\Groups\Group;
use Flarum\Core\Users\User;
class GroupWillBeSaved
{
/**
* The group that will be saved.
*
* @var Group
*/
public $group;
/**
* The user who is performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes to update on the group.
*
* @var array
*/
public $data;
/**
* @param Group $group The group that will be saved.
* @param User $actor The user who is performing the action.
* @param array $data The attributes to update on the group.
*/
public function __construct(Group $group, User $actor, array $data)
{
$this->group = $group;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@ -1,19 +0,0 @@
<?php namespace Flarum\Events;
use Flarum\Http\RouteCollection;
class RegisterApiRoutes
{
/**
* @var RouteCollection
*/
public $routes;
/**
* @param RouteCollection $routes
*/
public function __construct(RouteCollection $routes)
{
$this->routes = $routes;
}
}