[Pkg-owncloud-commits] [php-sabredav] 131/148: Creating principals via MKOCL and the browser plugin.

David Prévot taffit at moszumanska.debian.org
Wed Apr 15 01:37:32 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabredav.

commit 42ea6a1b37bb4d085d9c8d3fd2e6c81a52341d43
Author: Evert Pot <me at evertpot.com>
Date:   Sun Apr 12 23:29:42 2015 -0400

    Creating principals via MKOCL and the browser plugin.
---
 lib/CalDAV/Calendar.php                            |  6 +--
 lib/CalDAV/Plugin.php                              | 28 +-------------
 lib/CalDAV/Principal/Collection.php                |  2 +-
 lib/CardDAV/Plugin.php                             | 28 +-------------
 lib/DAV/Browser/Plugin.php                         | 32 +++++++++++++---
 lib/DAV/Server.php                                 |  6 ---
 lib/DAVACL/Plugin.php                              | 30 +++++++++++++++
 .../PrincipalBackend/CreatePrincipalSupport.php    | 30 +++++++++++++++
 lib/DAVACL/PrincipalBackend/PDO.php                | 33 ++++++++++------
 lib/DAVACL/PrincipalCollection.php                 | 44 +++++++++++++++++++++-
 10 files changed, 159 insertions(+), 80 deletions(-)

diff --git a/lib/CalDAV/Calendar.php b/lib/CalDAV/Calendar.php
index 7a092e4..493e2ef 100644
--- a/lib/CalDAV/Calendar.php
+++ b/lib/CalDAV/Calendar.php
@@ -85,10 +85,10 @@ class Calendar implements ICalendar, DAV\IProperties, DAV\Sync\ISyncCollection,
 
         $response = [];
 
