ios - Firebase, Swift - runTransactionBlock: Permission denied -
i have encountered problem while trying users star runtransaction "permission denied" firebase. added below firebase security rules posts , swift transaction code.
what fault in case? if did first ".write" rule "auth != null" works everyone. don't want this. want prevent other nodes, starcounter , stars need changeable.
if shed light , explain, great.
"posts": { ".read": "auth !== null", "$id": { ".write": "auth != null && ( (!data.exists() && newdata.child('ownerid').val() == auth.uid) || (!newdata.exists() && data.child('ownerid').val() == auth.uid) || root.child('admins').haschild(auth.uid))", "starcounter": { ".write": "auth !== null" }, "stars": { ".write": "auth !== null" }, } },
and here swift transaction code below..
dataservice.ds.ref_posts.child("\(self.post.postkey!)").runtransactionblock({ (currentdata: firmutabledata) -> firtransactionresult in if var post = currentdata.value as? [string: anyobject], let uid = firauth.auth()?.currentuser?.uid { var stars : dictionary<string, bool> stars = post["stars"] as? [string : bool] ?? [:] var starcount = post["starcounter"] as? int ?? 0 if let _ = stars[uid] { // unstar post , remove self stars starcount -= 1 stars.removevalueforkey(uid) self.starbtn.setbackgroundimage(uiimage(named: "star-full"), forstate: .normal) self.starbtn.settitle("unstar", forstate: .normal) } else { // star post , add self stars starcount += 1 stars[uid] = true self.starbtn.setbackgroundimage(self.starimageview!.image!, forstate: .normal) self.starbtn.settitle("star", forstate: .normal) } post["starcounter"] = starcount post["stars"] = stars currentdata.value = post dataservice.ds.ref_posts.child("\(self.post.postkey!)").child("starcounter").observesingleeventoftype(.value, withblock: { snapshot in if let starcount = snapshot.value as? int { self.starcountlbl.text = "\(starcount)" } }) return firtransactionresult.successwithvalue(currentdata) } return firtransactionresult.successwithvalue(currentdata) })
update:
i have posts node below. want handle “starcounter” , “stars” reachable (write , read) everyone. others write owner. i’m using runtransaction using same in save data transactions. i’m getting permission denied while trying star post users (auth.uid
) not owner. couldn’t both increase star counter transaction , adding in stars node.
posts post_id_0 post_title post_description post_ownerid ... starcounter stars person_1: true person_2: true ... post_id_1 ..
i have tried before security rules first advice (it’s answer ~10m. before), didn't help, couldn’t add new post. don’t know i’m missing in security rules in same parent node.
reduced code below above security rules..
post_ref.child("\(self.post.postkey!)").runtransactionblock({ (currentdata: firmutabledata) -> firtransactionresult in if var post = currentdata.value as? [string: anyobject], let uid = firauth.auth()?.currentuser?.uid { var stars : dictionary<string, bool> stars = post["stars"] as? [string : bool] ?? [:] var starcount = post["starcounter"] as? int ?? 0 if let _ = stars[uid] { starcount -= 1 stars.removevalueforkey(uid) } else { starcount += 1 stars[uid] = true } post["starcounter"] = starcount post["stars"] = stars currentdata.value = post return firtransactionresult.successwithvalue(currentdata) } return firtransactionresult.successwithvalue(currentdata) })
Comments
Post a Comment