Kiểm tra xem tài nguyên có tồn tại Android

Tôi đang triển khai đăng nhập bằng Microsoft trên ứng dụng Saas của mình, mọi thứ đều hoạt động với tài khoản trên đối tượng thuê Azure của tôi và với tài khoản cá nhân

Tuy nhiên, vì tôi muốn khách hàng của mình có thể sử dụng tính năng này, tôi đã tham gia chương trình dành cho nhà phát triển Microsoft 365 để mô phỏng một tổ chức của khách hàng, nhưng nó không hoạt động

Đây là lỗi tôi gặp phải khi cố gắng đăng nhập

invalid_client error_description=AADSTS650053. Ứng dụng 'XXX' đã yêu cầu phạm vi 'Lịch. ReadWrite, Người dùng. Read,openid,email,profile,offline_access' không tồn tại trên tài nguyên '00000003-0000-0000-c000-00000000000'. Liên hệ với nhà cung cấp ứng dụng

Ứng dụng của tôi được định cấu hình là nhiều bên thuê và nó cho phép mọi loại tài khoản đăng nhập

Vì tôi không quen thuộc với hệ sinh thái Microsoft 365 nên tôi không hiểu hết mình nên làm gì để hệ sinh thái này hoạt động. Nhân tiện, tôi đã cố gắng thêm các quyền API phù hợp với phạm vi tôi sử dụng nhưng không được. Tôi nghĩ rằng tôi sẽ chỉ phải làm cho ứng dụng của mình có nhiều đối tượng thuê, thêm các quyền cần thiết và sau đó bất kỳ ai, dù là tài khoản cá nhân hay tài khoản từ một tổ chức đều có thể đăng nhập vào ứng dụng của tôi

Cập nhật mọi thông tin được công bố tại Hội nghị thượng đỉnh Firebase và tìm hiểu cách Firebase có thể giúp bạn đẩy nhanh quá trình phát triển ứng dụng cũng như tự tin chạy ứng dụng của mình. Tìm hiểu thêm

  • căn cứ hỏa lực
  • Tài liệu
  • lò sưởi
  • Xây dựng

Gửi phản hồiĐiều kiện viết cho Quy tắc bảo mật của Cloud Firestore Sắp xếp ngăn nắp với các bộ sưu tập Lưu và phân loại nội dung dựa trên sở thích của bạn

Hướng dẫn này dựa trên hướng dẫn quy tắc bảo mật cấu trúc để chỉ ra cách thêm điều kiện vào Quy tắc bảo mật Cloud Firestore của bạn. Nếu bạn không quen thuộc với những điều cơ bản về Quy tắc bảo mật của Cloud Firestore, hãy xem hướng dẫn bắt đầu

Khối xây dựng chính của Quy tắc bảo mật Cloud Firestore là điều kiện. Một điều kiện là một biểu thức boolean xác định xem một hoạt động cụ thể sẽ được cho phép hay từ chối. Sử dụng các quy tắc bảo mật để viết các điều kiện kiểm tra xác thực người dùng, xác thực dữ liệu đến hoặc thậm chí truy cập các phần khác trong cơ sở dữ liệu của bạn

Ghi chú. Thư viện ứng dụng khách của máy chủ bỏ qua tất cả Quy tắc bảo mật của Cloud Firestore và thay vào đó xác thực thông qua Thông tin xác thực mặc định của ứng dụng Google. Nếu bạn đang sử dụng thư viện ứng dụng khách của máy chủ hoặc API REST hoặc RPC, hãy đảm bảo thiết lập Quản lý danh tính và truy cập [IAM] cho Cloud Firestore.

xác thực

Một trong những mẫu quy tắc bảo mật phổ biến nhất là kiểm soát quyền truy cập dựa trên trạng thái xác thực của người dùng. Ví dụ: ứng dụng của bạn có thể muốn chỉ cho phép người dùng đã đăng nhập ghi dữ liệu

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to access documents in the "cities" collection
    // only if they are authenticated.
    match /cities/{city} {
      allow read, write: if request.auth != null;
    }
  }
}

Một mô hình phổ biến khác là đảm bảo người dùng chỉ có thể đọc và ghi dữ liệu của chính họ

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}

Nếu ứng dụng của bạn sử dụng Xác thực Firebase hoặc Nền tảng nhận dạng đám mây của Google, thì biến request.auth chứa thông tin xác thực cho ứng dụng khách yêu cầu dữ liệu. Để biết thêm thông tin về request.auth, xem

Xác nhận dữ liệu

