Skip to content

Commit

Permalink
Merge pull request #209 from OpenSID/api-permision
Browse files Browse the repository at this point in the history
API untuk Manajemen Permision Pengguna OpenKab
  • Loading branch information
vickyrolanda authored Jul 5, 2023
2 parents b8bed8b + ee9f966 commit 43251f4
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 21 deletions.
20 changes: 2 additions & 18 deletions app/Enums/Modul.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,8 @@
*/
final class Modul extends Enum
{
const Data = [
'wilayah',
'kependudukan',
'penduduk',
'dokumen',
'bantuan',
'statistik',
'statistik-penduduk',
'statistik-keluarga',
'statistik-rtm',
'statistik-bantuan',
'master-data',
'master-data-bantuan',
'master-data-artikel',
'master-data-pengaturan',
'pengaturan',
'pengaturan-identitas',
'pengaturan-users',
const permision = [
'read', 'write', 'edit', 'delete',
];

const Menu = [
Expand Down
65 changes: 65 additions & 0 deletions app/Http/Controllers/Api/PermisionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Spatie\Permission\Contracts\Role;

class PermisionController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}

/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function show($id)
{
// $role = Role::where
}

/**
* Update the specified resource in storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function update(Request $request, $id)

public function show($id)
{
return $this->fractal($this->team->listTeam()->where('id', $id)->first(), function ($team) {
return $this->fractal($this->team->listTeam()->with(['role', 'role.permissions'])->where('id', $id)->first(), function ($team) {
return $team->toArray();
}, 'team')->respond();
}
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Permision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Permision extends Model
{
use HasFactory;
}
7 changes: 7 additions & 0 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\Permission\Models\Role;

class Team extends Model
{
Expand All @@ -26,4 +28,9 @@ class Team extends Model
protected $casts = [
'menu' => 'json',
];

public function role(): HasMany
{
return $this->hasMany(Role::class, 'team_id', 'id');
}
}
18 changes: 16 additions & 2 deletions database/migrations/2023_06_13_073258_create_role.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,29 @@ public function up()
'id_team' => $team->id,
]);

foreach (Modul::Data as $value) {
foreach (Modul::Menu as $main_menu) {
// buat role
$role = Role::create(
[
'name' => $value,
'name' => $main_menu['role'],
'team_id' => $team->id,
'guard_name' => 'web',
]
);
$user->assignRole($role->id);

if (isset($main_menu['submenu'])) {
foreach ($main_menu['submenu'] as $sub_menu) {
$role = Role::create(
[
'name' => $sub_menu['role'],
'team_id' => $team->id,
'guard_name' => 'web',
]
);
$user->assignRole($role->id);
}
}
}
}

Expand Down
56 changes: 56 additions & 0 deletions database/migrations/2023_06_21_190927_permision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use App\Enums\Modul;
use Illuminate\Database\Migrations\Migration;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$roles = Role::get();

foreach (Modul::Menu as $main_menu) {
// buat role

$roles = Role::where('name', $main_menu['role'])->get();

foreach (Modul::permision as $permision) {
$name_permision = $main_menu['role'].'-'.$permision;
$permision = Permission::create(['name' => $name_permision]);
foreach ($roles as $role) {
$role->givePermissionTo($name_permision);
}
}

if (isset($main_menu['submenu'])) {
foreach ($main_menu['submenu'] as $sub_menu) {
$roles = Role::where('name', $sub_menu['role'])->get();
foreach (Modul::permision as $permision) {
$name_permision = $sub_menu['role'].'-'.$permision;
Permission::create(['name' => $name_permision]);
foreach ($roles as $role) {
$role->givePermissionTo($name_permision);
}
}
}
}
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
};

0 comments on commit 43251f4

Please sign in to comment.