Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| UserModel | |
0.00% |
0 / 17 |
|
0.00% |
0 / 5 |
110 | |
0.00% |
0 / 1 |
| hashPassword | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| injectTenant | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| findByEmail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| forCurrentTenant | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| forTenant | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Modules\SecurityModule\Models; |
| 4 | |
| 5 | use CodeIgniter\Model; |
| 6 | use App\Modules\SecurityModule\Entities\UserEntity; |
| 7 | |
| 8 | class UserModel extends Model |
| 9 | { |
| 10 | protected $table = 'user'; |
| 11 | protected $primaryKey = 'id'; |
| 12 | |
| 13 | protected $returnType = UserEntity::class; |
| 14 | protected $useTimestamps = true; |
| 15 | |
| 16 | protected $allowedFields = [ |
| 17 | 'name', |
| 18 | 'email', |
| 19 | 'password', |
| 20 | 'role', |
| 21 | 'status', |
| 22 | 'last_login_at', |
| 23 | 'tenant', |
| 24 | ]; |
| 25 | |
| 26 | protected $beforeInsert = ['hashPassword', 'injectTenant']; |
| 27 | protected $beforeUpdate = ['hashPassword', 'injectTenant']; |
| 28 | |
| 29 | /** |
| 30 | * Hash du mot de passe avant sauvegarde |
| 31 | */ |
| 32 | protected function hashPassword(array $data) |
| 33 | { |
| 34 | if (!isset($data['data']['password']) || empty($data['data']['password'])) { |
| 35 | return $data; |
| 36 | } |
| 37 | |
| 38 | $password = $data['data']['password']; |
| 39 | |
| 40 | // Évite de re-hasher un mot de passe déjà hashé |
| 41 | if (password_get_info($password)['algo'] !== 0) { |
| 42 | return $data; |
| 43 | } |
| 44 | |
| 45 | $data['data']['password'] = password_hash($password, PASSWORD_BCRYPT); |
| 46 | |
| 47 | return $data; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Injection automatique du tenant (contexte société) |
| 52 | */ |
| 53 | protected function injectTenant(array $data) |
| 54 | { |
| 55 | if (!isset($data['data']['tenant']) || empty($data['data']['tenant'])) { |
| 56 | $data['data']['tenant'] = |
| 57 | session()->get('tenant') |
| 58 | ?? service('request')->getHeaderLine('X-Tenant') |
| 59 | ?? null; |
| 60 | } |
| 61 | |
| 62 | return $data; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Recherche par email |
| 67 | */ |
| 68 | public function findByEmail(string $email) |
| 69 | { |
| 70 | return $this->where('email', $email)->first(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Scope : filtrage par tenant courant |
| 75 | */ |
| 76 | public function forCurrentTenant() |
| 77 | { |
| 78 | $tenant = session()->get('tenant'); |
| 79 | |
| 80 | return $this->where('tenant', $tenant); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Scope : filtrage explicite par tenant |
| 85 | */ |
| 86 | public function forTenant(string $tenant) |
| 87 | { |
| 88 | return $this->where('tenant', $tenant); |
| 89 | } |
| 90 | } |