Advertisement
21ani

Controller auth_ds

Jan 5th, 2020
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.74 KB | None | 0 0
  1. <?php
  2. class Auth extends CI_Controller {
  3.  
  4.     public function __construct(){
  5.         parent::__construct();
  6.         $this->load->library('form_validation');
  7.         $this->load->model('Mahasiswa_model');
  8.        
  9.     }
  10.  
  11.  
  12.     function index()
  13.     {
  14.         $this->load->helper('url');
  15.         $data['judul'] = 'Halaman Home';
  16.         $this->load->view('template/home/vheader', $data);
  17.         $this->load->view('home/index');
  18.         $this->load->view('template/home/vfooter');
  19.     }
  20.  
  21.     public function lihat(){
  22.         $this->load->helper('url');
  23.         ////$data['judul'] = 'Halaman Home';
  24.         // $this->load->view('template/home/vheader');
  25.         // $this->load->view('home/vimhs');
  26.         // $this->load->view('template/home/vfooter');
  27.         $data['judul'] = 'Halaman Home';
  28.         $this->load->view('template/home/vheader', $data);
  29.         $this->load->view('home/vimhs');
  30.         $this->load->view('template/home/vfooter');
  31.        
  32.     }
  33.     public function fti(){
  34.         $this->load->helper('url');
  35.         $data['judul'] = 'Halaman Home';
  36.         $this->load->view('template/home/vheader', $data);
  37.         $this->load->view('home/fti');
  38.         $this->load->view('template/home/vfooter');
  39.        
  40.     }
  41.  
  42.  
  43.  
  44.     public function login(){
  45.         if ($this->session->userdata('nim','nidn')) {
  46.             redirect('user');
  47.         }
  48.  
  49.  
  50.         $this->form_validation->set_rules('nim', 'Nim', 'trim|required');
  51.         // $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
  52.         $this->form_validation->set_rules('password', 'Password', 'trim|required');
  53.  
  54.         if ($this->form_validation->run() == false){
  55.             $data['judul'] = 'Halaman Login';
  56.         $this->load->view('template/auth_vheader', $data);
  57.         $this->load->view('auth/login');
  58.         $this->load->view('template/auth_vfooter');
  59.         }
  60.         else{
  61.             //ketika validasi lolos atau sukses
  62.             $this->_login();
  63.         }
  64.     }
  65.  
  66.     private function _login(){
  67.         //$nim = $this->input->post('nim'); //ngambil email yang ada di elemen email
  68.         $nidn = $this->input->post('nidn'); //ngambil email yang ada di elemen email
  69.         $password = $this->input->post('password'); //ngambil password yang ada di elemen email
  70.  
  71.         //$user = $this->db->get_where('user',['email=> $email'])->row_array();
  72.         $dosen = $this->db->get_where('dosen',['nidn'=> $nidn])->row_array();
  73.         // var_dump($user);
  74.         // die;
  75.         if($dosen){
  76.            
  77.                 //cek password
  78.                 if (password_verify($password,$dosen['password'])){
  79.                     $data =[
  80.                         'nidn'=> $dosen['nidn'],
  81.                         // 'email'=> $user['email'],
  82.                         'role_id'=> $dosen['role_id']
  83.                     ];
  84.                     $this->session->set_userdata($data);
  85.                     if ($dosen['role_id'] == 1) {
  86.                         redirect('user');
  87.                     } else if ($dosen['role_id'] == 2) {
  88.                         redirect('dosen');
  89.                     } else {
  90.                         redirect('mhs');
  91.                     }
  92.  
  93.                 } else {
  94.                     $this->session->set_flashdata('massage','<div class="alert alert-danger" role="alert">
  95.                Wrong Password
  96.              </div>');
  97.                 //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  98.                 redirect('auth/login');
  99.  
  100.                 }
  101.  
  102.            
  103.  
  104.         } else{
  105.             $this->session->set_flashdata('massage','<div class="alert alert-danger" role="alert">
  106.            Email is not registered!
  107.          </div>');
  108.             //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  109.             redirect('auth/login');
  110.  
  111.         }
  112.     }
  113.    
  114.     public function registration(){
  115.  
  116.         //aturan untuk kolom inputan pada form
  117.  
  118.         $this->form_validation->set_rules('name', 'Name', 'required|trim|alphabets_text_field');
  119.         $this->form_validation->set_rules('nim', 'Nim', 'required|trim|is_unique[mhs.nim]',[
  120.             'is_unique' => 'This nim has already registered'
  121.             ]);
  122.            
  123.         $this->form_validation->set_rules('fakultas', 'Fakultas', 'required|trim');
  124.         $this->form_validation->set_rules('prodi', 'Prodi', 'required|trim');
  125.         // $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]',[
  126.         //     'is_unique' => 'This Email has already registered'
  127.         // ]);
  128.         $this->form_validation->set_rules('password1', 'Password', 'required|trim|
  129. min_length[3]|matches[password2]',[
  130.     'min_lenght' => 'Password too short!',
  131.     'matches' => 'Password dont matches'
  132. ]);
  133.         $this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
  134.  
  135.  
  136.         //logika untuk mengembalikan tampilan apabila form_validasi nya gagal
  137.         if ($this->form_validation->run() == false ){
  138.             $this->load->helper('url');
  139.  
  140.             $data['judul'] = 'Halaman Regis';
  141.  
  142.         $this->load->view('template/auth_vheader', $data);
  143.         $this->load->view('auth/vregistration');
  144.         $this->load->view('template/auth_vfooter');
  145.         } else {
  146.             //membuat sebuah variabel utk isian perintah memasukkan apa yang diinputkan di form input
  147.             //lalu memasukkan di tabel database
  148.             $data = [
  149.                 'name' => htmlspecialchars($this->input->post('name',true)),
  150.                 'nim' => htmlspecialchars($this->input->post('nim',true)),
  151.                 // 'email' => htmlspecialchars($this->input->post('email',true)),
  152.                 'image' => 'default.jpg',
  153.                 //'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
  154.                 'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
  155.                 'role_id' => 2,
  156.                 'is_active' => 1,
  157.                 'date_created' => time()
  158.             ];
  159.  
  160.             //memasukkan variabel tadi ke tabel user
  161.             $this->db->insert('mhs',$data);
  162.             // $this->db->insert('user',$data);
  163.  
  164.             //Kasih pesan sebelum redirect Buat flashdata pakai session
  165.             $this->session->set_flashdata('massage','<div class="alert alert-success" role="alert">
  166.            Congrats your account has been ceated!
  167.          </div>');
  168.             //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  169.             redirect('auth');
  170.         }
  171.        
  172.     }
  173.  
  174.     public function logout(){
  175.         $this->session->unset_userdata('nim');
  176.         // $this->session->unset_userdata('email');
  177.         $this->session->unset_userdata('role_id');
  178.         //Kasih pesan sebelum redirect Buat flashdata pakai session
  179.         $this->session->set_flashdata('massage','<div class="alert alert-success" role="alert">
  180.        You have been logged out!
  181.      </div>');
  182.         //setelah database berhasil masuk, kita  redirect ke halaman index auth.
  183.         redirect('auth');
  184.     }
  185.  
  186.  
  187.  
  188. public function fbimbing(){
  189.      
  190.     $this->load->helper('url');
  191.     $data['Bimbing'] = 'Halaman Dosen Pembimbing';
  192.     $data['bimbing'] = $this->dosenpem->get_all();
  193.      
  194.     $this->load->view('dbimbing/vheader', $data);
  195.     //$this->load->view('dbimbing/dosenp',$data);
  196.     $this->load->view('dbimbing/index');
  197.     $this->load->view('dbimbing/template/vfooter');  
  198. }
  199. public function search(){
  200.  $this->load->view('dbimbing/vheader', $data);
  201.   $keyword = $this->input->post('keyword');
  202.   $data['bimbings']=$this->dosenpem->get_dosen_keyword($keyword);
  203.   $this->load->view('dbimbing/cari',$data);
  204.           $this->load->view('dbimbing/template/vfooter');
  205. }
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement