Skip to content

Feature - Handle Custom Permissions #15045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 165 additions & 31 deletions app/src/main/java/com/nextcloud/ui/fileactions/FileAction.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2025 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-FileCopyrightText: 2022 Álvaro Brey <alvaro@alvarobrey.com>
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
Expand All @@ -11,8 +12,13 @@ import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import com.owncloud.android.R
import com.owncloud.android.datamodel.OCFile

enum class FileAction(@IdRes val id: Int, @StringRes val title: Int, @DrawableRes val icon: Int? = null) {
enum class FileAction(
@param:IdRes val id: Int,
@param:StringRes val title: Int,
@param:DrawableRes val icon: Int? = null
) {
// selection
SELECT_ALL(R.id.action_select_all_action_menu, R.string.select_all, R.drawable.ic_select_all),
SELECT_NONE(R.id.action_deselect_all_action_menu, R.string.deselect_all, R.drawable.ic_select_none),
Expand All @@ -21,6 +27,7 @@ enum class FileAction(@IdRes val id: Int, @StringRes val title: Int, @DrawableRe
EDIT(R.id.action_edit, R.string.action_edit, R.drawable.ic_edit),
SEE_DETAILS(R.id.action_see_details, R.string.actionbar_see_details, R.drawable.ic_information_outline),
REMOVE_FILE(R.id.action_remove_file, R.string.common_remove, R.drawable.ic_delete),
LEAVE_SHARE(R.id.action_remove_file, R.string.common_leave_this_share, R.drawable.ic_cancel),

// File moving
RENAME_FILE(R.id.action_rename_file, R.string.common_rename, R.drawable.ic_rename),
Expand Down Expand Up @@ -58,35 +65,162 @@ enum class FileAction(@IdRes val id: Int, @StringRes val title: Int, @DrawableRe
RETRY(R.id.action_retry, R.string.retry, R.drawable.ic_retry);

companion object {
/**
* All file actions, in the order they should be displayed
*/
@JvmField
val SORTED_VALUES = listOf(
UNLOCK_FILE,
EDIT,
FAVORITE,
UNSET_FAVORITE,
SEE_DETAILS,
LOCK_FILE,
RENAME_FILE,
MOVE_OR_COPY,
DOWNLOAD_FILE,
EXPORT_FILE,
STREAM_MEDIA,
SEND_SHARE_FILE,
SEND_FILE,
OPEN_FILE_WITH,
SYNC_FILE,
CANCEL_SYNC,
SELECT_ALL,
SELECT_NONE,
SET_ENCRYPTED,
UNSET_ENCRYPTED,
SET_AS_WALLPAPER,
REMOVE_FILE,
PIN_TO_HOMESCREEN,
RETRY
)
fun getActions(files: Collection<OCFile>): List<FileAction> {
return mutableListOf(
UNLOCK_FILE,
EDIT,
FAVORITE,
UNSET_FAVORITE,
SEE_DETAILS,
LOCK_FILE,
RENAME_FILE,
MOVE_OR_COPY,
DOWNLOAD_FILE,
EXPORT_FILE,
STREAM_MEDIA,
SEND_SHARE_FILE,
SEND_FILE,
OPEN_FILE_WITH,
SYNC_FILE,
CANCEL_SYNC,
SELECT_ALL,
SELECT_NONE,
SET_ENCRYPTED,
UNSET_ENCRYPTED,
SET_AS_WALLPAPER,
PIN_TO_HOMESCREEN,
RETRY
).apply {
val deleteOrLeaveShareAction = getDeleteOrLeaveShareAction(files) ?: return@apply
add(deleteOrLeaveShareAction)
}
}

fun getFilePreviewActions(file: OCFile?): List<Int> {
val result = mutableSetOf(
R.id.action_rename_file,
R.id.action_sync_file,
R.id.action_move_or_copy,
R.id.action_favorite,
R.id.action_unset_favorite,
R.id.action_pin_to_homescreen
)

if (file != null) {
val actionsToHide = getActionsToHide(setOf(file))
result.removeAll(actionsToHide)
}

return result.toList()
}

fun getFileDetailActions(file: OCFile?): List<Int> {
val result = mutableSetOf(
R.id.action_lock_file,
R.id.action_unlock_file,
R.id.action_edit,
R.id.action_favorite,
R.id.action_unset_favorite,
R.id.action_see_details,
R.id.action_move_or_copy,
R.id.action_stream_media,
R.id.action_send_share_file,
R.id.action_pin_to_homescreen
)

if (file?.isFolder == true) {
result.add(R.id.action_send_file)
result.add(R.id.action_sync_file)
}

if (file?.isAPKorAAB == true) {
result.add(R.id.action_download_file)
result.add(R.id.action_export_file)
}

if (file != null) {
val actionsToHide = getActionsToHide(setOf(file))
result.removeAll(actionsToHide)
}

return result.toList()
}

fun getFileListActionsToHide(checkedFiles: Set<OCFile>): List<Int> {
val result = mutableSetOf<Int>()

if (checkedFiles.any { it.isOfflineOperation }) {
result.addAll(
listOf(
R.id.action_favorite,
R.id.action_move_or_copy,
R.id.action_sync_file,
R.id.action_encrypted,
R.id.action_unset_encrypted,
R.id.action_edit,
R.id.action_download_file,
R.id.action_export_file,
R.id.action_set_as_wallpaper
)
)
}

if (checkedFiles.any { it.isAPKorAAB }) {
result.addAll(
listOf(
R.id.action_send_share_file,
R.id.action_export_file,
R.id.action_sync_file,
R.id.action_download_file
)
)
}

val actionsToHide = getActionsToHide(checkedFiles)
result.addAll(actionsToHide)

return result.toList()
}

fun getActionsToHide(files: Set<OCFile>): List<Int> {
if (files.isEmpty()) return emptyList()

val result = mutableListOf<Int>()

/*
if (files.any { it?.isSharedWithMe == false } || files.any { it?.canReshare() == true }) {
result.add(R.id.action_send_share_file)
}
*/
if (files.any { !it.canReshare() }) {
result.add(R.id.action_send_share_file)
}

if (files.any { !it.canRename() }) {
result.add(R.id.action_rename_file)
}

if (files.any { !it.canMove() }) {
result.add(R.id.action_move_or_copy)
}

if (files.any { !it.canWrite() }) {
result.add(R.id.action_edit)
}

return result
}

private fun getDeleteOrLeaveShareAction(files: Collection<OCFile>): FileAction? {
if (files.any { !it.canDeleteOrLeaveShare() }) {
return null
}

return if (files.any { it.isSharedWithMe }) {
LEAVE_SHARE
} else {
REMOVE_FILE
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class FileActionsViewModel @Inject constructor(
) {
viewModelScope.launch(Dispatchers.IO) {
val toHide = getHiddenActions(componentsGetter, numberOfAllFiles, files, isOverflow, inSingleFileFragment)
val availableActions = getActionsToShow(additionalFilter, toHide)
val availableActions = getActionsToShow(additionalFilter, toHide, files)
updateStateLoaded(files, availableActions)
}
}
Expand All @@ -98,9 +98,10 @@ class FileActionsViewModel @Inject constructor(
)
.getToHide(inSingleFileFragment)

private fun getActionsToShow(additionalFilter: IntArray?, toHide: List<Int>) = FileAction.SORTED_VALUES
.filter { additionalFilter == null || it.id !in additionalFilter }
.filter { it.id !in toHide }
private fun getActionsToShow(additionalFilter: IntArray?, toHide: List<Int>, files: Collection<OCFile>) =
FileAction.getActions(files)
.filter { additionalFilter == null || it.id !in additionalFilter }
.filter { it.id !in toHide }

private fun updateStateLoaded(files: Collection<OCFile>, availableActions: List<FileAction>) {
val state: UiState = when (files.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ fun List<OCFile>.filterByMimeType(mimeType: String): List<OCFile> =

fun List<OCFile>.limitToPersonalFiles(userId: String): List<OCFile> = filter { file ->
file.ownerId?.let { ownerId ->
ownerId == userId && !file.isSharedWithMe && !file.isGroupFolder
ownerId == userId && !file.isSharedWithMe && !file.mounted()
} == true
}
74 changes: 60 additions & 14 deletions app/src/main/java/com/owncloud/android/datamodel/OCFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,24 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.FileProvider;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import third_parties.daveKoeller.AlphanumComparator;

public class OCFile implements Parcelable, Comparable<OCFile>, ServerFileInterface {

private final static String PERMISSION_SHARED_WITH_ME = "S";
@VisibleForTesting
public final static String PERMISSION_CAN_RESHARE = "R";
private final static String PERMISSION_CAN_WRITE = "CK";
private final static String PERMISSION_GROUPFOLDER = "M";
private final static String PERMISSION_SHARED = "S";
private final static String PERMISSION_CAN_CREATE_FILE_AND_FOLDER = "CK";
private final static String PERMISSION_MOUNTED = "M";
private final static String PERMISSION_CAN_CREATE_FILE_INSIDE_FOLDER = "C";
private final static String PERMISSION_CAN_CREATE_FOLDER_INSIDE_FOLDER = "K";
private final static String PERMISSION_CAN_READ = "G";
private final static String PERMISSION_CAN_WRITE = "W";
private final static String PERMISSION_CAN_DELETE_OR_LEAVE_SHARE = "D";
private final static String PERMISSION_CAN_RENAME = "N";
private final static String PERMISSION_CAN_MOVE = "V";

private final static int MAX_FILE_SIZE_FOR_IMMEDIATE_PREVIEW_BYTES = 1024000;

public static final String PATH_SEPARATOR = "/";
Expand Down Expand Up @@ -629,27 +635,67 @@ public boolean isInConflict() {
}

public boolean isSharedWithMe() {
String permissions = getPermissions();
return permissions != null && permissions.contains(PERMISSION_SHARED_WITH_ME);
return hasPermission(PERMISSION_SHARED);
}

public boolean canReshare() {
String permissions = getPermissions();
return permissions != null && permissions.contains(PERMISSION_CAN_RESHARE);
return hasPermission(PERMISSION_CAN_RESHARE);
}

public boolean canCreateFileAndFolder() {
return hasPermission(PERMISSION_CAN_CREATE_FILE_AND_FOLDER);
}

public boolean mounted() {
return hasPermission(PERMISSION_MOUNTED);
}

public boolean canRead() {
return hasPermission(PERMISSION_CAN_READ);
}

public boolean canCreateFileInsideFolder() {
return hasPermission(PERMISSION_CAN_CREATE_FILE_INSIDE_FOLDER);
}

public boolean canCreateFolderInsideFolder() {
return hasPermission(PERMISSION_CAN_CREATE_FOLDER_INSIDE_FOLDER);
}

/**
* Determines whether the current account has the ability to delete the file or leave the share.
*
* <p>
* - If the file is shared with the current account (i.e., the user is the recipient),
* the user cannot delete the file itself but can leave the shared file.
* <p>
* - If the file is belongs to the current user. User can delete the file.
*
* @return true if the user is allowed to either delete or leave the share; false otherwise.
*/
public boolean canDeleteOrLeaveShare() {
return !encrypted && hasPermission(PERMISSION_CAN_DELETE_OR_LEAVE_SHARE);
}

public boolean canRename() {
return hasPermission(PERMISSION_CAN_RENAME);
}

public boolean canWrite() {
String permissions = getPermissions();
return permissions != null && permissions.contains(PERMISSION_CAN_WRITE);
return hasPermission(PERMISSION_CAN_WRITE);
}

public boolean canMove() {
return hasPermission(PERMISSION_CAN_MOVE);
}

public boolean isGroupFolder() {
private boolean hasPermission(String permission) {
String permissions = getPermissions();
return permissions != null && permissions.contains(PERMISSION_GROUPFOLDER);
return permissions != null && permissions.contains(permission);
}

public Integer getFileOverlayIconId(boolean isAutoUploadFolder) {
if (WebdavEntry.MountType.GROUP == mountType || isGroupFolder()) {
if (WebdavEntry.MountType.GROUP == mountType || mounted()) {
return R.drawable.ic_folder_overlay_account_group;
} else if (sharedViaLink && !encrypted) {
return R.drawable.ic_folder_overlay_link;
Expand Down
Loading
Loading