0012530: add repair container owner cli fn and improve update script
authorPhilipp Schüle <p.schuele@metaways.de>
Mon, 9 Jan 2017 14:05:22 +0000 (15:05 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 18 Jan 2017 14:24:38 +0000 (15:24 +0100)
https://forge.tine20.org/view.php?id=12530

Change-Id: I5fec1da7bcf28908944d26f5a3178bbef7b02422
Reviewed-on: http://gerrit.tine20.com/customers/4060
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/Container.php
tine20/Tinebase/Frontend/Cli.php
tine20/Tinebase/Setup/Update/Release9.php

index 73e383b..e765fb7 100644 (file)
@@ -1911,4 +1911,36 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract implements Tineba
         
         return parent::update($_record);
     }
+
+    public function setContainerOwners()
+    {
+        $select = $this->_getSelect('id')
+            ->where('type = ?', Tinebase_Model_Container::TYPE_PERSONAL)
+            ->where('owner_id is null or owner_id = ?', '');
+
+        $stmt = $this->_db->query($select);
+        $containers = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
+
+        $count = 0;
+        foreach ($containers as $container) {
+            $id = $container['id'];
+            $grants = $this->getGrantsOfContainer($id, /* ignore acl */ true);
+            foreach ($grants as $grant) {
+                if ($grant->adminGrant && $grant->account_type == 'user') {
+                    if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+                        . ' Set owner for container id ' . $id . ': ' .  $grant->account_id);
+                    $where  = array(
+                        $this->_db->quoteInto($this->_db->quoteIdentifier('id') . ' = ?', $id),
+                    );
+
+                    $this->_db->update($this->_tablePrefix . $this->_tableName, array('owner_id' => $grant->account_id), $where);
+
+                    $count++;
+                }
+            }
+        }
+
+        if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+            . ' Set owner for ' . $count . ' containers.');
+    }
 }
index 8b82be5..cf61013 100644 (file)
@@ -1047,4 +1047,21 @@ class Tinebase_Frontend_Cli extends Tinebase_Frontend_Cli_Abstract
 
         return 0;
     }
+
+    /**
+     *
+     *
+     * @return int
+     */
+    public function repairContainerOwner()
+    {
+        if (! $this->_checkAdminRight()) {
+            return -1;
+        }
+
+        $this->_addOutputLogWriter(6);
+        Tinebase_Container::getInstance()->setContainerOwners();
+
+        return 0;
+    }
 }
index 2a4d5a4..758be08 100644 (file)
@@ -90,45 +90,12 @@ class Tinebase_Setup_Update_Release9 extends Setup_Update_Abstract
         }
         $this->setTableVersion('container', '10');
 
-        //Tinebase_Core::getCache()->clean();
-        $this->_setContainerOwners();
+        Tinebase_Container::getInstance()->setContainerOwners();
 
         $this->setApplicationVersion('Tinebase', '9.5');
     }
 
     /**
-     * write owner to all personal containers
-     */
-    protected function _setContainerOwners()
-    {
-        $filter = new Tinebase_Model_ContainerFilter(array(
-            array('field' => 'type', 'operator' => 'equals', 'value' => Tinebase_Model_Container::TYPE_PERSONAL),
-            array('field' => 'owner_id', 'operator' => 'isnull', 'value' => ''),
-        ));
-        $count = 0;
-        $paging = new Tinebase_Model_Pagination(array(
-            'start' => 0,
-            'limit' => 100,
-        ));
-        $containers = Tinebase_Container::getInstance()->search($filter, $paging);
-        while (count($containers) > 0) {
-            foreach ($containers as $container) {
-                $ownerId = Tinebase_Container::getInstance()->getContainerOwner($container);
-                if ($ownerId) {
-                    $container->owner_id = $ownerId;
-                    Tinebase_Container::getInstance()->update($container);
-                    $count++;
-                }
-            }
-            $paging->start += $paging->limit;
-            $containers = Tinebase_Container::getInstance()->search($filter, $paging);
-        }
-
-        if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
-            . ' Set owner for ' . $count . ' containers.');
-    }
-
-    /**
      * update to 9.6
      *
      * change length of groups.description column from varchar(255) to text