UpdateSharePermissionsOperation.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2015 ownCloud Inc.
  7. * Copyright (C) 2018 Andy Scherzinger
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.operations;
  22. import android.text.TextUtils;
  23. import com.owncloud.android.lib.common.OwnCloudClient;
  24. import com.owncloud.android.lib.common.operations.RemoteOperation;
  25. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  26. import com.owncloud.android.lib.resources.files.FileUtils;
  27. import com.owncloud.android.lib.resources.shares.GetRemoteShareOperation;
  28. import com.owncloud.android.lib.resources.shares.OCShare;
  29. import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation;
  30. import com.owncloud.android.operations.common.SyncOperation;
  31. /**
  32. * Updates an existing private share for a given file.
  33. */
  34. public class UpdateSharePermissionsOperation extends SyncOperation {
  35. private long mShareId;
  36. private int mPermissions;
  37. private long mExpirationDateInMillis;
  38. private String mPassword;
  39. private String mPath;
  40. /**
  41. * Constructor
  42. *
  43. * @param shareId Private {@link OCShare} to update. Mandatory argument
  44. */
  45. public UpdateSharePermissionsOperation(long shareId) {
  46. mShareId = shareId;
  47. mPermissions = -1;
  48. mExpirationDateInMillis = 0L;
  49. mPassword = null;
  50. }
  51. /**
  52. * Set password to update in private share.
  53. *
  54. * @param password Password to set to the private share.
  55. * Empty string clears the current password.
  56. * Null results in no update applied to the password.
  57. */
  58. public void setPassword(String password) {
  59. mPassword = password;
  60. }
  61. /**
  62. * Set permissions to update in private share.
  63. *
  64. * @param permissions Permissions to set to the private share.
  65. * Values <= 0 result in no update applied to the permissions.
  66. */
  67. public void setPermissions(int permissions) {
  68. mPermissions = permissions;
  69. }
  70. /**
  71. * Set expiration date to update private share.
  72. *
  73. * @param expirationDateInMillis Expiration date to set to the public link.
  74. * A negative value clears the current expiration date.
  75. * Zero value (start-of-epoch) results in no update done on
  76. * the expiration date.
  77. */
  78. public void setExpirationDate(long expirationDateInMillis) {
  79. mExpirationDateInMillis = expirationDateInMillis;
  80. }
  81. @Override
  82. protected RemoteOperationResult run(OwnCloudClient client) {
  83. OCShare share = getStorageManager().getShareById(mShareId); // ShareType.USER | ShareType.GROUP
  84. if (share == null) {
  85. // TODO try to get remote share before failing?
  86. return new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
  87. }
  88. mPath = share.getPath();
  89. // Update remote share with password
  90. UpdateRemoteShareOperation updateOp = new UpdateRemoteShareOperation(share.getRemoteId());
  91. updateOp.setPassword(mPassword);
  92. updateOp.setPermissions(mPermissions);
  93. updateOp.setExpirationDate(mExpirationDateInMillis);
  94. RemoteOperationResult result = updateOp.execute(client);
  95. if (result.isSuccess()) {
  96. RemoteOperation getShareOp = new GetRemoteShareOperation(share.getRemoteId());
  97. result = getShareOp.execute(client);
  98. if (result.isSuccess()) {
  99. share = (OCShare) result.getData().get(0);
  100. // TODO check permissions are being saved
  101. updateData(share);
  102. }
  103. }
  104. return result;
  105. }
  106. public String getPath() {
  107. return mPath;
  108. }
  109. public String getPassword() {
  110. return mPassword;
  111. }
  112. private void updateData(OCShare share) {
  113. // Update DB with the response
  114. share.setPath(mPath); // TODO - check if may be moved to UpdateRemoteShareOperation
  115. if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
  116. share.setIsFolder(true);
  117. } else {
  118. share.setIsFolder(false);
  119. }
  120. share.setIsPasswordProtected(!TextUtils.isEmpty(mPassword));
  121. getStorageManager().saveShare(share);
  122. }
  123. }