chore: update seeders, add MockDataSeeder and AssignAllPermissions command
- DatabaseSeeder: replace old seeders with MockDataSeeder - RolesSeeder: update role definitions - Add AssignAllPermissions artisan command - Register MockDataSeeder in AppServiceProvider - Register command in ConsoleKernel
This commit is contained in:
@@ -24,3 +24,4 @@ public/vikon_core
|
||||
QWEN.md
|
||||
AGENTS.md
|
||||
docs
|
||||
.phpunit.result.cache
|
||||
File diff suppressed because one or more lines are too long
@@ -49,8 +49,10 @@ use Carbon\Carbon;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Support\Facades\FilamentView;
|
||||
use Filament\Tables\View\TablesRenderHook;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Vite;
|
||||
use Illuminate\Support\Facades\Str;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Sleep;
|
||||
use Intervention\Image\Facades\Image;
|
||||
@@ -70,6 +72,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
self::configureFactoryResolution();
|
||||
self::setObserversByModel();
|
||||
self::setLocaleTime();
|
||||
Model::preventLazyLoading(!app()->isProduction());
|
||||
@@ -81,6 +84,21 @@ class AppServiceProvider extends ServiceProvider
|
||||
});
|
||||
}
|
||||
|
||||
private static function configureFactoryResolution(): void
|
||||
{
|
||||
Factory::guessFactoryNamesUsing(function (string $modelName) {
|
||||
$appNamespace = 'App\\';
|
||||
|
||||
if (str_starts_with($modelName, $appNamespace.'Models\\')) {
|
||||
$modelName = substr($modelName, strlen($appNamespace.'Models\\'));
|
||||
} else {
|
||||
$modelName = substr($modelName, strlen($appNamespace));
|
||||
}
|
||||
|
||||
return 'Database\\Factories\\'.$modelName.'Factory';
|
||||
});
|
||||
}
|
||||
|
||||
private static function setObserversByModel() : void {
|
||||
Page::observe(PageObserver::class);
|
||||
Post::observe(PostObserver::class);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Ship\Commands;
|
||||
|
||||
use App\Ship\Abstracts\Commands\ConsoleCommand as AbstractConsoleCommand;
|
||||
use App\Containers\User\Models\User;
|
||||
use BezhanSalleh\FilamentShield\FilamentShield;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class AssignAllPermissions extends AbstractConsoleCommand
|
||||
{
|
||||
protected $signature = 'user:assign-all-permissions {email?}';
|
||||
|
||||
protected $description = 'Assign super_admin role with all permissions to a user';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$email = $this->argument('email') ?: 'admin@ntspi.ru';
|
||||
|
||||
$user = User::where('email', $email)->first();
|
||||
|
||||
if (!$user) {
|
||||
$this->error("User [{$email}] not found.");
|
||||
return static::FAILURE;
|
||||
}
|
||||
|
||||
$role = Role::firstOrCreate(
|
||||
['name' => 'super_admin', 'guard_name' => 'web']
|
||||
);
|
||||
|
||||
$allPerms = Permission::where('guard_name', 'web')->get();
|
||||
$role->syncPermissions($allPerms);
|
||||
|
||||
$user->syncRoles(['super_admin']);
|
||||
|
||||
$this->info("User [{$email}] assigned to [super_admin] with {$allPerms->count()} permissions.");
|
||||
return static::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Ship\Kernels;
|
||||
use AlxDorosenco\PortoForLaravel\Loaders\CommandsLoader;
|
||||
use AlxDorosenco\PortoForLaravel\Loaders\RoutesLoader;
|
||||
use App\Containers\Dashboard\Commands\FetchEmailNewsCommand;
|
||||
use App\Ship\Commands\AssignAllPermissions;
|
||||
use App\Ship\Commands\InitRoles;
|
||||
use App\Ship\Commands\SyncDashboardPermissions;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
@@ -33,6 +34,7 @@ class ConsoleKernel extends LaravelConsoleKernel
|
||||
}
|
||||
|
||||
protected $commands = [
|
||||
AssignAllPermissions::class,
|
||||
InitRoles::class,
|
||||
FetchEmailNewsCommand::class,
|
||||
SyncDashboardPermissions::class,
|
||||
|
||||
@@ -2,107 +2,14 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
use App\Containers\Article\Models\Post;
|
||||
use App\Containers\Schedule\Models\EducationalGroup;
|
||||
use App\Containers\Schedule\Models\Schedule;
|
||||
use App\Containers\Widget\Data\Seeders\SlideSeeder;
|
||||
use App\Ship\Seeders\ContactWidgetSeeder;
|
||||
use App\Ship\Seeders\PageReferenceListSeeder;
|
||||
use App\Ship\Seeders\SliderSeeder;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class DatabaseSeeder extends Seeder
|
||||
{
|
||||
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
|
||||
$this->call([
|
||||
ContactWidgetSeeder::class,
|
||||
PageReferenceListSeeder::class,
|
||||
SliderSeeder::class,
|
||||
SlideSeeder::class,
|
||||
MockDataSeeder::class,
|
||||
]);
|
||||
|
||||
|
||||
|
||||
// $faker = Factory::create();
|
||||
// User::create([
|
||||
// 'name' => 'Failj',
|
||||
// 'email' => 'Failj@bk.ru',
|
||||
// 'slug' => 'failj',
|
||||
// 'password' => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
|
||||
// ]);
|
||||
// User::factory()->count(50)->has(UserDetail::factory())->create();
|
||||
// EventCategory::factory()->count(10)->create();
|
||||
// Category::factory()->count(80)->create();
|
||||
// Post::factory()->count(1000)->create();
|
||||
// Event::factory()->count(200)->create();
|
||||
// $faculties = Faculty::factory()->count(6)->create();
|
||||
|
||||
// Затем создаем 40 групп, распределяя их по созданным факультетам
|
||||
// EducationalGroup::factory()
|
||||
// ->count(40)
|
||||
// ->sequence(fn () => [
|
||||
// 'faculty_id' => $faculties->random()->id
|
||||
// ])
|
||||
// ->has(Schedule::factory()->count(90 / 40)) // Распределяем 90 расписаний по 40 группам
|
||||
// ->create();
|
||||
// Department::factory()->count(12)->create();
|
||||
//
|
||||
// for ($i = 0; $i < 20; $i++) {
|
||||
// DB::table('workers_departments')->insert([
|
||||
// 'user_id' => User::inRandomOrder()->first()->id,
|
||||
// 'department_id' => Department::inRandomOrder()->first()->id,
|
||||
// 'position' => $faker->word,
|
||||
// 'created_at' => now(),
|
||||
// 'updated_at' => now(),
|
||||
// ]);
|
||||
// }
|
||||
//
|
||||
// for ($i = 0; $i < 20; $i++) {
|
||||
// DB::table('workers_faculties')->insert([
|
||||
// 'user_id' => User::inRandomOrder()->first()->id,
|
||||
// 'faculty_id' => Faculty::inRandomOrder()->first()->id,
|
||||
// 'position' => $faker->word,
|
||||
// 'created_at' => now(),
|
||||
// 'updated_at' => now(),
|
||||
// ]);
|
||||
// }
|
||||
//
|
||||
// for ($i = 0; $i < 30; $i++) {
|
||||
// DB::table('teachers_departments')->insert([
|
||||
// 'user_id' => User::inRandomOrder()->first()->id,
|
||||
// 'department_id' => Department::inRandomOrder()->first()->id,
|
||||
// 'teaching_position' => $faker->word,
|
||||
// 'created_at' => now(),
|
||||
// 'updated_at' => now(),
|
||||
// ]);
|
||||
// }
|
||||
// $this->call([RolesSeeder::class]);
|
||||
// $users = [
|
||||
// [
|
||||
// 'name' => 'Admin',
|
||||
// 'email' => 'Admin@mail.ru',
|
||||
// 'password' => 'R177p900',
|
||||
// 'role' => 'admin',
|
||||
// ],
|
||||
// [
|
||||
// 'name' => 'John',
|
||||
// 'email' => 'Test@mail.ru',
|
||||
// 'password' => 'R177p900',
|
||||
// 'role' => 'user',
|
||||
// ]
|
||||
// ];
|
||||
// foreach ($users as $user) {
|
||||
// $created_user = User::create([
|
||||
// 'name' => $user['name'],
|
||||
// 'email' => $user['email'],
|
||||
// 'password' => Hash::make($user['password']),
|
||||
// ]);
|
||||
// $created_user->assignRole($user['role']);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Containers\AdditionalEducation\Models\AdditionalEducation;
|
||||
use App\Containers\AdditionalEducation\Models\AdditionalEducationCategory;
|
||||
use App\Containers\AdditionalEducation\Models\DirectionAdditionalEducation;
|
||||
use App\Containers\AppStructure\Models\MainSection;
|
||||
use App\Containers\AppStructure\Models\Page;
|
||||
use App\Containers\AppStructure\Models\SubSection;
|
||||
use App\Containers\Article\Models\Category;
|
||||
use App\Containers\Article\Models\Post;
|
||||
use App\Containers\Article\Models\Tag;
|
||||
use App\Containers\Dashboard\Models\IntegrationCredential;
|
||||
use App\Containers\Education\Models\AdmissionCampaign;
|
||||
use App\Containers\Education\Models\AdmissionPlan;
|
||||
use App\Containers\Education\Models\DirectionStudy;
|
||||
use App\Containers\Education\Models\EducationalProgram;
|
||||
use App\Containers\Event\Models\Event;
|
||||
use App\Containers\Event\Models\EventCategory;
|
||||
use App\Containers\InstituteStructure\Models\Department;
|
||||
use App\Containers\InstituteStructure\Models\Division;
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
use App\Containers\Science\Models\AcademicJournal;
|
||||
use App\Containers\Science\Models\JournalIssue;
|
||||
use App\Containers\Schedule\Models\EducationalGroup;
|
||||
use App\Containers\Schedule\Models\Schedule;
|
||||
use App\Containers\User\Models\User;
|
||||
use App\Containers\User\Models\UserDetail;
|
||||
use App\Containers\Widget\Models\ContactWidget;
|
||||
use App\Containers\Widget\Models\CustomForm;
|
||||
use App\Containers\Widget\Models\CustomFormResponse;
|
||||
use App\Containers\Widget\Models\PageReferenceList;
|
||||
use App\Containers\Widget\Models\Slider;
|
||||
use App\Containers\Widget\Models\Slide;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class MockDataSeeder extends Seeder
|
||||
{
|
||||
use WithoutModelEvents;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$this->command?->info('Creating mock data...');
|
||||
|
||||
// 1. Users & Roles
|
||||
$this->createUsers();
|
||||
|
||||
// 2. Institute Structure
|
||||
$faculties = Faculty::factory()->count(8)->create();
|
||||
$departments = Department::factory()->count(15)->create();
|
||||
$divisions = Division::factory()->count(5)->create();
|
||||
|
||||
// 3. Pivot: workers_faculties, workers_departments, teachers_departments
|
||||
$this->attachWorkers($faculties, $departments);
|
||||
|
||||
// 4. Article
|
||||
$categories = Category::factory()->count(20)->create();
|
||||
Post::factory()->count(100)->create();
|
||||
Tag::factory()->count(30)->create();
|
||||
|
||||
// 5. Events
|
||||
$eventCategories = EventCategory::factory()->count(8)->create();
|
||||
Event::factory()->count(50)->create();
|
||||
|
||||
// 6. Education
|
||||
$directionStudies = DirectionStudy::factory()->count(10)->create();
|
||||
$programs = EducationalProgram::factory()->count(25)->create();
|
||||
$campaigns = AdmissionCampaign::factory()->count(5)->create();
|
||||
AdmissionPlan::factory()->count(30)->create();
|
||||
|
||||
// 7. Pivot: program_department
|
||||
$this->attachProgramsToDepartments($programs, $departments);
|
||||
|
||||
// 8. Additional Education
|
||||
$directions = DirectionAdditionalEducation::factory()->count(5)->create();
|
||||
$addCategories = AdditionalEducationCategory::factory()->count(12)->create();
|
||||
AdditionalEducation::factory()->count(20)->create();
|
||||
|
||||
// 9. Science
|
||||
$journals = AcademicJournal::factory()->count(4)->create();
|
||||
JournalIssue::factory()->count(15)->create();
|
||||
|
||||
// 10. Schedule
|
||||
$groups = EducationalGroup::factory()->count(40)->create();
|
||||
Schedule::factory()->count(90)->create();
|
||||
|
||||
// 11. Widgets
|
||||
Slider::create([
|
||||
'title' => 'Главный слайдер',
|
||||
'slug' => 'quos-velit-quisquam',
|
||||
'is_active' => true,
|
||||
]);
|
||||
Slider::factory()->count(4)->create();
|
||||
|
||||
$mainSlider = Slider::where('slug', 'quos-velit-quisquam')->first();
|
||||
Slide::factory()->count(5)->create([
|
||||
'slider_id' => $mainSlider->id,
|
||||
'is_active' => true,
|
||||
'start_time' => now()->subWeek(),
|
||||
'end_time' => now()->addMonth(),
|
||||
]);
|
||||
Slide::factory()->count(15)->create();
|
||||
|
||||
ContactWidget::create([
|
||||
'title' => 'Главная страница контакты',
|
||||
'slug' => 'glavnaia-stranica-kontakty',
|
||||
'content' => [
|
||||
[
|
||||
'title' => 'Контакты',
|
||||
'items' => [
|
||||
[
|
||||
'header' => 'Главный корпус',
|
||||
'details' => [
|
||||
['content' => '622031, Свердловская обл. Нижний Тагил ул. Красногвардейская, 57', 'url' => null],
|
||||
],
|
||||
],
|
||||
[
|
||||
'header' => 'Свяжитесь с нами',
|
||||
'details' => [
|
||||
['content' => '(3435) 25-48-00', 'url' => null],
|
||||
['content' => 'office@ntspi.ru', 'url' => null],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'is_active' => true,
|
||||
]);
|
||||
ContactWidget::factory()->count(2)->create();
|
||||
|
||||
PageReferenceList::create([
|
||||
'title' => 'Главная страница ресурсы',
|
||||
'slug' => 'glavnaia-stranica-resurs',
|
||||
'content' => [
|
||||
['title' => 'Приемная комиссия', 'url' => '/admission'],
|
||||
['title' => 'Образование', 'url' => '/education'],
|
||||
['title' => 'Наука', 'url' => '/science'],
|
||||
],
|
||||
'is_active' => true,
|
||||
]);
|
||||
PageReferenceList::factory()->count(4)->create();
|
||||
$forms = CustomForm::factory()->count(8)->create();
|
||||
CustomFormResponse::factory()->count(40)->create();
|
||||
|
||||
// 12. App Structure
|
||||
$mainSections = MainSection::factory()->count(5)->create();
|
||||
$subSections = SubSection::factory()
|
||||
->count(15)
|
||||
->sequence(fn () => ['main_section_id' => $mainSections->random()->id])
|
||||
->create();
|
||||
Page::factory()
|
||||
->count(40)
|
||||
->sequence(fn () => ['sub_section_id' => $subSections->random()->id])
|
||||
->create();
|
||||
|
||||
// 13. Integration Credentials
|
||||
IntegrationCredential::factory()->count(3)->create();
|
||||
|
||||
$this->command?->info('Mock data created successfully!');
|
||||
}
|
||||
|
||||
private function createUsers(): void
|
||||
{
|
||||
$admin = User::firstOrCreate(
|
||||
['email' => 'admin@ntspi.ru'],
|
||||
[
|
||||
'name' => 'Admin',
|
||||
'slug' => 'admin',
|
||||
'password' => Hash::make('password'),
|
||||
]
|
||||
);
|
||||
|
||||
if (!$admin->hasRole('super_admin')) {
|
||||
$admin->assignRole('super_admin');
|
||||
}
|
||||
|
||||
User::firstOrCreate(
|
||||
['email' => 'editor@ntspi.ru'],
|
||||
[
|
||||
'name' => 'Editor',
|
||||
'slug' => 'editor',
|
||||
'password' => Hash::make('password'),
|
||||
]
|
||||
);
|
||||
|
||||
User::factory()->count(50)->create();
|
||||
|
||||
UserDetail::factory()->count(30)->create();
|
||||
}
|
||||
|
||||
private function attachWorkers($faculties, $departments): void
|
||||
{
|
||||
$users = User::all();
|
||||
|
||||
foreach ($faculties as $faculty) {
|
||||
$workers = $users->random(fake()->numberBetween(2, 5));
|
||||
foreach ($workers as $worker) {
|
||||
DB::table('workers_faculties')->insert([
|
||||
'user_id' => $worker->id,
|
||||
'faculty_id' => $faculty->id,
|
||||
'position' => fake()->randomElement(['Преподаватель', 'Ст. преподаватель', 'Доцент', 'Профессор', 'Зав. кафедрой']),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($departments as $department) {
|
||||
$workers = $users->random(fake()->numberBetween(2, 4));
|
||||
foreach ($workers as $worker) {
|
||||
DB::table('workers_departments')->insert([
|
||||
'user_id' => $worker->id,
|
||||
'department_id' => $department->id,
|
||||
'position' => fake()->randomElement(['Преподаватель', 'Ст. преподаватель', 'Доцент', 'Лаборант']),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
$teachers = $users->random(fake()->numberBetween(3, 8));
|
||||
foreach ($teachers as $teacher) {
|
||||
DB::table('teachers_departments')->insert([
|
||||
'user_id' => $teacher->id,
|
||||
'department_id' => $department->id,
|
||||
'teaching_position' => fake()->randomElement(['Преподаватель', 'Ст. преподаватель', 'Доцент', 'Профессор']),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function attachProgramsToDepartments($programs, $departments): void
|
||||
{
|
||||
foreach ($programs as $program) {
|
||||
$dept = $departments->random(fake()->numberBetween(1, 3));
|
||||
foreach ($dept as $d) {
|
||||
DB::table('program_department')->insert([
|
||||
'educational_program_id' => $program->id,
|
||||
'department_id' => $d->id,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,33 +2,29 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use BezhanSalleh\FilamentShield\FilamentShield;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class RolesSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// $role_admin = Role::create(['name' => 'admin']);
|
||||
// $role_user = Role::create(['name' => 'user']);
|
||||
// $permission_read = Permission::create(['name' => 'read posts']);
|
||||
// $permission_create = Permission::create(['name' => 'create posts']);
|
||||
// $permission_edit = Permission::create(['name' => 'edit posts']);
|
||||
// $permission_delete = Permission::create(['name' => 'delete posts']);
|
||||
//
|
||||
// $permission_admin = [
|
||||
// $permission_create,
|
||||
// $permission_read,
|
||||
// $permission_edit,
|
||||
// $permission_delete,
|
||||
// ];
|
||||
//
|
||||
// $role_admin->syncPermissions($permission_admin);
|
||||
// $role_user->givePermissionTo($permission_read);
|
||||
FilamentShield::createRole(
|
||||
name: config('filament-shield.super_admin.name', 'super_admin')
|
||||
);
|
||||
|
||||
FilamentShield::createRole(
|
||||
name: config('filament-shield.dashboard_user.name', 'dashboard_user')
|
||||
);
|
||||
|
||||
FilamentShield::createRole(
|
||||
name: config('filament-shield.invited_user.name', 'invited_user')
|
||||
);
|
||||
|
||||
$adminRole = Role::findByName('super_admin');
|
||||
$allPerms = Permission::where('guard_name', 'web')->get();
|
||||
$adminRole->syncPermissions($allPerms);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user