mirror of
https://github.com/flarum/core.git
synced 2025-10-17 09:46:14 +02:00
Add group management actions to API
This commit is contained in:
30
src/Core/Groups/Commands/CreateGroup.php
Normal file
30
src/Core/Groups/Commands/CreateGroup.php
Normal 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;
|
||||
}
|
||||
}
|
50
src/Core/Groups/Commands/CreateGroupHandler.php
Normal file
50
src/Core/Groups/Commands/CreateGroupHandler.php
Normal 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;
|
||||
}
|
||||
}
|
42
src/Core/Groups/Commands/DeleteGroup.php
Normal file
42
src/Core/Groups/Commands/DeleteGroup.php
Normal 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;
|
||||
}
|
||||
}
|
45
src/Core/Groups/Commands/DeleteGroupHandler.php
Normal file
45
src/Core/Groups/Commands/DeleteGroupHandler.php
Normal 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;
|
||||
}
|
||||
}
|
40
src/Core/Groups/Commands/EditGroup.php
Normal file
40
src/Core/Groups/Commands/EditGroup.php
Normal 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;
|
||||
}
|
||||
}
|
60
src/Core/Groups/Commands/EditGroupHandler.php
Normal file
60
src/Core/Groups/Commands/EditGroupHandler.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user