improves group principals resolving / fixes user setting
authorPhilipp Schüle <p.schuele@metaways.de>
Mon, 18 Aug 2014 15:31:57 +0000 (17:31 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 4 Sep 2014 09:26:42 +0000 (11:26 +0200)
(resolving still needs more love)

+ users are now correctly set (with caching)
+ adds param "dataonly" to skip calendar import on multiproc event
import
+ improves logging
+ activates 2 step update again

Change-Id: Ifb70115fb94a79cda4dc579d8a22c3498ad2b2d2
Reviewed-on: http://gerrit.tine20.com/customers/1014
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Calendar/Frontend/Cli.php
tine20/Calendar/Import/CalDav/Client.php
tine20/Tinebase/Import/CalDav/Client.php

index 5c896f0..561d412 100644 (file)
@@ -222,7 +222,7 @@ class Calendar_Frontend_Cli extends Tinebase_Frontend_Cli_Abstract
         
         $users = $this->_readCalDavUserFile($args['caldavuserfile']);
         
-        if ($mode === 'import') {
+        if ($mode === 'import' && (empty($args['dataonly']) || $args['dataonly'] == false)) {
             // first import the calendars, serial sadly
             $this->_importAllCalendars($users, $args['url']);
         }
index dc0c3b7..435357a 100644 (file)
@@ -474,8 +474,9 @@ class Calendar_Import_CalDav_Client extends Tinebase_Import_CalDav_Client
                         $type[] = Tinebase_Acl_Rights::ACCOUNT_TYPE_USER;
                         $privilege[] = $ace['privilege'];
                     } else {
-                        if (Tinebase_Core::isLogLevel(Zend_Log::WARN))
-                            Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' there is an unresolved principal: ' . $principal . ' in group: ' . $ace['principal']);
+                        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG))
+                            Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ 
+                                . ' there is an unresolved principal: ' . $principal . ' in group: ' . $ace['principal']);
                     }
                 }
             } else {
@@ -520,8 +521,11 @@ class Calendar_Import_CalDav_Client extends Tinebase_Import_CalDav_Client
             $grants[$user[$i]]['account_id'] = $user[$i];
             $grants[$user[$i]]['account_type'] = $type[$i];
         }
-        if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE))
-            Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' found grants: ' . print_r($grants, true) . ' for calendar: ' . $calUri);
+        
+        if (Tinebase_Core::isLogLevel(Zend_Log::INFO))
+            Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' found ' . count($grants) . ' grants for calendar: ' . $calUri);
+        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE))
+            Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' grants: ' . print_r($grants, true));
         
         return new Tinebase_Record_RecordSet('Tinebase_Model_Grants', $grants, TRUE);
     }
