Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
77.78% |
7 / 9 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| User | |
77.78% |
7 / 9 |
|
33.33% |
1 / 3 |
11.10 | |
0.00% |
0 / 1 |
| _setPassword | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| lockAdminAccountError | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
| lockEnabledAccountError | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
4.59 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Model\Entity; |
| 5 | |
| 6 | use Authentication\PasswordHasher\DefaultPasswordHasher; |
| 7 | use Cake\ORM\Entity; |
| 8 | |
| 9 | /** |
| 10 | * User Entity |
| 11 | * |
| 12 | * @property string $id |
| 13 | * @property string $username |
| 14 | * @property string $password |
| 15 | * @property string|null $email |
| 16 | * @property bool $is_admin |
| 17 | * @property bool $active |
| 18 | * @property string|null $image |
| 19 | * @property string|null $dir |
| 20 | * @property string|null $keywords |
| 21 | * @property string|null $alt_text |
| 22 | * @property \Cake\I18n\DateTime|null $created |
| 23 | * @property \Cake\I18n\DateTime|null $modified |
| 24 | * |
| 25 | * @property \App\Model\Entity\Article[] $articles |
| 26 | * @property \App\Model\Entity\Comment[] $comments |
| 27 | */ |
| 28 | class User extends Entity |
| 29 | { |
| 30 | use ImageUrlTrait; |
| 31 | |
| 32 | /** |
| 33 | * Fields that can be mass assigned using newEntity() or patchEntity(). |
| 34 | * |
| 35 | * Note that when '*' is set to true, this allows all unspecified fields to |
| 36 | * be mass assigned. For security purposes, it is advised to set '*' to false |
| 37 | * (or remove it), and explicitly make individual fields accessible as needed. |
| 38 | * |
| 39 | * @var array<string, bool> |
| 40 | */ |
| 41 | protected array $_accessible = [ |
| 42 | 'username' => true, |
| 43 | 'password' => true, |
| 44 | 'email' => true, |
| 45 | 'created' => true, |
| 46 | 'modified' => true, |
| 47 | 'articles' => true, |
| 48 | 'image' => true, |
| 49 | 'is_admin' => false, |
| 50 | 'active' => false, |
| 51 | 'keywords' => true, |
| 52 | 'alt_text' => true, |
| 53 | ]; |
| 54 | |
| 55 | /** |
| 56 | * Fields that are excluded from JSON versions of the entity. |
| 57 | * |
| 58 | * @var list<string> |
| 59 | */ |
| 60 | protected array $_hidden = [ |
| 61 | 'password', |
| 62 | ]; |
| 63 | |
| 64 | /** |
| 65 | * Sets the password for the user entity. |
| 66 | * |
| 67 | * This method hashes the password using the DefaultPasswordHasher if the provided |
| 68 | * password is not empty. If the password is empty, it returns the original password value. |
| 69 | * This ensures that passwords are stored securely in the database and prevents |
| 70 | * overwriting existing passwords with empty values during updates. |
| 71 | * |
| 72 | * @param string $password The plain text password to be hashed. |
| 73 | * @return string|null The hashed password if the input is not empty, or the original password value. |
| 74 | */ |
| 75 | protected function _setPassword(string $password): ?string |
| 76 | { |
| 77 | if (strlen($password) > 0) { |
| 78 | return (new DefaultPasswordHasher())->hash($password); |
| 79 | } |
| 80 | |
| 81 | return $this->getOriginal('password'); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Checks if the account associated with the given user ID is being disabled by the user themselves. |
| 86 | * |
| 87 | * This method evaluates whether the 'active' flag is set to true in the provided data array |
| 88 | * and if the current object's ID matches the provided user ID. If both conditions are met, it returns true, |
| 89 | * indicating that the user is attempting to disable their own account. |
| 90 | * |
| 91 | * @param string $userId The ID of the user whose account is being checked. |
| 92 | * @param array $data An associative array containing account data, including the 'active' flag. |
| 93 | * @return bool|null Returns true if the account is being disabled by the user themselves, false otherwise. |
| 94 | * (Note: The method always returns a boolean, so the null part of the return type |
| 95 | * is not utilized in the current implementation.) |
| 96 | */ |
| 97 | public function lockAdminAccountError(string $userId, array $data): ?bool |
| 98 | { |
| 99 | //Setting is_admin to 0 for your own account |
| 100 | if (isset($data['is_admin']) && !$data['is_admin'] && $this->id == $userId) { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Checks if an admin account is being demoted by the account owner. |
| 109 | * |
| 110 | * This method determines whether the 'is_admin' flag is being set to false (0) |
| 111 | * for the account that matches the provided user ID. It returns true if the |
| 112 | * account owner is attempting to remove their own admin privileges, otherwise |
| 113 | * it returns false. |
| 114 | * |
| 115 | * @param string $userId The ID of the user attempting to modify the account. |
| 116 | * @param array $data An associative array containing the account data, including the 'is_admin' flag. |
| 117 | * @return bool|null Returns true if the admin account is being demoted by the owner, false otherwise. |
| 118 | * (Note: The method always returns a boolean, so the null part of the return type |
| 119 | * is not utilized in the current implementation.) |
| 120 | */ |
| 121 | public function lockEnabledAccountError(string $userId, array $data): ?bool |
| 122 | { |
| 123 | //Setting active to 0 for your own account |
| 124 | if (isset($data['active']) && !$data['active'] && $this->id == $userId) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | } |