Filter only '/' character in user input when version of server is 8.1 or later
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AccountUtils.java
index 3b79c39..1e67aa6 100644 (file)
@@ -1,6 +1,8 @@
-/* ownCloud Android client application\r
+/**\r
+ *   ownCloud Android client application\r
+ *\r
  *   Copyright (C) 2012  Bartek Przybylski\r
- *   Copyright (C) 2012-2013 ownCloud Inc.\r
+ *   Copyright (C) 2015 ownCloud Inc.\r
  *\r
  *   This program is free software: you can redistribute it and/or modify\r
  *   it under the terms of the GNU General Public License version 2,\r
 \r
 package com.owncloud.android.authentication;\r
 \r
+import java.util.Locale;\r
+\r
 import com.owncloud.android.MainApp;\r
-import com.owncloud.android.utils.OwnCloudVersion;\r
+import com.owncloud.android.lib.common.accounts.AccountTypeUtils;\r
+import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;\r
+import com.owncloud.android.lib.common.utils.Log_OC;\r
+import com.owncloud.android.lib.resources.status.OwnCloudVersion;\r
 \r
 import android.accounts.Account;\r
 import android.accounts.AccountManager;\r
-import android.accounts.AccountsException;\r
 import android.content.Context;\r
 import android.content.SharedPreferences;\r
+import android.net.Uri;\r
 import android.preference.PreferenceManager;\r
 \r
 public class AccountUtils {\r
-    public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";\r
-    public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";\r
-    public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";\r
+\r
+    private static final String TAG = AccountUtils.class.getSimpleName();\r
+\r
+    public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/webdav";\r
     private static final String ODAV_PATH = "/remote.php/odav";\r
     private static final String SAML_SSO_PATH = "/remote.php/webdav";\r
-    public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";\r
-    public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php";\r
     public static final String STATUS_PATH = "/status.php";\r
 \r
+    public static final int ACCOUNT_VERSION = 1;\r
+\r
     /**\r
      * Can be used to get the currently selected ownCloud {@link Account} in the\r
      * application preferences.\r
@@ -81,8 +89,18 @@ public class AccountUtils {
                 MainApp.getAccountType());\r
 \r
         if (account != null && account.name != null) {\r
-            for (Account ac : ocAccounts) {\r
-                if (ac.name.equals(account.name)) {\r
+            int lastAtPos = account.name.lastIndexOf("@");\r
+            String hostAndPort = account.name.substring(lastAtPos + 1);\r
+            String username = account.name.substring(0, lastAtPos);\r
+            String otherHostAndPort, otherUsername;\r
+            Locale currentLocale = context.getResources().getConfiguration().locale;\r
+            for (Account otherAccount : ocAccounts) {\r
+                lastAtPos = otherAccount.name.lastIndexOf("@");\r
+                otherHostAndPort = otherAccount.name.substring(lastAtPos + 1);\r
+                otherUsername = otherAccount.name.substring(0, lastAtPos);\r
+                if (otherHostAndPort.equals(hostAndPort) &&\r
+                        otherUsername.toLowerCase(currentLocale).\r
+                            equals(username.toLowerCase(currentLocale))) {\r
                     return true;\r
                 }\r
             }\r
@@ -91,19 +109,6 @@ public class AccountUtils {
     }\r
     \r
 \r
-    /**\r
-     * Checks, whether or not there are any ownCloud accounts setup.\r
-     * \r
-     * @return true, if there is at least one account.\r
-     */\r
-    public static boolean accountsAreSetup(Context context) {\r
-        AccountManager accMan = AccountManager.get(context);\r
-        Account[] accounts = accMan\r
-                .getAccountsByType(MainApp.getAccountType());\r
-        return accounts.length > 0;\r
-    }\r
-    \r
-    \r
     public static boolean setCurrentOwnCloudAccount(Context context, String accountName) {\r
         boolean result = false;\r
         if (accountName != null) {\r
@@ -127,94 +132,122 @@ public class AccountUtils {
     }\r
 \r
     /**\r
-     * \r
-     * @param version version of owncloud\r
-     * @return webdav path for given OC version, null if OC version unknown\r
-     */\r
-    public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth, boolean supportsSamlSso) {\r
-        if (version != null) {\r
-            if (supportsOAuth) {\r
-                return ODAV_PATH;\r
-            }\r
-            if (supportsSamlSso) {\r
-                return SAML_SSO_PATH;\r
-            }\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)\r
-                return WEBDAV_PATH_4_0;\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0\r
-                    || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)\r
-                return WEBDAV_PATH_2_0;\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)\r
-                return WEBDAV_PATH_1_2;\r
-        }\r
-        return null;\r
-    }\r
-    \r
-    /**\r
      * Returns the proper URL path to access the WebDAV interface of an ownCloud server,\r
      * according to its version and the authorization method used.\r
      * \r
      * @param   version         Version of ownCloud server.\r
-     * @param   authTokenType   Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in {@link AccountAuthenticator}. \r
-     * @return                  WebDAV path for given OC version and authorization method, null if OC version is unknown.\r
+     * @param   authTokenType   Authorization token type, matching some of the AUTH_TOKEN_TYPE_* constants in\r
+     *                          {@link AccountAuthenticator}.\r
+     * @return                  WebDAV path for given OC version and authorization method, null if OC version\r
+     *                          is unknown; versions prior to ownCloud 4 are not supported anymore\r
      */\r
     public static String getWebdavPath(OwnCloudVersion version, String authTokenType) {\r
         if (version != null) {\r
-            if (MainApp.getAuthTokenTypeAccessToken().equals(authTokenType)) {\r
+            if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(authTokenType)) {\r
                 return ODAV_PATH;\r
             }\r
-            if (MainApp.getAuthTokenTypeSamlSessionCookie().equals(authTokenType)) {\r
+            if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(authTokenType)) {\r
                 return SAML_SSO_PATH;\r
             }\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0)\r
-                return WEBDAV_PATH_4_0;\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0\r
-                    || version.compareTo(OwnCloudVersion.owncloud_v2) >= 0)\r
-                return WEBDAV_PATH_2_0;\r
-            if (version.compareTo(OwnCloudVersion.owncloud_v1) >= 0)\r
-                return WEBDAV_PATH_1_2;\r
+            return WEBDAV_PATH_4_0_AND_LATER;\r
         }\r
         return null;\r
     }\r
