Skip to content

Commit efbbf58

Browse files
Merge pull request #3043 from nextcloud/fix/share-permission
fix(share): permission
2 parents 0ed2602 + 31eec7c commit efbbf58

36 files changed

Lines changed: 879 additions & 760 deletions

app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,21 @@
5050
import it.niedermann.owncloud.notes.shared.model.Capabilities;
5151

5252
@Database(
53-
entities = {
54-
Account.class,
55-
Note.class,
56-
CategoryOptions.class,
57-
SingleNoteWidgetData.class,
58-
NotesListWidgetData.class,
59-
ShareEntity.class,
60-
Capabilities.class
61-
}, version = 28,
62-
autoMigrations = {
63-
@AutoMigration(from = 25, to = 26),
64-
@AutoMigration(from = 26, to = 27),
65-
@AutoMigration(from = 27, to = 28),
66-
}
53+
entities = {
54+
Account.class,
55+
Note.class,
56+
CategoryOptions.class,
57+
SingleNoteWidgetData.class,
58+
NotesListWidgetData.class,
59+
ShareEntity.class,
60+
Capabilities.class
61+
}, version = 29,
62+
autoMigrations = {
63+
@AutoMigration(from = 25, to = 26),
64+
@AutoMigration(from = 26, to = 27),
65+
@AutoMigration(from = 27, to = 28),
66+
@AutoMigration(from = 28, to = 29),
67+
}
6768
)
6869
@TypeConverters({Converters.class})
6970
public abstract class NotesDatabase extends RoomDatabase {

app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.nextcloud.android.sso.helper.SingleAccountHelper;
5050
import com.nextcloud.android.sso.model.SingleSignOnAccount;
5151
import com.owncloud.android.lib.common.utils.Log_OC;
52+
import com.owncloud.android.lib.resources.shares.OCShare;
5253

5354
import java.util.ArrayList;
5455
import java.util.Calendar;
@@ -1006,8 +1007,16 @@ public void addShareEntities(List<ShareEntity> entities) {
10061007
db.getShareDao().addShareEntities(entities);
10071008
}
10081009

1009-
public List<ShareEntity> getShareEntities(String path) {
1010-
return db.getShareDao().getShareEntities(path);
1010+
public ShareEntity getShareByPathAndDisplayName(@NonNull OCShare share) {
1011+
if (share.getPath() == null || share.getSharedWithDisplayName() == null) {
1012+
return null;
1013+
}
1014+
1015+
return db.getShareDao().getShareByPathAndDisplayName(share.getPath(), share.getSharedWithDisplayName());
1016+
}
1017+
1018+
public void deleteShareById(int id) {
1019+
db.getShareDao().deleteById(id);
10111020
}
10121021

10131022
public void updateNote(Note note) {

app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/ShareDao.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package it.niedermann.owncloud.notes.persistence.dao
88

99
import androidx.room.Dao
10+
import androidx.room.Delete
1011
import androidx.room.Insert
1112
import androidx.room.OnConflictStrategy
1213
import androidx.room.Query
@@ -17,6 +18,17 @@ interface ShareDao {
1718
@Insert(onConflict = OnConflictStrategy.REPLACE)
1819
fun addShareEntities(entities: List<ShareEntity>)
1920

20-
@Query("SELECT * FROM share_table WHERE path = :path")
21-
fun getShareEntities(path: String): List<ShareEntity>
21+
@Query("""
22+
SELECT * FROM share_table
23+
WHERE path = :path
24+
AND share_with_displayname = :displayName
25+
LIMIT 1
26+
""")
27+
fun getShareByPathAndDisplayName(
28+
path: String,
29+
displayName: String
30+
): ShareEntity?
31+
32+
@Query("DELETE FROM share_table WHERE id = :id")
33+
fun deleteById(id: Int)
2234
}

app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/ShareEntity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ data class ShareEntity(
2525
val uid_owner: String? = null,
2626
val displayname_owner: String? = null,
2727
val url: String? = null,
28-
val expiration_date: Long? = null
28+
val expiration_date: Long? = null,
29+
val permissions: Double? = null,
30+
val attributes: String? = null
2931
)

app/src/main/java/it/niedermann/owncloud/notes/persistence/sync/ShareAPI.kt

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import retrofit2.http.Query
2424

2525
interface ShareAPI {
2626
@GET("sharees")
27-
fun getSharees(
27+
fun fetchSharees(
2828
@Query("format") format: String = "json",
2929
@Query("itemType") itemType: String = "file",
3030
@Query("search") search: String,
@@ -33,14 +33,8 @@ interface ShareAPI {
3333
@Query("lookup") lookup: String = "false",
3434
): LinkedTreeMap<String, Any?>?
3535

36-
@GET("shares/{remoteId}?format=json")
37-
fun getShares(
38-
@Path("remoteId") remoteId: Long,
39-
@Query("include_tags") includeTags: Boolean = true,
40-
): Call<OcsResponse<List<CreateShareResponse>>>
41-
4236
@GET("shares/?format=json")
43-
fun getSharesForSpecificNote(
37+
fun fetchSharesForSpecificNote(
4438
@Query("path") path: String,
4539
@Query("reshares") reshares: Boolean = true,
4640
@Query("subfiles") subfiles: Boolean = true
@@ -52,9 +46,6 @@ interface ShareAPI {
5246
@POST("shares?format=json")
5347
fun addShare(@Body request: CreateShareRequest): Call<OcsResponse<CreateShareResponse>>
5448

55-
@POST("shares/{shareId}/send-email?format=json")
56-
fun sendEmail(@Path("shareId") shareId: Long, @Body password: SharePasswordRequest?): Call<Any>
57-
5849
@PUT("shares/{shareId}?format=json")
5950
fun updateShare(@Path("shareId") shareId: Long, @Body request: UpdateShareRequest): Call<OcsResponse<CreateShareResponse>>
6051

@@ -65,8 +56,8 @@ interface ShareAPI {
6556
): Call<OcsResponse<CreateShareResponse>>
6657

6758
@GET("shares/?format=json")
68-
fun getShareFromNote(
59+
fun fetchSharesFromNote(
6960
@Query("path") path: String,
70-
@Query("shared_with_me") sharedWithMe: Boolean = true
61+
@Query("shared_with_me") sharedWithMe: Boolean = false
7162
): Call<OcsResponse<List<CreateShareResponse>>>
7263
}

app/src/main/java/it/niedermann/owncloud/notes/share/NoteShareActivity.java

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import it.niedermann.owncloud.notes.share.listener.ShareeListAdapterListener;
7777
import it.niedermann.owncloud.notes.share.model.CreateShareResponse;
7878
import it.niedermann.owncloud.notes.share.model.CreateShareResponseExtensionsKt;
79+
import it.niedermann.owncloud.notes.share.model.UpdateShareRequest;
7980
import it.niedermann.owncloud.notes.share.model.UsersAndGroupsSearchConfig;
8081
import it.niedermann.owncloud.notes.share.repository.ShareRepository;
8182
import it.niedermann.owncloud.notes.shared.model.Capabilities;
@@ -84,6 +85,7 @@
8485
import it.niedermann.owncloud.notes.shared.util.ShareUtil;
8586
import it.niedermann.owncloud.notes.shared.util.clipboard.ClipboardUtil;
8687
import it.niedermann.owncloud.notes.shared.util.extensions.BundleExtensionsKt;
88+
import it.niedermann.owncloud.notes.util.DateUtil;
8789

8890
public class NoteShareActivity extends BrandedActivity implements ShareeListAdapterListener, NoteShareItemAction, QuickSharingPermissionsBottomSheetDialog.QuickPermissionSharingBottomSheetActions, SharePasswordDialogFragment.SharePasswordDialogListener {
8991

@@ -161,7 +163,7 @@ private void initializeArguments() {
161163
final var ssoAcc = SingleAccountHelper.getCurrentSingleSignOnAccount(NoteShareActivity.this);
162164
repository = new ShareRepository(NoteShareActivity.this, ssoAcc);
163165
capabilities = repository.getCapabilities();
164-
repository.getSharesForNotesAndSaveShareEntities();
166+
repository.fetchSharesForNotesAndSaveShareEntities();
165167

166168
runOnUiThread(() -> {
167169
binding.fileName.setText(note.getTitle());
@@ -396,7 +398,7 @@ public void createPublicShareLink() {
396398
requestPasswordForShareViaLink(true, capabilities.getAskForOptionalPassword());
397399
} else {
398400
executorService.submit(() -> {
399-
final var result = repository.addShare(note, ShareType.PUBLIC_LINK, "", "false", "", 0, "");
401+
final var result = repository.addShare(note, ShareType.PUBLIC_LINK, "");
400402
runOnUiThread(() -> {
401403
if (result instanceof ApiResult.Success<OcsResponse<CreateShareResponse>> successResponse && binding.sharesList.getAdapter() instanceof ShareeListAdapter adapter) {
402404
DisplayUtils.showSnackMessage(NoteShareActivity.this, successResponse.getMessage());
@@ -519,17 +521,16 @@ public void updateShareeListAdapter() {
519521
return;
520522
}
521523

522-
List<OCShare> tempShares = new ArrayList<>();
523-
524-
// to show share with users/groups info
524+
List<OCShare> remoteNotes;
525525
if (note != null) {
526-
// get shares from local DB
527-
populateSharesList(tempShares);
526+
remoteNotes = repository.fetchSharesFromNote(note);
527+
} else {
528+
remoteNotes = new ArrayList<>();
528529
}
529530

530531
runOnUiThread(() -> {
531532
shares.clear();
532-
shares.addAll(tempShares);
533+
shares.addAll(remoteNotes);
533534

534535
adapter.removeAll();
535536
adapter.addShares(shares);
@@ -544,35 +545,6 @@ public void updateShareeListAdapter() {
544545
});
545546
}
546547

547-
private void populateSharesList(List<OCShare> targetList) {
548-
// Get shares from local DB
549-
final var shareEntities = repository.getShareEntitiesForSpecificNote(note);
550-
for (var entity : shareEntities) {
551-
if (entity.getId() != null) {
552-
addSharesToList(entity.getId(), targetList);
553-
}
554-
}
555-
556-
// Get shares from remote
557-
final var remoteShares = repository.getShareFromNote(note);
558-
if (remoteShares != null) {
559-
for (var entity : remoteShares) {
560-
addSharesToList(entity.getId(), targetList);
561-
}
562-
}
563-
}
564-
565-
private void addSharesToList(long id, List<OCShare> targetList) {
566-
final var result = repository.getShares(id);
567-
if (result != null) {
568-
for (OCShare ocShare : result) {
569-
if (!targetList.contains(ocShare)) {
570-
targetList.add(ocShare);
571-
}
572-
}
573-
}
574-
}
575-
576548
private void addPublicShares(ShareeListAdapter adapter) {
577549
List<OCShare> publicShares = new ArrayList<>();
578550

@@ -672,13 +644,13 @@ public void search(String query) {
672644

673645
@Override
674646
public void advancedPermissions(OCShare share) {
675-
modifyExistingShare(share, NoteShareDetailActivity.SCREEN_TYPE_PERMISSION);
647+
modifyExistingShare(share, NoteShareDetailActivity.SCREEN_TYPE_PERMISSION, false);
676648
}
677649

678650

679651
@Override
680652
public void sendNewEmail(OCShare share) {
681-
modifyExistingShare(share, NoteShareDetailActivity.SCREEN_TYPE_NOTE);
653+
modifyExistingShare(share, NoteShareDetailActivity.SCREEN_TYPE_NOTE, true);
682654
}
683655

684656
@Override
@@ -711,13 +683,14 @@ public void addAnotherLink(OCShare share) {
711683
createPublicShareLink();
712684
}
713685

714-
private void modifyExistingShare(OCShare share, int screenTypePermission) {
686+
private void modifyExistingShare(OCShare share, int screenTypePermission, boolean sendEmail) {
715687
Bundle bundle = new Bundle();
716688

717689
bundle.putSerializable(NoteShareDetailActivity.ARG_OCSHARE, share);
718690
bundle.putInt(NoteShareDetailActivity.ARG_SCREEN_TYPE, screenTypePermission);
719691
bundle.putBoolean(NoteShareDetailActivity.ARG_RESHARE_SHOWN, !isReshareForbidden(share));
720692
bundle.putBoolean(NoteShareDetailActivity.ARG_EXP_DATE_SHOWN, getExpDateShown());
693+
bundle.putBoolean(NoteShareDetailActivity.ARG_SEND_EMAIL, sendEmail);
721694

722695
Intent intent = new Intent(this, NoteShareDetailActivity.class);
723696
intent.putExtras(bundle);
@@ -759,12 +732,17 @@ public void onQuickPermissionChanged(OCShare share, int permission) {
759732
}
760733

761734
private void updateShare(OCShare share) {
735+
if (note == null) {
736+
Log_OC.e(TAG, "note is null, cannot update share");
737+
return;
738+
}
739+
762740
executorService.submit(() -> {
763741
try {
764-
final var updatedShares = repository.getShares(share.getId());
742+
final var updatedShares = repository.fetchSharesFromNote(note);
765743

766744
runOnUiThread(() -> {
767-
if (updatedShares != null && binding.sharesList.getAdapter() instanceof ShareeListAdapter adapter) {
745+
if (binding.sharesList.getAdapter() instanceof ShareeListAdapter adapter) {
768746
OCShare updatedShare = null;
769747
for (int i=0;i<updatedShares.size();i++) {
770748
if (updatedShares.get(i).getId() == share.getId()) {
@@ -857,8 +835,7 @@ public void shareFileViaPublicShare(@Nullable Note note, @Nullable String passwo
857835
"",
858836
"false",
859837
password,
860-
repository.getCapabilities().getDefaultPermission(),
861-
""
838+
repository.getCapabilities().getDefaultPermission()
862839
);
863840

864841
runOnUiThread(() -> {
@@ -881,14 +858,22 @@ public void setPasswordToShare(@NotNull OCShare share, @Nullable String password
881858

882859
executorService.submit(() -> {
883860
{
884-
final var requestBody = repository.getUpdateShareRequest(
885-
false,
886-
share,
887-
"",
888-
password,
889-
false,
890-
-1,
891-
share.getPermissions()
861+
boolean isDownloadAndAllowsSyncEnabled = repository.isAllowDownloadAndSync(share);
862+
863+
String attributes = UpdateShareRequest.Companion.createAttributes(
864+
repository.getCapabilities(),
865+
isDownloadAndAllowsSyncEnabled,
866+
share.getShareType()
867+
);
868+
869+
final var requestBody = new UpdateShareRequest(
870+
share.getPermissions(),
871+
password,
872+
share.getNote(),
873+
share.getLabel(),
874+
DateUtil.INSTANCE.getExpirationDate(share.getExpirationDate()),
875+
Boolean.toString(share.isHideFileDownload()),
876+
attributes
892877
);
893878
final var result = repository.updateShare(share.getId(), requestBody);
894879

0 commit comments

Comments
 (0)