Merge pull request #1265 from nextcloud/ldap

Update user name with correct id found by user ocs call
This commit is contained in:
Andy Scherzinger 2017-08-07 18:58:25 +02:00 committed by GitHub
commit d9a5fecf38
2 changed files with 43 additions and 12 deletions

View file

@ -30,13 +30,18 @@ import android.preference.PreferenceManager;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.accounts.AccountTypeUtils; import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
import com.owncloud.android.lib.common.accounts.AccountUtils.Constants; import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.status.OwnCloudVersion; import com.owncloud.android.lib.resources.status.OwnCloudVersion;
import com.owncloud.android.ui.activity.ManageAccountsActivity; import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
import com.owncloud.android.operations.GetCapabilitiesOperarion; import com.owncloud.android.operations.GetCapabilitiesOperarion;
import com.owncloud.android.ui.activity.ManageAccountsActivity;
import java.util.Locale; import java.util.Locale;
@ -51,6 +56,7 @@ public class AccountUtils {
public static final String STATUS_PATH = "/status.php"; public static final String STATUS_PATH = "/status.php";
public static final int ACCOUNT_VERSION = 1; public static final int ACCOUNT_VERSION = 1;
public static final int ACCOUNT_VERSION_WITH_PROPER_ID = 2;
/** /**
* Can be used to get the currently selected ownCloud {@link Account} in the * Can be used to get the currently selected ownCloud {@link Account} in the
@ -238,8 +244,8 @@ public class AccountUtils {
if ( currentAccount != null ) { if ( currentAccount != null ) {
String currentAccountVersion = accountMgr.getUserData(currentAccount, Constants.KEY_OC_ACCOUNT_VERSION); String currentAccountVersion = accountMgr.getUserData(currentAccount, Constants.KEY_OC_ACCOUNT_VERSION);
if (currentAccountVersion == null) { if (!String.valueOf(ACCOUNT_VERSION_WITH_PROPER_ID).equalsIgnoreCase(currentAccountVersion)) {
Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION); Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION_WITH_PROPER_ID);
Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType()); Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType());
String serverUrl; String serverUrl;
String username; String username;
@ -249,8 +255,29 @@ public class AccountUtils {
for (Account account : ocAccounts) { for (Account account : ocAccounts) {
// build new account name // build new account name
serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL); serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL);
username = com.owncloud.android.lib.common.accounts.AccountUtils.
getUsernameForAccount(account); // update user name
try {
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton()
.getClientFor(ocAccount, context);
GetRemoteUserInfoOperation remoteUserNameOperation = new GetRemoteUserInfoOperation();
RemoteOperationResult result = remoteUserNameOperation.execute(client);
if (result.isSuccess()) {
UserInfo userInfo = (UserInfo) result.getData().get(0);
username = userInfo.id;
} else {
// skip account, try it next time
Log_OC.e(TAG, "Error while getting username for account: " + account.name);
continue;
}
} catch (Exception e) {
Log_OC.e(TAG, "Error while getting username: " + e.getMessage());
continue;
}
newAccountName = com.owncloud.android.lib.common.accounts.AccountUtils. newAccountName = com.owncloud.android.lib.common.accounts.AccountUtils.
buildAccountName(Uri.parse(serverUrl), username); buildAccountName(Uri.parse(serverUrl), username);
@ -312,11 +339,9 @@ public class AccountUtils {
} }
// at least, upgrade account version // at least, upgrade account version
Log_OC.d(TAG, "Setting version " + ACCOUNT_VERSION + " to " + newAccountName); Log_OC.d(TAG, "Setting version " + ACCOUNT_VERSION_WITH_PROPER_ID + " to " + newAccountName);
accountMgr.setUserData( accountMgr.setUserData(newAccount,
newAccount, Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(ACCOUNT_VERSION) Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(ACCOUNT_VERSION_WITH_PROPER_ID));
);
} }
} }
} }

View file

@ -156,8 +156,14 @@ public abstract class FileActivity extends DrawerActivity
false); false);
} }
AccountUtils.updateAccountVersion(this); // best place, before any access to AccountManager Thread t = new Thread(new Runnable() {
// or database @Override
public void run() {
// best place, before any access to AccountManager or database
AccountUtils.updateAccountVersion(getApplicationContext());
}
});
t.start();
setAccount(account, savedInstanceState != null); setAccount(account, savedInstanceState != null);