嗨,尊敬的程序员!我正在制作一个语音记录应用程序项目(具有云存储功能),在此应用程序中,音频被保存在手机存储中的文件夹“ Zrecorder”(我的应用程序名称)中,并且在其中创建了一个名为用户名的电子邮件(例如[email protected]),然后音频以mp3格式位于文件夹内。当用户备份数据时,仅当用户已经登录时,文件夹才会上载到Firebase存储。现在,在Firebase存储中,以相同的方式创建了与在电话存储中创建的文件夹相同的文件夹。因此,当用户还原数据时,首先获取用户名,然后在Firebase存储中找到userName文件夹,然后在电话存储中下载该文件夹(无法下载文件夹,因此我使用循环获取每个文件的链接并将其下载到通过下载管理器类指定的特定文件夹)。我希望你能理解我的意思。
所以这是我的问题:我希望每个用户只能访问自己的文件夹。我在firebase文档上看到了这一点,但是我不正确地理解了代码。他们说使用'uid'来做,但是我不明白。
这是用户登录到我的应用程序的方式。
public void loginUser(String email, String password) {
auth.signInWithEmailAndPassword(email, password)
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// For Saving the LoginState if previously logged in then no need again
SharedPreferences sharedPreferences = getSharedPreferences("LogSharedPref",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("LoggedInStatus",true);
editor.putString("UserEmail",textEmail.toLowerCase());
editor.apply();
Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, PermissionActivity.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(LoginActivity.this, "Invalid Details", Toast.LENGTH_SHORT).show();
login.setEnabled(true);
}
});
}
您可以看到我保存了用户名的共享首选项,以便每次用户还原数据时,我们都从sharedPref获取用户名。
这是我将数据上传到Firebase的方式。
/* Here we pass the userName into this method
and we get the files downloaded in the phone storage into the same UserName folder.*/
void getLinksText(String userName) {
storageReferenceDown = FirebaseStorage.getInstance().getReference();
StorageReference downloadRef = storageReferenceDown.child(userName);
// Here i am getting the list of items inside userName folder in firebase storage
downloadRef.listAll()
.addOnSuccessListener(new OnSuccessListener<ListResult>() {
@Override
public void onSuccess(ListResult listResult) {
for (final StorageReference item : listResult.getItems()) {
item.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onSuccess(Uri uri) {
String downloadUrl = uri.toString();
String name = item.getName();
startDownloading(downloadUrl, userName);
}
});
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Uh-oh, an error occurred!
}
});
}
所以你能告诉我如何让用户仅访问他自己的文件夹,即用户名(例如[email protected])吗?我是从Google Firebase文档中获得的。
Content-owner only access
These rules restrict access to the authenticated owner of the content only. The data is only readable and writable by one user, and the data path contains the user's ID.
When this rule works: This rule works well if data is siloed by user — if the only user that needs to access the data is the same user that created the data.
When this rule doesn't work: This ruleset doesn't work when multiple users need to write or read the same data — users will overwrite data or be unable to access data they've created.
To set up this rule: Create a rule that confirms the user requesting access to read or write data is the user that owns that data.
// Grants a user access to a node matching their user ID
service firebase.storage {
match /b/{bucket}/o {
// Files look like: "user/<UID>/path/to/file.txt"
match /user/{userId}/{allPaths=**} {
allow read, write: if request.auth.uid == userId;
}
}
}
我当前在Firebase存储中设置的规则
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
您能帮我获取Java代码,因为我不知道如何获取uid,然后将其设置为特定用户的特定文件夹。
[请帮助我,我已经搜索了youtube,stackoverflow,stackExchange,google docs,quora,但是我没有找到针对我问题的具体答案。我正在做一个项目,请帮助我,我真的需要这个!
另一件事-我正在使用Firebase存储,而不是云Firestore。
service firebase.storage {
match /b/{bucket}/o {
// Files look like: "Zrecorder/<email>/path/to/file.txt"
match /Zrecorder/{email}/{allPaths=**} {
allow read, write: if request.auth.token.email == email;
}
}
}
另请参阅:
[C0上的Firebase文档