-    \r
+\r
+\r
     /**\r
-     * Constructs full url to host and webdav resource basing on host version\r
-     * @param context\r
-     * @param account\r
-     * @return url or null on failure\r
-     * @throws AccountNotFoundException     When 'account' is unknown for the AccountManager\r
+     * Update the accounts in AccountManager to meet the current version of accounts expected by the app, if needed.\r
+     *\r
+     * Introduced to handle a change in the structure of stored account names needed to allow different OC servers\r
+     * in the same domain, but not in the same path.\r
+     *\r
+     * @param   context     Used to access the AccountManager.\r
      */\r
-    public static String constructFullURLForAccount(Context context, Account account) throws AccountNotFoundException {\r
-        AccountManager ama = AccountManager.get(context);\r
-        String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);\r
-        String strver  = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);\r
-        boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null);\r
-        boolean supportsSamlSso = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO) != null);\r
-        OwnCloudVersion ver = new OwnCloudVersion(strver);\r
-        String webdavpath = getWebdavPath(ver, supportsOAuth, supportsSamlSso);\r
-\r
-        if (baseurl == null || webdavpath == null) \r
-            throw new AccountNotFoundException(account, "Account not found", null);\r
-        \r
-        return baseurl + webdavpath;\r
-    }\r
-    \r
-    \r
-    public static class AccountNotFoundException extends AccountsException {\r
-        \r
-        /** Generated - should be refreshed every time the class changes!! */\r
-        private static final long serialVersionUID = -9013287181793186830L;\r
-        \r
-        private Account mFailedAccount; \r
-                \r
-        public AccountNotFoundException(Account failedAccount, String message, Throwable cause) {\r
-            super(message, cause);\r
-            mFailedAccount = failedAccount;\r
-        }\r
-        \r
-        public Account getFailedAccount() {\r
-            return mFailedAccount;\r
+    public static void updateAccountVersion(Context context) {\r
+        Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(context);\r
+        AccountManager accountMgr = AccountManager.get(context);\r
+\r
+        if ( currentAccount != null ) {\r
+            String currentAccountVersion = accountMgr.getUserData(currentAccount, Constants.KEY_OC_ACCOUNT_VERSION);\r
+\r
+            if (currentAccountVersion == null) {\r
+                Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION);\r
+                Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType());\r
+                String serverUrl, username, newAccountName, password;\r
+                Account newAccount;\r
+                for (Account account : ocAccounts) {\r
+                    // build new account name\r
+                    serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL);\r
+                    username = account.name.substring(0, account.name.lastIndexOf('@'));\r
+                    newAccountName = com.owncloud.android.lib.common.accounts.AccountUtils.\r
+                            buildAccountName(Uri.parse(serverUrl), username);\r
+\r
+                    // migrate to a new account, if needed\r
+                    if (!newAccountName.equals(account.name)) {\r
+                        Log_OC.d(TAG, "Upgrading " + account.name + " to " + newAccountName);\r
+\r
+                        // create the new account\r
+                        newAccount = new Account(newAccountName, MainApp.getAccountType());\r
+                        password = accountMgr.getPassword(account);\r
+                        accountMgr.addAccountExplicitly(newAccount, (password != null) ? password : "", null);\r
+\r
+                        // copy base URL\r
+                        accountMgr.setUserData(newAccount, Constants.KEY_OC_BASE_URL, serverUrl);\r
+\r
+                        // copy server version\r
+                        accountMgr.setUserData(\r
+                                newAccount,\r
+                                Constants.KEY_OC_VERSION,\r
+                                accountMgr.getUserData(account, Constants.KEY_OC_VERSION)\r
+                        );\r
+\r
+                        // copy cookies\r
+                        accountMgr.setUserData(\r
+                                newAccount,\r
+                                Constants.KEY_COOKIES,\r
+                                accountMgr.getUserData(account, Constants.KEY_COOKIES)\r
+                        );\r
+\r
+                        // copy type of authentication\r
+                        String isSamlStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO);\r
+                        boolean isSaml = "TRUE".equals(isSamlStr);\r
+                        if (isSaml) {\r
+                            accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");\r
+                        }\r
+\r
+                        String isOauthStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2);\r
+                        boolean isOAuth = "TRUE".equals(isOauthStr);\r
+                        if (isOAuth) {\r
+                            accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");\r
+                        }\r
+                    /* TODO - study if it's possible to run this method in a background thread to copy the authToken\r
+                    if (isOAuth || isSaml) {\r
+                        accountMgr.setAuthToken(newAccount, mAuthTokenType, mAuthToken);\r
+                    }\r
+                    */\r
+\r
+                        // don't forget the account saved in preferences as the current one\r
+                        if (currentAccount != null && currentAccount.name.equals(account.name)) {\r
+                            AccountUtils.setCurrentOwnCloudAccount(context, newAccountName);\r
+                        }\r
+\r
+                        // remove the old account\r
+                        accountMgr.removeAccount(account, null, null);  // will assume it succeeds, not a big deal otherwise\r
+\r
+                    } else {\r
+                        // servers which base URL is in the root of their domain need no change\r
+                        Log_OC.d(TAG, account.name + " needs no upgrade ");\r
+                        newAccount = account;\r
+                    }\r
+\r
+                    // at least, upgrade account version\r
+                    Log_OC.d(TAG, "Setting version " + ACCOUNT_VERSION + " to " + newAccountName);\r
+                    accountMgr.setUserData(newAccount, Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(ACCOUNT_VERSION));\r
+\r
+                }\r
+            }\r
         }\r
     }\r
 \r
+\r
 }\r