DataObjects/Core_person.php
authorAlan <alan@roojs.com>
Wed, 28 Jun 2023 04:49:01 +0000 (12:49 +0800)
committerAlan <alan@roojs.com>
Wed, 28 Jun 2023 04:49:01 +0000 (12:49 +0800)
DataObjects/Core_person.php

index d892a6a..a80254a 100644 (file)
@@ -1446,7 +1446,8 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
     function beforeInsert($req, $roo)
     {
         if (!empty($req['_bulk_update_passwords'])) {
-            $this->bulkUpdatePasswords($req['_bulk_update_passwords'], $roo)
+            $this->bulkUpdatePasswords($req['_bulk_update_passwords'], $roo);
+            return;
         }
         
         $p = DB_DataObject::factory('core_person');
@@ -1722,6 +1723,53 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
         
         return $content;
     }
-    
+    function bulkUpdatePasswords($data, $roo)
+    {
+        $rows = explode("\n",$data);
+        $upd = array();
+        $bad  = array();
+        
+        foreach($rows  as $i=>$row) {
+            if (!strlen(trim($row))) {
+                continue;
+            }
+            $bits = preg_split('/\s+/', trim($row));
+            if (count($bits) != 2) {
+                $bad[] = "Invalid line: {$row}";
+                continue;
+            }
+            // validate.
+            $upd[$bits[0]] = $bits[1];
+            
+        }
+        // next fetch them all.
+        $p = DB_DataObject::factory('core_person');
+        $p->whereAddIn('email', array_keys($upd), 'string');
+        foreach($p->fetchAll() as $p) {
+            $map[$p->email] = $p;
+        }
+        foreach($upd as $k=>$nv) {
+            if (!isset($map[$k])) {
+                $bad[] = "Missing account with email: " . $k;
+                continue;
+            }
+            if ($map[$k]->id == $roo->authUser->id) {
+                $bad[] = "You can not update your own password here: " . $k;
+                continue;
+            }
+        }
+        if (!empty($bad)) {
+            $this->jerr("ERRORS: ". implode("\n", $bad));
+            return;
+        }
+        foreach($map as $k => $p) {
+            $pp = clone($p);
+            $p->setPassword($upd[$k]);
+            $p->update($pp);
+        }
+        $this->jok("Updated");
+        
+        
+    }
     
  }