@@ -38,185 +38,185 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareable { |
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * AddressBook constructor. |
|
| 43 | - * |
|
| 44 | - * @param BackendInterface $carddavBackend |
|
| 45 | - * @param array $addressBookInfo |
|
| 46 | - * @param IL10N $l10n |
|
| 47 | - */ |
|
| 48 | - public function __construct(BackendInterface $carddavBackend, array $addressBookInfo, IL10N $l10n) { |
|
| 49 | - parent::__construct($carddavBackend, $addressBookInfo); |
|
| 50 | - |
|
| 51 | - if ($this->addressBookInfo['{DAV:}displayname'] === CardDavBackend::PERSONAL_ADDRESSBOOK_NAME && |
|
| 52 | - $this->getName() === CardDavBackend::PERSONAL_ADDRESSBOOK_URI) { |
|
| 53 | - $this->addressBookInfo['{DAV:}displayname'] = $l10n->t('Contacts'); |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Updates the list of shares. |
|
| 59 | - * |
|
| 60 | - * The first array is a list of people that are to be added to the |
|
| 61 | - * addressbook. |
|
| 62 | - * |
|
| 63 | - * Every element in the add array has the following properties: |
|
| 64 | - * * href - A url. Usually a mailto: address |
|
| 65 | - * * commonName - Usually a first and last name, or false |
|
| 66 | - * * summary - A description of the share, can also be false |
|
| 67 | - * * readOnly - A boolean value |
|
| 68 | - * |
|
| 69 | - * Every element in the remove array is just the address string. |
|
| 70 | - * |
|
| 71 | - * @param array $add |
|
| 72 | - * @param array $remove |
|
| 73 | - * @return void |
|
| 74 | - * @throws Forbidden |
|
| 75 | - */ |
|
| 76 | - public function updateShares(array $add, array $remove) { |
|
| 77 | - if ($this->isShared()) { |
|
| 78 | - throw new Forbidden(); |
|
| 79 | - } |
|
| 80 | - $this->carddavBackend->updateShares($this, $add, $remove); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Returns the list of people whom this addressbook is shared with. |
|
| 85 | - * |
|
| 86 | - * Every element in this array should have the following properties: |
|
| 87 | - * * href - Often a mailto: address |
|
| 88 | - * * commonName - Optional, for example a first + last name |
|
| 89 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 90 | - * * readOnly - boolean |
|
| 91 | - * * summary - Optional, a description for the share |
|
| 92 | - * |
|
| 93 | - * @return array |
|
| 94 | - */ |
|
| 95 | - public function getShares() { |
|
| 96 | - if ($this->isShared()) { |
|
| 97 | - return []; |
|
| 98 | - } |
|
| 99 | - return $this->carddavBackend->getShares($this->getResourceId()); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - public function getACL() { |
|
| 103 | - $acl = [ |
|
| 104 | - [ |
|
| 105 | - 'privilege' => '{DAV:}read', |
|
| 106 | - 'principal' => $this->getOwner(), |
|
| 107 | - 'protected' => true, |
|
| 108 | - ],[ |
|
| 109 | - 'privilege' => '{DAV:}write', |
|
| 110 | - 'principal' => $this->getOwner(), |
|
| 111 | - 'protected' => true, |
|
| 112 | - ] |
|
| 113 | - ]; |
|
| 114 | - |
|
| 115 | - if ($this->getOwner() === 'principals/system/system') { |
|
| 116 | - $acl[] = [ |
|
| 117 | - 'privilege' => '{DAV:}read', |
|
| 118 | - 'principal' => '{DAV:}authenticated', |
|
| 119 | - 'protected' => true, |
|
| 120 | - ]; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if (!$this->isShared()) { |
|
| 124 | - return $acl; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if ($this->getOwner() !== parent::getOwner()) { |
|
| 128 | - $acl[] = [ |
|
| 129 | - 'privilege' => '{DAV:}read', |
|
| 130 | - 'principal' => parent::getOwner(), |
|
| 131 | - 'protected' => true, |
|
| 132 | - ]; |
|
| 133 | - if ($this->canWrite()) { |
|
| 134 | - $acl[] = [ |
|
| 135 | - 'privilege' => '{DAV:}write', |
|
| 136 | - 'principal' => parent::getOwner(), |
|
| 137 | - 'protected' => true, |
|
| 138 | - ]; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $acl = $this->carddavBackend->applyShareAcl($this->getResourceId(), $acl); |
|
| 143 | - $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/system']; |
|
| 144 | - return array_filter($acl, function($rule) use ($allowedPrincipals) { |
|
| 145 | - return \in_array($rule['principal'], $allowedPrincipals, true); |
|
| 146 | - }); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - public function getChildACL() { |
|
| 150 | - return $this->getACL(); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function getChild($name) { |
|
| 154 | - |
|
| 155 | - $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'], $name); |
|
| 156 | - if (!$obj) { |
|
| 157 | - throw new NotFound('Card not found'); |
|
| 158 | - } |
|
| 159 | - $obj['acl'] = $this->getChildACL(); |
|
| 160 | - return new Card($this->carddavBackend, $this->addressBookInfo, $obj); |
|
| 161 | - |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * @return int |
|
| 166 | - */ |
|
| 167 | - public function getResourceId() { |
|
| 168 | - return $this->addressBookInfo['id']; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - public function getOwner() { |
|
| 172 | - if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 173 | - return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal']; |
|
| 174 | - } |
|
| 175 | - return parent::getOwner(); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - public function delete() { |
|
| 179 | - if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 180 | - $principal = 'principal:' . parent::getOwner(); |
|
| 181 | - $shares = $this->carddavBackend->getShares($this->getResourceId()); |
|
| 182 | - $shares = array_filter($shares, function($share) use ($principal){ |
|
| 183 | - return $share['href'] === $principal; |
|
| 184 | - }); |
|
| 185 | - if (empty($shares)) { |
|
| 186 | - throw new Forbidden(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $this->carddavBackend->updateShares($this, [], [ |
|
| 190 | - $principal |
|
| 191 | - ]); |
|
| 192 | - return; |
|
| 193 | - } |
|
| 194 | - parent::delete(); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - public function propPatch(PropPatch $propPatch) { |
|
| 198 | - if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 199 | - throw new Forbidden(); |
|
| 200 | - } |
|
| 201 | - parent::propPatch($propPatch); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - public function getContactsGroups() { |
|
| 205 | - return $this->carddavBackend->collectCardProperties($this->getResourceId(), 'CATEGORIES'); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - private function isShared() { |
|
| 209 | - if (!isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 210 | - return false; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'] !== $this->addressBookInfo['principaluri']; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - private function canWrite() { |
|
| 217 | - if (isset($this->addressBookInfo['{http://owncloud.org/ns}read-only'])) { |
|
| 218 | - return !$this->addressBookInfo['{http://owncloud.org/ns}read-only']; |
|
| 219 | - } |
|
| 220 | - return true; |
|
| 221 | - } |
|
| 41 | + /** |
|
| 42 | + * AddressBook constructor. |
|
| 43 | + * |
|
| 44 | + * @param BackendInterface $carddavBackend |
|
| 45 | + * @param array $addressBookInfo |
|
| 46 | + * @param IL10N $l10n |
|
| 47 | + */ |
|
| 48 | + public function __construct(BackendInterface $carddavBackend, array $addressBookInfo, IL10N $l10n) { |
|
| 49 | + parent::__construct($carddavBackend, $addressBookInfo); |
|
| 50 | + |
|
| 51 | + if ($this->addressBookInfo['{DAV:}displayname'] === CardDavBackend::PERSONAL_ADDRESSBOOK_NAME && |
|
| 52 | + $this->getName() === CardDavBackend::PERSONAL_ADDRESSBOOK_URI) { |
|
| 53 | + $this->addressBookInfo['{DAV:}displayname'] = $l10n->t('Contacts'); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Updates the list of shares. |
|
| 59 | + * |
|
| 60 | + * The first array is a list of people that are to be added to the |
|
| 61 | + * addressbook. |
|
| 62 | + * |
|
| 63 | + * Every element in the add array has the following properties: |
|
| 64 | + * * href - A url. Usually a mailto: address |
|
| 65 | + * * commonName - Usually a first and last name, or false |
|
| 66 | + * * summary - A description of the share, can also be false |
|
| 67 | + * * readOnly - A boolean value |
|
| 68 | + * |
|
| 69 | + * Every element in the remove array is just the address string. |
|
| 70 | + * |
|
| 71 | + * @param array $add |
|
| 72 | + * @param array $remove |
|
| 73 | + * @return void |
|
| 74 | + * @throws Forbidden |
|
| 75 | + */ |
|
| 76 | + public function updateShares(array $add, array $remove) { |
|
| 77 | + if ($this->isShared()) { |
|
| 78 | + throw new Forbidden(); |
|
| 79 | + } |
|
| 80 | + $this->carddavBackend->updateShares($this, $add, $remove); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Returns the list of people whom this addressbook is shared with. |
|
| 85 | + * |
|
| 86 | + * Every element in this array should have the following properties: |
|
| 87 | + * * href - Often a mailto: address |
|
| 88 | + * * commonName - Optional, for example a first + last name |
|
| 89 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 90 | + * * readOnly - boolean |
|
| 91 | + * * summary - Optional, a description for the share |
|
| 92 | + * |
|
| 93 | + * @return array |
|
| 94 | + */ |
|
| 95 | + public function getShares() { |
|
| 96 | + if ($this->isShared()) { |
|
| 97 | + return []; |
|
| 98 | + } |
|
| 99 | + return $this->carddavBackend->getShares($this->getResourceId()); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + public function getACL() { |
|
| 103 | + $acl = [ |
|
| 104 | + [ |
|
| 105 | + 'privilege' => '{DAV:}read', |
|
| 106 | + 'principal' => $this->getOwner(), |
|
| 107 | + 'protected' => true, |
|
| 108 | + ],[ |
|
| 109 | + 'privilege' => '{DAV:}write', |
|
| 110 | + 'principal' => $this->getOwner(), |
|
| 111 | + 'protected' => true, |
|
| 112 | + ] |
|
| 113 | + ]; |
|
| 114 | + |
|
| 115 | + if ($this->getOwner() === 'principals/system/system') { |
|
| 116 | + $acl[] = [ |
|
| 117 | + 'privilege' => '{DAV:}read', |
|
| 118 | + 'principal' => '{DAV:}authenticated', |
|
| 119 | + 'protected' => true, |
|
| 120 | + ]; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if (!$this->isShared()) { |
|
| 124 | + return $acl; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if ($this->getOwner() !== parent::getOwner()) { |
|
| 128 | + $acl[] = [ |
|
| 129 | + 'privilege' => '{DAV:}read', |
|
| 130 | + 'principal' => parent::getOwner(), |
|
| 131 | + 'protected' => true, |
|
| 132 | + ]; |
|
| 133 | + if ($this->canWrite()) { |
|
| 134 | + $acl[] = [ |
|
| 135 | + 'privilege' => '{DAV:}write', |
|
| 136 | + 'principal' => parent::getOwner(), |
|
| 137 | + 'protected' => true, |
|
| 138 | + ]; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $acl = $this->carddavBackend->applyShareAcl($this->getResourceId(), $acl); |
|
| 143 | + $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/system']; |
|
| 144 | + return array_filter($acl, function($rule) use ($allowedPrincipals) { |
|
| 145 | + return \in_array($rule['principal'], $allowedPrincipals, true); |
|
| 146 | + }); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + public function getChildACL() { |
|
| 150 | + return $this->getACL(); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function getChild($name) { |
|
| 154 | + |
|
| 155 | + $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'], $name); |
|
| 156 | + if (!$obj) { |
|
| 157 | + throw new NotFound('Card not found'); |
|
| 158 | + } |
|
| 159 | + $obj['acl'] = $this->getChildACL(); |
|
| 160 | + return new Card($this->carddavBackend, $this->addressBookInfo, $obj); |
|
| 161 | + |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * @return int |
|
| 166 | + */ |
|
| 167 | + public function getResourceId() { |
|
| 168 | + return $this->addressBookInfo['id']; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + public function getOwner() { |
|
| 172 | + if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 173 | + return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal']; |
|
| 174 | + } |
|
| 175 | + return parent::getOwner(); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + public function delete() { |
|
| 179 | + if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 180 | + $principal = 'principal:' . parent::getOwner(); |
|
| 181 | + $shares = $this->carddavBackend->getShares($this->getResourceId()); |
|
| 182 | + $shares = array_filter($shares, function($share) use ($principal){ |
|
| 183 | + return $share['href'] === $principal; |
|
| 184 | + }); |
|
| 185 | + if (empty($shares)) { |
|
| 186 | + throw new Forbidden(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $this->carddavBackend->updateShares($this, [], [ |
|
| 190 | + $principal |
|
| 191 | + ]); |
|
| 192 | + return; |
|
| 193 | + } |
|
| 194 | + parent::delete(); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + public function propPatch(PropPatch $propPatch) { |
|
| 198 | + if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 199 | + throw new Forbidden(); |
|
| 200 | + } |
|
| 201 | + parent::propPatch($propPatch); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + public function getContactsGroups() { |
|
| 205 | + return $this->carddavBackend->collectCardProperties($this->getResourceId(), 'CATEGORIES'); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + private function isShared() { |
|
| 209 | + if (!isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) { |
|
| 210 | + return false; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'] !== $this->addressBookInfo['principaluri']; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + private function canWrite() { |
|
| 217 | + if (isset($this->addressBookInfo['{http://owncloud.org/ns}read-only'])) { |
|
| 218 | + return !$this->addressBookInfo['{http://owncloud.org/ns}read-only']; |
|
| 219 | + } |
|
| 220 | + return true; |
|
| 221 | + } |
|
| 222 | 222 | } |