mirror of
https://github.com/flarum/core.git
synced 2025-10-12 07:24:27 +02:00
Extract Flarum\Group namespace
This commit is contained in:
@@ -1,36 +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\Core\Access;
|
||||
|
||||
use Flarum\Core\Group;
|
||||
use Flarum\User\AbstractPolicy;
|
||||
use Flarum\User\User;
|
||||
|
||||
class GroupPolicy extends AbstractPolicy
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $model = Group::class;
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @param string $ability
|
||||
* @return bool|null
|
||||
*/
|
||||
public function after(User $actor, $ability)
|
||||
{
|
||||
if ($actor->hasPermission('group.'.$ability)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -13,10 +13,10 @@ namespace Flarum\Core\Command;
|
||||
|
||||
use Flarum\Core\Access\AssertPermissionTrait;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\Core\Group;
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\Core\Validator\GroupValidator;
|
||||
use Flarum\Event\GroupWillBeSaved;
|
||||
use Flarum\Group\GroupValidator;
|
||||
use Flarum\Group\Event\Saving;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class CreateGroupHandler
|
||||
@@ -25,13 +25,13 @@ class CreateGroupHandler
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var GroupValidator
|
||||
* @var \Flarum\Group\GroupValidator
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
* @param GroupValidator $validator
|
||||
* @param \Flarum\Group\GroupValidator $validator
|
||||
*/
|
||||
public function __construct(Dispatcher $events, GroupValidator $validator)
|
||||
{
|
||||
@@ -41,7 +41,7 @@ class CreateGroupHandler
|
||||
|
||||
/**
|
||||
* @param CreateGroup $command
|
||||
* @return Group
|
||||
* @return \Flarum\Group\Group
|
||||
* @throws \Flarum\User\Exception\PermissionDeniedException
|
||||
*/
|
||||
public function handle(CreateGroup $command)
|
||||
@@ -59,7 +59,7 @@ class CreateGroupHandler
|
||||
);
|
||||
|
||||
$this->events->fire(
|
||||
new GroupWillBeSaved($group, $actor, $data)
|
||||
new Saving($group, $actor, $data)
|
||||
);
|
||||
|
||||
$this->validator->assertValid($group->getAttributes());
|
||||
|
@@ -13,9 +13,9 @@ namespace Flarum\Core\Command;
|
||||
|
||||
use Flarum\Core\Access\AssertPermissionTrait;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\Core\Repository\GroupRepository;
|
||||
use Flarum\Group\GroupRepository;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\Event\GroupWillBeDeleted;
|
||||
use Flarum\Group\Event\Deleting;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class DeleteGroupHandler
|
||||
@@ -39,7 +39,7 @@ class DeleteGroupHandler
|
||||
|
||||
/**
|
||||
* @param DeleteGroup $command
|
||||
* @return \Flarum\Core\Group
|
||||
* @return \Flarum\Group\Group
|
||||
* @throws PermissionDeniedException
|
||||
*/
|
||||
public function handle(DeleteGroup $command)
|
||||
@@ -51,7 +51,7 @@ class DeleteGroupHandler
|
||||
$this->assertCan($actor, 'delete', $group);
|
||||
|
||||
$this->events->fire(
|
||||
new GroupWillBeDeleted($group, $actor, $command->data)
|
||||
new Deleting($group, $actor, $command->data)
|
||||
);
|
||||
|
||||
$group->delete();
|
||||
|
@@ -13,11 +13,11 @@ namespace Flarum\Core\Command;
|
||||
|
||||
use Flarum\Core\Access\AssertPermissionTrait;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\Core\Group;
|
||||
use Flarum\Core\Repository\GroupRepository;
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\Group\GroupRepository;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\Core\Validator\GroupValidator;
|
||||
use Flarum\Event\GroupWillBeSaved;
|
||||
use Flarum\Group\GroupValidator;
|
||||
use Flarum\Group\Event\Saving;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class EditGroupHandler
|
||||
@@ -26,7 +26,7 @@ class EditGroupHandler
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var GroupRepository
|
||||
* @var \Flarum\Group\GroupRepository
|
||||
*/
|
||||
protected $groups;
|
||||
|
||||
@@ -76,7 +76,7 @@ class EditGroupHandler
|
||||
}
|
||||
|
||||
$this->events->fire(
|
||||
new GroupWillBeSaved($group, $actor, $data)
|
||||
new Saving($group, $actor, $data)
|
||||
);
|
||||
|
||||
$this->validator->assertValid($group->getDirty());
|
||||
|
@@ -114,7 +114,7 @@ class CoreServiceProvider extends AbstractServiceProvider
|
||||
$events->subscribe('Flarum\Discussion\DiscussionRenamedNotifier');
|
||||
|
||||
$events->subscribe('Flarum\Discussion\DiscussionPolicy');
|
||||
$events->subscribe('Flarum\Core\Access\GroupPolicy');
|
||||
$events->subscribe('Flarum\Group\GroupPolicy');
|
||||
$events->subscribe('Flarum\Post\PostPolicy');
|
||||
$events->subscribe('Flarum\User\UserPolicy');
|
||||
|
||||
|
@@ -1,135 +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\Core;
|
||||
|
||||
use Flarum\Foundation\EventGeneratorTrait;
|
||||
use Flarum\Database\ScopeVisibilityTrait;
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Event\GroupWasCreated;
|
||||
use Flarum\Event\GroupWasDeleted;
|
||||
use Flarum\Event\GroupWasRenamed;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name_singular
|
||||
* @property string $name_plural
|
||||
* @property string|null $color
|
||||
* @property string|null $icon
|
||||
* @property \Illuminate\Database\Eloquent\Collection $users
|
||||
* @property \Illuminate\Database\Eloquent\Collection $permissions
|
||||
*/
|
||||
class Group extends AbstractModel
|
||||
{
|
||||
use EventGeneratorTrait;
|
||||
use ScopeVisibilityTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $table = 'groups';
|
||||
|
||||
/**
|
||||
* The ID of the administrator group.
|
||||
*/
|
||||
const ADMINISTRATOR_ID = 1;
|
||||
|
||||
/**
|
||||
* The ID of the guest group.
|
||||
*/
|
||||
const GUEST_ID = 2;
|
||||
|
||||
/**
|
||||
* The ID of the member group.
|
||||
*/
|
||||
const MEMBER_ID = 3;
|
||||
|
||||
/**
|
||||
* The ID of the mod group.
|
||||
*/
|
||||
const MODERATOR_ID = 4;
|
||||
|
||||
/**
|
||||
* Boot the model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::deleted(function (Group $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;
|
||||
|
||||
$this->raise(new GroupWasRenamed($this));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the group's users.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function users()
|
||||
{
|
||||
return $this->belongsToMany('Flarum\User\User', 'users_groups');
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the group's permissions.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function permissions()
|
||||
{
|
||||
return $this->hasMany('Flarum\Core\Permission');
|
||||
}
|
||||
}
|
@@ -1,66 +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\Core;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* @todo document database columns with @property
|
||||
*/
|
||||
class Permission extends AbstractModel
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $table = 'permissions';
|
||||
|
||||
/**
|
||||
* Define the relationship with the group that this permission is for.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function group()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Group', 'group_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the keys for a save update query.
|
||||
*
|
||||
* @param Builder $query
|
||||
* @return Builder
|
||||
*/
|
||||
protected function setKeysForSaveQuery(Builder $query)
|
||||
{
|
||||
$query->where('group_id', $this->group_id)
|
||||
->where('permission', $this->permission);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a map of permissions to the group IDs that have them.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public static function map()
|
||||
{
|
||||
$permissions = [];
|
||||
|
||||
foreach (static::get() as $permission) {
|
||||
$permissions[$permission->permission][] = (string) $permission->group_id;
|
||||
}
|
||||
|
||||
return $permissions;
|
||||
}
|
||||
}
|
@@ -1,75 +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\Core\Repository;
|
||||
|
||||
use Flarum\Core\Group;
|
||||
use Flarum\User\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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a group by name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return User|null
|
||||
*/
|
||||
public function findByName($name, User $actor = null)
|
||||
{
|
||||
$query = Group::where('name_singular', $name)->orWhere('name_plural', $name);
|
||||
|
||||
return $this->scopeVisibleTo($query, $actor)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
@@ -1,22 +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\Core\Validator;
|
||||
|
||||
use Flarum\Foundation\AbstractValidator;
|
||||
|
||||
class GroupValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'name_singular' => ['required'],
|
||||
'name_plural' => ['required']
|
||||
];
|
||||
}
|
Reference in New Issue
Block a user