Nhiều ứng dụng lưu trữ thông tin kiểm soát truy cập dưới dạng các trường trên tài liệu trong cơ sở dữ liệu. Quy tắc bảo mật của Cloud Firestore có thể tự động cho phép hoặc từ chối quyền truy cập dựa trên dữ liệu tài liệu

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document has the 'visibility'
    // field set to 'public'
    match /cities/{city} {
      allow read: if resource.data.visibility == 'public';
    }
  }
}

Biến

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
1 đề cập đến tài liệu được yêu cầu và
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
2 là bản đồ của tất cả các trường và giá trị được lưu trữ trong tài liệu. Để biết thêm thông tin về biến
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
1, hãy xem tài liệu tham khảo

Khi ghi dữ liệu, bạn có thể muốn so sánh dữ liệu đến với dữ liệu hiện có. Trong trường hợp này, nếu bộ quy tắc của bạn cho phép ghi đang chờ xử lý, biến

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
4 chứa trạng thái tương lai của tài liệu. Đối với các hoạt động
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
5 chỉ sửa đổi một tập hợp con của các trường tài liệu, biến
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
4 sẽ chứa trạng thái tài liệu đang chờ xử lý sau hoạt động. Bạn có thể kiểm tra các giá trị trường trong
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
4 để ngăn cập nhật dữ liệu không mong muốn hoặc không nhất quán

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure all cities have a positive population and
    // the name is not changed
    match /cities/{city} {
      allow update: if request.resource.data.population > 0
                    && request.resource.data.name == resource.data.name;
    }
  }
}

Truy cập các tài liệu khác

Sử dụng các hàm

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
8 và
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
9, các quy tắc bảo mật của bạn có thể đánh giá các yêu cầu gửi đến đối với các tài liệu khác trong cơ sở dữ liệu. Các hàm
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
8 và
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
9 đều mong đợi các đường dẫn tài liệu được chỉ định đầy đủ. Khi sử dụng biến để xây dựng đường dẫn cho
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
8 và
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
9, bạn cần thoát biến một cách rõ ràng bằng cách sử dụng cú pháp
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document has the 'visibility'
    // field set to 'public'
    match /cities/{city} {
      allow read: if resource.data.visibility == 'public';
    }
  }
}
4

Trong ví dụ bên dưới, biến

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document has the 'visibility'
    // field set to 'public'
    match /cities/{city} {
      allow read: if resource.data.visibility == 'public';
    }
  }
}
5 được bắt bởi câu lệnh khớp
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document has the 'visibility'
    // field set to 'public'
    match /cities/{city} {
      allow read: if resource.data.visibility == 'public';
    }
  }
}
6 và được sử dụng để tạo thành đường dẫn

service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city} {
      // Make sure a 'users' document exists for the requesting user before
      // allowing any writes to the 'cities' collection
      allow create: if request.auth != null && exists[/databases/$[database]/documents/users/$[request.auth.uid]]

      // Allow the user to delete cities if their user document has the
      // 'admin' field set to 'true'
      allow delete: if request.auth != null && get[/databases/$[database]/documents/users/$[request.auth.uid]].data.admin == true
    }
  }
}

Đối với thao tác ghi, bạn có thể sử dụng hàm

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document has the 'visibility'
    // field set to 'public'
    match /cities/{city} {
      allow read: if resource.data.visibility == 'public';
    }
  }
}
7 để truy cập trạng thái của tài liệu sau khi một giao dịch hoặc lô ghi hoàn tất nhưng trước khi giao dịch hoặc lô đó được thực hiện. Giống như
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth != null && request.auth.uid == userId;
      allow create: if request.auth != null;
    }
  }
}
8, hàm
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document has the 'visibility'
    // field set to 'public'
    match /cities/{city} {
      allow read: if resource.data.visibility == 'public';
    }
  }
}
7 có một đường dẫn tài liệu được chỉ định đầy đủ. Bạn có thể sử dụng
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document has the 'visibility'
    // field set to 'public'
    match /cities/{city} {
      allow read: if resource.data.visibility == 'public';
    }
  }
}
7 để xác định các nhóm ghi phải diễn ra cùng nhau dưới dạng giao dịch hoặc lô

Truy cập giới hạn cuộc gọi

Có giới hạn đối với các cuộc gọi truy cập tài liệu trên mỗi đánh giá bộ quy tắc

  • 10 cho yêu cầu tài liệu đơn và yêu cầu truy vấn
  • 20 để đọc, giao dịch và ghi nhiều tài liệu. Giới hạn trước đó là 10 cũng áp dụng cho từng thao tác

    Ví dụ: hãy tưởng tượng bạn tạo một yêu cầu ghi theo lô với 3 thao tác ghi và các quy tắc bảo mật của bạn sử dụng 2 lệnh gọi truy cập tài liệu để xác thực mỗi lần ghi. Trong trường hợp này, mỗi lần ghi sử dụng 2 trong số 10 lệnh gọi truy cập của nó và yêu cầu ghi theo đợt sử dụng 6 trong số 20 lệnh gọi truy cập của nó

