diff options
| author | polo <ordipolo@gmx.fr> | 2026-10-23 00:47:32 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2026-10-23 00:47:32 +0200 |
| commit | f27cca08ff786ceff1870147f6d6bad14d71117f (patch) | |
| tree | d36597a72025313d6faf4ed228755fa267ebe2f6 /src/controller/UserController.php | |
| parent | c7309f900ef05bd5cb313110ec159b761af394ec (diff) | |
| download | cms-f27cca08ff786ceff1870147f6d6bad14d71117f.tar.gz cms-f27cca08ff786ceff1870147f6d6bad14d71117f.tar.bz2 cms-f27cca08ff786ceff1870147f6d6bad14d71117f.zip | |
dossiers avec majuscule, namespace pour toutes les classes, autoload psr-4, UserDataService::createZip déplacé dans Backup
Diffstat (limited to 'src/controller/UserController.php')
| -rw-r--r-- | src/controller/UserController.php | 230 |
1 files changed, 0 insertions, 230 deletions
diff --git a/src/controller/UserController.php b/src/controller/UserController.php deleted file mode 100644 index b68421f..0000000 --- a/src/controller/UserController.php +++ /dev/null | |||
| @@ -1,230 +0,0 @@ | |||
| 1 | <?php | ||
| 2 | // src/controller/UserController.php | ||
| 3 | // | ||
| 4 | /* actuellement un fourre-tout de méthodes en rapport avec les utilisateurs | ||
| 5 | pour l'améliorer on pourrait appliquer le principe de reponsabilité unique | ||
| 6 | => UserController devrait se limiter à gérer des requêtes et réponses (changement transparent pour le routeur) | ||
| 7 | il instancierait un classe correspondant au formulaire (prend POST et SESSION) et une classe "métier" UserService | ||
| 8 | => UserService contiendrait des méthodes utilisant l'objet formulaire pour agir sur la BDD | ||
| 9 | => les formulaires peuvent tenir dans une classe "UserUpdateForm" avec des stratégies ou plusieurs, les données y sont validées | ||
| 10 | => il est aussi possible de découper UserController en contrôleurs par fonctionnalité: | ||
| 11 | Auth (connexion, deconnexion), User (infos, choix photo), Account (créer, supprimer compte), Avatar (upload photo...) | ||
| 12 | */ | ||
| 13 | |||
| 14 | declare(strict_types=1); | ||
| 15 | |||
| 16 | use Doctrine\ORM\EntityManager; | ||
| 17 | use App\Entity\User; | ||
| 18 | use App\Entity\Log; | ||
| 19 | use Symfony\Component\HttpFoundation\RedirectResponse; | ||
| 20 | |||
| 21 | class UserController | ||
| 22 | { | ||
| 23 | // account | ||
| 24 | static public function createAdminUser(EntityManager $entityManager): RedirectResponse | ||
| 25 | { | ||
| 26 | unset($_SESSION['user']); | ||
| 27 | |||
| 28 | $form = new FormValidation($_POST, ValidationStrategy::CreateUser); | ||
| 29 | |||
| 30 | $url = new URL; | ||
| 31 | $error = ''; | ||
| 32 | if($form->validate()){ | ||
| 33 | $password = password_hash($_POST['password'], PASSWORD_DEFAULT); | ||
| 34 | $user = new User($_POST['login'], 'admin', $password); | ||
| 35 | $entityManager->persist($user); | ||
| 36 | $entityManager->flush(); | ||
| 37 | } | ||
| 38 | else{ | ||
| 39 | $error = $form->getError(); | ||
| 40 | } | ||
| 41 | |||
| 42 | if(!empty($error)){ | ||
| 43 | $url->addParams(['error' => $error]); | ||
| 44 | } | ||
| 45 | |||
| 46 | return new RedirectResponse((string)$url); | ||
| 47 | } | ||
| 48 | |||
| 49 | // account | ||
| 50 | //static public function createUser(EntityManager $entityManager){} | ||
| 51 | |||
| 52 | // auth | ||
| 53 | static public function connect(EntityManager $entityManager): RedirectResponse | ||
| 54 | { | ||
| 55 | $url = new URL; | ||
| 56 | if(IS_ADMIN){ // déjà connecté? | ||
| 57 | return new RedirectResponse((string)$url); | ||
| 58 | } | ||
| 59 | unset($_SESSION['user']); | ||
| 60 | |||
| 61 | $form = new FormValidation($_POST, ValidationStrategy::Connection); | ||
| 62 | |||
| 63 | $error = ''; | ||
| 64 | if($form->validate()){ | ||
| 65 | // à mettre dans une classe métier UserService, Authentication, AuthService? | ||
| 66 | $user = self::getUserByName($_POST['login'], $entityManager); | ||
| 67 | if(!empty($user) && $_POST['login'] === $user->getLogin() && password_verify($_POST['password'], $user->getPassword())){ | ||
| 68 | $log = new Log(true); | ||
| 69 | |||
| 70 | // protection fixation de session, si l'attaquant crée un cookie de session, il est remplacé | ||
| 71 | session_regenerate_id(true); | ||
| 72 | $_SESSION['user']['id'] = $user->getId(); | ||
| 73 | $_SESSION['user']['username'] = $user->getLogin(); | ||
| 74 | $_SESSION['user']['role'] = $user->getRole(); | ||
| 75 | |||
| 76 | EmailService::cleanEmails($entityManager); | ||
| 77 | |||
| 78 | try{ | ||
| 79 | if(file_exists('../var/backups')){ | ||
| 80 | Backup::mySQLdump($entityManager, 'auto'); // créer un nouveau backup, sauf si les dossiers nécessaires sont encore à créer | ||
| 81 | } | ||
| 82 | } | ||
| 83 | catch(RuntimeException $e){ | ||
| 84 | echo '<script>window.error_message = "' . $e->getMessage() . '";</script>'; | ||
| 85 | } | ||
| 86 | |||
| 87 | $url->addParams(isset($_GET['from']) ? ['page' => $_GET['from']] : []); | ||
| 88 | isset($_GET['id']) ? $url->addParams(['id' => $_GET['id']]) : ''; | ||
| 89 | } | ||
| 90 | else | ||
| 91 | { | ||
| 92 | $log = new Log(false); | ||
| 93 | $error = 'bad_login_or_password'; | ||
| 94 | } | ||
| 95 | $entityManager->persist($log); | ||
| 96 | $entityManager->flush(); | ||
| 97 | } | ||
| 98 | else{ | ||
| 99 | $error = $form->getError(); | ||
| 100 | } | ||
| 101 | |||
| 102 | if(!empty($error)){ | ||
| 103 | sleep(1); // défense basique à la force brute | ||
| 104 | $url->addParams(['page' => 'connection']); | ||
| 105 | isset($_GET['from']) ? $url->addParams(['from' => $_GET['from']]) : null; | ||
| 106 | isset($_GET['id']) ? $url->addParams(['id' => $_GET['id']]) : null; | ||
| 107 | $url->addParams(['error' => $error]); | ||
| 108 | } | ||
| 109 | |||
| 110 | return new RedirectResponse((string)$url); | ||
| 111 | } | ||
| 112 | |||
| 113 | // auth | ||
| 114 | static public function disconnect(): RedirectResponse | ||
| 115 | { | ||
| 116 | // nettoyage complet | ||
| 117 | unset($_SESSION['user']); // mémoire vive | ||
| 118 | session_destroy(); // fichier côté serveur | ||
| 119 | setcookie('PHPSESSID', '', time() - 86400, '/'); // cookie de session | ||
| 120 | |||
| 121 | // retour même page | ||
| 122 | $url = new URL; | ||
| 123 | isset($_GET['from']) ? $url->addParams(['page' => $_GET['from']]) : ''; | ||
| 124 | isset($_GET['id']) ? $url->addParams(['id' => $_GET['id']]) : ''; | ||
| 125 | return new RedirectResponse((string)$url); | ||
| 126 | } | ||
| 127 | |||
| 128 | // user | ||
| 129 | static public function updateUsername(EntityManager $entityManager): RedirectResponse | ||
| 130 | { | ||
| 131 | if(!IS_ADMIN){ // superflux, fait dans le routeur | ||
| 132 | return self::disconnect(); | ||
| 133 | } | ||
| 134 | |||
| 135 | $url = new URL(['page' => 'user_edit']); | ||
| 136 | isset($_GET['from']) ? $url->addParams(['from' => $_GET['from']]) : null; | ||
| 137 | |||
| 138 | $form = new FormValidation($_POST, ValidationStrategy::UsernameUpdate); | ||
| 139 | |||
| 140 | $error = ''; | ||
| 141 | if($form->validate()){ | ||
| 142 | // à mettre dans une classe métier UserService? | ||
| 143 | $user = self::getUserByName($_POST['login'], $entityManager); | ||
| 144 | if(password_verify($_POST['password'], $user->getPassword())){ | ||
| 145 | $user->setLogin($_POST['new_login']); | ||
| 146 | $entityManager->flush(); | ||
| 147 | $_SESSION['user']['username'] = $_POST['new_login']; | ||
| 148 | |||
| 149 | $url->addParams(['success_username' => 'new_login']); | ||
| 150 | $error = ''; | ||
| 151 | } | ||
| 152 | else{ | ||
| 153 | $error = 'bad_login_or_password'; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | else{ | ||
| 157 | $error = $form->getError(); | ||
| 158 | } | ||
| 159 | |||
| 160 | if(!empty($error)){ | ||
| 161 | sleep(1); | ||
| 162 | $url->addParams(['error_username' => $error]); | ||
| 163 | } | ||
| 164 | return new RedirectResponse((string)$url); | ||
| 165 | } | ||
| 166 | |||
| 167 | // user | ||
| 168 | static public function updatePassword(EntityManager $entityManager): RedirectResponse | ||
| 169 | { | ||
| 170 | if(!IS_ADMIN){ // superflux, fait dans le routeur | ||
| 171 | return self::disconnect(); | ||
| 172 | } | ||
| 173 | |||
| 174 | $url = new URL(['page' => 'user_edit']); | ||
| 175 | isset($_GET['from']) ? $url->addParams(['from' => $_GET['from']]) : null; | ||
| 176 | |||
| 177 | $form = new FormValidation($_POST, ValidationStrategy::PasswordUpdate); | ||
| 178 | |||
| 179 | $error = ''; | ||
| 180 | if($form->validate()){ | ||
| 181 | // à mettre dans une classe métier UserService? | ||
| 182 | $user = self::getUserByName($_POST['login'], $entityManager); | ||
| 183 | if(password_verify($_POST['password'], $user->getPassword())){ | ||
| 184 | $new_password = password_hash($_POST['new_password'], PASSWORD_DEFAULT); | ||
| 185 | $user->setPassword($new_password); | ||
| 186 | $entityManager->flush(); | ||
| 187 | |||
| 188 | $url->addParams(['success_password' => 'new_password']); | ||
| 189 | $error = ''; | ||
| 190 | } | ||
| 191 | else{ | ||
| 192 | $error = 'bad_login_or_password'; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | else{ | ||
| 196 | $error = $form->getError(); | ||
| 197 | } | ||
| 198 | |||
| 199 | if(!empty($error)){ | ||
| 200 | sleep(1); | ||
| 201 | $url->addParams(['error_password' => $error]); | ||
| 202 | } | ||
| 203 | return new RedirectResponse((string)$url); | ||
| 204 | } | ||
| 205 | |||
| 206 | // dans une classe mère ou un trait après découpage de UserController? | ||
| 207 | static private function getUserByName(string $login, EntityManager $entityManager): ?User | ||
| 208 | { | ||
| 209 | $users = $entityManager->getRepository(User::class)->findBy(['login' => $login]); | ||
| 210 | foreach($users as $user){ | ||
| 211 | if($user->getLogin() === $login){ | ||
| 212 | return $user; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | return null; | ||
| 216 | } | ||
| 217 | |||
| 218 | static public function getUserById(int $id, EntityManager $entityManager): ?User | ||
| 219 | { | ||
| 220 | return $entityManager->find(User::class, $id); | ||
| 221 | } | ||
| 222 | |||
| 223 | // dans une classe Form? | ||
| 224 | // erreurs à la création des mots de passe | ||
| 225 | static private function removeSpacesTabsCRLF(string $chaine): string | ||
| 226 | { | ||
| 227 | $cibles = [' ', "\t", "\n", "\r"]; // doubles quotes !! | ||
| 228 | return(str_replace($cibles, '', $chaine)); | ||
| 229 | } | ||
| 230 | } \ No newline at end of file | ||
