How to limit file uploads by time on Firebase Storage - firebase-storage

I'm trying to write a rule to block a malicious user from updating their profile picture at a fast rate.
I found this example in the docs
// Allow reads if the resource was updated less than an hour ago
allow read: if resource.updated < request.time + duration.value(60, "m")
I want to do something similar, I want to allow the user to update the file only if an x amount of time has passed since the last update.
Let's say I don't want to let users update their profile pictures more than once every hour, I wrote this rule.
request.time >= resource.updated + duration.value(1, "h")
This rule is blocking the write every time, for testing proposes I was setting the duration to 10sec duration.value(10, "s") but even if I wait 10 min the upload still fails.
I'm not sure why this is not working, I know the problem is with this rule because everything works as expected when I remove it.
Here are my rules for write
allow write: if request.resource.size < 5 * 1024 * 1024
&& request.resource.contentType.matches('image/.*')
// Only allow uploads once every hour
&& request.time >= resource.updated + duration.value(1, "h")
// Require Authentication
&& request.auth != null && uid == request.auth.uid
// Don't allow anonymous users
&& request.auth.token.firebase.sign_in_provider != 'anonymous';
The docs don't show an example for writing, I begin wondering if this only works for read

Change it to "<" instead of ">=" and use resource.data:
allow write: if request.time < resource.data.updatedAt + duration.value(1, 'h')

Related

How to throw an error during a parameter check without if-statements in Google-Earth-Engine?

I am working on a new version of the bfast monitor algorithm in Google Earth Engine. See the code of the original algorithm on Github.
The function bfastMonitor() takes user-defined parameters and applies some parameter checks before starting actual calculations. When the user-defined parameter settings are incompatible with the algorithm, an error should be raised.
During the parameter check, two types of if statements are made: statements that only check the parameter boundaries and raise an error at incompatible values, and statements that check and rewrite the contents of a parameter and raise an error at incompatible values. For the sake of the focus of this question, I will consider only the latter one.
Obviously, in a conventional coding paradigm, if-statements can be used to do this parameter check, however, using if-statements goes against the client-server model of GEE.
Consider the period parameter, which can only be 2,4,6,8, or 10. This parameter code used to index a list later in the code (line 459 on Github), where a period-value of 4 means that the list should be indexed at position 1, for instance.
Currently the implementation looks like this, using if-statements:
period = period||10
if (period == 2) {
period = 0;
} else if (period == 4){
period = 1;
}else if (period == 6){
period = 2;
}else if (period == 8){
period = 3;
}else if (period == 10){
period = 4;
}else {
alert("Error: for period parameter, we only have 2, 4, 6, 8,10. Choose one of these values");
}
Later on, the period parameter is used a form like this (from Github):
var exampleList = ee.List([0.001, 0.002, 0.003, 0.004, 0.005]);
var exampleValue = exampleList[period];
The code could be rewritten easily to get rid of the if-statements, like this for instance:
var period = ee.Number(6);
var periodDict = ee.Dictionary({
'2':0,
'4':1,
'6':2,
'8':3,
'10':4,
});
var exampleList = ee.List([0.001, 0.002, 0.003, 0.004, 0.005]);
var exampleValue = exampleList.get(periodDict.get(period.format()));
But then I don't know how to retain the opportunity to throw an error when the value for period is out of bounds.
How can I check the parameters of a function in Google Earth Engine and throw errors while avoiding if-statements?
There is nothing at all wrong with using a JavaScript if statement when it works. The advice you linked is about using ee.Algorithms.If which is unfortunately often inefficient — that's completely unrelated. The usual problem with a JavaScript if is when you're trying to use it on a server-side value that hasn't been computed yet.
But in your case, it looks like you want to validate a user-provided parameter. if is a perfectly fine way to do this.
I'll suggest one improvement: instead of using alert("error message");, use throw new Error:
throw new Error("For period parameter, we only have 2, 4, 6, 8,10. Choose one of these values");
This has two advantages:
It doesn't pop a dialog that the user must interact with before fixing the problem, but just results in an error message in the usual place, the Code Editor's Console.
It will stop the rest of the code from executing, which alert() doesn't.

Bounded Waiting in Test and Set Instruction

