Initial commit - CMS Gov Bapenda Garut dengan EditorJS
This commit is contained in:
66
app/Models/UserModel.php
Normal file
66
app/Models/UserModel.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class UserModel extends Model
|
||||
{
|
||||
protected $table = 'users';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = ['role_id', 'username', 'email', 'phone_number', 'password_hash', 'telegram_id', 'is_active', 'last_login_at'];
|
||||
|
||||
protected bool $allowEmptyInserts = false;
|
||||
|
||||
protected $useTimestamps = true;
|
||||
protected $dateFormat = 'datetime';
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
protected $deletedField = null;
|
||||
|
||||
protected $validationRules = [
|
||||
'username' => 'required|max_length[100]|is_unique[users.username,id,{id}]',
|
||||
'email' => 'required|valid_email|max_length[255]|is_unique[users.email,id,{id}]',
|
||||
'phone_number' => 'permit_empty|max_length[20]|is_unique[users.phone_number,id,{id}]',
|
||||
'telegram_id' => 'permit_empty|integer|is_unique[users.telegram_id,id,{id}]',
|
||||
'role_id' => 'required|integer',
|
||||
];
|
||||
|
||||
protected $validationMessages = [];
|
||||
protected $skipValidation = false;
|
||||
protected $cleanValidationRules = true;
|
||||
|
||||
protected $beforeInsert = ['hashPassword'];
|
||||
protected $beforeUpdate = ['hashPassword'];
|
||||
|
||||
protected function hashPassword(array $data)
|
||||
{
|
||||
if (isset($data['data']['password_hash']) && !empty($data['data']['password_hash'])) {
|
||||
// Only hash if it's not already hashed (check if it starts with $2y$ which is bcrypt)
|
||||
if (!preg_match('/^\$2[ayb]\$.{56}$/', $data['data']['password_hash'])) {
|
||||
$data['data']['password_hash'] = password_hash($data['data']['password_hash'], PASSWORD_DEFAULT);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getUserByUsername(string $username)
|
||||
{
|
||||
return $this->where('username', $username)->first();
|
||||
}
|
||||
|
||||
public function getUserByEmail(string $email)
|
||||
{
|
||||
return $this->where('email', $email)->first();
|
||||
}
|
||||
|
||||
public function verifyPassword(string $password, string $hash): bool
|
||||
{
|
||||
return password_verify($password, $hash);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user