From: David A. Velasco Date: Thu, 22 Aug 2013 15:38:26 +0000 (+0200) Subject: Added check to avoid the creation of the same account (same oc_username in the same... X-Git-Tag: oc-android-1.4.6~18^2~7 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/d7c6472bd3835903974d5928fde5bd365fa7ebcb?ds=inline Added check to avoid the creation of the same account (same oc_username in the same server) more than once --- diff --git a/res/layout-land/account_setup.xml b/res/layout-land/account_setup.xml index 5b3ec5d2..73da2b4b 100644 --- a/res/layout-land/account_setup.xml +++ b/res/layout-land/account_setup.xml @@ -143,21 +143,6 @@ - - - - - - - - Testing connection… Malformed server configuration It seems that your server instance is not correctly configured. Contact your administrator for more details. + An account for the same user and server already exists in the device Unknown error occurred! An unknown error occurred. Please contact support and include logs from your device. Couldn\'t find host diff --git a/src/com/owncloud/android/authentication/AccountUtils.java b/src/com/owncloud/android/authentication/AccountUtils.java index a4bc7781..47269bc3 100644 --- a/src/com/owncloud/android/authentication/AccountUtils.java +++ b/src/com/owncloud/android/authentication/AccountUtils.java @@ -75,6 +75,20 @@ public class AccountUtils { } + public static boolean exists(Account account, Context context) { + Account[] ocAccounts = AccountManager.get(context).getAccountsByType( + AccountAuthenticator.ACCOUNT_TYPE); + + if (account != null && account.name != null) { + for (Account ac : ocAccounts) { + if (ac.name.equals(account.name)) { + return true; + } + } + } + return false; + } + /** * Checks, whether or not there are any ownCloud accounts setup. diff --git a/src/com/owncloud/android/authentication/AuthenticatorActivity.java b/src/com/owncloud/android/authentication/AuthenticatorActivity.java index 138c8609..477af967 100644 --- a/src/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/src/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -18,6 +18,8 @@ package com.owncloud.android.authentication; +import java.net.URLDecoder; + import android.accounts.Account; import android.accounts.AccountManager; import android.app.AlertDialog; @@ -33,6 +35,7 @@ import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; +import android.support.v4.app.Fragment; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; @@ -50,6 +53,7 @@ import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; import android.widget.Toast; +import com.actionbarsherlock.app.SherlockDialogFragment; import com.owncloud.android.Log_OC; import com.owncloud.android.R; import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener; @@ -98,6 +102,8 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList private static final String KEY_AUTH_STATUS_TEXT = "AUTH_STATUS_TEXT"; private static final String KEY_AUTH_STATUS_ICON = "AUTH_STATUS_ICON"; private static final String KEY_REFRESH_BUTTON_ENABLED = "KEY_REFRESH_BUTTON_ENABLED"; + + private static final String KEY_OC_USERNAME_EQUALS = "oc_username="; private static final String AUTH_ON = "on"; private static final String AUTH_OFF = "off"; @@ -148,7 +154,6 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList private TextView mOAuthAuthEndpointText; private TextView mOAuthTokenEndpointText; - private TextView mAccountNameInput; private SamlWebViewDialog mSamlDialog; private View mOkButton; @@ -175,7 +180,6 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList mOAuthAuthEndpointText = (TextView)findViewById(R.id.oAuthEntryPoint_1); mOAuthTokenEndpointText = (TextView)findViewById(R.id.oAuthEntryPoint_2); mOAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check); - mAccountNameInput = (EditText) findViewById(R.id.account_name); mOkButton = findViewById(R.id.buttonOK); mAuthStatusLayout = (TextView) findViewById(R.id.auth_status_text); @@ -297,6 +301,8 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList /// bind view elements to listeners and other friends mHostUrlInput.setOnFocusChangeListener(this); + mHostUrlInput.setImeOptions(EditorInfo.IME_ACTION_NEXT); + mHostUrlInput.setOnEditorActionListener(this); mHostUrlInput.addTextChangedListener(new TextWatcher() { @Override @@ -986,6 +992,9 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList case OAUTH2_ERROR_ACCESS_DENIED: mAuthStatusText = R.string.auth_oauth_error_access_denied; break; + case ACCOUNT_NOT_NEW: + mAuthStatusText = R.string.auth_account_not_new; + break; case UNHANDLED_HTTP_CODE: case UNKNOWN_ERROR: mAuthStatusText = R.string.auth_unknown_error_title; @@ -1050,14 +1059,17 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList if (result.isSuccess()) { Log_OC.d(TAG, "Successful access - time to save the account"); + boolean success = true; if (mAction == ACTION_CREATE) { - createAccount(); + success = createAccount(); } else { updateToken(); } - finish(); + if (success) { + finish(); + } } else if (result.isServerFail() || result.isException()) { /// if server fail or exception in authorization, the UI is updated as when a server check failed @@ -1127,7 +1139,7 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList * * TODO Decide how to name the OAuth accounts */ - private void createAccount() { + private boolean createAccount() { /// create and save new ownCloud account boolean isOAuth = AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(mCurrentAuthTokenType); boolean isSaml = AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType); @@ -1135,7 +1147,7 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList Uri uri = Uri.parse(mHostBaseUrl); String username = mUsernameInput.getText().toString().trim(); if (isSaml) { - username = mAccountNameInput.getText().toString().trim(); + username = getUserNameForSamlSso(); } else if (isOAuth) { username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong(); @@ -1145,48 +1157,75 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList accountName += ":" + uri.getPort(); } mAccount = new Account(accountName, AccountAuthenticator.ACCOUNT_TYPE); - if (isOAuth || isSaml) { - mAccountMgr.addAccountExplicitly(mAccount, "", null); // with external authorizations, the password is never input in the app + if (AccountUtils.exists(mAccount, getApplicationContext())) { + // fail - not a new account, but an existing one; disallow + RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_NEW); + updateAuthStatusIconAndText(result); + showAuthStatus(); + Log_OC.d(TAG, result.getLogMessage()); + return false; + + } else { - mAccountMgr.addAccountExplicitly(mAccount, mPasswordInput.getText().toString(), null); - } - - /// add the new account as default in preferences, if there is none already - Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this); - if (defaultAccount == null) { - SharedPreferences.Editor editor = PreferenceManager - .getDefaultSharedPreferences(this).edit(); - editor.putString("select_oc_account", accountName); - editor.commit(); + + if (isOAuth || isSaml) { + mAccountMgr.addAccountExplicitly(mAccount, "", null); // with external authorizations, the password is never input in the app + } else { + mAccountMgr.addAccountExplicitly(mAccount, mPasswordInput.getText().toString(), null); + } + + /// add the new account as default in preferences, if there is none already + Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this); + if (defaultAccount == null) { + SharedPreferences.Editor editor = PreferenceManager + .getDefaultSharedPreferences(this).edit(); + editor.putString("select_oc_account", accountName); + editor.commit(); + } + + /// prepare result to return to the Authenticator + // TODO check again what the Authenticator makes with it; probably has the same effect as addAccountExplicitly, but it's not well done + final Intent intent = new Intent(); + intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE); + intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mAccount.name); + /*if (!isOAuth) + intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE); */ + intent.putExtra(AccountManager.KEY_USERDATA, username); + if (isOAuth || isSaml) { + mAccountMgr.setAuthToken(mAccount, mCurrentAuthTokenType, mAuthToken); + } + /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA + mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION, mDiscoveredVersion.toString()); + mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL, mHostBaseUrl); + if (isSaml) { + mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); + } else if (isOAuth) { + mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_OAUTH2, "TRUE"); + } + + setAccountAuthenticatorResult(intent.getExtras()); + setResult(RESULT_OK, intent); + + /// immediately request for the synchronization of the new account + Bundle bundle = new Bundle(); + bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); + ContentResolver.requestSync(mAccount, AccountAuthenticator.AUTHORITY, bundle); + return true; } + } - /// prepare result to return to the Authenticator - // TODO check again what the Authenticator makes with it; probably has the same effect as addAccountExplicitly, but it's not well done - final Intent intent = new Intent(); - intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE); - intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mAccount.name); - /*if (!isOAuth) - intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE); */ - intent.putExtra(AccountManager.KEY_USERDATA, username); - if (isOAuth || isSaml) { - mAccountMgr.setAuthToken(mAccount, mCurrentAuthTokenType, mAuthToken); - } - /// add user data to the new account; TODO probably can be done in the last parameter addAccountExplicitly, or in KEY_USERDATA - mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION, mDiscoveredVersion.toString()); - mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL, mHostBaseUrl); - if (isSaml) { - mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE"); - } else if (isOAuth) { - mAccountMgr.setUserData(mAccount, AccountAuthenticator.KEY_SUPPORTS_OAUTH2, "TRUE"); + + private String getUserNameForSamlSso() { + if (mAuthToken != null) { + String [] cookies = mAuthToken.split(";"); + for (int i=0; i