auth: store global user metadata in User object
[minimedit.git] / login / pass.inc.php
index 2b4665c01765a4b61fdbb247031725f9eb084e03..5b03c8090eedd5ff9ce198e4cea43f2cf7eb5466 100644 (file)
@@ -1,21 +1,21 @@
 <?php
-function passform($user, $input = [])
+function passform(&$user, $input = [])
 {
        if (empty($user)) {
                return "Log eerst (opnieuw?) in.";
        }
 
-       $pwfile = "{$user['dir']}/.passwd";
-       if (!is_writable($pwfile)) {
+       $pwfile = "{$user->dir}/.passwd";
+       if (file_exists($pwfile) and !is_writable($pwfile)) {
                return "Het wachtwoord kan niet worden aangepast voor deze gebruiker.";
        }
 
-       if (!empty($user['pass'])) {
+       if (!empty($user->pass)) {
                if (empty($input['oldpass'])) {
                        return "Als extra beveiliging tegen ongewenste aanpassingen moet het bestaande wachtwoord worden ingevoerd.";
                }
 
-               if (!login_password_verify($input['oldpass'], $user['pass'])) {
+               if (!login_password_verify($input['oldpass'], $user->pass)) {
                        return "Het bestaande wachtwoord is onjuist ingevoerd; niet aangepast.";
                }
        }
@@ -28,19 +28,26 @@ function passform($user, $input = [])
                return "Zo'n kort wachtwoord is een slecht idee.";
        }
 
-       if ($input['newpass'] == $user['name']) {
+       if ($input['newpass'] == $user->login) {
                return "De loginnaam is wel heel makkelijk raadbaar als wachtwoord.";
        }
 
-       if (!file_put_contents($pwfile, $input['newpass'])) {
+       if ($input['newpass'] != $input['passconf']) {
+               return "Zorg dat bij de bevestiging precies het zelfde wachtwoord staat.";
+       }
+
+       $passstore = password_hash($input['newpass'], PASSWORD_DEFAULT);
+       if (empty($passstore) or !file_put_contents($pwfile, $passstore)) {
                return "Het nieuwe wachtwoord kon niet worden opgeslagen. Het oude wachtwoord is behouden.";
        }
 
-       @unlink("{$user['dir']}/.token"); # invalidate reset token
+       @unlink("{$user->dir}/.token"); # invalidate reset token
 
-       $authhash = md5($input['newpass']);
-       $user['auth'] = "{$user['name']}:$authhash";
-       login_setcookie();
+       $authhash = md5($passstore);
+       $user->auth = "{$user->login}:$authhash";
+       if ($GLOBALS['User'] === $user) {
+               login_setcookie();
+       }
        return;
 }