In order to gurantee Bounded wait in Test and set Instruction,following is the code given in Operating system book,Galvin -:
do {
1 waiting[i] = true;
2 while (waiting[i] && test_and_set(&lock)) ;
3 waiting[i] = false;
/* critical section */
4 j = (i + 1) % n;
5 while ((j != i) && !waiting[j])
6 j = (j + 1) % n;
7 if (j == i)
8 lock = false;
9 else
10 waiting[j] = false;
/* remainder section */
} while (true);
I am getting the complete code and concluded that
A process P_i will be in the critical section if either
Waiting [i]=false or test_and_set(&lock)=FALSE which ensures that Lock was FALSE previously. so Exit Section is either setting Waiting[j] or lock to FALSE.
But i have got some doubts-:
if in the exit section section it is found that same process again requests for critical section i.e
if j==i
then according to the code,that process have to start its execution form line number 2,i.e will execute
test_and_set(&lock))
in while loop and find the return value of test_and_set(&lock)) as false and then move to critical section.My doubt is that if same process wants to be in critical section ,is it necessary to start its exection right from line number 2
2.Now i want to do following Permutation and want to check the possible outcome.i want to swap line number 8 and 10
in line number 8 if i make
waiting[j]=false;
then also it will move to critical section even though lock =true now.
in line number 10 if i make
lock=false
then also it(process p_j) will move to critical section even though waiting[i]=true and i think it would be better because line number 3 will assign waiting[i]=false ,after the while loop breaks due to test_and_set(&lock)=false.
On the other hand if i make this change process have to execute test_and_set(&lock) which is time consuming
Is my assumption for point2 right?
what is the correct reason for point 1?
Thanks
Regarding point 1:
My doubt is that if same process wants to be in critical section ,is
it necessary to start its exection right from line number 2
A process is basically an program in execution. A process just cannot jump around choosing which instruction to execute next. The control flow decides that. The control of the code(which is the process itself) suggests that if a process successfully enters critical section, and again wants to enter critical section, then it will first execute lines 4 to 10 and then execute remainder section and would have to start execution right from line 1
Regarding your point 2
Now i want to do following Permutation and want to check the possible
outcome.i want to swap line number 8 and 10
If you swap the lines, the bounded waiting condition would no longer exist.
Proof
Suppose that only 1 process P(i) made the request to access Critical Section and it successfully entered. So
lock = true and waiting[i] = true
because only then it would have been able to come out of the for loop. Now it starts executing from line 4
Then j takes following values:
i + 2 , i + 3, ......0 , 1 , 2 , 3 , 4....i
wrap around of values occurs because of % operator. And Because no other process made request to enter critical section, waiting[j] = false for j != i
Therefore the condition while ((j != i) && !waiting[j]) becomes false when j equals i, and now we are at line 7. The new code is:
if (j == i)
waiting[j] = false;
else
lock = false;
Now if any process makes a request to enter critical section, then while (waiting[i] && test_and_set(&lock)) ; would always evaluate to true because lock is true and waiting[i] is also true and it would be stuck in spinlock. There would be no progress.

How would I use recursion to create an array of extra change($100, 50, 20, 10, 5, 1) MATLAB

How would I correctly make a recursive call within every if-statement to get the change of money? Im specifically focusing on the "change" variable.Thanks
TEST CASE 1-------------------------------------------------------------------------------
<>> [change,flag] = makeChangeRecursive(2,100)
change =
50
20
20
5
2
1
flag =
1
My code is the following
function [change,flag] = makeChangeRecursive(cost,paid)
if extra > 0
flag = true;
elseif extra == 0
change = 0;
flag = true;
return
elseif cost > paid;
flag = false;
change = [];
warning('That''s not enough to buy that item.');
return
end
if extra >= 100
change = [change; makeChangeRecursive(cost,paid - change )];
paid =paid-100;
elseif extra >= 50
change = [change; 50];
paid =paid-50;
elseif
This continues for all dollar values.
Let's take a look at your first case:
if extra >= 100
change = [change; makeChangeRecursive(cost,paid - change )];
paid =paid-100;
elseif ...
The first time we call your function, the variable change doesn't have anything in it. In fact, it will never have anything in it at the beginning of the function call because you don't pass it in as a parameter or give it a value prior to this line. So putting change on the right-hand side of the assignment will give you an error.
But that's okay, because that's not what you want to do anyway. You want to build change up from the beginning.
In addition, change is a list of values. We want to pass the recursive calls a single value, paid after updating its value.
Let's build this up step by step:
if extra >= 100
If this is true, we want subtract 100 from the amount paid (what we pass in to the recursive call) and add 100 to our list of change. Let's do the first part:
paid = paid - 100;
As I said, we want to update paid first because we're going to use this value in the recursive call, which happens next, along with adding our new change value to the list:
change = [100; makeChangeRecursive(cost, paid)];
elseif ...
And so on for the remainder of the change values. I'm sure you can take care of the rest of them now by yourself.
I also noticed that you didn't assign a value to extra. This might have been just a cut-and-paste error, but you need to make sure that you have that at the beginning of your function.

A database architecture and algorithm to support querying for availability over a week

I have an iPhone app where a user can set his availability for different times of the day, for each day of the week. Having just three availability periods per day (morning/afternoon/evening) will work for the time being, but I'd want to implement times in the future. A user will try to search for other users based on the availability. What is a good database architecture and search algorithm to implement this? I use stackmob as my backend. If some body can give me some hints or point me to an algorithm that will be able to search for an availability match, I'd really appreciate it. I have a crude way to do it currently - 21 variables (boolean each for 7 days * 3 time periods). I want to find a smarter way.
I would do the following:
Store availability using date/times in the database. One record for each slot they are available. It is arbitrary what time you use to represent each slot. E.g. maybe morning is 9am, afternoon is 1pm and evening is 5pm. But by using date/time you are setting yourself up to change this to something more precise later (e.g. a start-time, end-time).
Query each users availability within the required time frame (e.g. between now and 1 week time). Lets calling the resulting arrays A & B.
Now run the following algorithm...
NSUInteger a = 0;
NSUInteger b = 0;
BOOL foundMatch = NO;
while(a < A.count && b < B.count) {
NSComparisonResult result = [A[a] compare:B[b]];
if(result == NSOrderedSame) {
foundMatch = YES;
break;
} else if(result == NSOrderedDescending) { //a>b
b++;
} else {
a++;
}
}

Can I set a maximum and minimum payment amount on Paypal

I am new to making a payment gate way and require restricting purchases between a predefined minimum and maximum. I searched Google, finding this as the most useful result. Is there a way to accomplish this?
You can set the maximum with MAXAMT - read more
However there is no option to set the minimum with Paypal. But you can write a simple JS that checks on submit the form field value and throws an error if the amount is less than expected:
function checkMinimum() {
var min = 5;
if( parseFloat( document.formName.fieldName.value ) < min ) {
alert("Minimum amount should be not less than $" + min + ". Spend more.");
return false;
}
return true;
}