From 249cb901ebd392373d0ef812b8f0a2489705c0ea Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Wed, 16 Dec 2015 16:32:48 +0100 Subject: [PATCH] Added support to update share permissions --- .../shares/SharePermissionsBuilder.java | 106 ++++++++++++++++++ .../shares/UpdateRemoteShareOperation.java | 23 +++- 2 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 src/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.java diff --git a/src/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.java b/src/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.java new file mode 100644 index 00000000..85fcf7ff --- /dev/null +++ b/src/com/owncloud/android/lib/resources/shares/SharePermissionsBuilder.java @@ -0,0 +1,106 @@ +/* ownCloud Android Library is available under MIT license + * @author David A. Velasco + * Copyright (C) 2015 ownCloud Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.resources.shares; + + +/** + * Provides method to define a set of share permissions and calculate the appropiate + * int value representing it. + */ +public class SharePermissionsBuilder { + + /** Set of permissions */ + private int mPermissions = OCShare.READ_PERMISSION_FLAG; // READ is minimum permission + + /** + * Sets or clears permission to reshare a file or folder. + * + * @param enabled 'True' to set, 'false' to clear. + * @return Instance to builder itself, to allow consecutive calls to setters + */ + public SharePermissionsBuilder setSharePermission(boolean enabled) { + updatePermission(OCShare.SHARE_PERMISSION_FLAG, enabled); + return this; + } + + /** + * Sets or clears permission to update a folder or folder. + * + * @param enabled 'True' to set, 'false' to clear. + * @return Instance to builder itself, to allow consecutive calls to setters + */ + public SharePermissionsBuilder setUpdatePermission(boolean enabled) { + updatePermission(OCShare.UPDATE_PERMISSION_FLAG, enabled); + return this; + } + + /** + * Sets or clears permission to create files in share folder. + * + * @param enabled 'True' to set, 'false' to clear. + * @return Instance to builder itself, to allow consecutive calls to setters + */ + public SharePermissionsBuilder setCreatePermission(boolean enabled) { + updatePermission(OCShare.CREATE_PERMISSION_FLAG, enabled); + return this; + } + + /** + * Sets or clears permission to delete files in a shared folder. + * + * @param enabled 'True' to set, 'false' to clear. + * @return Instance to builder itself, to allow consecutive calls to setters + */ + public SharePermissionsBuilder setDeletePermission(boolean enabled) { + updatePermission(OCShare.DELETE_PERMISSION_FLAG, enabled); + return this; + } + + /** + * Common code to update the value of the set of permissions. + * + * @param permissionsFlag Flag for the permission to update. + * @param enable 'True' to set, 'false' to clear. + */ + private void updatePermission(int permissionsFlag, boolean enable) { + if (enable) { + // add permission + mPermissions |= permissionsFlag; + } else { + // delete permission + mPermissions &= ~permissionsFlag; + } + } + + /** + * 'Builds' the int value for the accumulated set of permissions. + * + * @return An int value representing the accumulated set of permissions. + */ + public int build() { + return mPermissions; + } +} diff --git a/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java index aa6ae537..00178ed8 100644 --- a/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java @@ -56,6 +56,7 @@ public class UpdateRemoteShareOperation extends RemoteOperation { private static final String PARAM_PASSWORD = "password"; private static final String PARAM_EXPIRATION_DATE = "expireDate"; + private static final String PARAM_PERMISSIONS = "permissions"; private static final String FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"; private static final String ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded"; private static final String ENTITY_CHARSET = "UTF-8"; @@ -70,6 +71,9 @@ public class UpdateRemoteShareOperation extends RemoteOperation { /** Expiration date to set for the public link */ private long mExpirationDateInMillis; + /** Access permissions for the file bound to the share */ + private int mPermissions; + /** * Constructor. No update is initialized by default, need to be applied with setters below. @@ -98,7 +102,7 @@ public class UpdateRemoteShareOperation extends RemoteOperation { /** * Set expiration date to update in Share resource. * - * @param expirationDateInMillis Expiration date to set to the public link. + * @param expirationDateInMillis Expiration date to set to the target share. * A negative value clears the current expiration date. * Zero value (start-of-epoch) results in no update done on * the expiration date. @@ -108,6 +112,16 @@ public class UpdateRemoteShareOperation extends RemoteOperation { } + /** + * Set permissions to update in Share resource. + * + * @param permissions Permissions date to set to the target share. + * Values <= 0 result in no update applied to the permissions. + */ + public void setPermissions(int permissions) { + mPermissions = permissions; + } + @Override protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result = null; @@ -131,11 +145,12 @@ public class UpdateRemoteShareOperation extends RemoteOperation { parametersToUpdate.add(new Pair(PARAM_EXPIRATION_DATE, formattedExpirationDate)); } // else, ignore - no update + if (mPermissions > 0) { + // set permissions + parametersToUpdate.add(new Pair(PARAM_PERMISSIONS, Integer.toString(mPermissions))); + } /* TODO complete rest of parameters - if (mPermissions > 0) { - parametersToUpdate.add(new Pair("permissions", Integer.toString(mPermissions))); - } if (mPublicUpload != null) { parametersToUpdate.add(new Pair("publicUpload", mPublicUpload.toString()); }