Firestore rules to Read, Update and Delete - google-cloud-functions

I’m a non developer that is looking for some guidance with security rules for a Firestore project.
Firestore database screenshot:
Each document in the Swaps collection should be accessible by two users only, a giver and a receiver.
To be allowed to create a new document in the Swaps collection, the user needs to assign herself as the receiver, which means the authenticated userid must match the ReceiverID of the request.
However, both the giver and receiver should be allowed to Update, Read and Delete the document. To do this, I want to match the receiver to the Swap document's ReceiverID and the giver to the Swap document's GiverID
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /Swaps/{document=**} {
allow read: if request.auth.uid == resource.data.GiverID;
allow update: if request.auth.uid == resource.data.GiverID;
allow delete: if request.auth.uid ==resource.data.GiverID;
allow read: if request.auth.uid == resource.data.ReceiverID;
allow update: if request.auth.uid == resource.data.ReceiverID;
allow delete: if request.auth.uid == resource.data.ReceiverID;
allow create: if request.auth.uid == request.resource.data.ReceiverID;
}
Currently, the only rule that seams to be working is Create. Read, update and delete are not working, either for giver or receiver.

Try like this:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /Swaps/{Swap} {
allow read: if request.auth.uid == resource.data.GiverID || request.auth.uid == resource.data.ReceiverID;
allow update: if request.auth.uid == resource.data.GiverID || request.auth.uid == resource.data.ReceiverID;
allow delete: if request.auth.uid == resource.data.GiverID || request.auth.uid == resource.data.ReceiverID;
allow create: if request.auth.uid == request.resource.data.ReceiverID;
}

Related

Google Appscript IF Or statement not working

