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.Acl is already defined, we have a namespace clash!
 22 if (nl.sara.webdav.Acl !== undefined) {
 23   throw new nl.sara.webdav.Exception('Namespace nl.sara.webdav.Acl already taken, could not load JavaScript library for WebDAV connectivity.', nl.sara.webdav.Exception.NAMESPACE_TAKEN);
 24 }
 25 
 26 /**
 27  * @class Access Control List
 28  *
 29  * @param  {Node}  [xmlNode]  Optional; the xmlNode describing the acl object (should be compliant with RFC 3744)
 30  */
 31 nl.sara.webdav.Acl = function(xmlNode) {
 32   // First define private attributes
 33   Object.defineProperty(this, '_aces', {
 34     'value': [],
 35     'enumerable': false,
 36     'configurable': false,
 37     'writable': true
 38   });
 39 
 40   // Constructor logic
 41   // Parse the XML
 42   if (typeof xmlNode !== 'undefined') {
 43     if ((xmlNode.namespaceURI !== 'DAV:') || (nl.sara.webdav.Ie.getLocalName(xmlNode) !== 'acl')) {
 44       throw new nl.sara.webdav.Exception('Node is not of type DAV:acl', nl.sara.webdav.Exception.WRONG_XML);
 45     }
 46     for (var i = 0; i < xmlNode.childNodes.length; i++) {
 47       var child = xmlNode.childNodes.item(i);
 48       if ((child.namespaceURI === null) || (child.namespaceURI !== 'DAV:') || (nl.sara.webdav.Ie.getLocalName(child) !== 'ace')) { // Skip if not the right element
 49         continue;
 50       }
 51       this.addAce(new nl.sara.webdav.Ace(child));
 52     }
 53   }
 54 };
 55 
 56 //########################## DEFINE PUBLIC METHODS #############################
 57 /**
 58  * Adds an ACE
 59  *
 60  * @param    {nl.sara.webdav.Ace}  ace       The ACE to add
 61  * @param    {Number}              position  Optional; The position to add this ACE. If the position is lower than 1, 0 is assumed, of the position is higher than the current length of the ACL or not specified, the ACE is appended to the end.
 62  * @returns  {nl.sara.webdav.Acl}            The ACL itself for chaining methods
 63  */
 64 nl.sara.webdav.Acl.prototype.addAce = function(ace, position) {
 65   if (!nl.sara.webdav.Ie.isIE && !(ace instanceof nl.sara.webdav.Ace)) {
 66     throw new nl.sara.webdav.Exception('Ace should be instance of Ace', nl.sara.webdav.Exception.WRONG_TYPE);
 67   }
 68   if ((position === undefined) || (position > (this._aces.length - 1))) {
 69     this._aces.push(ace);
 70   }else{
 71     position = Number(position);
 72     if (position < 1) {
 73       this._aces.unshift(ace);
 74     }else{
 75       this._aces.splice(position, 0, ace);
 76     }
 77   }
 78   return this;
 79 };
 80 
 81 /**
 82  * Gets the ACL as an array
 83  *
 84  * @returns  {nl.sara.webdav.Ace[]}  An array of the ACE's in this ACL
 85  */
 86 nl.sara.webdav.Acl.prototype.getAces = function() {
 87   return this._aces;
 88 };
 89 
 90 /**
 91  * Gets one ACE from a certain position
 92  *
 93  * @param    {Number}              position  The position of the ACE
 94  * @returns  {nl.sara.webdav.Ace}            The ACE
 95  */
 96 nl.sara.webdav.Acl.prototype.getAce = function(position) {
 97   position = Number(position);
 98   if ((position < 0) || (position >= this.getLength())) {
 99     throw new nl.sara.webdav.Exception('No ACE found on position ' + position, nl.sara.webdav.Exception.UNEXISTING_PROPERTY);
100   }
101   return this._aces[position];
102 };
103 
104 /**
105  * Gets the length of the ACL
106  *
107  * @returns  {Number}  The length of the ACL
108  */
109 nl.sara.webdav.Acl.prototype.getLength = function() {
110   return this._aces.length;
111 };
112 
113 // End of library
114