Search in sources :

Example 1 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class UpdateSharePermissionsOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    // ShareType.USER | ShareType.GROUP
    OCShare share = getStorageManager().getShareById(mShareId);
    if (share == null) {
        // TODO try to get remote share before failing?
        return new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
    }
    mPath = share.getPath();
    // Update remote share with password
    UpdateRemoteShareOperation updateOp = new UpdateRemoteShareOperation(share.getRemoteId());
    updateOp.setPermissions(mPermissions);
    RemoteOperationResult result = updateOp.execute(client);
    if (result.isSuccess()) {
        RemoteOperation getShareOp = new GetRemoteShareOperation(share.getRemoteId());
        result = getShareOp.execute(client);
        if (result.isSuccess()) {
            share = (OCShare) result.getData().get(0);
            // TODO check permissions are being saved
            updateData(share);
        }
    }
    return result;
}
Also used : RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) GetRemoteShareOperation(com.owncloud.android.lib.resources.shares.GetRemoteShareOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UpdateRemoteShareOperation(com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation) OCShare(com.owncloud.android.lib.resources.shares.OCShare)

Example 2 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class AuthenticatorActivity method createAccount.

/**
     * Creates a new account through the Account Authenticator that started this activity. 
     * 
     * This makes the account permanent.
     * 
     * TODO Decide how to name the OAuth accounts
     */
private boolean createAccount(RemoteOperationResult authResult) {
    /// create and save new ownCloud account
    boolean isOAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType);
    boolean isSaml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType);
    String lastPermanentLocation = authResult.getLastPermanentLocation();
    if (lastPermanentLocation != null) {
        mServerInfo.mBaseUrl = AccountUtils.trimWebdavSuffix(lastPermanentLocation);
    }
    Uri uri = Uri.parse(mServerInfo.mBaseUrl);
    String username = mUsernameInput.getText().toString().trim();
    if (isOAuth) {
        username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong();
    }
    String accountName = com.owncloud.android.lib.common.accounts.AccountUtils.buildAccountName(uri, username);
    Account newAccount = new Account(accountName, MainApp.getAccountType());
    if (AccountUtils.exists(newAccount, 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 {
        mAccount = newAccount;
        if (isOAuth || isSaml) {
            // with external authorizations, the password is never input in the app
            mAccountMgr.addAccountExplicitly(mAccount, "", null);
        } else {
            mAccountMgr.addAccountExplicitly(mAccount, mPasswordInput.getText().toString(), null);
        }
        // include account version with the new account
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_ACCOUNT_VERSION, Integer.toString(AccountUtils.ACCOUNT_VERSION));
        /// 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, MainApp.getAccountType());
        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
        intent.putExtra(AccountManager.KEY_USERDATA, username);
        if (isOAuth || isSaml) {
            mAccountMgr.setAuthToken(mAccount, mAuthTokenType, 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, Constants.KEY_OC_VERSION, mServerInfo.mVersion.getVersion());
        mAccountMgr.setUserData(mAccount, Constants.KEY_OC_BASE_URL, mServerInfo.mBaseUrl);
        if (authResult.getData() != null) {
            try {
                UserInfo userInfo = (UserInfo) authResult.getData().get(0);
                mAccountMgr.setUserData(mAccount, Constants.KEY_DISPLAY_NAME, userInfo.mDisplayName);
            } catch (ClassCastException c) {
                Log_OC.w(TAG, "Couldn't get display name for " + username);
            }
        } else {
            Log_OC.w(TAG, "Couldn't get display name for " + username);
        }
        if (isSaml) {
            mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_SAML_WEB_SSO, "TRUE");
        } else if (isOAuth) {
            mAccountMgr.setUserData(mAccount, Constants.KEY_SUPPORTS_OAUTH2, "TRUE");
        }
        setAccountAuthenticatorResult(intent.getExtras());
        setResult(RESULT_OK, intent);
        return true;
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) SharedPreferences(android.content.SharedPreferences) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) Intent(android.content.Intent) UserInfo(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo) Uri(android.net.Uri)

Example 3 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class AuthenticatorAsyncTask method doInBackground.