Vượt quá một trong hai giới hạn dẫn đến lỗi quyền bị từ chối. Một số cuộc gọi truy cập tài liệu có thể được lưu vào bộ đệm và các cuộc gọi được lưu trong bộ nhớ cache không được tính vào giới hạn

Để biết giải thích chi tiết về cách các giới hạn này ảnh hưởng đến các giao dịch và ghi hàng loạt, hãy xem hướng dẫn dành cho

Truy cập các cuộc gọi và giá cả

Việc sử dụng các hàm này sẽ thực thi thao tác đọc trong cơ sở dữ liệu của bạn, điều đó có nghĩa là bạn sẽ bị tính phí vì đã đọc tài liệu ngay cả khi các quy tắc của bạn từ chối yêu cầu. Xem để biết thêm thông tin thanh toán cụ thể

chức năng tùy chỉnh

Khi các quy tắc bảo mật của bạn trở nên phức tạp hơn, bạn có thể muốn gói các bộ điều kiện trong các hàm mà bạn có thể sử dụng lại trên bộ quy tắc của mình. Quy tắc bảo mật hỗ trợ các chức năng tùy chỉnh. Cú pháp cho các hàm tùy chỉnh hơi giống JavaScript, nhưng các hàm quy tắc bảo mật được viết bằng ngôn ngữ dành riêng cho miền có một số hạn chế quan trọng

  • Các hàm chỉ có thể chứa một câu lệnh
    service cloud.firestore {
      match /databases/{database}/documents {
        // Make sure all cities have a positive population and
        // the name is not changed
        match /cities/{city} {
          allow update: if request.resource.data.population > 0
                        && request.resource.data.name == resource.data.name;
        }
      }
    }
    
    1. Chúng không thể chứa bất kỳ logic bổ sung nào. Ví dụ: họ không thể thực hiện các vòng lặp hoặc gọi các dịch vụ bên ngoài
  • Các hàm có thể tự động truy cập các hàm và biến từ phạm vi mà chúng được xác định. Ví dụ: một hàm được xác định trong phạm vi
    service cloud.firestore {
      match /databases/{database}/documents {
        // Make sure all cities have a positive population and
        // the name is not changed
        match /cities/{city} {
          allow update: if request.resource.data.population > 0
                        && request.resource.data.name == resource.data.name;
        }
      }
    }
    
    2 có quyền truy cập vào biến
    service cloud.firestore {
      match /databases/{database}/documents {
        // Make sure the uid of the requesting user matches name of the user
        // document. The wildcard expression {userId} makes the userId variable
        // available in rules.
        match /users/{userId} {
          allow read, update, delete: if request.auth != null && request.auth.uid == userId;
          allow create: if request.auth != null;
        }
      }
    }
    
    1 và các hàm dựng sẵn như
    service cloud.firestore {
      match /databases/{database}/documents {
        // Make sure the uid of the requesting user matches name of the user
        // document. The wildcard expression {userId} makes the userId variable
        // available in rules.
        match /users/{userId} {
          allow read, update, delete: if request.auth != null && request.auth.uid == userId;
          allow create: if request.auth != null;
        }
      }
    }
    
    8 và
    service cloud.firestore {
      match /databases/{database}/documents {
        // Make sure the uid of the requesting user matches name of the user
        // document. The wildcard expression {userId} makes the userId variable
        // available in rules.
        match /users/{userId} {
          allow read, update, delete: if request.auth != null && request.auth.uid == userId;
          allow create: if request.auth != null;
        }
      }
    }
    
    9
  • Các chức năng có thể gọi các chức năng khác nhưng không thể lặp lại. Tổng độ sâu ngăn xếp cuộc gọi được giới hạn ở 10
  • Trong phiên bản quy tắc
    service cloud.firestore {
      match /databases/{database}/documents {
        // Make sure all cities have a positive population and
        // the name is not changed
        match /cities/{city} {
          allow update: if request.resource.data.population > 0
                        && request.resource.data.name == resource.data.name;
        }
      }
    }
    
    6, các hàm có thể xác định các biến bằng cách sử dụng từ khóa
    service cloud.firestore {
      match /databases/{database}/documents {
        // Make sure all cities have a positive population and
        // the name is not changed
        match /cities/{city} {
          allow update: if request.resource.data.population > 0
                        && request.resource.data.name == resource.data.name;
        }
      }
    }
    
    7. Hàm có thể có tối đa 10 ràng buộc let nhưng phải kết thúc bằng câu lệnh return

