1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Use user id in LDAP requests

This commit is contained in:
davigonz 2018-11-07 14:31:03 +01:00
parent 7a50007ba3
commit 2c5ceeb555
15 changed files with 67 additions and 36 deletions

View File

@ -29,6 +29,7 @@ import android.accounts.AccountManager;
import android.accounts.AccountsException; import android.accounts.AccountsException;
import android.net.Uri; import android.net.Uri;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.authentication.OwnCloudCredentials; import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory;
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials;
@ -53,8 +54,8 @@ import static com.owncloud.android.lib.common.http.HttpConstants.OC_X_REQUEST_ID
public class OwnCloudClient extends HttpClient { public class OwnCloudClient extends HttpClient {
public static final String NEW_WEBDAV_FILES_PATH_4_0 = "/remote.php/dav/files/"; public static final String WEBDAV_FILES_PATH_4_0 = "/remote.php/dav/files/";
public static final String NEW_WEBDAV_UPLOADS_PATH_4_0 = "/remote.php/dav/uploads/"; public static final String WEBDAV_UPLOADS_PATH_4_0 = "/remote.php/dav/uploads/";
public static final String STATUS_PATH = "/status.php"; public static final String STATUS_PATH = "/status.php";
public static final String FILES_WEB_PATH = "/index.php/apps/files"; public static final String FILES_WEB_PATH = "/index.php/apps/files";
@ -80,7 +81,6 @@ public class OwnCloudClient extends HttpClient {
private String mRedirectedLocation; private String mRedirectedLocation;
private boolean mFollowRedirects; private boolean mFollowRedirects;
public OwnCloudClient(Uri baseUri) { public OwnCloudClient(Uri baseUri) {
if (baseUri == null) { if (baseUri == null) {
throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL"); throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
@ -208,7 +208,7 @@ public class OwnCloudClient extends HttpClient {
: method.getRequestHeader("destination"); : method.getRequestHeader("destination");
if (destination != null) { if (destination != null) {
final int suffixIndex = location.lastIndexOf(getNewFilesWebDavUri().toString()); final int suffixIndex = location.lastIndexOf(getUserFilesWebDavUri().toString());
final String redirectionBase = location.substring(0, suffixIndex); final String redirectionBase = location.substring(0, suffixIndex);
final String destinationPath = destination.substring(mBaseUri.toString().length()); final String destinationPath = destination.substring(mBaseUri.toString().length());
@ -252,16 +252,26 @@ public class OwnCloudClient extends HttpClient {
} }
} }
public Uri getNewFilesWebDavUri() { public Uri getBaseFilesWebDavUri(){
return mCredentials instanceof OwnCloudAnonymousCredentials return Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0);
? Uri.parse(mBaseUri + NEW_WEBDAV_FILES_PATH_4_0)
: Uri.parse(mBaseUri + NEW_WEBDAV_FILES_PATH_4_0 + mCredentials.getUsername());
} }
public Uri getNewUploadsWebDavUri() { public Uri getUserFilesWebDavUri() {
return mCredentials instanceof OwnCloudAnonymousCredentials return mCredentials instanceof OwnCloudAnonymousCredentials
? Uri.parse(mBaseUri + NEW_WEBDAV_UPLOADS_PATH_4_0) ? Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0)
: Uri.parse(mBaseUri + NEW_WEBDAV_UPLOADS_PATH_4_0 + mCredentials.getUsername()); : Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0 + AccountUtils.getUserId(
mAccount.getSavedAccount(), getContext()
)
);
}
public Uri getUploadsWebDavUri() {
return mCredentials instanceof OwnCloudAnonymousCredentials
? Uri.parse(mBaseUri + WEBDAV_UPLOADS_PATH_4_0)
: Uri.parse(mBaseUri + WEBDAV_UPLOADS_PATH_4_0 + AccountUtils.getUserId(
mAccount.getSavedAccount(), getContext()
)
);
} }
/** /**

View File

@ -64,7 +64,7 @@ public class AccountUtils {
try { try {
OwnCloudCredentials ownCloudCredentials = getCredentialsForAccount(context, account); OwnCloudCredentials ownCloudCredentials = getCredentialsForAccount(context, account);
webDavUrlForAccount = getBaseUrlForAccount(context, account) + OwnCloudClient.NEW_WEBDAV_FILES_PATH_4_0 webDavUrlForAccount = getBaseUrlForAccount(context, account) + OwnCloudClient.WEBDAV_FILES_PATH_4_0
+ ownCloudCredentials.getUsername(); + ownCloudCredentials.getUsername();
} catch (OperationCanceledException e) { } catch (OperationCanceledException e) {
e.printStackTrace(); e.printStackTrace();
@ -188,6 +188,16 @@ public class AccountUtils {
return credentials; return credentials;
} }
/**
* Get the user id corresponding OC account.
* @param account ownCloud account
* @return user id
*/
public static String getUserId(Account account, Context context) {
AccountManager accountMgr = AccountManager.get(context);
return accountMgr.getUserData(account, Constants.KEY_ID);
}
public static String buildAccountNameOld(Uri serverBaseUrl, String username) { public static String buildAccountNameOld(Uri serverBaseUrl, String username) {
if (serverBaseUrl.getScheme() == null) { if (serverBaseUrl.getScheme() == null) {
serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString()); serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString());
@ -244,7 +254,7 @@ public class AccountUtils {
// Account Manager // Account Manager
AccountManager am = AccountManager.get(context.getApplicationContext()); AccountManager am = AccountManager.get(context.getApplicationContext());
Uri serverUri = (client.getBaseUri() != null) ? client.getBaseUri() : client.getNewFilesWebDavUri(); Uri serverUri = (client.getBaseUri() != null) ? client.getBaseUri() : client.getUserFilesWebDavUri();
String cookiesString = am.getUserData(account, Constants.KEY_COOKIES); String cookiesString = am.getUserData(account, Constants.KEY_COOKIES);
if (cookiesString != null) { if (cookiesString != null) {
@ -318,6 +328,11 @@ public class AccountUtils {
*/ */
public static final String KEY_OC_ACCOUNT_VERSION = "oc_account_version"; public static final String KEY_OC_ACCOUNT_VERSION = "oc_account_version";
/**
* User's id
*/
public static final String KEY_ID = "oc_id";
/** /**
* User's display name * User's display name
*/ */

View File

@ -103,8 +103,8 @@ public class CopyRemoteFileOperation extends RemoteOperation {
/// perform remote operation /// perform remote operation
RemoteOperationResult result = null; RemoteOperationResult result = null;
try { try {
CopyMethod copyMethod = new CopyMethod(new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mSrcRemotePath)), CopyMethod copyMethod = new CopyMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mSrcRemotePath)),
client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath),
mOverwrite); mOverwrite);
copyMethod.setReadTimeout(COPY_READ_TIMEOUT, TimeUnit.SECONDS); copyMethod.setReadTimeout(COPY_READ_TIMEOUT, TimeUnit.SECONDS);

View File

@ -100,7 +100,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation {
private RemoteOperationResult createFolder(OwnCloudClient client) { private RemoteOperationResult createFolder(OwnCloudClient client) {
RemoteOperationResult result; RemoteOperationResult result;
try { try {
Uri webDavUri = createChunksFolder ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); Uri webDavUri = createChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri();
final MkColMethod mkcol = new MkColMethod(new URL(webDavUri + WebdavUtils.encodePath(mRemotePath))); final MkColMethod mkcol = new MkColMethod(new URL(webDavUri + WebdavUtils.encodePath(mRemotePath)));
mkcol.setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS); mkcol.setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS);
mkcol.setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS); mkcol.setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS);

View File

@ -101,7 +101,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
RemoteOperationResult result; RemoteOperationResult result;
int status; int status;
boolean savedFile = false; boolean savedFile = false;
mGet = new GetMethod(new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); mGet = new GetMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)));
Iterator<OnDatatransferProgressListener> it; Iterator<OnDatatransferProgressListener> it;
FileOutputStream fos = null; FileOutputStream fos = null;

View File

@ -56,6 +56,7 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
private String mPath; private String mPath;
private boolean mSuccessIfAbsent; private boolean mSuccessIfAbsent;
private boolean mIsLogin;
/** /**
* Sequence of redirections followed. Available only after executing the operation * Sequence of redirections followed. Available only after executing the operation
@ -68,10 +69,13 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
* @param remotePath Path to append to the URL owned by the client instance. * @param remotePath Path to append to the URL owned by the client instance.
* @param successIfAbsent When 'true', the operation finishes in success if the path does * @param successIfAbsent When 'true', the operation finishes in success if the path does
* NOT exist in the remote server (HTTP 404). * NOT exist in the remote server (HTTP 404).
* @param isLogin When `true`, the username won't be added at the end of the PROPFIND url since is not
* needed to check user credentials
*/ */
public ExistenceCheckRemoteOperation(String remotePath, boolean successIfAbsent) { public ExistenceCheckRemoteOperation(String remotePath, boolean successIfAbsent, boolean isLogin) {
mPath = (remotePath != null) ? remotePath : ""; mPath = (remotePath != null) ? remotePath : "";
mSuccessIfAbsent = successIfAbsent; mSuccessIfAbsent = successIfAbsent;
mIsLogin = isLogin;
} }
@Override @Override
@ -80,8 +84,11 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
boolean previousFollowRedirects = client.followRedirects(); boolean previousFollowRedirects = client.followRedirects();
try { try {
String stringUrl = mIsLogin ?
client.getBaseFilesWebDavUri().toString() :
client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mPath);
PropfindMethod propfindMethod = new PropfindMethod( PropfindMethod propfindMethod = new PropfindMethod(
new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mPath)), new URL(stringUrl),
0, 0,
DavUtils.getAllPropset() DavUtils.getAllPropset()
); );
@ -102,9 +109,8 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
* 207 MULTI_STATUS: path exists. * 207 MULTI_STATUS: path exists.
*/ */
Log_OC.d(TAG, "Existence check for " + client.getNewFilesWebDavUri() + Log_OC.d(TAG, "Existence check for " + stringUrl + WebdavUtils.encodePath(mPath) +
WebdavUtils.encodePath(mPath) + " targeting for " + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") +
(mSuccessIfAbsent ? " absence " : " existence ") +
"finished with HTTP status " + status + (!isSuccess(status) ? "(FAIL)" : "")); "finished with HTTP status " + status + (!isSuccess(status) ? "(FAIL)" : ""));
return isSuccess(status) return isSuccess(status)
@ -113,7 +119,7 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
} catch (Exception e) { } catch (Exception e) {
final RemoteOperationResult result = new RemoteOperationResult<>(e); final RemoteOperationResult result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Existence check for " + client.getNewFilesWebDavUri() + Log_OC.e(TAG, "Existence check for " + client.getUserFilesWebDavUri() +
WebdavUtils.encodePath(mPath) + " targeting for " + WebdavUtils.encodePath(mPath) + " targeting for " +
(mSuccessIfAbsent ? " absence " : " existence ") + ": " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " +
result.getLogMessage(), result.getException()); result.getLogMessage(), result.getException());

View File

@ -111,11 +111,11 @@ public class MoveRemoteFileOperation extends RemoteOperation {
try { try {
// After finishing a chunked upload, we have to move the resulting file from uploads folder to files one, // After finishing a chunked upload, we have to move the resulting file from uploads folder to files one,
// so this uri has to be customizable // so this uri has to be customizable
Uri srcWebDavUri = moveChunkedFile ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); Uri srcWebDavUri = moveChunkedFile ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri();
final MoveMethod move = new MoveMethod( final MoveMethod move = new MoveMethod(
new URL(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)), new URL(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)),
client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath),
mOverwrite); mOverwrite);
if (moveChunkedFile) { if (moveChunkedFile) {

View File

@ -75,7 +75,7 @@ public class ReadRemoteFileOperation extends RemoteOperation<RemoteFile> {
/// take the duty of check the server for the current state of the file there /// take the duty of check the server for the current state of the file there
try { try {
// remote request // remote request
propfind = new PropfindMethod(new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), propfind = new PropfindMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)),
DEPTH_0, DEPTH_0,
DavUtils.getAllPropset()); DavUtils.getAllPropset());

View File

@ -75,7 +75,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation<ArrayList<RemoteF
try { try {
PropfindMethod propfindMethod = new PropfindMethod( PropfindMethod propfindMethod = new PropfindMethod(
new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)),
DavConstants.DEPTH_1, DavConstants.DEPTH_1,
DavUtils.getAllPropset()); DavUtils.getAllPropset());

View File

@ -47,7 +47,7 @@ import at.bitfire.dav4android.property.owncloud.OCPrivatelink;
import at.bitfire.dav4android.property.owncloud.OCSize; import at.bitfire.dav4android.property.owncloud.OCSize;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import static com.owncloud.android.lib.common.OwnCloudClient.NEW_WEBDAV_FILES_PATH_4_0; import static com.owncloud.android.lib.common.OwnCloudClient.WEBDAV_FILES_PATH_4_0;
/** /**
* Contains the data of a Remote File from a WebDavEntry * Contains the data of a Remote File from a WebDavEntry
@ -228,7 +228,7 @@ public class RemoteFile implements Parcelable, Serializable {
private static String getRemotePathFromUrl(HttpUrl url, String displayName) { private static String getRemotePathFromUrl(HttpUrl url, String displayName) {
final String davPath = NEW_WEBDAV_FILES_PATH_4_0 + displayName; final String davPath = WEBDAV_FILES_PATH_4_0 + displayName;
final String pathToOc = url.encodedPath().split(davPath)[0]; final String pathToOc = url.encodedPath().split(davPath)[0];
return Uri.decode(url.encodedPath()).replace(pathToOc + davPath, ""); return Uri.decode(url.encodedPath()).replace(pathToOc + davPath, "");
} }

View File

@ -70,7 +70,7 @@ public class RemoveRemoteFileOperation extends RemoteOperation {
RemoteOperationResult result; RemoteOperationResult result;
try { try {
Uri srcWebDavUri = removeChunksFolder ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); Uri srcWebDavUri = removeChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri();
DeleteMethod deleteMethod = new DeleteMethod( DeleteMethod deleteMethod = new DeleteMethod(
new URL(srcWebDavUri + WebdavUtils.encodePath(mRemotePath))); new URL(srcWebDavUri + WebdavUtils.encodePath(mRemotePath)));

View File

@ -104,9 +104,9 @@ public class RenameRemoteFileOperation extends RemoteOperation {
return new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE); return new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE);
} }
final MoveMethod move = new MoveMethod(new URL(client.getNewFilesWebDavUri() + final MoveMethod move = new MoveMethod(new URL(client.getUserFilesWebDavUri() +
WebdavUtils.encodePath(mOldRemotePath)), WebdavUtils.encodePath(mOldRemotePath)),
client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false); client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false);
move.setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.SECONDS); move.setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.SECONDS);
move.setConnectionTimeout(RENAME_READ_TIMEOUT, TimeUnit.SECONDS); move.setConnectionTimeout(RENAME_READ_TIMEOUT, TimeUnit.SECONDS);
@ -138,7 +138,7 @@ public class RenameRemoteFileOperation extends RemoteOperation {
*/ */
private boolean targetPathIsUsed(OwnCloudClient client) { private boolean targetPathIsUsed(OwnCloudClient client) {
ExistenceCheckRemoteOperation existenceCheckRemoteOperation = ExistenceCheckRemoteOperation existenceCheckRemoteOperation =
new ExistenceCheckRemoteOperation(mNewRemotePath, false); new ExistenceCheckRemoteOperation(mNewRemotePath, false, false);
RemoteOperationResult exists = existenceCheckRemoteOperation.run(client); RemoteOperationResult exists = existenceCheckRemoteOperation.run(client);
return exists.isSuccess(); return exists.isSuccess();
} }

View File

@ -89,7 +89,7 @@ public class UploadRemoteFileOperation extends RemoteOperation {
try { try {
mPutMethod = new PutMethod( mPutMethod = new PutMethod(
new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)));
mPutMethod.setRetryOnConnectionFailure(false); mPutMethod.setRetryOnConnectionFailure(false);

View File

@ -84,7 +84,7 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
} }
long offset = 0; long offset = 0;
String uriPrefix = client.getNewUploadsWebDavUri() + FileUtils.PATH_SEPARATOR + String.valueOf(mTransferId); String uriPrefix = client.getUploadsWebDavUri() + FileUtils.PATH_SEPARATOR + String.valueOf(mTransferId);
long totalLength = fileToUpload.length(); long totalLength = fileToUpload.length();
long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE);

View File

@ -89,7 +89,7 @@ public class GetRemoteUserQuotaOperation extends RemoteOperation<GetRemoteUserQu
try { try {
PropfindMethod propfindMethod = new PropfindMethod( PropfindMethod propfindMethod = new PropfindMethod(
new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)),
DEPTH_0, DEPTH_0,
DavUtils.getQuotaPropSet()); DavUtils.getQuotaPropSet());