role basics

This commit is contained in:
xiaomlove
2022-08-19 15:30:16 +08:00
parent e013c90c55
commit 05f281b7e3
26 changed files with 81 additions and 497 deletions
+5 -4
View File
@@ -13,7 +13,6 @@ 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;
@@ -39,14 +38,14 @@ use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use JeroenG\Explorer\Domain\Syntax\Matching;
use JeroenG\Explorer\Infrastructure\Scout\ElasticEngine;
use League\Flysystem\StorageAttributes;
use Nexus\Database\NexusDB;
use Nexus\Imdb\Imdb;
use NexusPlugin\Menu\Filament\MenuItemResource\Pages\ManageMenuItems;
use NexusPlugin\Menu\MenuRepository;
use NexusPlugin\Menu\Models\MenuItem;
use NexusPlugin\Permission\Models\Permission;
use NexusPlugin\Permission\Models\Role;
use NexusPlugin\PostLike\PostLikeRepository;
use NexusPlugin\StickyPromotion\Models\StickyPromotion;
use NexusPlugin\StickyPromotion\Models\StickyPromotionParticipator;
@@ -87,7 +86,9 @@ class Test extends Command
*/
public function handle()
{
Role::initClassRoles();
$user = User::query()->find(10003);
$permissions = $user->rolePermissions;
dd($permissions);
}
@@ -1,68 +0,0 @@
<?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('/'),
];
}
}
@@ -1,21 +0,0 @@
<?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(),
];
}
}
@@ -1,79 +0,0 @@
<?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'),
];
}
}
@@ -1,12 +0,0 @@
<?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;
}
@@ -1,19 +0,0 @@
<?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(),
];
}
}
@@ -1,20 +0,0 @@
<?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(),
];
}
}
@@ -75,7 +75,7 @@ class UserProfile extends Page
$actions[] = $this->buildEnableDisableAction();
$actions[] = $this->buildEnableDisableDownloadPrivilegesAction();
}
return $actions;
return apply_filter('user_profile_actions', $actions);
}
private function buildEnableDisableAction(): Actions\Action
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace App\Models;
class Permission extends NexusModel
{
public $timestamps = true;
protected $fillable = ['name', ];
}
-37
View File
@@ -1,37 +0,0 @@
<?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);
}
}
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace App\Models;
class RolePermission extends NexusModel
{
public $timestamps = true;
protected $fillable = ['role_id', 'permission_id'];
}
+4
View File
@@ -11,6 +11,10 @@ class Setting extends NexusModel
public $timestamps = true;
public static array $permissionDegeneration = [
'torrent-approval' => 'torrentmanage',
];
/**
* get setting autoload = yes with cache
*
+13 -2
View File
@@ -17,6 +17,8 @@ use Laravel\Sanctum\HasApiTokens;
use Nexus\Database\NexusDB;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasName;
use NexusPlugin\Permission\Models\Permission;
use NexusPlugin\Permission\Models\Role;
class User extends Authenticatable implements FilamentUser, HasName
{
@@ -98,7 +100,7 @@ class User extends Authenticatable implements FilamentUser, HasName
public static function getClassText($class)
{
if (!isset(self::$classes[$class])) {
if (!is_numeric($class)|| !isset(self::$classes[$class])) {
return '';
}
if ($class >= self::CLASS_VIP) {
@@ -113,6 +115,15 @@ class User extends Authenticatable implements FilamentUser, HasName
return $classText;
}
public static function listClass(): array
{
$result = [];
foreach (self::$classes as $class => $info) {
$result[$class] = self::getClassText($class);
}
return $result;
}
public function canAccessFilament(): bool
{
return $this->canAccessAdmin();
@@ -463,7 +474,7 @@ class User extends Authenticatable implements FilamentUser, HasName
return $this->belongsToMany(Role::class, 'user_roles', 'uid', 'role_id')->withTimestamps();
}
public function permissions()
public function directPermissions()
{
return $this->belongsToMany(Permission::class, 'user_permissions', 'uid', 'permission_id')->withTimestamps();
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace App\Models;
class UserPermission extends NexusModel
{
public $timestamps = true;
protected $fillable = ['uid', 'permission_id'];
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace App\Models;
class UserRole extends NexusModel
{
public $timestamps = true;
protected $fillable = ['uid', 'role_id'];
}
+6 -5
View File
@@ -149,13 +149,14 @@ class UserRepository extends BaseRepository
return true;
}
/**
* @deprecated use User::listClass() instead !
*
* @return array
*/
public function listClass()
{
$out = [];
foreach(User::$classes as $key => $value) {
$out[(string)$key] = $value['text'];
}
return $out;
return User::listClass();
}
public function disableUser(User $operator, $uid, $reason)
@@ -1,33 +0,0 @@
<?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');
}
};
@@ -1,32 +0,0 @@
<?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');
}
};
@@ -1,33 +0,0 @@
<?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');
}
};
@@ -1,33 +0,0 @@
<?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');
}
};
@@ -1,34 +0,0 @@
<?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');
}
};
+13 -4
View File
@@ -482,7 +482,7 @@ function get_user_id()
return auth()->user()->id ?? 0;
}
function get_user_class_name($class, $compact = false, $b_colored = false, $I18N = false)
function get_user_class_name($class, $compact = false, $b_colored = false, $I18N = false, array $options = [])
{
if (!IN_NEXUS) {
return \App\Models\User::getClassName($class, $compact, $b_colored, $I18N);
@@ -557,9 +557,14 @@ function get_user_class_name($class, $compact = false, $b_colored = false, $I18N
case UC_SYSOP: {$class_name_color = $en_lang_functions['text_sysops']; break;}
case UC_STAFFLEADER: {$class_name_color = $en_lang_functions['text_staff_leader']; break;}
}
$class_name = ( $compact == true ? str_replace(" ", "",$class_name) : $class_name);
if ($class_name) return ($b_colored == true ? "<b class='" . str_replace(" ", "",$class_name_color) . "_Name'>" . $class_name . "</b>" : $class_name);
if (isset($options['uid'], $options['with_role'])) {
$class_name = implode('&nbsp;|&nbsp;', apply_filter('user_class_name', [$class_name], $options['uid']));
}
if ($class_name && $b_colored) {
$class_name = "<b class='" . str_replace(" ", "",$class_name_color) . "_Name'>" . $class_name . "</b>";
}
return $class_name;
}
function is_valid_user_class($class)
@@ -3748,7 +3753,11 @@ function get_username($id, $big = false, $link = true, $bold = true, $target = f
}
$href = getSchemeAndHttpHost() . "/userdetails.php?id=$id";
$username = ($link == true ? "<a ". $link_ext . " href=\"" . $href . "\"" . ($target == true ? " target=\"_blank\"" : "") . " class='". get_user_class_name($arr['class'],true) . "_Name'>" . $username . "</a>" : $username) . $pics . ($withtitle == true ? " (" . ($arr['title'] == "" ? get_user_class_name($arr['class'],false,true,true) : "<span class='".get_user_class_name($arr['class'],true) . "_Name'><b>".htmlspecialchars($arr['title'])) . "</b></span>)" : "");
$options = [
'uid' => $id,
'with_role' => true,
];
$username = ($link == true ? "<a ". $link_ext . " href=\"" . $href . "\"" . ($target == true ? " target=\"_blank\"" : "") . " class='". get_user_class_name($arr['class'],true, false, false, $options) . "_Name'>" . $username . "</a>" : $username) . $pics . ($withtitle == true ? " (" . ($arr['title'] == "" ? get_user_class_name($arr['class'],false,true,true, $options) : "<span class='".get_user_class_name($arr['class'],true, false, false, $options) . "_Name'><b>".htmlspecialchars($arr['title'])) . "</b></span>)" : "");
$username = "<span class=\"nowrap\">" . ( $bracket == true ? "(" . $username . ")" : $username) . "$medalHtml</span>";
}
+25
View File
@@ -895,3 +895,28 @@ function clear_user_cache($uid, $passkey = '')
\Nexus\Database\NexusDB::cache_del('user_passkey_'.$passkey.'_content');
}
}
function clear_setting_cache()
{
\Nexus\Database\NexusDB::cache_del('nexus_settings_in_laravel');
\Nexus\Database\NexusDB::cache_del('nexus_settings_in_nexus');
}
function user_can($permission, $uid = 0): bool
{
if ($uid == 0) {
$uid = get_user_id();
}
if ($uid <= 0) {
return false;
}
$result = apply_filter('nexus_user_can', null, $permission, $uid);
if (is_bool($result)) {
return $result;
}
if (isset(\App\Models\Setting::$permissionDegeneration[$permission])) {
$permission = \App\Models\Setting::$permissionDegeneration[$permission];
}
$requireClass = get_setting("authority.$permission");
return is_numeric($requireClass) && $requireClass < get_user_class();
}
+3 -2
View File
@@ -50,13 +50,13 @@ $classes = array_chunk(\App\Models\User::$classes, 4, true);
</tr>
<tr><td class="rowhead" valign="top">Amount </td><td class="rowfollow"><input type=text name=amount size=10></td></tr>
<tr>
<td class="rowhead" valign="top">Add to</td><td class="rowfollow">
<td class="rowhead" valign="top">Add to Class</td><td class="rowfollow">
<table style="border: 0" width="100%" cellpadding="0" cellspacing="0">
<?php
foreach ($classes as $chunk) {
printf('<tr>');
foreach ($chunk as $class => $info) {
printf('<td style="border: 0"><label><input type="checkbox" name="clases[]" value="%s" />%s</label></td>', $class, $info['text']);
printf('<td style="border: 0"><label><input type="checkbox" name="classes[]" value="%s" />%s</label></td>', $class, $info['text']);
}
printf('</tr>');
}
@@ -64,6 +64,7 @@ $classes = array_chunk(\App\Models\User::$classes, 4, true);
</table>
</td>
</tr>
<?php do_action('increment_bulk_filters') ?>
<tr><td class="rowhead" valign="top">Subject </td><td class="rowfollow"><input type=text name=subject size=82></td></tr>
<tr><td class="rowhead" valign="top">Reason </td><td class="rowfollow"><textarea name=msg cols=80 rows=5><?php echo $body ?? ''?></textarea></td></tr>
<tr>
+1 -2
View File
@@ -11,8 +11,7 @@ permissiondenied();
function go_back()
{
global $lang_settings;
\Nexus\Database\NexusDB::cache_del('nexus_settings_in_laravel');
\Nexus\Database\NexusDB::cache_del('nexus_settings_in_nexus');
clear_setting_cache();
stdmsg($lang_settings['std_message'], $lang_settings['std_click']."<a class=\"altlink\" href=\"settings.php\">".$lang_settings['std_here']."</a>".$lang_settings['std_to_go_back']);
}
+10 -12
View File
@@ -29,25 +29,23 @@ if (!isset($validTypeMap[$type])) {
if ($type == 'uploaded') {
$amount = sqlesc(getsize_int($amount,"G"));
}
$updateset = $_POST['clases'];
if (is_array($updateset)) {
foreach ($updateset as $class) {
if (!is_valid_id($class) && $class != 0)
stderr("Error","Invalid Class");
}
}else{
if (!is_valid_id($updateset) && $updateset != 0)
stderr("Error","Invalid Class");
}
$subject = trim($_POST['subject']);
$size = 10000;
$page = 1;
set_time_limit(300);
$classStr = implode(",", $updateset);
$conditions = [];
if (!empty($_POST['classes'])) {
$conditions[] = "class IN (" . implode(', ', $_POST['classes']) . ")";
}
$conditions = apply_filter("increment_bulk_conditions", $conditions, $_POST);
if (empty($conditions)) {
stderr("Error","No valid filter");
}
$whereStr = implode(' OR ', $conditions);
while (true) {
$msgValues = $idArr = [];
$offset = ($page - 1) * $size;
$query = sql_query("SELECT id FROM users WHERE class IN ($classStr) and `enabled` = 'yes' and `status` = 'confirmed' limit $offset, $size");
$query = sql_query("SELECT id FROM users WHERE ($whereStr) and `enabled` = 'yes' and `status` = 'confirmed' limit $offset, $size");
while($dat=mysql_fetch_assoc($query))
{
$idArr[] = $dat['id'];