mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-28 17:07:49 +00:00 
			
		
		
		
	Use user id in LDAP requests
This commit is contained in:
		
							parent
							
								
									7a50007ba3
								
							
						
					
					
						commit
						2c5ceeb555
					
				| @ -29,6 +29,7 @@ import android.accounts.AccountManager; | ||||
| import android.accounts.AccountsException; | ||||
| 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.OwnCloudCredentialsFactory; | ||||
| 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 static final String NEW_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_FILES_PATH_4_0 = "/remote.php/dav/files/"; | ||||
|     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 FILES_WEB_PATH = "/index.php/apps/files"; | ||||
| 
 | ||||
| @ -80,7 +81,6 @@ public class OwnCloudClient extends HttpClient { | ||||
|     private String mRedirectedLocation; | ||||
|     private boolean mFollowRedirects; | ||||
| 
 | ||||
| 
 | ||||
|     public OwnCloudClient(Uri baseUri) { | ||||
|         if (baseUri == null) { | ||||
|             throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL"); | ||||
| @ -208,7 +208,7 @@ public class OwnCloudClient extends HttpClient { | ||||
|                         : method.getRequestHeader("destination"); | ||||
| 
 | ||||
|                 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 destinationPath = destination.substring(mBaseUri.toString().length()); | ||||
| 
 | ||||
| @ -252,16 +252,26 @@ public class OwnCloudClient extends HttpClient { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public Uri getNewFilesWebDavUri() { | ||||
|         return mCredentials instanceof OwnCloudAnonymousCredentials | ||||
|                 ? Uri.parse(mBaseUri + NEW_WEBDAV_FILES_PATH_4_0) | ||||
|                 : Uri.parse(mBaseUri + NEW_WEBDAV_FILES_PATH_4_0 + mCredentials.getUsername()); | ||||
|     public Uri getBaseFilesWebDavUri(){ | ||||
|         return Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0); | ||||
|     } | ||||
| 
 | ||||
|     public Uri getNewUploadsWebDavUri() { | ||||
|     public Uri getUserFilesWebDavUri() { | ||||
|         return mCredentials instanceof OwnCloudAnonymousCredentials | ||||
|                 ? Uri.parse(mBaseUri + NEW_WEBDAV_UPLOADS_PATH_4_0) | ||||
|                 : Uri.parse(mBaseUri + NEW_WEBDAV_UPLOADS_PATH_4_0 + mCredentials.getUsername()); | ||||
|                 ? Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0) | ||||
|                 : 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() | ||||
|                 ) | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | ||||
| @ -64,7 +64,7 @@ public class AccountUtils { | ||||
| 
 | ||||
|         try { | ||||
|             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(); | ||||
|         } catch (OperationCanceledException e) { | ||||
|             e.printStackTrace(); | ||||
| @ -188,6 +188,16 @@ public class AccountUtils { | ||||
|         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) { | ||||
|         if (serverBaseUrl.getScheme() == null) { | ||||
|             serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString()); | ||||
| @ -244,7 +254,7 @@ public class AccountUtils { | ||||
|             // Account Manager | ||||
|             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); | ||||
|             if (cookiesString != null) { | ||||
| @ -318,6 +328,11 @@ public class AccountUtils { | ||||
|          */ | ||||
|         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 | ||||
|          */ | ||||
|  | ||||
| @ -103,8 +103,8 @@ public class CopyRemoteFileOperation extends RemoteOperation { | ||||
|         /// perform remote operation | ||||
|         RemoteOperationResult result = null; | ||||
|         try { | ||||
|             CopyMethod copyMethod = new CopyMethod(new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mSrcRemotePath)), | ||||
|                     client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), | ||||
|             CopyMethod copyMethod = new CopyMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mSrcRemotePath)), | ||||
|                     client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), | ||||
|                     mOverwrite); | ||||
| 
 | ||||
|             copyMethod.setReadTimeout(COPY_READ_TIMEOUT, TimeUnit.SECONDS); | ||||
|  | ||||
| @ -100,7 +100,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation { | ||||
|     private RemoteOperationResult createFolder(OwnCloudClient client) { | ||||
|         RemoteOperationResult result; | ||||
|         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))); | ||||
|             mkcol.setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS); | ||||
|             mkcol.setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS); | ||||
|  | ||||
| @ -101,7 +101,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation { | ||||
|         RemoteOperationResult result; | ||||
|         int status; | ||||
|         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; | ||||
| 
 | ||||
|         FileOutputStream fos = null; | ||||
|  | ||||
| @ -56,6 +56,7 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | ||||
| 
 | ||||
|     private String mPath; | ||||
|     private boolean mSuccessIfAbsent; | ||||
|     private boolean mIsLogin; | ||||
| 
 | ||||
|     /** | ||||
|      * 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 successIfAbsent When 'true', the operation finishes in success if the path does | ||||
|      *                        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 : ""; | ||||
|         mSuccessIfAbsent = successIfAbsent; | ||||
|         mIsLogin = isLogin; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
| @ -80,8 +84,11 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | ||||
|         boolean previousFollowRedirects = client.followRedirects(); | ||||
| 
 | ||||
|         try { | ||||
|             String stringUrl = mIsLogin ? | ||||
|                     client.getBaseFilesWebDavUri().toString() : | ||||
|                     client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mPath); | ||||
|             PropfindMethod propfindMethod = new PropfindMethod( | ||||
|                     new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mPath)), | ||||
|                     new URL(stringUrl), | ||||
|                     0, | ||||
|                     DavUtils.getAllPropset() | ||||
|             ); | ||||
| @ -102,9 +109,8 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | ||||
|              *  207 MULTI_STATUS: path exists. | ||||
|              */ | ||||
| 
 | ||||
|             Log_OC.d(TAG, "Existence check for " + client.getNewFilesWebDavUri() + | ||||
|                     WebdavUtils.encodePath(mPath) + " targeting for " + | ||||
|                     (mSuccessIfAbsent ? " absence " : " existence ") + | ||||
|             Log_OC.d(TAG, "Existence check for " + stringUrl + WebdavUtils.encodePath(mPath) + | ||||
|                     " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + | ||||
|                     "finished with HTTP status " + status + (!isSuccess(status) ? "(FAIL)" : "")); | ||||
| 
 | ||||
|             return isSuccess(status) | ||||
| @ -113,7 +119,7 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | ||||
| 
 | ||||
|         } catch (Exception 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 " + | ||||
|                     (mSuccessIfAbsent ? " absence " : " existence ") + ": " + | ||||
|                     result.getLogMessage(), result.getException()); | ||||
|  | ||||
| @ -111,11 +111,11 @@ public class MoveRemoteFileOperation extends RemoteOperation { | ||||
|         try { | ||||
|             // 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 | ||||
|             Uri srcWebDavUri = moveChunkedFile ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); | ||||
|             Uri srcWebDavUri = moveChunkedFile ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); | ||||
| 
 | ||||
|             final MoveMethod move = new MoveMethod( | ||||
|                     new URL(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)), | ||||
|                 client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), | ||||
|                 client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), | ||||
|                     mOverwrite); | ||||
| 
 | ||||
|             if (moveChunkedFile) { | ||||
|  | ||||
| @ -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 | ||||
|         try { | ||||
|             // remote request | ||||
|             propfind = new PropfindMethod(new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), | ||||
|             propfind = new PropfindMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), | ||||
|                     DEPTH_0, | ||||
|                     DavUtils.getAllPropset()); | ||||
| 
 | ||||
|  | ||||
| @ -75,7 +75,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation<ArrayList<RemoteF | ||||
| 
 | ||||
|         try { | ||||
|             PropfindMethod propfindMethod = new PropfindMethod( | ||||
|                     new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), | ||||
|                     new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), | ||||
|                     DavConstants.DEPTH_1, | ||||
|                     DavUtils.getAllPropset()); | ||||
| 
 | ||||
