Browse Source

update CRUD permission

pull/1/head
farhantock 11 months ago
parent
commit
f88ce60ba8
  1. 118
      app/Http/Controllers/MenuController.php
  2. 2
      app/Models/RoleMenu.php

118
app/Http/Controllers/MenuController.php

@ -23,32 +23,33 @@ class MenuController extends Controller
$result = Menu::create($data);
if($result){
return response()->json(['status'=>'success','message'=>'add data menu successfully!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'add data menu failed!','code'=>400], 400);
if ($result) {
return response()->json(['status' => 'success', 'message' => 'add data menu successfully!', 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'add data menu failed!', 'code' => 400], 400);
}
}
public function edit($id){
if(!$id || (int) $id < 0 || $id==""){
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
public function edit($id)
{
if (!$id || (int) $id < 0 || $id == "") {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
die();
}
$result = Menu::find($id);
if($result){
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get data menu, please try again later!','code'=>400], 400);
if ($result) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'failed get data menu, please try again later!', 'code' => 400], 400);
}
}
public function update(Request $request, $id)
{
if(!$id || (int) $id < 0 || $id==""){
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
if (!$id || (int) $id < 0 || $id == "") {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
}
$this->validate($request, [
'icon' => 'required',
@ -58,21 +59,21 @@ class MenuController extends Controller
]);
$data = Menu::find($id);
$request->name !== $data['name'] ? $this->validate($request,['name'=>'unique:m_menu,name']) : '';
$request->url !== $data['url'] ? $this->validate($request,['url'=>'unique:m_menu,url']) : '';
$request->name !== $data['name'] ? $this->validate($request, ['name' => 'unique:m_menu,name']) : '';
$request->url !== $data['url'] ? $this->validate($request, ['url' => 'unique:m_menu,url']) : '';
if($data){
if ($data) {
$result = $data->update($request->all());
}else{
return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400);
} else {
return response()->json(['status' => 'failed', 'message' => 'data menu not found!', 'code' => 400], 400);
die();
}
if($result){
return response()->json(['status'=>'success','message'=>'data menu successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data menu failed updated!','code'=>400], 400);
if ($result) {
return response()->json(['status' => 'success', 'message' => 'data menu successfully updated!', 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'data menu failed updated!', 'code' => 400], 400);
}
}
@ -80,18 +81,18 @@ class MenuController extends Controller
{
$data = Menu::find($id);
if($data){
if ($data) {
$delete = $data->delete();
}else{
return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400);
} else {
return response()->json(['status' => 'failed', 'message' => 'data menu not found!', 'code' => 400], 400);
die();
}
if($delete){
return response()->json(['status'=>'success','message'=>'data menu successfully deleted!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data menu failed deleted!','code'=>400], 400);
if ($delete) {
return response()->json(['status' => 'success', 'message' => 'data menu successfully deleted!', 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'data menu failed deleted!', 'code' => 400], 400);
}
}
@ -103,41 +104,52 @@ class MenuController extends Controller
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
}
public function listMenu($id)
{
$dataParent = RoleMenu::select("m_menu.*")
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id')
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id')
->whereNull('m_menu.parent_id')
->where('m_roles.id', $id)
->orderBy('m_menu.sequence', 'ASC')
->get();
$dataParent = RoleMenu::select(
'm_menu.*',
"t_roles_menu.create",
"t_roles_menu.read",
"t_roles_menu.update",
"t_roles_menu.delete"
)
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id')
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id')
->whereNull('m_menu.parent_id')
->where('m_roles.id', $id)
->orderBy('m_menu.sequence', 'ASC')
->get();
$finalData = [];
foreach($dataParent as $data){
foreach ($dataParent as $data) {
$data->children = $this->getChildrenMenu($data->id, $id);
$finalData[] = $data;
}
return response()->json(['status'=>'success','code'=>200,'data'=>$finalData], 200);
return response()->json(['status' => 'success', 'code' => 200, 'data' => $finalData], 200);
}
protected function getChildrenMenu($id, $role_id)
{
$dataChildren = RoleMenu::select("m_menu.*")
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id')
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id')
->where('m_roles.id', $role_id)
->where('m_menu.parent_id', $id)
->orderBy('m_menu.sequence', 'ASC')
->get();
$dataChildren = RoleMenu::select(
'm_menu.*',
"t_roles_menu.create",
"t_roles_menu.read",
"t_roles_menu.update",
"t_roles_menu.delete"
)
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id')
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id')
->where('m_roles.id', $role_id)
->where('m_menu.parent_id', $id)
->orderBy('m_menu.sequence', 'ASC')
->get();
$children = [];
if($dataChildren){
foreach($dataChildren as $data)
{
if ($dataChildren) {
foreach ($dataChildren as $data) {
$data->children = $this->getChildrenMenu($data->id, $role_id);
$children[] = $data;
}
@ -150,10 +162,10 @@ class MenuController extends Controller
$data = Menu::all();
$countData = $data->count();
if($data){
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
}else{
return response()->json(['status'=>'failed','message'=>'failed get list menu, please try again later!','code'=>400], 400);
if ($data) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'failed get list menu, please try again later!', 'code' => 400], 400);
}
}
}

2
app/Models/RoleMenu.php

@ -12,6 +12,6 @@ class RoleMenu extends Model
const UPDATED_AT = 'updated_at';
protected $fillable = [
'role_id', 'menu_id', 'created_at', 'created_by', 'updated_at', 'updated_by'
'role_id', 'menu_id', 'create', 'read', 'update', 'delete', 'created_at', 'created_by', 'updated_at', 'updated_by'
];
}

Loading…
Cancel
Save