Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SharePermissionsEditor.vue « components « src « files_sharing « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c0a2378af84070f9b26853381f673143d2c4338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<!--
  - @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me>
  -
  - @author Louis Chmn <louis@chmn.me>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program. If not, see <http://www.gnu.org/licenses/>.
  -
  -->

<template>
	<span>
		<!-- file -->
		<ActionCheckbox v-if="!isFolder"
			:checked="shareHasPermissions(atomicPermissions.UPDATE)"
			:disabled="saving"
			@update:checked="toggleSharePermissions(atomicPermissions.UPDATE)">
			{{ t('files_sharing', 'Allow editing') }}
		</ActionCheckbox>

		<!-- folder -->
		<template v-if="isFolder && fileHasCreatePermission && config.isPublicUploadEnabled">
			<template v-if="!showCustomPermissionsForm">
				<ActionRadio :checked="sharePermissionEqual(bundledPermissions.READ_ONLY)"
					:value="bundledPermissions.READ_ONLY"
					:name="randomFormName"
					:disabled="saving"
					@change="setSharePermissions(bundledPermissions.READ_ONLY)">
					{{ t('files_sharing', 'Read only') }}
				</ActionRadio>

				<ActionRadio :checked="sharePermissionEqual(bundledPermissions.UPLOAD_AND_UPDATE)"
					:value="bundledPermissions.UPLOAD_AND_UPDATE"
					:disabled="saving"
					:name="randomFormName"
					@change="setSharePermissions(bundledPermissions.UPLOAD_AND_UPDATE)">
					{{ t('files_sharing', 'Allow upload and editing') }}
				</ActionRadio>
				<ActionRadio :checked="sharePermissionEqual(bundledPermissions.FILE_DROP)"
					:value="bundledPermissions.FILE_DROP"
					:disabled="saving"
					:name="randomFormName"
					class="sharing-entry__action--public-upload"
					@change="setSharePermissions(bundledPermissions.FILE_DROP)">
					{{ t('files_sharing', 'File drop (upload only)') }}
				</ActionRadio>

				<!-- custom permissions button -->
				<ActionButton :title="t('files_sharing', 'Custom permissions')"
					@click="showCustomPermissionsForm = true">
					<template #icon>
						<Tune />
					</template>
					{{ sharePermissionsIsBundle ? "" : sharePermissionsSummary }}
				</ActionButton>
			</template>

			<!-- custom permissions -->
			<span v-else :class="{error: !sharePermissionsSetIsValid}">
				<ActionCheckbox :checked="shareHasPermissions(atomicPermissions.READ)"
					:disabled="saving || !canToggleSharePermissions(atomicPermissions.READ)"
					@update:checked="toggleSharePermissions(atomicPermissions.READ)">
					{{ t('files_sharing', 'Read') }}
				</ActionCheckbox>
				<ActionCheckbox :checked="shareHasPermissions(atomicPermissions.CREATE)"
					:disabled="saving || !canToggleSharePermissions(atomicPermissions.CREATE)"
					@update:checked="toggleSharePermissions(atomicPermissions.CREATE)">
					{{ t('files_sharing', 'Upload') }}
				</ActionCheckbox>
				<ActionCheckbox :checked="shareHasPermissions(atomicPermissions.UPDATE)"
					:disabled="saving || !canToggleSharePermissions(atomicPermissions.UPDATE)"
					@update:checked="toggleSharePermissions(atomicPermissions.UPDATE)">
					{{ t('files_sharing', 'Edit') }}
				</ActionCheckbox>
				<ActionCheckbox :checked="shareHasPermissions(atomicPermissions.DELETE)"
					:disabled="saving || !canToggleSharePermissions(atomicPermissions.DELETE)"
					@update:checked="toggleSharePermissions(atomicPermissions.DELETE)">
					{{ t('files_sharing', 'Delete') }}
				</ActionCheckbox>

				<ActionButton @click="showCustomPermissionsForm = false">
					<template #icon>
						<ChevronLeft />
					</template>
					{{ t('files_sharing', 'Bundled permissions') }}
				</ActionButton>
			</span>
		</template>
	</span>
</template>

<script>
import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
import ActionRadio from '@nextcloud/vue/dist/Components/ActionRadio'
import ActionCheckbox from '@nextcloud/vue/dist/Components/ActionCheckbox'

import SharesMixin from '../mixins/SharesMixin'
import {
	ATOMIC_PERMISSIONS,
	BUNDLED_PERMISSIONS,
	hasPermissions,
	permissionsSetIsValid,
	togglePermissions,
	canTogglePermissions,
} from '../lib/SharePermissionsToolBox'