Một hàm được xác định bằng từ khóa

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure all cities have a positive population and
    // the name is not changed
    match /cities/{city} {
      allow update: if request.resource.data.population > 0
                    && request.resource.data.name == resource.data.name;
    }
  }
}
8 và không nhận hoặc nhiều đối số. Ví dụ: bạn có thể muốn kết hợp hai loại điều kiện được sử dụng trong các ví dụ trên thành một hàm duy nhất

service cloud.firestore {
  match /databases/{database}/documents {
    // True if the user is signed in or the requested data is 'public'
    function signedInOrPublic[] {
      return request.auth.uid != null || resource.data.visibility == 'public';
    }

    match /cities/{city} {
      allow read, write: if signedInOrPublic[];
    }

    match /users/{user} {
      allow read, write: if signedInOrPublic[];
    }
  }
}

Việc sử dụng các hàm trong quy tắc bảo mật của bạn giúp chúng dễ bảo trì hơn khi độ phức tạp của quy tắc tăng lên

Quy tắc không phải là bộ lọc

Sau khi bạn bảo mật dữ liệu của mình và bắt đầu viết truy vấn, hãy nhớ rằng quy tắc bảo mật không phải là bộ lọc. Bạn không thể viết truy vấn cho tất cả tài liệu trong bộ sưu tập và yêu cầu Cloud Firestore chỉ trả về những tài liệu mà ứng dụng khách hiện tại có quyền truy cập

Ví dụ: lấy quy tắc bảo mật sau

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document has the 'visibility'
    // field set to 'public'
    match /cities/{city} {
      allow read: if resource.data.visibility == 'public';
    }
  }
}

Bị từ chối . Quy tắc này từ chối truy vấn sau đây vì tập kết quả có thể bao gồm các tài liệu mà

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure all cities have a positive population and
    // the name is not changed
    match /cities/{city} {
      allow update: if request.resource.data.population > 0
                    && request.resource.data.name == resource.data.name;
    }
  }
}
9 không phải là
service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city} {
      // Make sure a 'users' document exists for the requesting user before
      // allowing any writes to the 'cities' collection
      allow create: if request.auth != null && exists[/databases/$[database]/documents/users/$[request.auth.uid]]

      // Allow the user to delete cities if their user document has the
      // 'admin' field set to 'true'
      allow delete: if request.auth != null && get[/databases/$[database]/documents/users/$[request.auth.uid]].data.admin == true
    }
  }
}
0.

mạng
db.collection["cities"].get[]
    .then[function[querySnapshot] {
        querySnapshot.forEach[function[doc] {
            console.log[doc.id, " => ", doc.data[]];
    }];
}];

Được phép . Quy tắc này cho phép truy vấn sau vì mệnh đề

service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city} {
      // Make sure a 'users' document exists for the requesting user before
      // allowing any writes to the 'cities' collection
      allow create: if request.auth != null && exists[/databases/$[database]/documents/users/$[request.auth.uid]]

      // Allow the user to delete cities if their user document has the
      // 'admin' field set to 'true'
      allow delete: if request.auth != null && get[/databases/$[database]/documents/users/$[request.auth.uid]].data.admin == true
    }
  }
}
1 đảm bảo rằng tập kết quả thỏa mãn điều kiện của quy tắc.

mạng
db.collection["cities"].where["visibility", "==", "public"].get[]
    .then[function[querySnapshot] {
        querySnapshot.forEach[function[doc] {
            console.log[doc.id, " => ", doc.data[]];
        }];
    }];

Các quy tắc bảo mật của Cloud Firestore đánh giá từng truy vấn dựa trên kết quả tiềm năng của nó và yêu cầu không thành công nếu nó có thể trả về một tài liệu mà khách hàng không có quyền đọc. Các truy vấn phải tuân theo các ràng buộc do quy tắc bảo mật của bạn đặt ra. Để biết thêm về các quy tắc và truy vấn bảo mật, hãy xem dữ liệu truy vấn an toàn

Công dụng của Getresources[] trong android là gì?

Phương thức getResource[] của lớp Lớp java được sử dụng để trả về tài nguyên của mô-đun chứa lớp này . Giá trị trả về từ hàm này tồn tại ở dạng đối tượng của lớp URL.

Làm cách nào để có thể rút được từ ID tài nguyên trong Android?

Sử dụng getDrawable[int id, Tài nguyên. chủ đề giao diện] thay vào đó. Phương thức getTheme[] sẽ hữu ích.

Làm cách nào để kiểm tra xem ID có hay không có trong Java?

getResourceName[id]; . . Tên

Lớp nào được sử dụng để truy cập tài nguyên trong Android?

Truy cập tài nguyên . Bạn có thể sử dụng lớp R để truy cập tài nguyên đó bằng thư mục con và tên tài nguyên hoặc ID tài nguyên trực tiếp. R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.

Chủ Đề