1 | <?php |
||||||
2 | /** |
||||||
3 | * Codeigniter 3 Auth |
||||||
4 | * @author Irfa Ardiasnyah |
||||||
5 | * @link https://github.com/irfaardy/codeigniter3-auth |
||||||
6 | * @version 1.2.1 |
||||||
7 | */ |
||||||
8 | class Auth { |
||||||
9 | |||||||
10 | private $CI; |
||||||
11 | |||||||
12 | function __construct() { |
||||||
13 | $this->CI = & get_instance(); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
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 (empty($get)) { |
||||||
25 | return false; |
||||||
26 | } |
||||||
27 | |||||||
28 | if (password_verify($password, $get->password)) { |
||||||
29 | $user_datas = array( |
||||||
30 | 'user_id' => $get->id, |
||||||
31 | 'logged_in' => TRUE, |
||||||
32 | 'login_token' => sha1($get->id.time().mt_rand(1000, 9999)) |
||||||
33 | ); |
||||||
34 | $this->CI->session->sess_regenerate(); |
||||||
35 | $this->CI->session->set_userdata($user_datas); |
||||||
36 | |||||||
37 | return true; |
||||||
38 | } else { |
||||||
39 | return false; |
||||||
40 | } |
||||||
41 | } |
||||||
42 | /** |
||||||
43 | * Cek sudah login apa belum. |
||||||
44 | * |
||||||
45 | * @return boolean |
||||||
46 | */ |
||||||
47 | public function check() { |
||||||
48 | if ($this->CI->session->logged_in) { |
||||||
49 | return true; |
||||||
50 | } |
||||||
51 | |||||||
52 | return false; |
||||||
53 | } |
||||||
54 | |||||||
55 | /** |
||||||
56 | * Cek hak akses. |
||||||
57 | * |
||||||
58 | * @return mixed |
||||||
59 | */ |
||||||
60 | public function hakAkses($hakAksesId) { |
||||||
61 | if ($this->user()->level != $hakAksesId) { |
||||||
62 | $this->CI->session->set_flashdata('warning', 'Anda tidak dapat mengakses halaman ini.'); |
||||||
63 | return redirect(array_key_exists('HTTP_REFERER', $_SERVER) ? $_SERVER['HTTP_REFERER'] : base_url('/')); |
||||||
0 ignored issues
–
show
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
![]() 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
![]() |
|||||||
64 | } |
||||||
65 | } |
||||||
66 | |||||||
67 | /** |
||||||
68 | * Ambil data user sesuai dengan id yang login. |
||||||
69 | * |
||||||
70 | * @return mixed |
||||||
71 | */ |
||||||
72 | public function user() { |
||||||
73 | if ($this->check()) { |
||||||
74 | $get = $this->CI->user->getBy(['id' => $this->CI->session->user_id]); |
||||||
75 | return $get; |
||||||
76 | } |
||||||
77 | |||||||
78 | return false; |
||||||
79 | } |
||||||
80 | |||||||
81 | /** |
||||||
82 | * Keluar dari sesi. |
||||||
83 | * |
||||||
84 | * @return void |
||||||
85 | */ |
||||||
86 | public function logout() { |
||||||
87 | if (empty($this->CI->input->get('token'))) { |
||||||
88 | return redirect($_SERVER['HTTP_REFERER']); |
||||||
0 ignored issues
–
show
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
![]() |
|||||||
89 | } |
||||||
90 | |||||||
91 | if ($this->CI->session->login_token === $this->CI->input->get('token')) { |
||||||
92 | $this->destroy(); |
||||||
93 | } else { |
||||||
94 | return redirect($_SERVER['HTTP_REFERER']); |
||||||
95 | } |
||||||
96 | } |
||||||
97 | |||||||
98 | private function destroy() { |
||||||
99 | $this->CI->session->sess_regenerate(TRUE); |
||||||
100 | $this->CI->session->sess_destroy(); |
||||||
101 | return redirect(base_url('login')); |
||||||
0 ignored issues
–
show
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
![]() 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
![]() |
|||||||
102 | } |
||||||
103 | /** |
||||||
104 | * Mencegah guest untuk mengakses halaman |
||||||
105 | * @return void |
||||||
106 | */ |
||||||
107 | public function protect($hakAksesId = null) { |
||||||
108 | if (!$this->check()) { |
||||||
109 | $this->destroy(); |
||||||
110 | } else { |
||||||
111 | if (!empty($hakAksesId)) { |
||||||
112 | if (is_array($hakAksesId)) { |
||||||
113 | if (!in_array($this->user()->level, $hakAksesId)) { |
||||||
114 | $this->CI->session->set_flashdata('warning', 'Anda tidak dapat mengakses halaman ini.'); |
||||||
115 | return /** @scrutinizer ignore-call */ redirect(array_key_exists('HTTP_REFERER', $_SERVER) ? $_SERVER['HTTP_REFERER'] : /** @scrutinizer ignore-call */base_url('/')); |
||||||
116 | } |
||||||
117 | } else { |
||||||
118 | if ($this->user()->level != $hakAksesId) { |
||||||
119 | $this->CI->session->set_flashdata('warning', 'Anda tidak dapat mengakses halaman ini.'); |
||||||
120 | return /** @scrutinizer ignore-call */redirect(array_key_exists('HTTP_REFERER', $_SERVER) ? $_SERVER['HTTP_REFERER'] : /** @scrutinizer ignore-call */base_url('/')); |
||||||
121 | } |
||||||
122 | } |
||||||
123 | } |
||||||
124 | } |
||||||
125 | } |
||||||
126 | } |