-
Notifications
You must be signed in to change notification settings - Fork 0
/
User_model.php
93 lines (78 loc) · 2.49 KB
/
User_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
class User_model extends CI_Model
{
private $_table = "users";
public $user_id;
public $full_name;
public $password;
public $email;
public $role;
public function rules()
{
return [
['field' => 'full_name',
'label' => 'Name',
'rules' => 'required'],
['field' => 'password',
'label' => 'Password',
'rules' => 'required|min_length[3]'],
['field' => 'email',
'label' => 'Email',
'rules' => 'required|valid_email']
];
}
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["user_id" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->username=$post['username'];
$this->full_name = $post["full_name"];
$this->email = $post["email"];
$this->password = password_hash($post["password"], PASSWORD_DEFAULT);
$this->role = $post["role"] ?? "customer";
$this->db->insert($this->_table, $this);
}
public function update()
{
$post = $this->input->post();
$this->full_name = $post["full_name"];
$this->username = $post["username"];
$this->password = $post["password"];
$this->email = $post["email"];
$this->db->update($this->_table, $this, array('user_id' => $post['id']));
}
public function doLogin(){
$post = $this->input->post();
$this->db->where('email', $post["email"])
->or_where('username', $post["email"]);
$user = $this->db->get($this->_table)->row();
if($user){
$isPasswordTrue = password_verify($post["password"], $user->password);
$isAdmin = $user->role == "admin";
if($isPasswordTrue){
$this->session->set_userdata(['user_logged' => $user]);
$this->_updateLastLogin($user->user_id);
return true;
}
}
return false;
}
public function isNotLogin(){
return $this->session->userdata('user_logged') === null;
}
private function _updateLastLogin($user_id){
$sql = "UPDATE {$this->_table} SET last_login=now() WHERE user_id={$user_id}";
$this->db->query($sql);
}
public function delete($id)
{
return $this->db->delete($this->_table, array("user_id" => $id));
}
}