@@ -530,14 +534,14 @@ class Calendar_Import_CalDav_Client extends Tinebase_Import_CalDav_Client
     {
         $result = true;
         // first only update/import events where the current user is also the organizer
-//         foreach ($users as $username => $pwd) {
-//             $this->clearCurrentUserCalendarData();
-//             $this->userName = $username;
-//             $this->password = $pwd;
-//             if (!$this->updateAllCalendarData(true)) {
-//                 $result = false;
-//             }
-//         }
+        foreach ($users as $username => $pwd) {
+            $this->clearCurrentUserCalendarData();
+            $this->userName = $username;
+            $this->password = $pwd;
+            if (!$this->updateAllCalendarData(true)) {
+                $result = false;
+            }
+        }
         // then update all events again
         foreach ($users as $username => $pwd) {
             $this->clearCurrentUserCalendarData();
index f707229..eec0958 100644 (file)
@@ -73,27 +73,27 @@ class Tinebase_Import_CalDav_Client extends \Sabre\DAV\Client
     {
         $cacheId = convertCacheId('findCurrentUserPrincipal' . $this->userName);
         if (Tinebase_Core::getCache()->test($cacheId)) {
-            $this->currentUserPrincipal = Tinebase_Core::getCache()->load($cacheId);
-            $this->principals[$this->currentUserPrincipal] = Tinebase_User::getInstance()->getUserByLoginName($this->userName, 'Tinebase_Model_FullUser');
             if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . ' ' . __LINE__
                     . ' Loading user principal from cache');
+            
+            $this->currentUserPrincipal = Tinebase_Core::getCache()->load($cacheId);
+            $user = $this->_setUser();
+            if (! $user) {
+                return false;
+            }
+            
             return true;
         }
         
         $result = $this->calDavRequest('PROPFIND', '/principals/', self::findCurrentUserPrincipalRequest, 0, $tries);
         if (isset($result['{DAV:}current-user-principal']))
         {
-            try {
-                $user = Tinebase_User::getInstance()->getUserByLoginName($this->userName, 'Tinebase_Model_FullUser');
-                Tinebase_Core::set(Tinebase_Core::USER, $user);
-                $credentialCache = Tinebase_Auth_CredentialCache::getInstance()->cacheCredentials($this->userName, $this->password);
-                Tinebase_Core::set(Tinebase_Core::USERCREDENTIALCACHE, $credentialCache);
-            } catch (Tinebase_Exception_NotFound $e) {
-                Tinebase_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ' can\'t find tine20 user: ' . $this->userName);
+            $this->currentUserPrincipal = $result['{DAV:}current-user-principal'];
+            $user = $this->_setUser();
+            if (! $user) {
                 return false;
             }
-            $this->currentUserPrincipal = $result['{DAV:}current-user-principal'];
-            $this->principals[$this->currentUserPrincipal] = $user;
+            
             Tinebase_Core::getCache()->save($this->currentUserPrincipal, $cacheId, array(), /* 1 week */ 24*3600*7);
             return true;
         }
@@ -102,6 +102,23 @@ class Tinebase_Import_CalDav_Client extends \Sabre\DAV\Client
         return false;
     }
     
+    protected function _setUser()
+    {
+        try {
+            $user = Tinebase_User::getInstance()->getUserByLoginName($this->userName, 'Tinebase_Model_FullUser');
+            Tinebase_Core::set(Tinebase_Core::USER, $user);
+            $credentialCache = Tinebase_Auth_CredentialCache::getInstance()->cacheCredentials($this->userName, $this->password);
+            Tinebase_Core::set(Tinebase_Core::USERCREDENTIALCACHE, $credentialCache);
+        } catch (Tinebase_Exception_NotFound $e) {
+            Tinebase_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ' can\'t find tine20 user: ' . $this->userName);
+            return null;
+        }
+        
+        $this->principals[$this->currentUserPrincipal] = $user;
+        
+        return $user;
+    }
+    
     public function findCurrentUserPrincipalForUsers(array &$users)
     {
         foreach ($users as $username => $pwd) {
@@ -134,6 +151,9 @@ class Tinebase_Import_CalDav_Client extends \Sabre\DAV\Client
      */
     public function findCalendarHomeSet()
     {
+        if ('' == $this->currentUserPrincipal && ! $this->findCurrentUserPrincipal(/* tries = */ 3)) {
+            return false;
+        }
         $cacheId = convertCacheId('findCalendarHomeSet' . $this->userName);
         if (Tinebase_Core::getCache()->test($cacheId)) {
             $this->calendarHomeSet = Tinebase_Core::getCache()->load($cacheId);
@@ -142,10 +162,6 @@ class Tinebase_Import_CalDav_Client extends \Sabre\DAV\Client
             return true;
         }
         
-        if ('' == $this->currentUserPrincipal && ! $this->findCurrentUserPrincipal(/* tries = */ 3)) {
-            return false;
-        }
-        
         $result = $this->calDavRequest('PROPFIND', $this->currentUserPrincipal, self::findCalendarHomeSetRequest);
         
         if (isset($result['{urn:ietf:params:xml:ns:caldav}calendar-home-set'])) {
@@ -158,13 +174,22 @@ class Tinebase_Import_CalDav_Client extends \Sabre\DAV\Client
         return false;
     }
     
+    /**
+     * resolve principals
+     * 
+     * @param array $privileges
+     * 
+     * @todo improve algorithm (recursive?)
+     */
     public function resolvePrincipals(array $privileges)
     {
         foreach ($privileges as $ace)
         {
             if ( $ace['principal'] == '{DAV:}authenticated' || $ace['principal'] == $this->currentUserPrincipal ||
-                 isset($this->principals[$ace['principal']]) || isset($this->principalGroups[$ace['principal']]))
-                         continue;
+                 isset($this->principals[$ace['principal']]) || isset($this->principalGroups[$ace['principal']])) {
+                     continue;
+            }
+            
             $result = $this->calDavRequest('PROPFIND', $ace['principal'], self::resolvePrincipalRequest);
             if (isset($result['{DAV:}group-member-set'])) {
                 $principals = $result['{DAV:}group-member-set']->getPrincipals();
@@ -175,14 +200,20 @@ class Tinebase_Import_CalDav_Client extends \Sabre\DAV\Client
                 $groupPrincipals = array();
                 foreach ($principals as $key => $principal) {
                     if (! isset($this->principals[$principal])) {
+                        if (isset($this->principalGroups[$principal])) {
+                            $groupPrincipals = array_merge($groupPrincipals, $this->principalGroups[$principal]);
+                            continue;
+                        }
+                        
                         $result = $this->calDavRequest('PROPFIND', $principal, self::resolvePrincipalRequest);
                         if (isset($result['{DAV:}group-member-set'])) {
                             
                             $groupMemberPrincipals = $result['{DAV:}group-member-set']->getPrincipals();
                             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__
-                                    . ' Found group member principals (group: ' . principal . '): ' . print_r($groupMemberPrincipals, true));
+                                    . ' Found group member principals (group: ' . $principal . '): ' . print_r($groupMemberPrincipals, true));
                             
                             $groupPrincipals = array_merge($groupPrincipals, $groupMemberPrincipals);
+                            $this->principalGroups[$principal] = $groupMemberPrincipals;
 
                             unset($principals[$key]);
                         }