Это моя структура базы данных:
Я хочу, чтобы значения под узлом пользователя 1 были доступны только для чтения и записи для пользователя 1, а значения под узлом пользователя 2 только для чтения и записи для пользователя 2 и так далее для других новых пользователей в будущем. Это означает, что пользователь 1 не может читать и записывать значения в узлах других пользователей, и наоборот. Для этого мне нужно было установить правило для моей базы данных.
Правило ниже - это то, что я пробовал. Пока что пользователь 1 может записывать значения только под своим собственным узлом, а не под узлами других пользователей, и наоборот, что именно то, что я хочу. Проблема в том, что пользователь 1 все еще может читать значения в узлах других пользователей, что нежелательно. Я прочитал некоторые документы, объясняющие правила базы данных, но это очень трудно для меня понять. Я пробовал несколько способов переписать мое правило, но все равно не получилось. Мне нужна помощь, чтобы это исправить.
{
"rules": {
".read": "auth.uid != null",
".write":"auth.uid != null",
"Users' Input History": {
"$user": {
".validate":"$user === auth.uid"
}
},
"Users' Vocabulary List": {
"$user": {
".validate":"$user === auth.uid"
}
}
}
}
Для записи я использовал симулятор правил, как на следующих изображениях. Я включил опцию «проверено», выбрал «Google» в качестве поставщика, установил имитированный UID как «Kad06bqeNChhjaksxgP9cVtoFMh1» (который является пользователем 1) и нажал кнопку «Выполнить». В результате были разрешены как «чтение», так и «установка» в строках 3 и 4.
Ниже приведен мой код для аутентификации пользователя и отправки значений в базу данных:
GoogleSignInActivity:
public class GoogleSignInActivity extends BaseActivity implements
View.OnClickListener {
private static final String TAG = "GoogleActivity";
private static final int RC_SIGN_IN = 9001;
// [START declare_auth]
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
// [END declare_auth]
private GoogleSignInClient mGoogleSignInClient;
private TextView mStatusTextView; // For displaying user's email
private TextView mDetailTextView; // For displaying user's UID
private TextView mScreenNameTextView; // For displaying user's display name (user.getDisplayName())
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_sign_in);
// Views
mStatusTextView = findViewById(R.id.status);
mDetailTextView = findViewById(R.id.detail);
mScreenNameTextView = findViewById(R.id.screen_name_textView);
setProgressBar(R.id.progressBar);
// Button listeners
findViewById(R.id.signInButton).setOnClickListener(this);
// [START config_signin]
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
// [END config_signin]
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// [START initialize_auth]
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// [END initialize_auth]
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
toastMessage("Successfully signed in with: " + user.getEmail());
mStatusTextView.setText(getString(R.string.Google_status_fmt, user.getEmail()));
mDetailTextView.setText(getString(R.string.Firebase_status_fmt, user.getUid()));
username = mDetailTextView.getText().toString();
}
}
};
}
// [START on_start_check_user]
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
// Check if user is signed in (non-null).
FirebaseUser currentUser = mAuth.getCurrentUser();
}
// [END on_start_check_user]
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
// [START onActivityResult]
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
Toast.makeText(getApplicationContext(), getResources().getString(R.string.Login_successful), Toast.LENGTH_LONG).show();
username = mDetailTextView.getText().toString();
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
}
}
}
// [END onActivityResult]
// [START auth_with_google]
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
// [START_EXCLUDE silent]
showProgressBar();
// [END_EXCLUDE]
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
}
// [START_EXCLUDE]
hideProgressBar();
// [END_EXCLUDE]
}
});
}
// [END auth_with_google]
// [START signin]
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signin]
@Override
public void onClick(View v) {
int i = v.getId();
if (i == R.id.signInButton) {
signIn();
}
}
}
Мой код для отправки значений в базу данных:
EditText wordInputView;
String searchKeyword;
String username;
public static DatabaseReference mRootReference = FirebaseDatabase.getInstance().getReference();
public static DatabaseReference mChildReferenceForInputHistory = mRootReference.child("Users' Input History");
public static DatabaseReference mChildReferenceForVocabularyList = mRootReference.child("Users' Vocabulary List");
searchKeyword = wordInputView.getText().toString();
Query query = mChildReferenceForInputHistory.child(username).orderByValue().equalTo(searchKeyword);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
//If a duplicate value (searchKeyword) is found, remove it.
snapshot.getRef().setValue(null);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
mChildReferenceForInputHistory.child(username).push().setValue(searchKeyword);
//Initialize the adapter
userInputArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, userInputArraylist);
userInputListview.setAdapter(userInputArrayAdapter);
mChildReferenceForInputHistory.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String previousChildKey) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
String value = snapshot.getValue(String.class);
userInputArraylist.add(value);
HashSet<String> myVocabularyArraylistHashSet = new HashSet<>();
myVocabularyArraylistHashSet.addAll(userInputArraylist);
userInputArraylist.clear();
userInputArraylist.addAll(myVocabularyArraylistHashSet);
//Alphabetic sorting
Collections.sort(userInputArraylist);
userInputArrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});