Good day everyone; I am running into an error I can't explain. The scenario is as follows, I have two input boxes that collect information. If no value is entered, I want the if statement to handle it and cause a break. The Input box also has an "x" to close the box, which returns a value of "Cancel". What I am trying to do is capture a condition where if no value is entered OR cancel is passed through, a break will occur. Right now, the problem is Google completely ignores the Or statement. I know individually, my IF logic works, but when coupled with OR it doesn't recognize the condition.
This is my current code:
var propnumber = Browser.inputBox('Enter RFI/RFQ Number', Browser.Buttons.OK);
if(propnumber != "" || propnumber != 'cancel'){} else{
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
};
var myName = Browser.inputBox("Enter the Component Name",Browser.Buttons.OK_CANCEL);
if(myName != 'cancel')
{
I do something
}
As I mentioned in my description, my propnumber condition ignores the or and always accepts the value of cancel or blank. If I remove the or ( || ) then it works with one condition at a time.
I am sure this is something trivial any help appreciated.
What's wrong
The logic in the following part of your code
if(propnumber != "" || propnumber != 'cancel'){
// I assume some code will go here
} else{
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
};
does not match the logic you've described here:
if no value is entered OR cancel is passed through, a break will occur.
Consider the case where propnumber is 'cancel':
propnumber != "" evaluates to true
propnumber != 'cancel' evaluates to false
Therefore the if(... || ...) condition in your code evaluates to true and the (currently empty) if block runs, rather than the else.
How to fix it
Option 1: A literal translation of the logic
if no value is entered OR cancel is passed through, a break will occur
would be
if(propnumber == "" || propnumber == 'cancel') {
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
} else {
// Some action
}
Option 2: If you wish to swap the if and else clauses, you must negate the entire condition. So this will also work:
if(!(propnumber == "" || propnumber == 'cancel')) {
// Some action
} else {
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
}
Note the added parentheses and single negation.
Option 3: use AND instead of OR in your existing code.
The expression !(A || B) is NOT logically equivalent to !A || !B. Instead, it is equivalent to !A && !B (see DeMorgan's Law). So this will also work:
if(propnumber != "" && propnumber != 'cancel') {
// Some action
} else {
SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
return
}

Having trouble with Create-Only rules in Firebase Realtime Database

I'm trying to create a card deck database.
I have a node, "decks". Inside it are more nodes, numbered from '00001'. Inside '00001' is a node for each card, numbered appropriately, and inside each card node is the card data.
deck
00001
card001
SetCode:Alpha
SetID:001
card002
etc.
My problem is when it comes to the security rules. I don't want anyone to be able to update or delete any decks.
"decks":{
"$deckId":{
".validate": "$deckId.matches(/^[0-9]{5,5}$/) && $deckId.length == 5 && data.val() == null && newData.val() != null",
"$cardNumber":{
".validate": "$cardNumber.matches(/^[C][a][r][d][0](30|2[0-9]|1[0-9]|[0][1-9])$/) && $cardNumber.length == 7",
"SetCode":{".validate": "newData.isString()&& newData.val().matches(/^[A-Z][a-z]{4}$/)"},
"SetID":{".validate": "newData.isString()&& newData.val().matches(/^[0-9]{3}$/)"},
}}}
and I can't seem to find a set of rules that lets me make a new deck while also preventing anyone from deleting it or updating an existing deck.
Some rules I've tried are:
".write": "auth != null",
".write": "data.val() == null && newData.hasChildren() != null",
".write": "!data.exists()"
".write": "data.val() == null && newData.val() != null"
in various 'depths' of the rules, but I've been hitting my head against this for hours now. If someone could point me to the correct rule in the correct 'depth', I would be very grateful.
Edit: The main instance that I thought would work - and that I had found on multiple resources for Creation-Only rules, was:
'decks':{
".write": "data.val() == null && newData.val() != null",
}
It could be that I'm putting it in the wrong depth or if there's some other factor I haven't taken into account, but I don't understand why this doesn't work. I don't have any other read/write rules on higher levels.
The problem is where you defined the rule, not their contents.
You need to define the rule on the exact path where the write operation occurs. Since you want to allow creating a new deck, you need the rule to be on the individual deck, which you can do by using a wildcard variable:
'decks':{
"$deckid": {
".write": "!data.exist" // allows creation, but not updating or deletion
}
}

DMF and Advanced accounting structure

I have 10 dimensions in standard accounting structure and 7 dimensions defined in advanced rule.
Importing journals through DMF in excel throws error for 17 dimensions but works with 10 dimensions.
What's the right way to resolve this?
I got the solution. The issue was in generateDynamicDimension() method in DmfDimensionHelper Class.
Although It was bringing in all the dimensions values from the Segmented Entry Dimension column from Excel Sheet, it was picking only those Dimension names from the table where DimensionHierarchyType is AccountStructure and not from the Advanced Rule. I included DimensionHierarchyType of AccountRuleStructure as well. Now it's working.
while select Level from dimHierarchyLevel
order by dimHierarchyLevel.DimensionHierarchy, dimHierarchyLevel.Level
where (dimHierarchyLevel.DimensionHierarchy == dimHierarchyId
&& dimHierarchy.IsDraft == false
&& dimHierarchy.IsSystemGenerated == false
&& (dimHierarchy.StructureType == DimensionHierarchyType::AccountStructure || dimHierarchy.StructureType == DimensionHierarchyType::AccountRuleStructure)
join * from dimAttribute where
dimAttribute.RecId == dimHierarchyLevel.DimensionAttribute
exists join ledgerStructure
where ledgerStructure.DimensionHierarchy == dimHierarchy.RecId
&& ledgerStructure.Ledger == Ledger::current()
This is the additional Condtition I entered:
dimHierarchy.StructureType == DimensionHierarchyType::AccountRuleStructure

How do I make all the answers to questions in HTML produce one result?

I want to make all the answers to questions or the user preferences lead to one result please help. I want to do something like how Craigslist gives you preferences and leads you to one result. such as how do i link these two questions : are you a bird?
yes
no
Can you fly
yes
no
so that if the user was to choose yes to both it would give the result of "your a penguin"
please note that I'm only giving you this answer because I'm super bored since I'm debugging a program that basically just needs to crush numbers, please elaborate your question more and include what you tried yourself next time.
in javascript:
var q1 = true // question 1 answer (yes = true, no = false)
var q2 = false // question 1 answer (yes = true, no = false)
if(q1 == true && q2 == true) {
//your actions here, for example
alert("you are a parrot");
} else if(q1 == true && q2 == false) {
//your actions here, for example
alert("you are a penguin");
} else if(q1 == false && q2 == true) {
//your actions here, for example
alert("you are a military apache helicopter");
} else //last option, so q1 == false and q2 == false {
//your actions here, for example
alert("you are a human");
}
remember: this is just a hard coded example, if you want a user to make a quiz thing like this himself, you should probably loop through all the values and read them out of an array or something...

ASP.NET-MVC Linq2Sql logic to get linked items on condition

I have a subcontract table with a company field. On the company page, I do not want the company to be able to be deleted if it is attached to an active subcontract. I am currently using the following expression to display the delete button. (Doesn't actually delete, just sets company to inactive.)
<% if (item.company1.subcontracts.Count == 0) { %>
This works for excluding all companies which are attached to subcontracts. However, my subcontract table also has an active_status field. What I really want is to be able to delete companies which are either not attached to a subcontract or are attached to an inactive subcontract (active_status == 0).
How about the following:
<% var subcontracts = item.company1.subcontracts;
if (subcontracts.Count == 0 || subcontracts.Any(x => x.active_status == 0)) { %>
This solves your problem if the active_status is accessible through subcontracts
Maybe I am misunderstanding you, but it seems adding just an OR to the IF should do the trick:
<% if (item.company1.subcontracts.Count == 0 || item.company1.active_status == 0) { %>