Passed
Push — master ( 3ee27e...b81719 )
by IRFA
01:07
created

Auth::hakAkses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 1
1
<?php 
2
/**
3
 * Auth Library
4
 * @author	Irfa Ardiasnyah
5
 * @link	https://github.com/irfaardy/codeigniter3-auth
6
 * @version	1.1.0
7
 */
8
class Auth{
9
	
10
	private $CI;
11
12
	function __construct(){
13
		$this->CI = & get_instance();
0 ignored issues
show
Bug introduced by
The function get_instance was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

13
		$this->CI = & /** @scrutinizer ignore-call */ get_instance();
Loading history...
14
	}
15
	/**
16
     * Cek username dan password, jika password yang diinputkan sama dengan di database sama maka login sukses.
17
     *
18
     * @param string $username
19
     * @param string $password
20
     * @return boolean
21
     */
22
	public function verify($username,$password){
23
		$get = $this->CI->user->getBy(['username' => $username]);
24
		if(password_verify($password, $get->password)) {
25
			$user_datas = array(
26
			        'user_id'  => $get->id,
27
			        'logged_in' => TRUE,
28
			        'login_token' => sha1($get->id.time().mt_rand(1000,9999))
29
			);
30
			$this->CI->session->sess_regenerate();
31
			$this->CI->session->set_userdata($user_datas);
32
33
			return true;
34
		} else {
35
			return false;
36
		}
37
	}
38
	/**
39
     * Cek sudah login apa belum.
40
     *
41
     * @return boolean
42
     */
43
	public function check(){
44
		if($this->CI->session->logged_in) {
45
			return true;
46
		} 
47
48
		return false;
49
	}
50
51
	/**
52
     * Cek hak akses.
53
     *
54
     * @return mixed
55
     */
56
	public function hakAkses($hakAksesId){
57
		if($this->user()->level != $hakAksesId) {
58
			$this->CI->session->set_flashdata('warning','Anda tidak dapat mengakses halaman ini.');
59
			return redirect($_SERVER['HTTP_REFERER']);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
			return /** @scrutinizer ignore-call */ redirect($_SERVER['HTTP_REFERER']);
Loading history...
60
		} 
61
	}
62
63
	/**
64
     * Ambil data user sesuai dengan id yang login.
65
     *
66
     * @return mixed
67
     */
68
	public function user(){
69
		if($this->check()) {
70
			$get = $this->CI->user->getBy(['id' => $this->CI->session->user_id]);
71
			return $get;
72
		}
73
74
		return false;
75
	}
76
77
	/**
78
     * Keluar dari sesi.
79
     *
80
     * @return void
81
     */
82
	public function logout(){
83
		if(empty($this->CI->input->get('token'))){
84
			return redirect($_SERVER['HTTP_REFERER']);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
			return /** @scrutinizer ignore-call */ redirect($_SERVER['HTTP_REFERER']);
Loading history...
85
		}
86
87
		if($this->CI->session->login_token === $this->CI->input->get('token')){
88
			$this->destroy();
89
		} else{
90
			return redirect($_SERVER['HTTP_REFERER']);
91
		}
92
	}
93
94
	private function destroy(){
95
		$this->CI->session->sess_regenerate(TRUE);
96
		$this->CI->session->sess_destroy();
97
		return redirect(base_url('login'));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
		return /** @scrutinizer ignore-call */ redirect(base_url('login'));
Loading history...
Bug introduced by
The function base_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
		return redirect(/** @scrutinizer ignore-call */ base_url('login'));
Loading history...
98
	}
99
	/** 
100
	* Mencegah guest untuk mengakses halaman
101
	* @return void
102
	*/
103
	public function protect(){
104
		if(!$this->check()){
105
			$this->destroy();
106
		}
107
	}
108
}