mirror of
https://github.com/flarum/core.git
synced 2025-10-12 15:34:26 +02:00
Extract Flarum\Group namespace
This commit is contained in:
38
src/Group/Event/Created.php
Normal file
38
src/Group/Event/Created.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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\Group\Event;
|
||||
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\User\User;
|
||||
|
||||
class Created
|
||||
{
|
||||
/**
|
||||
* @var \Flarum\Group\Group
|
||||
*/
|
||||
public $group;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* @param Group $group
|
||||
* @param User $actor
|
||||
*/
|
||||
public function __construct(Group $group, User $actor = null)
|
||||
{
|
||||
$this->group = $group;
|
||||
$this->actor = $actor;
|
||||
}
|
||||
}
|
38
src/Group/Event/Deleted.php
Normal file
38
src/Group/Event/Deleted.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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\Group\Event;
|
||||
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\User\User;
|
||||
|
||||
class Deleted
|
||||
{
|
||||
/**
|
||||
* @var \Flarum\Group\Group
|
||||
*/
|
||||
public $group;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* @param \Flarum\Group\Group $group
|
||||
* @param User $actor
|
||||
*/
|
||||
public function __construct(Group $group, User $actor = null)
|
||||
{
|
||||
$this->group = $group;
|
||||
$this->actor = $actor;
|
||||
}
|
||||
}
|
51
src/Group/Event/Deleting.php
Normal file
51
src/Group/Event/Deleting.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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\Group\Event;
|
||||
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\User\User;
|
||||
|
||||
class Deleting
|
||||
{
|
||||
/**
|
||||
* The group that will be deleted.
|
||||
*
|
||||
* @var Group
|
||||
*/
|
||||
public $group;
|
||||
|
||||
/**
|
||||
* The user who is performing the action.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* Any user input associated with the command.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* @param Group $group The group that will be deleted.
|
||||
* @param User $actor The user performing the action.
|
||||
* @param array $data Any user input associated with the command.
|
||||
*/
|
||||
public function __construct(Group $group, User $actor, array $data)
|
||||
{
|
||||
$this->group = $group;
|
||||
$this->actor = $actor;
|
||||
$this->data = $data;
|
||||
}
|
||||
}
|
38
src/Group/Event/Renamed.php
Normal file
38
src/Group/Event/Renamed.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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\Group\Event;
|
||||
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\User\User;
|
||||
|
||||
class Renamed
|
||||
{
|
||||
/**
|
||||
* @var \Flarum\Group\Group
|
||||
*/
|
||||
public $group;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* @param \Flarum\Group\Group $group
|
||||
* @param User $actor
|
||||
*/
|
||||
public function __construct(Group $group, User $actor = null)
|
||||
{
|
||||
$this->group = $group;
|
||||
$this->actor = $actor;
|
||||
}
|
||||
}
|
51
src/Group/Event/Saving.php
Normal file
51
src/Group/Event/Saving.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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\Group\Event;
|
||||
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\User\User;
|
||||
|
||||
class Saving
|
||||
{
|
||||
/**
|
||||
* The group that will be saved.
|
||||
*
|
||||
* @var \Flarum\Group\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;
|
||||
}
|
||||
}
|
135
src/Group/Group.php
Executable file
135
src/Group/Group.php
Executable file
@@ -0,0 +1,135 @@
|
||||
<?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\Group;
|
||||
|
||||
use Flarum\Foundation\EventGeneratorTrait;
|
||||
use Flarum\Database\ScopeVisibilityTrait;
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Group\Event\Created;
|
||||
use Flarum\Group\Event\Deleting;
|
||||
use Flarum\Group\Event\Renamed;
|
||||
|
||||
/**
|
||||
* @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 Deleting($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 Created($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 Renamed($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\Group\Permission');
|
||||
}
|
||||
}
|
35
src/Group/GroupPolicy.php
Normal file
35
src/Group/GroupPolicy.php
Normal 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\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;
|
||||
}
|
||||
}
|
||||
}
|
74
src/Group/GroupRepository.php
Normal file
74
src/Group/GroupRepository.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?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\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 \Flarum\Group\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;
|
||||
}
|
||||
}
|
22
src/Group/GroupValidator.php
Normal file
22
src/Group/GroupValidator.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?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\Group;
|
||||
|
||||
use Flarum\Foundation\AbstractValidator;
|
||||
|
||||
class GroupValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'name_singular' => ['required'],
|
||||
'name_plural' => ['required']
|
||||
];
|
||||
}
|
66
src/Group/Permission.php
Executable file
66
src/Group/Permission.php
Executable file
@@ -0,0 +1,66 @@
|
||||
<?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\Group;
|
||||
|
||||
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\Group\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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user