-        foreach($requestedProperties as $prop) {
+        foreach($this->calendarInfo as $propName=>$propValue) {
 
-            if (isset($this->calendarInfo[$prop]))
-                $response[$prop] = $this->calendarInfo[$prop];
+            if ($propName[0]==='{')
+                $response[$propName] = $this->calendarInfo[$propName];
 
         }
         return $response;
diff --git a/lib/CalDAV/Plugin.php b/lib/CalDAV/Plugin.php
index 5ca6aa7..e80c761 100644
--- a/lib/CalDAV/Plugin.php
+++ b/lib/CalDAV/Plugin.php
@@ -174,7 +174,6 @@ class Plugin extends DAV\ServerPlugin {
         $server->on('report',              [$this,'report']);
         $server->on('propFind',            [$this,'propFind']);
         $server->on('onHTMLActionsPanel',  [$this,'htmlActionsPanel']);
-        $server->on('onBrowserPostAction', [$this,'browserPostAction']);
         $server->on('beforeCreateFile',    [$this,'beforeCreateFile']);
         $server->on('beforeWriteContent',  [$this,'beforeWriteContent']);
         $server->on('afterMethod:GET',     [$this,'httpAfterGET']);
@@ -897,7 +896,8 @@ class Plugin extends DAV\ServerPlugin {
 
         $output.= '<tr><td colspan="2"><form method="post" action="">
             <h3>Create new calendar</h3>
-            <input type="hidden" name="sabreAction" value="mkcalendar" />
+            <input type="hidden" name="sabreAction" value="mkcol" />
+            <input type="hidden" name="resourceType" value="{DAV:}collection,{' . self::NS_CALDAV . '}calendar" />
             <label>Name (uri):</label> <input type="text" name="name" /><br />
             <label>Display name:</label> <input type="text" name="{DAV:}displayname" /><br />
             <input type="submit" value="create" />
@@ -909,30 +909,6 @@ class Plugin extends DAV\ServerPlugin {
     }
 
     /**
-     * This method allows us to intercept the 'mkcalendar' sabreAction. This
-     * action enables the user to create new calendars from the browser plugin.
-     *
-     * @param string $uri
-     * @param string $action
-     * @param array $postVars
-     * @return bool
-     */
-    function browserPostAction($uri, $action, array $postVars) {
-
-        if ($action!=='mkcalendar')
-            return;
-
-        $resourceType = ['{DAV:}collection','{urn:ietf:params:xml:ns:caldav}calendar'];
-        $properties = [];
-        if (isset($postVars['{DAV:}displayname'])) {
-            $properties['{DAV:}displayname'] = $postVars['{DAV:}displayname'];
-        }
-        $this->server->createCollection($uri . '/' . $postVars['name'], new MkCol($resourceType,$properties));
-        return false;
-
-    }
-
-    /**
      * This event is triggered after GET requests.
      *
      * This is used to transform data into jCal, if this was requested.
diff --git a/lib/CalDAV/Principal/Collection.php b/lib/CalDAV/Principal/Collection.php
index 63748b0..c850c53 100644
--- a/lib/CalDAV/Principal/Collection.php
+++ b/lib/CalDAV/Principal/Collection.php
@@ -15,7 +15,7 @@ use Sabre\DAVACL;
  * @author Evert Pot (http://evertpot.com/)
  * @license http://sabre.io/license/ Modified BSD License
  */
-class Collection extends DAVACL\AbstractPrincipalCollection {
+class Collection extends DAVACL\PrincipalCollection {
 
     /**
      * Returns a child object based on principal information
diff --git a/lib/CardDAV/Plugin.php b/lib/CardDAV/Plugin.php
index 42f12b2..0aeff36 100644
--- a/lib/CardDAV/Plugin.php
+++ b/lib/CardDAV/Plugin.php
@@ -70,7 +70,6 @@ class Plugin extends DAV\ServerPlugin {
         $server->on('propFind',            [$this, 'propFindLate'],150);
         $server->on('report',              [$this, 'report']);
         $server->on('onHTMLActionsPanel',  [$this, 'htmlActionsPanel']);
-        $server->on('onBrowserPostAction', [$this, 'browserPostAction']);
         $server->on('beforeWriteContent',  [$this, 'beforeWriteContent']);
         $server->on('beforeCreateFile',    [$this, 'beforeCreateFile']);
         $server->on('afterMethod:GET',     [$this, 'httpAfterGet']);
@@ -686,7 +685,8 @@ class Plugin extends DAV\ServerPlugin {
 
         $output.= '<tr><td colspan="2"><form method="post" action="">
             <h3>Create new address book</h3>
-            <input type="hidden" name="sabreAction" value="mkaddressbook" />
+            <input type="hidden" name="sabreAction" value="mkcol" />
+            <input type="hidden" name="resourceType" value="{DAV:}collection,{' . self::NS_CARDDAV . '}addressbook" />
             <label>Name (uri):</label> <input type="text" name="name" /><br />
             <label>Display name:</label> <input type="text" name="{DAV:}displayname" /><br />
             <input type="submit" value="create" />
@@ -726,30 +726,6 @@ class Plugin extends DAV\ServerPlugin {
     }
 
     /**
-     * This method allows us to intercept the 'mkaddressbook' sabreAction. This
-     * action enables the user to create new addressbooks from the browser plugin.
-     *
-     * @param string $uri
-     * @param string $action
-     * @param array $postVars
-     * @return bool
-     */
-    function browserPostAction($uri, $action, array $postVars) {
-
-        if ($action!=='mkaddressbook')
-            return;
-
-        $resourceType = ['{DAV:}collection','{urn:ietf:params:xml:ns:carddav}addressbook'];
-        $properties = [];
-        if (isset($postVars['{DAV:}displayname'])) {
-            $properties['{DAV:}displayname'] = $postVars['{DAV:}displayname'];
-        }
-        $this->server->createCollection($uri . '/' . $postVars['name'], new MkCol($resourceType,$properties));
-        return false;
-
-    }
-
-    /**
      * This helper function performs the content-type negotiation for vcards.
      *
      * It will return one of the following strings:
diff --git a/lib/DAV/Browser/Plugin.php b/lib/DAV/Browser/Plugin.php
index 2b3eb12..4c4ae28 100644
--- a/lib/DAV/Browser/Plugin.php
+++ b/lib/DAV/Browser/Plugin.php
@@ -2,10 +2,12 @@
 
 namespace Sabre\DAV\Browser;
 
-use Sabre\DAV;
-use Sabre\HTTP\URLUtil;
-use Sabre\HTTP\RequestInterface;
-use Sabre\HTTP\ResponseInterface;
+use
+    Sabre\DAV,
+    Sabre\DAV\MkCol,
+    Sabre\HTTP\URLUtil,
+    Sabre\HTTP\RequestInterface,
+    Sabre\HTTP\ResponseInterface;
 
 /**
  * Browser Plugin
@@ -152,7 +154,27 @@ class Plugin extends DAV\ServerPlugin {
                     if (isset($postVars['name']) && trim($postVars['name'])) {
                         // Using basename() because we won't allow slashes
                         list(, $folderName) = URLUtil::splitPath(trim($postVars['name']));
-                        $this->server->createDirectory($uri . '/' . $folderName);
+
+                        if (isset($postVars['resourceType'])) {
+                            $resourceType = explode(',',$postVars['resourceType']);
+                        } else {
+                            $resourceType = ['{DAV:}collection'];
+                        }
+
+                        $properties = [];
+                        foreach($postVars as $varName => $varValue) {
+                            // Any _POST variable in clark notation is treated
+                            // like a property.
+                            if ($varName[0] === '{') {
+                                $properties[$varName] = $varValue;
+                            }
+                        }
+
+                        $mkCol = new MkCol(
+                            $resourceType,
+                            $properties
+                        );
+                        $this->server->createCollection($uri . '/' . $folderName, $mkCol);
                     }
                     break;
 
diff --git a/lib/DAV/Server.php b/lib/DAV/Server.php
index 30d2b71..34b884c 100644
--- a/lib/DAV/Server.php
+++ b/lib/DAV/Server.php
@@ -1110,12 +1110,6 @@ class Server extends EventEmitter {
 
         list($parentUri,$newName) = URLUtil::splitPath($uri);
 
-        // Making sure {DAV:}collection was specified as resourceType
-        if (!$mkCol->hasResourceType('{DAV:}collection')) {
-            throw new Exception\InvalidResourceType('The resourceType for this collection must at least include {DAV:}collection');
-        }
-
-
         // Making sure the parent exists
         try {
             $parent = $this->tree->getNodeForPath($parentUri);
diff --git a/lib/DAVACL/Plugin.php b/lib/DAVACL/Plugin.php
index aa358aa..bc04b32 100644
--- a/lib/DAVACL/Plugin.php
+++ b/lib/DAVACL/Plugin.php
@@ -686,6 +686,7 @@ class Plugin extends DAV\ServerPlugin {
         $server->on('beforeUnlock',        [$this,'beforeUnlock'],20);
         $server->on('report',              [$this,'report']);
         $server->on('method:ACL',          [$this,'httpAcl']);
+        $server->on('onHTMLActionsPanel',  [$this,'htmlActionsPanel']);
 
         array_push($server->protectedProperties,
             '{DAV:}alternate-URI-set',
@@ -1273,6 +1274,35 @@ class Plugin extends DAV\ServerPlugin {
     /* }}} */
 
     /**
+     * This method is used to generate HTML output for the
+     * DAV\Browser\Plugin. This allows us to generate an interface users
+     * can use to create new calendars.
+     *
+     * @param DAV\INode $node
+     * @param string $output
+     * @return bool
+     */
+    function htmlActionsPanel(DAV\INode $node, &$output) {
+
+        if (!$node instanceof PrincipalCollection)
+            return;
+
+        $output.= '<tr><td colspan="2"><form method="post" action="">
+            <h3>Create new principal</h3>
+            <input type="hidden" name="sabreAction" value="mkcol" />
+            <input type="hidden" name="resourceType" value="{DAV:}principal" />
+            <label>Name (uri):</label> <input type="text" name="name" /><br />
+            <label>Display name:</label> <input type="text" name="{DAV:}displayname" /><br />
+            <label>Email address:</label> <input type="text" name="{http://sabreav.org/ns}email-address" /><br />
+            <input type="submit" value="create" />
+            </form>
+            </td></tr>';
+
+        return false;
+
+    }
+
+    /**
      * Returns a bunch of meta-data about the plugin.
      *
      * Providing this information is optional, and is mainly displayed by the
diff --git a/lib/DAVACL/PrincipalBackend/CreatePrincipalSupport.php b/lib/DAVACL/PrincipalBackend/CreatePrincipalSupport.php
new file mode 100644
index 0000000..2c1df8e
--- /dev/null
+++ b/lib/DAVACL/PrincipalBackend/CreatePrincipalSupport.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Sabre\DAVACL\PrincipalBackend;
+
+use Sabre\DAV\MkCol;
+
+/**
+ * Implement this interface to add support for creating new principals to your
+ * principal backend.
+ *
+ * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+interface CreatePrincipalSupport extends BackendInterface {
+
+    /**
+     * Creates a new principal.
+     *
+     * This method receives a full path for the new principal. The mkCol object
+     * contains any additional webdav properties specified during the creation
+     * of the principal.
+     *
+     * @param string $path
+     * @param MkCol $mkCol
+     * @return void
+     */
+    function createPrincipal($path, MkCol $mkCol);
+
+}
diff --git a/lib/DAVACL/PrincipalBackend/PDO.php b/lib/DAVACL/PrincipalBackend/PDO.php
index 75f94f3..f6b4c2b 100644
--- a/lib/DAVACL/PrincipalBackend/PDO.php
+++ b/lib/DAVACL/PrincipalBackend/PDO.php
@@ -4,6 +4,7 @@ namespace Sabre\DAVACL\PrincipalBackend;
 
 use
     Sabre\DAV,
+    Sabre\DAV\MkCol,
     Sabre\DAVACL,
     Sabre\HTTP\URLUtil;
 
@@ -18,7 +19,7 @@ use
  * @author Evert Pot (http://evertpot.com/)
  * @license http://sabre.io/license/ Modified BSD License
  */
-class PDO extends AbstractBackend {
+class PDO extends AbstractBackend implements CreatePrincipalSupport {
 
     /**
      * PDO table name for 'principals'
@@ -56,17 +57,6 @@ class PDO extends AbstractBackend {
         ],
 
         /**
-         * This property is actually used by the CardDAV plugin, where it gets
-         * mapped to {http://calendarserver.orgi/ns/}me-card.
-         *
-         * The reason we don't straight-up use that property, is because
-         * me-card is defined as a property on the users' addressbook
-         * collection.
-         */
-        '{http://sabredav.org/ns}vcard-url' => [
-            'dbField' => 'vcardurl',
-        ],
-        /**
          * This is the users' primary email-address.
          */
         '{http://sabredav.org/ns}email-address' =>[
@@ -377,4 +367,23 @@ class PDO extends AbstractBackend {
 
     }
 
+    /**
+     * Creates a new principal.
+     *
+     * This method receives a full path for the new principal. The mkCol object
+     * contains any additional webdav properties specified during the creation
+     * of the principal.
+     *
+     * @param string $path
+     * @param MkCol $mkCol
+     * @return void
+     */
+    function createPrincipal($path, MkCol $mkCol) {
+
+        $stmt = $this->pdo->prepare('INSERT INTO ' . $this->tableName . ' (uri) VALUES (?)');
+        $stmt->execute([$path]);
+        $this->updatePrincipal($path, $mkCol);
+
+    }
+
 }
diff --git a/lib/DAVACL/PrincipalCollection.php b/lib/DAVACL/PrincipalCollection.php
index 472dfcd..ff0c801 100644
--- a/lib/DAVACL/PrincipalCollection.php
+++ b/lib/DAVACL/PrincipalCollection.php
@@ -2,6 +2,11 @@
 
 namespace Sabre\DAVACL;
 
+use
+    Sabre\DAV\Exception\InvalidResourceType,
+    Sabre\DAV\IExtendedCollection,
+    Sabre\DAV\MkCol;
+
 /**
  * Principals Collection
  *
@@ -12,7 +17,7 @@ namespace Sabre\DAVACL;
  * @author Evert Pot (http://evertpot.com/)
  * @license http://sabre.io/license/ Modified BSD License
  */
-class PrincipalCollection extends AbstractPrincipalCollection {
+class PrincipalCollection extends AbstractPrincipalCollection implements IExtendedCollection {
 
     /**
      * This method returns a node for a principal.
@@ -30,4 +35,41 @@ class PrincipalCollection extends AbstractPrincipalCollection {
 
     }
 
+    /**
+     * Creates a new collection.
+     *
+     * This method will receive a MkCol object with all the information about
+     * the new collection that's being created.
+     *
+     * The MkCol object contains information about the resourceType of the new
+     * collection. If you don't support the specified resourceType, you should
+     * throw Exception\InvalidResourceType.
+     *
+     * The object also contains a list of WebDAV properties for the new
+     * collection.
+     *
+     * You should call the handle() method on this object to specify exactly
+     * which properties you are storing. This allows the system to figure out
+     * exactly which properties you didn't store, which in turn allows other
+     * plugins (such as the propertystorage plugin) to handle storing the
+     * property for you.
+     *
+     * @param string $name
+     * @param MkCol $mkCol
+     * @throws Exception\InvalidResourceType
+     * @return void
+     */
+    function createExtendedCollection($name, MkCol $mkCol) {
+
+        if (!$mkCol->hasResourceType('{DAV:}principal')) {
+            throw new InvalidResourceType('Only resources of type {DAV:}principal may be created here');
+        }
+
+        $this->principalBackend->createPrincipal(
+            $this->principalPrefix . '/' . $name,
+            $mkCol
+        );
+
+    }
+
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list