diff --git a/app/Models/Menu.php b/app/Models/Menu.php new file mode 100644 index 0000000..f6c7998 --- /dev/null +++ b/app/Models/Menu.php @@ -0,0 +1,63 @@ +hasOne(Menu::class, 'id', 'parent_id')->without(['childs'])->with(['parent']); + } + + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function childs() + { + return $this->hasMany(Menu::class, 'parent_id', 'id')->with(['parent', 'childs'])->orderBy('position'); + } + + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function permissions() + { + return $this->belongsToMany(Permission::class); + } + + /** + * @return \Illuminate\Database\Eloquent\Casts\Attribute + */ + public function actives() : Attribute + { + return Attribute::make( + get: fn ($value) => json_decode($value), + set: fn ($value) => is_array($value) || $value instanceof JsonSerializable || $value instanceof stdClass ? json_encode($value) : $value, + ); + } +} diff --git a/database/migrations/2022_07_15_192421_create_menus_table.php b/database/migrations/2022_07_15_192421_create_menus_table.php new file mode 100644 index 0000000..c0abb19 --- /dev/null +++ b/database/migrations/2022_07_15_192421_create_menus_table.php @@ -0,0 +1,46 @@ +id(); + $table->unsignedBigInteger('parent_id') + ->nullable() + ->default(null); + $table->string('name'); + $table->string('icon') + ->default('circle'); + $table->string('route_or_url')->default('#'); + $table->unsignedTinyInteger('position'); + $table->boolean('enable')->default(true); + $table->boolean('deleteable')->default(true); + $table->json('actives')->default('[]'); + $table->timestamps(); + + $table->foreign('parent_id') + ->references('id') + ->on('menus'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('menus'); + } +}; diff --git a/database/migrations/2022_07_15_192434_create_menu_permission_table.php b/database/migrations/2022_07_15_192434_create_menu_permission_table.php new file mode 100644 index 0000000..b1bcb42 --- /dev/null +++ b/database/migrations/2022_07_15_192434_create_menu_permission_table.php @@ -0,0 +1,37 @@ +id(); + $table->foreignId('menu_id') + ->constrained() + ->cascadeOnDelete(); + $table->foreignId('permission_id') + ->constrained() + ->cascadeOnDelete(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('menu_permission'); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 8a38994..03f1803 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -16,6 +16,7 @@ class DatabaseSeeder extends Seeder { $this->call([ InitialSeeder::class, + MenuSeeder::class, ]); } } diff --git a/database/seeders/MenuSeeder.php b/database/seeders/MenuSeeder.php new file mode 100644 index 0000000..85eb5f9 --- /dev/null +++ b/database/seeders/MenuSeeder.php @@ -0,0 +1,90 @@ + 'dashboard', + 'icon' => 'tachometer-alt', + 'route_or_url' => 'dashboard', + 'position' => 1, + 'deleteable' => false, + ]); + + $builtin = Menu::create([ + 'name' => 'builtin', + 'icon' => 'circle', + 'position' => 2, + 'deleteable' => false, + ]); + + $permission = $builtin->childs()->create([ + 'name' => 'permission', + 'route_or_url' => 'superuser.permission.index', + 'icon' => 'key', + 'position' => 1, + 'deleteable' => false, + ]); + + $permission->permissions()->attach( + Permission::whereIn('name', [ + 'create permission', 'read permission', 'update permission', 'delete permission', + ])->get(['id']) + ); + + $role = $builtin->childs()->create([ + 'name' => 'role', + 'route_or_url' => 'superuser.role.index', + 'icon' => 'user-cog', + 'position' => 2, + 'deleteable' => false, + ]); + + $role->permissions()->attach( + Permission::whereIn('name', [ + 'create role', 'read role', 'update role', 'delete role', + ])->get(['id']) + ); + + $user = $builtin->childs()->create([ + 'name' => 'user', + 'route_or_url' => 'superuser.user.index', + 'icon' => 'user', + 'position' => 3, + 'deleteable' => false, + ]); + + $user->permissions()->attach( + Permission::whereIn('name', [ + 'create user', 'read user', 'update user', 'delete user', + ])->get(['id']) + ); + + $menu = $builtin->childs()->create([ + 'name' => 'menu', + 'route_or_url' => 'superuser.menu.index', + 'icon' => 'bars', + 'position' => 4, + 'deleteable' => false, + ]); + + $menu->permissions()->attach( + Permission::whereIn('name', [ + 'create menu', 'read menu', 'update menu', 'delete menu', + ])->get(['id']) + ); + } +}