appwrite / sdk-for-flutter

[READ-ONLY] Official Appwrite Flutter SDK 💙
https://appwrite.io
BSD 3-Clause "New" or "Revised" License
369 stars 111 forks source link

🚀 Feature: Is it possible to add permission values to document objects? #218

Open xinlake opened 1 month ago

xinlake commented 1 month ago

🔖 Feature description

There is a page to view the content of the document, which displays edit buttons if the logged-in user is the content creator (Figure 1) and does not display edit buttons if he or she is not the content owner (Figure 2)

Image Image

So the client app needs to know the permissions of the document, and I see that there is a $permission property in the document object to indicate the document's permissions (Figure 3)

Image

Currently I'm parsing the $permission string to learn the document permissions (the role and id), However, I think it might be better to make this feature in the sdk.

I think it would be useful if the document object's permission provided the role and id, like:

document.permission[0].user.id //String
document.permission[0].user.role //Enum 

🎤 Pitch

This is the code that I use to find the id of the user who has the permission to delete documents. Just showing ideas, not coding suggestions.

...
final deletePattern = RegExp(r'delete\((.*?)\)');
final userPattern = RegExp(r'(?<=\("\s*)(.*?)(?=\s*"\))');

String? deletionUserId;

if (document.$permissions.singleWhere(
  (permission) {
    if ((permission as String?) case final permissionString?) {
      return deletePattern.hasMatch(permissionString);
    } else {
      return false;
    }
  },
)
    case final String? deletePermission when deletePermission != null) {
  final user = userPattern.firstMatch(deletePermission)?.group(0)?.split(":");
  if (user != null && user.length == 2) {
    if (user[0] == "user") {
      deletionUserId = user[1];
    }
  }
}

ownerUserId = deletionUserId;
...

👀 Have you spent some time to check if this issue has been raised before?

🏢 Have you read the Code of Conduct?