Anons79 Mini Shell

Directory : /home/aplikasiposinfo/public_html/audigitalstore.fixmate.id/app/Http/Controllers/
Upload File :
Current File : /home/aplikasiposinfo/public_html/audigitalstore.fixmate.id/app/Http/Controllers/RoleController.php

<?php

namespace App\Http\Controllers;

use App\Models\Permission;
use App\Models\Role;
use App\Models\Menu;
use App\Helpers\Helper;
use App\Models\Toko;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {   
        if (!Helper::hakAkses('pengaturan', 'View')) {
            return abort(403, 'Unauthorized action.');
        }
        $datas = Role::paginate(10);

        $data = [
            "datas" => $datas,
        ];

        return response()->view('role.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {   
        if (!Helper::hakAkses('pengaturan', 'View')) {
            return abort(403, 'Unauthorized action.');
        }
        $menus = Menu::orderBy('nama_menus')->orderBy('aksi_menu')->get();
        $menu_model = new Menu;
        $menu_lists = Menu::select("nama_menu")->orderBy('nama_menu')->orderBy('aksi_menu')->groupBy('nama_menu')->get();
        $tokos = Toko::all();

        $data = [
            "menus" => $menus,
            "menu_lists" => $menu_lists,
            "menu_model" => $menu_model,
            "tokos" => $tokos,
        ];
        return response()->view('role.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        if (!Helper::hakAkses('pengaturan', 'View')) {
            return abort(403, 'Unauthorized action.');
        }
        DB::beginTransaction();
        try {
            $role = Role::create([
                'nama_role' => $request->nama_role
            ]);

            if ($request->menu_id == NULL) {
                return redirect()->back()->with('error', 'Minimal Pilih satu menu');
            }

            if ($role) {
                $role->menu()->attach($request->menu_id);
                $role->tokos()->sync($request->toko_id ?? []);
            }
            

            Helper::addUserLog("Menambah data role", $role->toArray());
            DB::commit();
            return redirect()->route('role.index')->with('success', "Role Berhasil ditambah");
        } catch (\Throwable $th) {
            DB::rollback();
            return redirect()->route('role.create')->with('error', $th->getMessage());
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {   
        if (!Helper::hakAkses('pengaturan', 'View')) {
            return abort(403, 'Unauthorized action.');
        }
        $datas = Role::with('tokos')->find($id);
        $menus = Menu::all();
        $menu_model = new Menu;
        $menu_lists = Menu::select("nama_menu")->groupBy('nama_menu')->get();
        $tokos = Toko::all();
        $data = [
            "id" => $id,
            "datas" => $datas,
            "menus" => $menus,
            "menu_lists" => $menu_lists,
            "menu_model" => $menu_model,
            "tokos" => $tokos,
            "toko_ids" => $datas->tokos->pluck('id')->toArray()
        ];
        return response()->view('role.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {   
        if (!Helper::hakAkses('pengaturan', 'View')) {
            return abort(403, 'Unauthorized action.');
        }
        DB::beginTransaction();
        try {
            $role = Role::find($id);
            $old = $role->toArray();

            if ($request->menu_id == NULL) {
                return redirect()->back()->with('error', 'Minimal Pilih satu menu');
            }

            $role->update([
                "nama_role" => $request->nama_role,
            ]);

            $role->menu()->sync([]);
            $role->menu()->attach($request->menu_id);
            $role->tokos()->sync($request->toko_id ?? []);

            Helper::addUserLog("Mengubah data role", $role->toArray());
            DB::commit();
            return redirect()->route('role.index')->with('success', "Role Berhasil ditambah");
        } catch (\Throwable $th) {
            DB::rollback();
            dd($th);
            return redirect()->back()->with('error', $th->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        if (!Helper::hakAkses('pengaturan', 'View')) {
            return abort(403, 'Unauthorized action.');
        }
        if($id == auth()->user()->role_id){
            return redirect()->route('role.index')->with('error', "Anda tidak bisa menghapus role anda sendiri"); 
        }

        $role = Role::findOrFail($id);
        if($role->pengguna()->count() > 0){
            return redirect()->route('role.index')->with('error', "Role ini masih digunakan oleh pengguna lain");
        }
        DB::beginTransaction();
        try {
            
            $permission = Permission::where("role_id",  $id);

            $role->delete();
            $permission->delete();

            Helper::addUserLog("Menghapus data role", $role->toArray());
            DB::commit();
            return redirect()->route('role.index')->with('success', 'Role berhasil dihapus');
        } catch (\Throwable $th) {
            DB::rollback();
            return redirect()->route('role.index')->with('error', "Role gagal dihapus");
        }
    }
}

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]