import Tune from 'vue-material-design-icons/Tune'
import ChevronLeft from 'vue-material-design-icons/ChevronLeft'

export default {
	name: 'SharePermissionsEditor',

	components: {
		ActionButton,
		ActionCheckbox,
		ActionRadio,
		Tune,
		ChevronLeft,
	},

	mixins: [SharesMixin],

	data() {
		return {
			randomFormName: Math.random().toString(27).substring(2),

			showCustomPermissionsForm: false,

			atomicPermissions: ATOMIC_PERMISSIONS,
			bundledPermissions: BUNDLED_PERMISSIONS,
		}
	},

	computed: {
		/**
		 * Return the summary of custom checked permissions.
		 *
		 * @return {string}
		 */
		sharePermissionsSummary() {
			return Object.values(this.atomicPermissions)
				.filter(permission => this.shareHasPermissions(permission))
				.map(permission => {
					switch (permission) {
					case this.atomicPermissions.CREATE:
						return this.t('files_sharing', 'Upload')
					case this.atomicPermissions.READ:
						return this.t('files_sharing', 'Read')
					case this.atomicPermissions.UPDATE:
						return this.t('files_sharing', 'Edit')
					case this.atomicPermissions.DELETE:
						return this.t('files_sharing', 'Delete')
					default:
						return ''
					}
				})
				.join(', ')
		},

		/**
		 * Return whether the share's permission is a bundle.
		 *
		 * @return {boolean}
		 */
		sharePermissionsIsBundle() {
			return Object.values(BUNDLED_PERMISSIONS)
				.map(bundle => this.sharePermissionEqual(bundle))
				.filter(isBundle => isBundle)
				.length > 0
		},

		/**
		 * Return whether the share's permission is valid.
		 *
		 * @return {boolean}
		 */
		sharePermissionsSetIsValid() {
			return permissionsSetIsValid(this.share.permissions)
		},

		/**
		 * Is the current share a folder ?
		 * TODO: move to a proper FileInfo model?
		 *
		 * @return {boolean}
		 */
		isFolder() {
			return this.fileInfo.type === 'dir'
		},

		/**
		 * Does the current file/folder have create permissions.
		 * TODO: move to a proper FileInfo model?
		 *
		 * @return {boolean}
		 */
		fileHasCreatePermission() {
			return !!(this.fileInfo.permissions & ATOMIC_PERMISSIONS.CREATE)
		},
	},

	mounted() {
		// Show the Custom Permissions view on open if the permissions set is not a bundle.
		this.showCustomPermissionsForm = !this.sharePermissionsIsBundle
	},

	methods: {
		/**
		 * Return whether the share has the exact given permissions.
		 *
		 * @param {number} permissions - the permissions to check.
		 *
		 * @return {boolean}
		 */
		sharePermissionEqual(permissions) {
			// We use the share's permission without PERMISSION_SHARE as it is not relevant here.
			return (this.share.permissions & ~ATOMIC_PERMISSIONS.SHARE) === permissions
		},

		/**
		 * Return whether the share has the given permissions.
		 *
		 * @param {number} permissions - the permissions to check.
		 *
		 * @return {boolean}
		 */
		shareHasPermissions(permissions) {
			return hasPermissions(this.share.permissions, permissions)
		},

		/**
		 * Set the share permissions to the given permissions.
		 *
		 * @param {number} permissions - the permissions to set.
		 *
		 * @return {void}
		 */
		setSharePermissions(permissions) {
			this.share.permissions = permissions
			this.queueUpdate('permissions')
		},

		/**
		 * Return whether some given permissions can be toggled.
		 *
		 * @param {number} permissionsToToggle - the permissions to toggle.
		 *
		 * @return {boolean}
		 */
		canToggleSharePermissions(permissionsToToggle) {
			return canTogglePermissions(this.share.permissions, permissionsToToggle)
		},

		/**
		 * Toggle a given permission.
		 *
		 * @param {number} permissions - the permissions to toggle.
		 *
		 * @return {void}
		 */
		toggleSharePermissions(permissions) {
			this.share.permissions = togglePermissions(this.share.permissions, permissions)

			if (!permissionsSetIsValid(this.share.permissions)) {
				return
			}

			this.queueUpdate('permissions')
		},
	},
}
</script>
<style lang="scss" scoped>
.error {
	::v-deep .action-checkbox__label:before {
		border: 1px solid var(--color-error);
	}
}
</style>