Anons79 Mini Shell

Directory : /home/aplikasiposinfo/www/astra.aplikasipos.info/laravel_backend/app/Http/Livewire/
Upload File :
Current File : /home/aplikasiposinfo/www/astra.aplikasipos.info/laravel_backend/app/Http/Livewire/Users.php

<?php

namespace App\Http\Livewire;

use App\Helpers\Helper;
use App\Models\Perusahaan;
use App\Models\Role;
use App\Models\User;
use App\Models\Dealer;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
use Illuminate\Support\Str;
use Kreait\Laravel\Firebase\Facades\Firebase;

class Users extends Component
{   
    use WithPagination;
    use WithFileUploads;

    public $searchTerms;

    public $photo;

    //nama field
    public $nama, $email, $password,$profile_photo_path,$c_password,$role_id,$perusahaan_id,$dealer_id,$uuid,$no_hp,$alamat,$posisi,$kd_user;

    //0 = tambah, 1 = edit
    public $action = 0;

    //keterangan halaman
    public $title = "Pengguna";
    public $link = "/user";


    public $roles;
    public $perusahaans;
    public $dealers;
    public $posisis = [];
    public $hakAkses = [];
  
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function render()
    {   
        
        $this->hakAkses = Helper::cekAkses([
            [$this->title=>'Lihat'],//0
            [$this->title=>'Tambah'],//1
            [$this->title=>'Ubah'],//2
            [$this->title=>'Hapus']//3
        ],null,true);

        if(!$this->hakAkses[0]){
            session()->flash('error', 'Anda tidak mempunyai akses');
            return view('livewire.402');
        }
        $this->posisis = ['Main Dealer','Warehouse','Ekspedisi','Dealer'];
        $this->roles = Role::all();
        $this->perusahaans = Perusahaan::all();
        $this->dealers = Dealer::all();
        $searchTerms = '%'.$this->searchTerms.'%';
        
        return view('livewire.user.users',[
            'users' => User::with('role')->where('nama','LIKE',$searchTerms)
            ->orWhere('email','LIKE',$searchTerms)
            ->orderBy('created_at','DESC')
            ->paginate(10),
        ]);
    }
  
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function create()
    {   
        if(!$this->hakAkses[1]){
            session()->flash('error', 'Anda tidak mempunyai akses');
            return;
        }
        $this->resetInputFields();
        $this->action = 0;
    }
  
  
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function resetInputFields(){
        $this->uuid = '';
        $this->kd_user = \Helper::kodeUser();
        $this->nama = '';
        $this->email = '';
        $this->password = '';
        $this->no_hp = '';
        $this->alamat = '';
        $this->role_id = $this->roles[0]->id ?? null;
        $this->perusahaan_id = $this->perusahaans[0]->id ?? null;
        $this->dealer_id = null;
        $this->profile_photo_path = null;
        $this->posisi = $this->posisis[0] ?? '';
    }
     
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function store()
    {
        try {
            if($this->action == 0){
                if ($this->posisi == "Dealer") {
                    $this->validate([
                        'dealer_id' => 'required'
                    ]);
                }

                $this->validate(
                    [
                        'nama' => 'required',
                        'email'=>'required',
                        'password' => 'required|min:6|string',
                        'role_id'=>'required',
                        'posisi'=>'required',
                        'perusahaan_id'=>'required',
                        'kd_user'=>'required'
                    ],
                    [
                        'password.min' => 'Password Minimal 6 Karakter',
                    ]
                );
                
                $this->uuid = (string) Str::uuid();
                $profile_photo_path = $this->profile_photo_path;
                if($this->photo){
                    $profile_photo_path = $this->photo->store("user/".$this->uuid,'public');
                }
                $auth = Firebase::auth();
                $fu = $auth->createUserWithEmailAndPassword($this->email, $this->password);
                //$output = json_encode($fu, \JSON_PRETTY_PRINT);
                $user = User::create([
                    'id'=>$this->uuid,
                    'uid'=>$fu->uid,
                    'kd_user'=> $this->kd_user,
                    'nama' => $this->nama,
                    'email'=>$this->email,
                    'password' => Hash::make($this->password),
                    'no_hp'=>$this->no_hp,
                    'alamat'=>$this->alamat,
                    'profile_photo_path'=>$profile_photo_path,
                    'role_id'=>$this->role_id,
                    'perusahaan_id'=>$this->perusahaan_id,
                    'dealer_id'=>$this->dealer_id,
                    'posisi'=>$this->posisi
                ]);

                \Helper::addLog("Menambah User ".$user->nama);
        
            } else {
                $this->validate([
                    'nama' => 'required',
                    'email'=>'required',
                ]);

                if ($this->posisi == "Dealer") {
                    $this->validate([
                        'dealer_id' => 'required'
                    ]);
                }

                $user = User::find($this->uuid);
                $this->validate([
                    'email'=>Rule::unique('users')->ignore($user->id)
                ]);
                
                // if ($this->profile_photo_path != null) {
                //     $user->updateProfilePhoto($this->profile_photo_path);
                // }

                $profile_photo_path = $this->profile_photo_path;
                if($this->photo){
                    $profile_photo_path = $this->photo->store("user/".$this->uuid,'public');
                }
				
				if($this->password){
                    if($user->uid != null){
                        $auth = Firebase::auth();
                        $fu = $auth->changeUserPassword($user->uid, $this->password);
                    }
					$user->update([
                        'nama' => $this->nama,
                        'email'=>$this->email,
                        'no_hp'=>$this->no_hp,
                        'alamat'=>$this->alamat,
                        'profile_photo_path'=>$profile_photo_path,
                        'role_id'=>$this->role_id,
                        'perusahaan_id'=>$this->perusahaan_id,
                        'dealer_id'=>$this->dealer_id,
                        'posisi'=>$this->posisi,
                        'password'=>Hash::make($this->password)
                    ]);
				}else{
					$user->update([
                        'nama' => $this->nama,
                        'email'=>$this->email,
                        'no_hp'=>$this->no_hp,
                        'alamat'=>$this->alamat,
                        'profile_photo_path'=>$profile_photo_path,
                        'role_id'=>$this->role_id,
                        'perusahaan_id'=>$this->perusahaan_id,
                        'dealer_id'=>$this->dealer_id,
                        'posisi'=>$this->posisi
                    ]);
				}
				\Helper::addLog("Mengubah User ".$user->nama);
                
            }
      
            session()->flash('success', 
                $this->id ? $this->nama.' berhasil di simpan' : $this->nama.' berhasil di simpan');
            if($this->action == 0){
                $this->resetInputFields();
            }
            $this->emit('dataStore',true);
        } catch (\Exception $th) {
            session()->flash('error', 'data gagal di simpan : '.$th->getMessage());
            $this->emit('dataStore',false);
        }
    }
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function edit($uuid)
    {   
        if(!$this->hakAkses[2]){
            session()->flash('error', 'Anda tidak mempunyai akses');
            return;
        }
        $user = User::findOrFail($uuid);
        $this->uuid = $uuid;
        $this->kd_user = $user->kd_user ?? NULL;
        $this->nama = $user->nama;
        $this->email = $user->email;
        $this->no_hp = $user->no_hp;
        $this->alamat = $user->alamat;
        $this->role_id = $user->role_id;
        $this->perusahaan_id = $user->perusahaan_id;
        $this->dealer_id = $user->dealer_id;
        //$this->password = $user->password;
        $this->profile_photo_path = $user->profile_photo_path ?? $user->profile_photo_url;
        $this->posisi = $user->posisi;
        $this->action = 1;
        $this->emit('edit');
    }
     
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function delete($uuid)
    {   
        if(!$this->hakAkses[3]){
            session()->flash('error', 'Anda tidak mempunyai akses');
            return;
        }
        $data = User::find($uuid);
        try {
            $data->delete();
            \Helper::addLog("Menghapus User ".$data->nama);
            session()->flash('success', 'berhasil menghapus '.$data->nama);
        } catch (\Exception $th) {
            session()->flash('success', 'gagal menghapus '.$data->nama.' : '.$th->getMessage());
        }
    }
}

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