cresate role basic tables

This commit is contained in:
xiaomlove
2022-08-17 16:01:55 +08:00
parent 78a25071d1
commit c7ac4ba41e
20 changed files with 492 additions and 7 deletions

View File

@@ -13,6 +13,7 @@ use App\Models\ExamUser;
use App\Models\HitAndRun;
use App\Models\Medal;
use App\Models\Peer;
use App\Models\Role;
use App\Models\SearchBox;
use App\Models\Snatch;
use App\Models\Tag;
@@ -86,8 +87,7 @@ class Test extends Command
*/
public function handle()
{
$r = date('P');
dd($r);
Role::initClassRoles();
}

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Filament\Resources\Permission;
use App\Filament\Resources\Permission\PermissionResource\Pages;
use App\Filament\Resources\Permission\PermissionResource\RelationManagers;
use App\Models\Permission;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class PermissionResource extends Resource
{
protected static ?string $model = Permission::class;
protected static ?string $navigationIcon = 'heroicon-o-lock-open';
protected static ?string $navigationGroup = 'Permission';
protected static ?int $navigationSort = 2;
protected static function getNavigationLabel(): string
{
return __('admin.sidebar.permissions');
}
public static function getBreadcrumb(): string
{
return self::getNavigationLabel();
}
public static function form(Form $form): Form
{
return $form
->schema([
//
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
//
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}
public static function getPages(): array
{
return [
'index' => Pages\ManagePermissions::route('/'),
];
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Filament\Resources\Permission\PermissionResource\Pages;
use App\Filament\Resources\Permission\PermissionResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ManageRecords;
class ManagePermissions extends ManageRecords
{
protected static string $resource = PermissionResource::class;
protected ?string $maxContentWidth = 'full';
protected function getActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace App\Filament\Resources\Permission;
use App\Filament\Resources\Permission\RoleResource\Pages;
use App\Filament\Resources\Permission\RoleResource\RelationManagers;
use App\Models\Role;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class RoleResource extends Resource
{
protected static ?string $model = Role::class;
protected static ?string $navigationIcon = 'heroicon-o-user-group';
protected static ?string $navigationGroup = 'Permission';
protected static ?int $navigationSort = 1;
protected static function getNavigationLabel(): string
{
return __('admin.sidebar.roles');
}
public static function getBreadcrumb(): string
{
return self::getNavigationLabel();
}
public static function form(Form $form): Form
{
return $form
->schema([
//
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id'),
Tables\Columns\TextColumn::make('name')->label(__('label.name')),
Tables\Columns\TextColumn::make('classText')->label(__('label.role.class')),
Tables\Columns\TextColumn::make('updated_at')->label(__('label.updated_at')),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListRoles::route('/'),
'create' => Pages\CreateRole::route('/create'),
'edit' => Pages\EditRole::route('/{record}/edit'),
];
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Filament\Resources\Permission\RoleResource\Pages;
use App\Filament\Resources\Permission\RoleResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\CreateRecord;
class CreateRole extends CreateRecord
{
protected static string $resource = RoleResource::class;
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\Permission\RoleResource\Pages;
use App\Filament\Resources\Permission\RoleResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\EditRecord;
class EditRole extends EditRecord
{
protected static string $resource = RoleResource::class;
protected function getActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Filament\Resources\Permission\RoleResource\Pages;
use App\Filament\PageList;
use App\Filament\Resources\Permission\RoleResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ListRecords;
class ListRoles extends PageList
{
protected static string $resource = RoleResource::class;
protected function getActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}

11
app/Models/Permission.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
namespace App\Models;
class Permission extends NexusModel
{
public $timestamps = true;
protected $fillable = ['name', ];
}

37
app/Models/Role.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
namespace App\Models;
class Role extends NexusModel
{
public $timestamps = true;
protected $fillable = ['name', 'class'];
public function permissions(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Permission::class, 'role_id');
}
public function getClassTextAttribute()
{
if ($this->class < 0) {
return '';
}
return User::getClassText($this->class);
}
public static function initClassRoles()
{
foreach (User::$classes as $class => $info) {
$attributes = [
'class' => $class
];
$values = [
'name' => $info['text'],
];
Role::query()->firstOrCreate($attributes, $values);
}
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Models;
class RolePermission extends NexusModel
{
public $timestamps = true;
protected $fillable = ['role_id', 'permission_id'];
}

View File

@@ -93,11 +93,20 @@ class User extends Authenticatable implements FilamentUser, HasName
public function getClassTextAttribute(): string
{
if (!isset(self::$classes[$this->class]['text'])) {
return self::getClassText($this->class);
}
public static function getClassText($class)
{
if (!isset(self::$classes[$class])) {
return '';
}
$classText = self::$classes[$this->class]['text'];
$alias = Setting::get("account.{$this->class}_alias");
if ($class >= self::CLASS_VIP) {
$classText = nexus_trans('user.class_names.' . $class);
} else {
$classText = self::$classes[$class]['text'];
}
$alias = Setting::get("account.{$class}_alias");
if (!empty($alias)) {
$classText .= "({$alias})";
}
@@ -449,6 +458,16 @@ class User extends Authenticatable implements FilamentUser, HasName
return $this->hasMany(UsernameChangeLog::class, 'uid');
}
public function roles()
{
return $this->belongsToMany(Role::class, 'user_roles', 'uid', 'role_id')->withTimestamps();
}
public function permissions()
{
return $this->belongsToMany(Permission::class, 'user_permissions', 'uid', 'permission_id')->withTimestamps();
}
public function getAvatarAttribute($value)
{
if ($value) {

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Models;
class UserPermission extends NexusModel
{
public $timestamps = true;
protected $fillable = ['uid', 'permission_id'];
}

11
app/Models/UserRole.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
namespace App\Models;
class UserRole extends NexusModel
{
public $timestamps = true;
protected $fillable = ['uid', 'role_id'];
}

View File

@@ -40,6 +40,7 @@ class AppServiceProvider extends ServiceProvider
'User',
'Torrent',
'Other',
'Permission',
'System',
]);
});

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('class')->default(-1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
};

View File

@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('permissions', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('permissions');
}
};

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('role_permissions', function (Blueprint $table) {
$table->id();
$table->integer('role_id');
$table->integer('permission_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('role_permissions');
}
};

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_roles', function (Blueprint $table) {
$table->id();
$table->integer('uid');
$table->integer('role_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_roles');
}
};

View File

@@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_permissions', function (Blueprint $table) {
$table->id();
$table->integer('uid');
$table->integer('permission_id');
$table->text('payload');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_permissions');
}
};

View File

@@ -15,8 +15,6 @@ return [
'claims' => '用户认领',
'torrent_list' => '种子',
'torrent_state' => '全站优惠',
'roles_list' => '角色',
'ability_list' => '权限',
'seed_box_records' => 'SeedBox',
'upload_speed' => '上行带宽',
'download_speed' => '下行带宽',
@@ -24,6 +22,8 @@ return [
'menu' => '自定义菜单',
'username_change_log' => '改名记录',
'torrent_deny_reason' => '拒绝原因',
'roles' => '角色',
'permissions' => '权限',
],
'resources' => [
'agent_allow' => [