1 /*
  2  * Copyright ©2012 SARA bv, The Netherlands
  3  *
  4  * This file is part of js-webdav-client.
  5  *
  6  * js-webdav-client is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published
  8  * by the Free Software Foundation, either version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * js-webdav-client is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU Lesser General Public License
 17  * along with js-webdav-client.  If not, see <http://www.gnu.org/licenses/>.
 18  */
 19 "use strict";
 20 
 21 // If nl.sara.webdav.codec.AclCodec is already defined, we have a namespace clash!
 22 if (nl.sara.webdav.codec.AclCodec !== undefined) {
 23   throw new nl.sara.webdav.Exception('Namespace nl.sara.webdav.codec.AclCodec already taken, could not load JavaScript library for WebDAV connectivity.', nl.sara.webdav.Exception.NAMESPACE_TAKEN);
 24 }
 25 
 26 /**
 27  * @class Adds a codec that converts DAV: acl to an nl.sara.webdav.Acl object
 28  * @augments nl.sara.webdav.Codec
 29  */
 30 nl.sara.webdav.codec.AclCodec = new nl.sara.webdav.Codec();
 31 nl.sara.webdav.codec.AclCodec.namespace = 'DAV:';
 32 nl.sara.webdav.codec.AclCodec.tagname = 'acl';
 33 
 34 nl.sara.webdav.codec.AclCodec.fromXML = function(nodelist) {
 35   // The constructor of Acl will parse a DAV: acl node itself, so just make sure this nodelist is part of a DAV: acl node
 36   var xmlDoc = document.implementation.createDocument("DAV:", "acl", null);
 37   var acl = xmlDoc.documentElement;
 38   for (var i = 0; i < nodelist.length; i++) {
 39     acl.appendChild( xmlDoc.importNode( nodelist.item( i ), true ) );
 40   }
 41   return new nl.sara.webdav.Acl(acl);
 42 };
 43 
 44 nl.sara.webdav.codec.AclCodec.toXML = function(acl, xmlDoc){
 45   var aclLength = acl.getLength();
 46   for (var i = 0; i < aclLength; i++) { // Loop over the ACE's in this ACL
 47     var ace = acl.getAce(i);
 48     var aceBody = xmlDoc.createElementNS('DAV:', 'ace');
 49 
 50     // First create a principal node
 51     var principal = xmlDoc.createElementNS('DAV:', 'principal');
 52     var princVal = ace.principal;
 53     switch (princVal) {
 54       case nl.sara.webdav.Ace.ALL:
 55         principal.appendChild(xmlDoc.createElementNS('DAV:', 'all'));
 56         break;
 57       case nl.sara.webdav.Ace.AUTHENTICATED:
 58         principal.appendChild(xmlDoc.createElementNS('DAV:', 'authenticated'));
 59         break;
 60       case nl.sara.webdav.Ace.UNAUTHENTICATED:
 61         principal.appendChild(xmlDoc.createElementNS('DAV:', 'unauthenticated'));
 62         break;
 63       case nl.sara.webdav.Ace.SELF:
 64         principal.appendChild(xmlDoc.createElementNS('DAV:', 'self'));
 65         break;
 66       default: // If it isn't one of the constants, it should be either a Property object or a string/URL
 67         if (typeof princVal === 'string') { // It is a string; the URL of the principal
 68           var href = xmlDoc.createElementNS('DAV:', 'href');
 69           href.appendChild(xmlDoc.createCDATASection(princVal));
 70           principal.appendChild(href);
 71         }else{ // And else it is a property
 72           var property = xmlDoc.createElementNS('DAV:', 'property');
 73           var prop = xmlDoc.createElementNS(princVal.namespace, princVal.tagname);
 74           if (princVal.xmlvalue !== null) {
 75             for (var j = 0; j < princVal.xmlvalue.length; j++) {
 76               var nodeCopy = xmlDoc.importNode(princVal.xmlValue.item(j), true);
 77               prop.appendChild(nodeCopy);
 78             }
 79           }
 80           property.appendChild(prop);
 81           principal.appendChild(property);
 82         }
 83       break;
 84     }
 85 
 86     // If the principal should be inverted, put it in an 'invert' element
 87     if (ace.invertprincipal) {
 88       var invert = xmlDoc.createElementNS('DAV:', 'invert');
 89       invert.appendChild(principal);
 90       aceBody.appendChild(invert);
 91     }else{
 92       aceBody.appendChild(principal);
 93     }
 94 
 95     // Then prepare the privileges
 96     // grant or deny?
 97     var privilegeParent = null;
 98     if (ace.grantdeny === nl.sara.webdav.Ace.DENY) {
 99       privilegeParent = xmlDoc.createElementNS('DAV:', 'deny');
100     }else if (ace.grantdeny === nl.sara.webdav.Ace.GRANT) {
101       privilegeParent = xmlDoc.createElementNS('DAV:', 'grant');
102     }else{
103       throw new nl.sara.webdav.Exception('\'grantdeny\' property not set on one of the ACE\'s in this ACL', nl.sara.webdav.Exception.WRONG_VALUE);
104     }
105     var namespaces = ace.getNamespaceNames();
106     for (j = 0; j < namespaces.length; j++) { // loop through namespaces
107       var namespace = namespaces[j];
108       var privileges = ace.getPrivilegeNames(namespace);
109       for (var k = 0; k < privileges.length; k++) { // loop through each privilege in this namespace
110         var privilege = ace.getPrivilege(namespace, privileges[k]);
111         var privilegeElement = xmlDoc.createElementNS('DAV:', 'privilege');
112         var priv = xmlDoc.createElementNS(privilege.namespace, privilege.tagname);
113         if (privilege.xmlvalue !== null) {
114           for (var l = 0; l < privilege.xmlvalue.length; l++) {
115             var nodeCopy = xmlDoc.importNode(privilege.xmlValue.item(j), true);
116             priv.appendChild(nodeCopy);
117           }
118         }
119         privilegeElement.appendChild(priv);
120         privilegeParent.appendChild(privilegeElement);
121       }
122     }
123     aceBody.appendChild(privilegeParent);
124 
125     xmlDoc.documentElement.appendChild(aceBody);
126   }
127   return xmlDoc;
128 };
129 
130 nl.sara.webdav.Property.addCodec(nl.sara.webdav.codec.AclCodec);
131 
132 // End of file
133