Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Reglas de Firestore usando obtener devoluciones permisos insuficientes

Estoy haciendo una consulta a firestore usando:

 setDoc(doc(db, "vendors", this.vendorId), { title: "test listing", vendorId: this.vendorId }, {merge: true}).then(i => { console.log("i", i) }).catch(e => { console.log("error", e) })

Tengo 2 colecciones la primera tiene una subcolección

Colección usuarios/${userId}

 { "userId": "123.uuu", "role": "vendor" }

Usuarios de subcolección/${userId}/vendors/${vendorId}

 { "user": "123.uuu", "vendor": "123..vvv" }

La segunda colección es Proveedores de colección/${vendorId}

 { "userId": "123.uuu", "role": "vendor" }

Quiero poder actualizar los proveedores de la segunda colección/${vendorId}, pero solo si la subcolección de usuarios users/${userId}/vendors/${vendorId} existe y, por lo tanto, tiene acceso a ella.

La siguiente regla no funciona: devuelve FirebaseError: Permisos faltantes o insuficientes.

 match /vendors/{vendorId}{ allow update: if get(/databases/$(database)/documents/users/$(request.auth.uid)/vendors/$(vendorId)).data.user == request.auth.uid }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Según @Doug Stevenson: "Las reglas de seguridad no significan nada a menos que se combinen con el código que realiza la consulta", puede intentar seguir el código de ejemplo a continuación que funciona al replicar en mi extremo:

 // Where the user.uid is the users document id; // and the vendorId is the value of the "user" under vendors' sub-collection const userRef = doc(db, "users", {user.uid}, "vendors", {vendorId}); const userSnap = await getDoc(userRef); if (userSnap.exists()){ // Where the vendorId is the value of the user under vendors' collection await setDoc(doc(db, "vendors", {vendorId}), { title: "test listing", vendorId: this.vendorId }, {merge: true}).catch(e => { console.log("error", e); }) } else{ // doc.data() will be undefined in this case console.log("No such document!"); }

Además, siguiendo la respuesta de @Dharmaraj, puedes probar esta regla:

 match /vendors/{vendorId} { // 'user' field in collection 'vendors', and users' sub-collection 'vendors', should have the same value for this rule to work // this will check 'vendors' and users' sub-collection 'vendors' user field if its the same or not allow read, create, update: if resource.data.user == request.auth.uid && request.auth.uid != null }

Para obtener más información, puede consultar la siguiente guía:

Cómo reparar el error de Firestore: PERMISSION_DENIED: Permisos faltantes o insuficientes

about 4 years ago · Juan Pablo Isaza Report

0

allow update solo permitirá operaciones de actualización, es decir, el documento ya debe existir. Dado que está usando setDoc() , eso significa que se está creando un nuevo documento si aún no existe. Intenta permitir create también:

 allow create, update: if ....;

Consulte la documentación sobre las reglas de seguridad y también este Firecast

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!