Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 61f26c61 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

1975-ignore_eeo_one_account_username_modification

parent 219e8eff
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ configurations {

def versionMajor = 1
def versionMinor = 0
def versionPatch = 5
def versionPatch = 6
def releasePatch = "release"
def libName = "Nextcloud-Android-Library"
def groupName = "foundation.e"
+3 −3
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ public class OwnCloudClientFactory {
        Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account));
        AccountManager am = AccountManager.get(appContext);
        // TODO avoid calling to getUserData here
        String userId = AccountUtils.getUserId(am, account);
        String userId = AccountUtils.getUpdatedUserId(am, account);

        OwnCloudClient client = createOwnCloudClient(baseUri, appContext, true);
        client.setUserId(userId);
@@ -106,7 +106,7 @@ public class OwnCloudClientFactory {
        Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account));
        AccountManager am = AccountManager.get(appContext);
        // TODO avoid calling to getUserData here
        String userId = AccountUtils.getUserId(am, account);
        String userId = AccountUtils.getUpdatedUserId(am, account);

        OwnCloudClient client = createOwnCloudClient(baseUri, appContext, true);
        client.setUserId(userId);
@@ -212,7 +212,7 @@ public class OwnCloudClientFactory {
        Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account));
        AccountManager am = AccountManager.get(appContext);
        // TODO avoid calling to getUserData here
        String userId = AccountUtils.getUserId(am, account);
        String userId = AccountUtils.getUpdatedUserId(am, account);
        String username = AccountUtils.getUsernameForAccount(account);
        String password;
        try {
+2 −2
Original line number Diff line number Diff line
@@ -122,7 +122,7 @@ public class OwnCloudClientManager {
            Account savedAccount = account.getSavedAccount();

            if (savedAccount != null) {
                final String userId = AccountUtils.getUserId(accountManager, account.getSavedAccount());
                final String userId = AccountUtils.getUpdatedUserId(accountManager, account.getSavedAccount());

                client.setUserId(userId);
            }
@@ -213,7 +213,7 @@ public class OwnCloudClientManager {

            String userId;
            if (savedAccount != null) {
                userId = AccountUtils.getUserId(accountManager, account.getSavedAccount());
                userId = AccountUtils.getUpdatedUserId(accountManager, account.getSavedAccount());
            } else {
                userId = "";
            }
+49 −20
Original line number Diff line number Diff line
@@ -55,9 +55,6 @@ public class AccountUtils {
    public static final String DAV_UPLOAD = "/remote.php/dav/uploads";
    public static final String STATUS_PATH = "/status.php";

    private static final String AT_SIGN = "@";
    private static final String EELO_ACCOUNT_END_PART = "@e.email";

    /**
     * Extracts url server from the account
     *
@@ -108,17 +105,11 @@ public class AccountUtils {
        }

        String username = account.name;

        if (!username.contains(AT_SIGN) || username.endsWith(EELO_ACCOUNT_END_PART)) {
            return username;
        if (username == null) {
            return null;
        }

        try {
            username = account.name.substring(0, account.name.lastIndexOf('@'));
        } catch (Exception e) {
            Log_OC.e(TAG, "Couldn't get a username for the given account", e);
        }
        return username;
        return username.trim();
    }

    /**
@@ -323,14 +314,52 @@ public class AccountUtils {
    }

    @Nullable
    public static String getUserId(@NonNull AccountManager accountManager, @NonNull Account account) {
        String userId = accountManager.getUserData(account, AccountUtils.Constants.KEY_USER_ID);
    public static String getUpdatedUserId(@NonNull AccountManager accountManager, @NonNull Account account) {
        final String userId = accountManager.getUserData(account, Constants.KEY_USER_ID);

        if (userId != null && !userId.isBlank()) {
            return userId.trim();
        }

        final String userName = account.name;
        if (userName == null || userName.trim().isBlank()) {
            return userName;
        }

        final String host = getHostForAccount(accountManager, account);
        final String newUserId = retrieveUserId(host, userName);

        accountManager.setUserData(account, Constants.KEY_USER_ID, newUserId);
        return newUserId;
    }

    static String retrieveUserId(@Nullable String host, @NonNull String userName) {
        userName = userName.trim();

        if (host == null) {
            return userName;
        }

        final String userNameEndPart = ("@" + host.trim()).toLowerCase();

        if (!userName.toLowerCase().endsWith(userNameEndPart)) {
            return userName;
        }

        int lengthOfUserId = userName.length() - userNameEndPart.length();  // abc@ff.com - @ff.com = abc
        return userName.substring(0, lengthOfUserId);
    }

    @Nullable
    private static String getHostForAccount(@NonNull AccountManager accountManager, @NonNull Account account) {
        final String baseurl = accountManager.getUserData(account, Constants.KEY_OC_BASE_URL);

        if (userId == null || userId.isBlank()) {
            userId = account.name;
        if (baseurl == null) {
            return null;
        }

        return userId;
        final Uri baseUri = Uri.parse(baseurl);
        return baseUri.getHost();
    }


+59 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2024
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package com.owncloud.android.lib.common.accounts;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class AccountUtilsTest {

    @Test
    public void test_retrieveUserId_with_empty_input() {
        var userName = "";
        var expected = "";
        var actual = AccountUtils.retrieveUserId(null, userName);
        assertEquals(expected, actual);
    }

    @Test
    public void test_retrieveUserId_with_input_without_url_part() {
        var userName = "username";
        var host = "murena.io";
        var expected = "username";
        var actual = AccountUtils.retrieveUserId(host, userName);
        assertEquals(expected, actual);
    }

    @Test
    public void test_retrieveUserId_with_case_sensitivity() {
        var userName = "User@E.EMAIL@MURENA.io";
        var host = "murena.io";
        var expected = "User@E.EMAIL";
        var actual = AccountUtils.retrieveUserId(host, userName);
        assertEquals(expected, actual);
    }

    @Test
    public void test_retrieveUserId_with_leading_or_trailing_spaces() {
        var userName = " user@domain.com ";
        var host = " domain.com  ";
        var expected = "user";
        var actual = AccountUtils.retrieveUserId(host, userName);
        assertEquals(expected, actual);
    }
}