Skip to content

Commit 7afa196

Browse files
Merge pull request #1505 from nextcloud/improve-authentication
Fix for authentication/favorite/etc
2 parents 620d18d + 3cbcd28 commit 7afa196

File tree

9 files changed

+49
-142
lines changed

9 files changed

+49
-142
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ dependencies {
184184
/// dependencies for app building
185185
implementation name: 'touch-image-view'
186186
implementation 'com.android.support:multidex:1.0.2'
187-
implementation 'com.github.nextcloud:android-library:1.0.28'
187+
compile 'com.github.nextcloud:android-library:1.0.30'
188188
implementation "com.android.support:support-v4:${supportLibraryVersion}"
189189
implementation "com.android.support:design:${supportLibraryVersion}"
190190
implementation 'com.jakewharton:disklrucache:2.0.2'

scripts/lint/lint-results.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
DO NOT TOUCH; GENERATED BY DRONE
2-
<span class="mdl-layout-title">Lint Report: 1 error and 513 warnings</span>
2+
<span class="mdl-layout-title">Lint Report: 1 error and 512 warnings</span>

src/main/java/com/owncloud/android/authentication/AccountUtils.java

Lines changed: 1 addition & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,16 @@
2424
import android.accounts.AccountManager;
2525
import android.content.Context;
2626
import android.content.SharedPreferences;
27-
import android.net.Uri;
2827
import android.preference.PreferenceManager;
2928

3029
import com.owncloud.android.MainApp;
3130
import com.owncloud.android.datamodel.ArbitraryDataProvider;
3231
import com.owncloud.android.datamodel.FileDataStorageManager;
33-
import com.owncloud.android.lib.common.OwnCloudAccount;
34-
import com.owncloud.android.lib.common.OwnCloudClient;
35-
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
36-
import com.owncloud.android.lib.common.UserInfo;
3732
import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
3833
import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
3934
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
4035
import com.owncloud.android.lib.common.utils.Log_OC;
4136
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
42-
import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
4337
import com.owncloud.android.operations.GetCapabilitiesOperarion;
4438
import com.owncloud.android.ui.activity.ManageAccountsActivity;
4539

@@ -228,126 +222,7 @@ public static String getWebdavPath(OwnCloudVersion version, String authTokenType
228222
return null;
229223
}
230224

231-
232-
/**
233-
* Update the accounts in AccountManager to meet the current version of accounts expected by the app, if needed.
234-
*
235-
* Introduced to handle a change in the structure of stored account names needed to allow different OC servers
236-
* in the same domain, but not in the same path.
237-
*
238-
* @param context Used to access the AccountManager.
239-
*/
240-
public static void updateAccountVersion(Context context) {
241-
Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(context);
242-
AccountManager accountMgr = AccountManager.get(context);
243-
244-
if ( currentAccount != null ) {
245-
String currentAccountVersion = accountMgr.getUserData(currentAccount, Constants.KEY_OC_ACCOUNT_VERSION);
246-
247-
if (!String.valueOf(ACCOUNT_VERSION_WITH_PROPER_ID).equalsIgnoreCase(currentAccountVersion)) {
248-
Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION_WITH_PROPER_ID);
249-
Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType());
250-
String serverUrl;
251-
String username;
252-
String newAccountName;
253-
String password;
254-
Account newAccount;
255-
for (Account account : ocAccounts) {
256-
// build new account name
257-
serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL);
258-
259-
// update user name
260-
try {
261-
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
262-
OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton()
263-
.getClientFor(ocAccount, context);
264-
265-
GetRemoteUserInfoOperation remoteUserNameOperation = new GetRemoteUserInfoOperation();
266-
RemoteOperationResult result = remoteUserNameOperation.execute(client);
267-
268-
if (result.isSuccess()) {
269-
UserInfo userInfo = (UserInfo) result.getData().get(0);
270-
username = userInfo.id;
271-
} else {
272-
// skip account, try it next time
273-
Log_OC.e(TAG, "Error while getting username for account: " + account.name);
274-
continue;
275-
}
276-
} catch (Exception e) {
277-
Log_OC.e(TAG, "Error while getting username: " + e.getMessage());
278-
continue;
279-
}
280-
281-
newAccountName = com.owncloud.android.lib.common.accounts.AccountUtils.
282-
buildAccountName(Uri.parse(serverUrl), username);
283-
284-
// migrate to a new account, if needed
285-
if (!newAccountName.equals(account.name)) {
286-
Log_OC.d(TAG, "Upgrading " + account.name + " to " + newAccountName);
287-
288-
// create the new account
289-
newAccount = new Account(newAccountName, MainApp.getAccountType());
290-
password = accountMgr.getPassword(account);
291-
accountMgr.addAccountExplicitly(newAccount, (password != null) ? password : "", null);
292-
293-
// copy base URL
294-
accountMgr.setUserData(newAccount, Constants.KEY_OC_BASE_URL, serverUrl);
295-
296-
// copy server version
297-
accountMgr.setUserData(
298-
newAccount,
299-
Constants.KEY_OC_VERSION,
300-
accountMgr.getUserData(account, Constants.KEY_OC_VERSION)
301-
);
302-
303-
// copy cookies
304-
accountMgr.setUserData(
305-
newAccount,
306-
Constants.KEY_COOKIES,
307-
accountMgr.getUserData(account, Constants.KEY_COOKIES)
308-
);
309-
310-
// copy type of authentication
311-
final String isSamlStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_SAML_WEB_SSO);
312-
if (Boolean.parseBoolean(isSamlStr)) {
313-
accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");
314-
}
315-
316-
final String isOauthStr = accountMgr.getUserData(account, Constants.KEY_SUPPORTS_OAUTH2);
317-
if (Boolean.parseBoolean(isOauthStr)) {
318-
accountMgr.setUserData(newAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");
319-
}
320-
/* TODO - study if it's possible to run this method in a background thread to copy the authToken
321-
if (isOAuth || isSaml) {
322-
accountMgr.setAuthToken(newAccount, mAuthTokenType, mAuthToken);
323-
}
324-
*/
325-
326-
// don't forget the account saved in preferences as the current one
327-
if (currentAccount.name.equals(account.name)) {
328-
AccountUtils.setCurrentOwnCloudAccount(context, newAccountName);
329-
}
330-
331-
// remove the old account
332-
accountMgr.removeAccount(account, null, null);
333-
// will assume it succeeds, not a big deal otherwise
334-
335-
} else {
336-
// servers which base URL is in the root of their domain need no change
337-
Log_OC.d(TAG, account.name + " needs no upgrade ");
338-
newAccount = account;
339-
}
340-
341-
// at least, upgrade account version
342-
Log_OC.d(TAG, "Setting version " + ACCOUNT_VERSION_WITH_PROPER_ID + " to " + newAccountName);
343-
accountMgr.setUserData(newAccount,
344-
Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(ACCOUNT_VERSION_WITH_PROPER_ID));
345-
}
346-
}
347-
}
348-
}
349-
350-
225+
351226
public static String trimWebdavSuffix(String url) {
352227
while(url.endsWith("/")) {
353228
url = url.substring(0, url.length() - 1);

src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,7 @@ private boolean createAccount(RemoteOperationResult authResult) {
19141914
if (isOAuth) {
19151915
username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();
19161916
}
1917+
19171918
String accountName = com.owncloud.android.lib.common.accounts.AccountUtils.
19181919
buildAccountName(uri, username);
19191920
Account newAccount = new Account(accountName, MainApp.getAccountType());
@@ -1979,12 +1980,17 @@ private boolean createAccount(RemoteOperationResult authResult) {
19791980
mAccountMgr.setUserData(
19801981
mAccount, Constants.KEY_OC_BASE_URL, mServerInfo.mBaseUrl
19811982
);
1983+
19821984
if (authResult.getData() != null) {
19831985
try {
19841986
UserInfo userInfo = (UserInfo) authResult.getData().get(0);
19851987
mAccountMgr.setUserData(
19861988
mAccount, Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName()
19871989
);
1990+
1991+
mAccountMgr.setUserData(
1992+
mAccount, Constants.KEY_USER_ID, userInfo.getId()
1993+
);
19881994
} catch (ClassCastException c) {
19891995
Log_OC.w(TAG, "Couldn't get display name for " + username);
19901996
}

src/main/java/com/owncloud/android/operations/GetUserProfileOperation.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ protected RemoteOperationResult run(OwnCloudClient client) {
6565
AccountUtils.Constants.KEY_DISPLAY_NAME,
6666
userInfo.getDisplayName()
6767
);
68+
69+
accountManager.setUserData(
70+
storedAccount,
71+
AccountUtils.Constants.KEY_USER_ID,
72+
userInfo.getId()
73+
);
6874
}
6975
return result;
7076
}

src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import android.support.v4.widget.DrawerLayout;
4040
import android.support.v7.app.ActionBarDrawerToggle;
4141
import android.text.Html;
42+
import android.text.TextUtils;
4243
import android.view.Menu;
4344
import android.view.MenuItem;
4445
import android.view.View;
@@ -932,15 +933,34 @@ private void getAndDisplayUserQuota() {
932933
// set user space information
933934
Thread t = new Thread(new Runnable() {
934935
public void run() {
936+
AccountManager mAccountMgr = AccountManager.get(MainApp.getAppContext());
937+
938+
String userId = mAccountMgr.getUserData(AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this),
939+
com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
940+
941+
RemoteOperation getQuotaInfoOperation;
942+
if (TextUtils.isEmpty(userId)) {
943+
getQuotaInfoOperation = new GetRemoteUserInfoOperation();
944+
} else {
945+
getQuotaInfoOperation = new GetRemoteUserInfoOperation(userId);
946+
}
935947

936-
RemoteOperation getQuotaInfoOperation = new GetRemoteUserInfoOperation();
937948
RemoteOperationResult result = getQuotaInfoOperation.execute(
938949
AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this), DrawerActivity.this);
939950

940951
if (result.isSuccess() && result.getData() != null) {
941952
final UserInfo userInfo = (UserInfo) result.getData().get(0);
942953
final Quota quota = userInfo.getQuota();
943954

955+
// Since we always call this method, might as well put it here
956+
if (userInfo.getId() != null) {
957+
mAccountMgr.setUserData(
958+
AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this),
959+
com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID,
960+
userInfo.getId()
961+
);
962+
}
963+
944964
if (quota != null) {
945965
final long used = quota.getUsed();
946966
final long total = quota.getTotal();

src/main/java/com/owncloud/android/ui/activity/FileActivity.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
import com.owncloud.android.MainApp;
4040
import com.owncloud.android.R;
41-
import com.owncloud.android.authentication.AccountUtils;
4241
import com.owncloud.android.authentication.AuthenticatorActivity;
4342
import com.owncloud.android.datamodel.OCFile;
4443
import com.owncloud.android.files.services.FileDownloader;
@@ -156,15 +155,6 @@ protected void onCreate(Bundle savedInstanceState) {
156155
false);
157156
}
158157

159-
Thread t = new Thread(new Runnable() {
160-
@Override
161-
public void run() {
162-
// best place, before any access to AccountManager or database
163-
AccountUtils.updateAccountVersion(getApplicationContext());
164-
}
165-
});
166-
t.start();
167-
168158
setAccount(account, savedInstanceState != null);
169159

170160
mOperationsServiceConnection = new OperationsServiceConnection();

src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package com.owncloud.android.ui.fragment;
2323

2424
import android.accounts.Account;
25+
import android.accounts.AccountManager;
2526
import android.accounts.AuthenticatorException;
2627
import android.accounts.OperationCanceledException;
2728
import android.app.Activity;
@@ -40,6 +41,7 @@
4041
import android.support.design.widget.Snackbar;
4142
import android.support.v4.widget.DrawerLayout;
4243
import android.support.v4.widget.SwipeRefreshLayout;
44+
import android.text.TextUtils;
4345
import android.util.SparseBooleanArray;
4446
import android.view.ActionMode;
4547
import android.view.LayoutInflater;
@@ -1415,6 +1417,8 @@ public void onMessageEvent(FavoriteEvent event) {
14151417
Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
14161418

14171419
OwnCloudAccount ocAccount = null;
1420+
AccountManager mAccountMgr = AccountManager.get(getActivity());
1421+
14181422
try {
14191423
ocAccount = new OwnCloudAccount(
14201424
currentAccount,
@@ -1424,8 +1428,15 @@ public void onMessageEvent(FavoriteEvent event) {
14241428
OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
14251429
getClientFor(ocAccount, MainApp.getAppContext());
14261430

1431+
String userId = mAccountMgr.getUserData(currentAccount,
1432+
com.owncloud.android.lib.common.accounts.AccountUtils.Constants.KEY_USER_ID);
1433+
1434+
if (TextUtils.isEmpty(userId)) {
1435+
userId = mClient.getCredentials().getUsername();
1436+
}
1437+
14271438
ToggleFavoriteOperation toggleFavoriteOperation = new ToggleFavoriteOperation(event.shouldFavorite,
1428-
event.remotePath);
1439+
event.remotePath, userId);
14291440
RemoteOperationResult remoteOperationResult = toggleFavoriteOperation.execute(mClient);
14301441

14311442
if (remoteOperationResult.isSuccess()) {

src/main/res/values/styles.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,4 @@
256256
</style>
257257
<style name="NextcloudTextAppearanceSmall" parent="style/TextAppearance.AppCompat.Small">
258258
</style>
259-
<style name="Test" parent=""/>
260-
</resources>
259+
</resources>

0 commit comments

Comments
 (0)