@Override
protected RemoteOperationResult doInBackground(Object... params) {
    RemoteOperationResult result;
    if (params != null && params.length == 2) {
        String url = (String) params[0];
        OwnCloudCredentials credentials = (OwnCloudCredentials) params[1];
        // Client
        Uri uri = Uri.parse(url);
        OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
        client.setCredentials(credentials);
        // Operation - try credentials
        ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(REMOTE_PATH, mContext, SUCCESS_IF_ABSENT);
        result = operation.execute(client);
        String targetUrlAfterPermanentRedirection = null;
        if (operation.wasRedirected()) {
            RedirectionPath redirectionPath = operation.getRedirectionPath();
            targetUrlAfterPermanentRedirection = redirectionPath.getLastPermanentLocation();
        }
        // Operation - get display name
        if (result.isSuccess()) {
            GetRemoteUserInfoOperation remoteUserNameOperation = new GetRemoteUserInfoOperation();
            if (targetUrlAfterPermanentRedirection != null) {
                // we can't assume that any subpath of the domain is correctly redirected; ugly stuff
                client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(AccountUtils.trimWebdavSuffix(targetUrlAfterPermanentRedirection)), mContext, true);
                client.setCredentials(credentials);
            }
            result = remoteUserNameOperation.execute(client);
        }
        // let the caller knows what is real URL that should be accessed for the account
        // being authenticated if the initial URL is being redirected permanently (HTTP code 301)
        result.setLastPermanentLocation(targetUrlAfterPermanentRedirection);
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return result;
}
Also used : RedirectionPath(com.owncloud.android.lib.common.network.RedirectionPath) GetRemoteUserInfoOperation(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Uri(android.net.Uri) OwnCloudCredentials(com.owncloud.android.lib.common.OwnCloudCredentials)

Example 4 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class CreateShareViaLinkOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    // Check if the share link already exists
    RemoteOperation operation = new GetRemoteSharesForFileOperation(mPath, false, false);
    RemoteOperationResult result = operation.execute(client);
    // Create public link if doesn't exist yet
    boolean publicShareExists = false;
    if (result.isSuccess()) {
        OCShare share = null;
        for (int i = 0; i < result.getData().size(); i++) {
            share = (OCShare) result.getData().get(i);
            if (ShareType.PUBLIC_LINK.equals(share.getShareType())) {
                publicShareExists = true;
                break;
            }
        }
    }
    if (!publicShareExists) {
        CreateRemoteShareOperation createOp = new CreateRemoteShareOperation(mPath, ShareType.PUBLIC_LINK, "", false, mPassword, OCShare.DEFAULT_PERMISSION);
        createOp.setGetShareDetails(true);
        result = createOp.execute(client);
    }
    if (result.isSuccess()) {
        if (result.getData().size() > 0) {
            Object item = result.getData().get(0);
            if (item instanceof OCShare) {
                updateData((OCShare) item);
            } else {
                ArrayList<Object> data = result.getData();
                result = new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
                result.setData(data);
            }
        } else {
            result = new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
        }
    }
    return result;
}
Also used : CreateRemoteShareOperation(com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OCShare(com.owncloud.android.lib.resources.shares.OCShare) GetRemoteSharesForFileOperation(com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation)

Example 5 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class GetServerInfoOperation method run.

/**
     * Performs the operation
     * 
     * @return      Result of the operation. If successful, includes an instance of 
     *              {@link ServerInfo} with the information retrieved from the server. 
     *              Call {@link RemoteOperationResult#getData()}.get(0) to get it.
     */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    // first: check the status of the server (including its version)
    GetRemoteStatusOperation getStatus = new GetRemoteStatusOperation(mContext);
    RemoteOperationResult result = getStatus.execute(client);
    if (result.isSuccess()) {
        // second: get authentication method required by the server
        mResultData.mVersion = (OwnCloudVersion) (result.getData().get(0));
        mResultData.mIsSslConn = (result.getCode() == ResultCode.OK_SSL);
        mResultData.mBaseUrl = normalizeProtocolPrefix(mUrl, mResultData.mIsSslConn);
        RemoteOperationResult detectAuthResult = detectAuthorizationMethod(client);
        // third: merge results
        if (detectAuthResult.isSuccess()) {
            mResultData.mAuthMethod = (AuthenticationMethod) detectAuthResult.getData().get(0);
            ArrayList<Object> data = new ArrayList<Object>();
            data.add(mResultData);
            result.setData(data);
        } else {
            result = detectAuthResult;
        }
    }
    return result;
}
Also used : GetRemoteStatusOperation(com.owncloud.android.lib.resources.status.GetRemoteStatusOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArrayList(java.util.ArrayList)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)172 OCFile (com.owncloud.android.datamodel.OCFile)48 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)24 ArrayList (java.util.ArrayList)24 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)22 IOException (java.io.IOException)22 Account (android.accounts.Account)19 User (com.nextcloud.client.account.User)18 OCShare (com.owncloud.android.lib.resources.shares.OCShare)18 File (java.io.File)18 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)17 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)16 Context (android.content.Context)15 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)12 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)12 FileNotFoundException (java.io.FileNotFoundException)12 Intent (android.content.Intent)11 JSONObject (org.json.JSONObject)11 Test (org.junit.Test)11 Uri (android.net.Uri)10