|  | ||||
| @ -47,7 +47,7 @@ import at.bitfire.dav4android.property.owncloud.OCPrivatelink; | ||||
| import at.bitfire.dav4android.property.owncloud.OCSize; | ||||
| 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 | ||||
| @ -228,7 +228,7 @@ public class RemoteFile implements Parcelable, Serializable { | ||||
| 
 | ||||
| 
 | ||||
|     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]; | ||||
|         return Uri.decode(url.encodedPath()).replace(pathToOc + davPath, ""); | ||||
|     } | ||||
|  | ||||
| @ -70,7 +70,7 @@ public class RemoveRemoteFileOperation extends RemoteOperation { | ||||
|         RemoteOperationResult result; | ||||
| 
 | ||||
|         try { | ||||
|             Uri srcWebDavUri = removeChunksFolder ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); | ||||
|             Uri srcWebDavUri = removeChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); | ||||
| 
 | ||||
|             DeleteMethod deleteMethod = new DeleteMethod( | ||||
|                     new URL(srcWebDavUri + WebdavUtils.encodePath(mRemotePath))); | ||||
|  | ||||
| @ -104,9 +104,9 @@ public class RenameRemoteFileOperation extends RemoteOperation { | ||||
|                 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)), | ||||
|                     client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false); | ||||
|                     client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false); | ||||
| 
 | ||||
|             move.setReadTimeout(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) { | ||||
|         ExistenceCheckRemoteOperation existenceCheckRemoteOperation = | ||||
|             new ExistenceCheckRemoteOperation(mNewRemotePath, false); | ||||
|             new ExistenceCheckRemoteOperation(mNewRemotePath, false, false); | ||||
|         RemoteOperationResult exists = existenceCheckRemoteOperation.run(client); | ||||
|         return exists.isSuccess(); | ||||
|     } | ||||
|  | ||||
| @ -89,7 +89,7 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
| 
 | ||||
|         try { | ||||
|             mPutMethod = new PutMethod( | ||||
|                     new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); | ||||
|                     new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); | ||||
| 
 | ||||
|             mPutMethod.setRetryOnConnectionFailure(false); | ||||
| 
 | ||||
|  | ||||
| @ -84,7 +84,7 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | ||||
|         } | ||||
| 
 | ||||
|         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 chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); | ||||
| 
 | ||||
|  | ||||
| @ -89,7 +89,7 @@ public class GetRemoteUserQuotaOperation extends RemoteOperation<GetRemoteUserQu | ||||
| 
 | ||||
|         try { | ||||
|             PropfindMethod propfindMethod = new PropfindMethod( | ||||
|                     new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), | ||||
|                     new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), | ||||
|                     DEPTH_0, | ||||
|                     DavUtils.getQuotaPropSet()); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user