|
23 | 23 |
|
24 | 24 | namespace OCA\Richdocuments; |
25 | 25 |
|
| 26 | +use OCP\Constants; |
| 27 | +use OCP\Files\Node; |
| 28 | +use OCP\IConfig; |
26 | 29 | use OCP\IGroupManager; |
27 | 30 | use OCP\IUserManager; |
28 | 31 | use OCP\IUserSession; |
| 32 | +use OCP\Share\IAttributes; |
| 33 | +use OCP\Share\IShare; |
| 34 | +use OCP\SystemTag\ISystemTagObjectMapper; |
29 | 35 |
|
30 | 36 | class PermissionManager { |
31 | 37 | /** @var AppConfig */ |
| 38 | + private $appConfig; |
| 39 | + /** @var IConfig */ |
32 | 40 | private $config; |
33 | 41 | /** @var IGroupManager */ |
34 | 42 | private $groupManager; |
35 | 43 | /** @var IUserManager */ |
36 | 44 | private $userManager; |
37 | 45 | /** @var IUserSession */ |
38 | 46 | private $userSession; |
| 47 | + /** @var ISystemTagObjectMapper */ |
| 48 | + private $systemTagObjectMapper; |
39 | 49 |
|
40 | 50 | public function __construct( |
41 | | - AppConfig $config, |
42 | | - IGroupManager $groupManager, |
43 | | - IUserManager $userManager, |
44 | | - IUserSession $userSession |
| 51 | + AppConfig $appConfig, |
| 52 | + IConfig $config, |
| 53 | + IGroupManager $groupManager, |
| 54 | + IUserManager $userManager, |
| 55 | + IUserSession $userSession, |
| 56 | + ISystemTagObjectMapper $systemTagObjectMapper |
45 | 57 | ) { |
| 58 | + $this->appConfig = $appConfig; |
46 | 59 | $this->config = $config; |
47 | 60 | $this->groupManager = $groupManager; |
48 | 61 | $this->userManager = $userManager; |
49 | 62 | $this->userSession = $userSession; |
| 63 | + $this->systemTagObjectMapper = $systemTagObjectMapper; |
50 | 64 | } |
51 | 65 |
|
52 | 66 | private function userMatchesGroupList(?string $userId = null, ?array $groupList = []): bool { |
@@ -79,26 +93,96 @@ private function userMatchesGroupList(?string $userId = null, ?array $groupList |
79 | 93 | } |
80 | 94 |
|
81 | 95 | public function isEnabledForUser(string $userId = null): bool { |
82 | | - if ($this->userMatchesGroupList($userId, $this->config->getUseGroups())) { |
| 96 | + if ($this->userMatchesGroupList($userId, $this->appConfig->getUseGroups())) { |
83 | 97 | return true; |
84 | 98 | } |
85 | 99 |
|
86 | 100 | return false; |
87 | 101 | } |
88 | 102 |
|
89 | 103 | public function userCanEdit(string $userId = null): bool { |
90 | | - if ($this->userMatchesGroupList($userId, $this->config->getEditGroups())) { |
| 104 | + if ($this->userMatchesGroupList($userId, $this->appConfig->getEditGroups())) { |
91 | 105 | return true; |
92 | 106 | } |
93 | 107 |
|
94 | 108 | return false; |
95 | 109 | } |
96 | 110 |
|
97 | 111 | public function userIsFeatureLocked(string $userId = null): bool { |
98 | | - if ($this->config->isReadOnlyFeatureLocked() && !$this->userCanEdit($userId)) { |
| 112 | + if ($this->appConfig->isReadOnlyFeatureLocked() && !$this->userCanEdit($userId)) { |
99 | 113 | return true; |
100 | 114 | } |
101 | 115 |
|
102 | 116 | return false; |
103 | 117 | } |
| 118 | + |
| 119 | + public function shouldWatermark(Node $node, ?string $userId = null, ?IShare $share = null): bool { |
| 120 | + if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_enabled', 'no') === 'no') { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + $fileId = $node->getId(); |
| 125 | + |
| 126 | + $isUpdatable = $node->isUpdateable() && (!$share || $share->getPermissions() & Constants::PERMISSION_UPDATE); |
| 127 | + |
| 128 | + $hasShareAttributes = $share && method_exists($share, 'getAttributes') && $share->getAttributes() instanceof IAttributes; |
| 129 | + $isDisabledDownload = $hasShareAttributes && $share->getAttributes()->getAttribute('permissions', 'download') === false; |
| 130 | + $isHideDownload = $share && $share->getHideDownload(); |
| 131 | + $isSecureView = $isDisabledDownload || $isHideDownload; |
| 132 | + |
| 133 | + if ($share && $share->getShareType() === IShare::TYPE_LINK) { |
| 134 | + if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_linkAll', 'no') === 'yes') { |
| 135 | + return true; |
| 136 | + } |
| 137 | + if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_linkRead', 'no') === 'yes' && !$isUpdatable) { |
| 138 | + return true; |
| 139 | + } |
| 140 | + if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_linkSecure', 'no') === 'yes' && $isSecureView) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_linkTags', 'no') === 'yes') { |
| 144 | + $tags = $this->appConfig->getAppValueArray('watermark_linkTagsList'); |
| 145 | + $fileTags = $this->systemTagObjectMapper->getTagIdsForObjects([$fileId], 'files')[$fileId]; |
| 146 | + foreach ($fileTags as $tagId) { |
| 147 | + if (in_array($tagId, $tags, true)) { |
| 148 | + return true; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_shareAll', 'no') === 'yes') { |
| 155 | + if ($node->getOwner()->getUID() !== $userId) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_shareRead', 'no') === 'yes' && !$isUpdatable) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_shareDisabledDownload', 'no') === 'yes' && $isDisabledDownload) { |
| 165 | + return true; |
| 166 | + } |
| 167 | + |
| 168 | + if ($userId !== null && $this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_allGroups', 'no') === 'yes') { |
| 169 | + $groups = $this->appConfig->getAppValueArray('watermark_allGroupsList'); |
| 170 | + foreach ($groups as $group) { |
| 171 | + if ($this->groupManager->isInGroup($userId, $group)) { |
| 172 | + return true; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_allTags', 'no') === 'yes') { |
| 177 | + $tags = $this->appConfig->getAppValueArray('watermark_allTagsList'); |
| 178 | + $fileTags = $this->systemTagObjectMapper->getTagIdsForObjects([$fileId], 'files')[$fileId]; |
| 179 | + foreach ($fileTags as $tagId) { |
| 180 | + if (in_array($tagId, $tags, true)) { |
| 181 | + return true; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return false; |
| 187 | + } |
104 | 188 | } |
0 commit comments