Dinamycally find out the maximum permissions to reshare

This commit is contained in:
David A. Velasco 2016-01-18 14:08:38 +01:00
parent 81b8918939
commit 3b4b8ceadd
6 changed files with 64 additions and 17 deletions

@ -1 +1 @@
Subproject commit 249cb901ebd392373d0ef812b8f0a2489705c0ea
Subproject commit 275c042f7884f8a56cb4b5cb94ef9be98884e698

View file

@ -228,8 +228,9 @@ public class FileOperationsHelper {
* @param file The file to share.
* @param shareeName Name (user name or group name) of the target sharee.
* @param shareType The share type determines the sharee type.
* @param permissions Permissions to grant to sharee on the shared file.
*/
public void shareFileWithSharee(OCFile file, String shareeName, ShareType shareType) {
public void shareFileWithSharee(OCFile file, String shareeName, ShareType shareType, int permissions) {
if (file != null) {
// TODO check capability?
mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
@ -241,6 +242,7 @@ public class FileOperationsHelper {
service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
service.putExtra(OperationsService.EXTRA_SHARE_WITH, shareeName);
service.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
service.putExtra(OperationsService.EXTRA_SHARE_PERMISSIONS, permissions);
mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
} else {

View file

@ -26,12 +26,16 @@ package com.owncloud.android.operations;
*/
import android.accounts.Account;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
import com.owncloud.android.lib.resources.shares.OCShare;
import com.owncloud.android.lib.resources.shares.ShareType;
import com.owncloud.android.operations.common.SyncOperation;
@ -43,6 +47,7 @@ public class CreateShareWithShareeOperation extends SyncOperation {
private String mPath;
private String mShareeName;
private ShareType mShareType;
private int mPermissions;
/**
* Constructor.
@ -51,25 +56,49 @@ public class CreateShareWithShareeOperation extends SyncOperation {
* @param shareeName User or group name of the target sharee.
* @param shareType Type of share determines type of sharee; {@link ShareType#USER} and {@link ShareType#GROUP}
* are the only valid values for the moment.
* @param permissions Share permissions key as detailed in
* https://doc.owncloud.org/server/8.2/developer_manual/core/ocs-share-api.html .
* If < 0, maximum permissions are dynamically found out and requested. This requires
* an extra interaction with the server, and should be used only for RESHARING.
*/
public CreateShareWithShareeOperation(String path, String shareeName, ShareType shareType) {
public CreateShareWithShareeOperation(String path, String shareeName, ShareType shareType, int permissions) {
if (!ShareType.USER.equals(shareType) && !ShareType.GROUP.equals(shareType)) {
throw new IllegalArgumentException("Illegal share type " + shareType);
}
mPath = path;
mShareeName = shareeName;
mShareType = shareType;
mPermissions = permissions;
}
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
// Check if the share link already exists
// TODO or not
/*
RemoteOperation operation = new GetRemoteSharesForFileOperation(mPath, false, false);
RemoteOperationResult result = operation.execute(client);
if (!result.isSuccess() || result.getData().size() <= 0) {
*/
int permissions = mPermissions;
if (permissions < 0) {
// find out maximum permissions
RemoteOperation operation = new GetRemoteSharesForFileOperation(mPath, true, false);
RemoteOperationResult result = operation.execute(client);
if (!result.isSuccess()) {
return result;
} else {
OCShare share = null;
Account account = getStorageManager().getAccount();
String userId = account.name.substring(0, account.name.lastIndexOf("@"));
// TODO OcAccount needed everywhere :(
boolean sharedWithMe = false;
for (int i=0; i<result.getData().size() && !sharedWithMe; i++) {
share = (OCShare) result.getData().get(i);
sharedWithMe = userId.equals(share.getShareWith());
}
if (share != null && sharedWithMe) {
permissions = share.getPermissions();
} else {
permissions = OCShare.DEFAULT_PERMISSION;
}
}
}
CreateRemoteShareOperation operation = new CreateRemoteShareOperation(
mPath,
@ -77,7 +106,7 @@ public class CreateShareWithShareeOperation extends SyncOperation {
mShareeName,
false,
"",
OCShare.DEFAULT_PERMISSION
permissions
);
operation.setGetShareDetails(true);
RemoteOperationResult result = operation.execute(client);

View file

@ -69,7 +69,6 @@ import com.owncloud.android.operations.UpdateShareViaLinkOperation;
import com.owncloud.android.operations.common.SyncOperation;
import java.io.IOException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -598,11 +597,13 @@ public class OperationsService extends Service {
String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
String shareeName = operationIntent.getStringExtra(EXTRA_SHARE_WITH);
ShareType shareType = (ShareType) operationIntent.getSerializableExtra(EXTRA_SHARE_TYPE);
int permissions = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, -1);
if (remotePath.length() > 0) {
operation = new CreateShareWithShareeOperation(
remotePath,
shareeName,
shareType
shareType,
permissions
);
}

View file

@ -122,10 +122,25 @@ public class ShareActivity extends FileActivity
getFileOperationsHelper().shareFileWithSharee(
getFile(),
shareeName,
(isGroup ? ShareType.GROUP : ShareType.USER)
(isGroup ? ShareType.GROUP : ShareType.USER),
getMaximumPermissions()
);
}
private int getMaximumPermissions() {
if (getFile().isSharedWithMe()) {
return -1; // maximum permissions will be requested dynamically
} else if (getFile().isFolder()) {
return OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER;
} else { // isFile
return OCShare.MAXIMUM_PERMISSIONS_FOR_FILE;
}
}
@Override
public void showSearchUsersAndGroups() {
// replace ShareFragment with SearchFragment on demand

View file

@ -130,7 +130,7 @@ public class EditShareFragment extends Fragment {
View view = inflater.inflate(R.layout.edit_share_layout, container, false);
// Setup layout
initPrivileges(view);
initPermissions(view);
initUnshareButton(view);
initDoneButton(view);
@ -143,7 +143,7 @@ public class EditShareFragment extends Fragment {
*
* @param editShareView Root view in the fragment.
*/
private void initPrivileges(View editShareView) {
private void initPermissions(View editShareView) {
boolean setListener = false;
if (mOnPrivilegeChangeListener == null) {
mOnPrivilegeChangeListener = new OnPrivilegeChangeListener();
@ -213,7 +213,7 @@ public class EditShareFragment extends Fragment {
} else {
if (getView() != null) {
initPrivileges(getView());
initPermissions(getView());
}
}
}