X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/863f3f68d20b34dce2a1a7cb5230a23983da1f30..e901b609baa4dd5f681e2a5257c9e504997e3377:/src/com/owncloud/android/authentication/AccountUtils.java diff --git a/src/com/owncloud/android/authentication/AccountUtils.java b/src/com/owncloud/android/authentication/AccountUtils.java index cfa00e32..dc023444 100644 --- a/src/com/owncloud/android/authentication/AccountUtils.java +++ b/src/com/owncloud/android/authentication/AccountUtils.java @@ -18,11 +18,14 @@ package com.owncloud.android.authentication; -import com.owncloud.android.utils.OwnCloudVersion; +import java.util.Locale; + +import com.owncloud.android.MainApp; +import com.owncloud.android.lib.common.accounts.AccountTypeUtils; +import com.owncloud.android.lib.resources.status.OwnCloudVersion; import android.accounts.Account; import android.accounts.AccountManager; -import android.accounts.AccountsException; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; @@ -32,6 +35,7 @@ public class AccountUtils { public static final String WEBDAV_PATH_2_0 = "/files/webdav.php"; public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav"; private static final String ODAV_PATH = "/remote.php/odav"; + private static final String SAML_SSO_PATH = "/remote.php/webdav"; public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php"; public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php"; public static final String STATUS_PATH = "/status.php"; @@ -47,7 +51,7 @@ public class AccountUtils { */ public static Account getCurrentOwnCloudAccount(Context context) { Account[] ocAccounts = AccountManager.get(context).getAccountsByType( - AccountAuthenticator.ACCOUNT_TYPE); + MainApp.getAccountType()); Account defaultAccount = null; SharedPreferences appPreferences = PreferenceManager @@ -74,6 +78,30 @@ public class AccountUtils { } + public static boolean exists(Account account, Context context) { + Account[] ocAccounts = AccountManager.get(context).getAccountsByType( + MainApp.getAccountType()); + + if (account != null && account.name != null) { + int lastAtPos = account.name.lastIndexOf("@"); + String hostAndPort = account.name.substring(lastAtPos + 1); + String username = account.name.substring(0, lastAtPos); + String otherHostAndPort, otherUsername; + Locale currentLocale = context.getResources().getConfiguration().locale; + for (Account otherAccount : ocAccounts) { + lastAtPos = otherAccount.name.lastIndexOf("@"); + otherHostAndPort = otherAccount.name.substring(lastAtPos + 1); + otherUsername = otherAccount.name.substring(0, lastAtPos); + if (otherHostAndPort.equals(hostAndPort) && + otherUsername.toLowerCase(currentLocale). + equals(username.toLowerCase(currentLocale))) { + return true; + } + } + } + return false; + } + /** * Checks, whether or not there are any ownCloud accounts setup. @@ -83,7 +111,7 @@ public class AccountUtils { public static boolean accountsAreSetup(Context context) { AccountManager accMan = AccountManager.get(context); Account[] accounts = accMan - .getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE); + .getAccountsByType(MainApp.getAccountType()); return accounts.length > 0; } @@ -92,7 +120,7 @@ public class AccountUtils { boolean result = false; if (accountName != null) { Account[] ocAccounts = AccountManager.get(context).getAccountsByType( - AccountAuthenticator.ACCOUNT_TYPE); + MainApp.getAccountType()); boolean found = false; for (Account account : ocAccounts) { found = (account.name.equals(accountName)); @@ -111,15 +139,21 @@ public class AccountUtils { } /** + * Returns the proper URL path to access the WebDAV interface of an ownCloud server, + * according to its version and the authorization method used. * - * @param version version of owncloud - * @return webdav path for given OC version, null if OC version unknown + * @param version Version of ownCloud server. + * @param authTokenType Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}. + * @return WebDAV path for given OC version and authorization method, null if OC version is unknown. */ - public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth) { + public static String getWebdavPath(OwnCloudVersion version, String authTokenType) { if (version != null) { - if (supportsOAuth) { + if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(authTokenType)) { return ODAV_PATH; } + if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(authTokenType)) { + return SAML_SSO_PATH; + } if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0) return WEBDAV_PATH_4_0; if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0 @@ -131,42 +165,4 @@ public class AccountUtils { return null; } - /** - * Constructs full url to host and webdav resource basing on host version - * @param context - * @param account - * @return url or null on failure - * @throws AccountNotFoundException When 'account' is unknown for the AccountManager - */ - public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException { - AccountManager ama = AccountManager.get(context); - String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL); - String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION); - boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null); - OwnCloudVersion ver = new OwnCloudVersion(strver); - String webdavpath = getWebdavPath(ver, supportsOAuth); - - if (baseurl == null || webdavpath == null) - throw new AccountNotFoundException(account, "Account not found", null); - - return baseurl + webdavpath; - } - - - public static class AccountNotFoundException extends AccountsException { - - private static final long serialVersionUID = 4276870654168776992L; - - private Account mFailedAccount; - - public AccountNotFoundException(Account failedAccount, String message, Throwable cause) { - super(message, cause); - mFailedAccount = failedAccount; - } - - public Account getFailedAccount() { - return mFailedAccount; - } - } - }