Firebase: Permission denied - swift

2017-10-22 18:18:02.498 studycook[17238]
[Firebase/Database][I-RDB038012] setValue: or removeValue: at
/users/user_id/test/abc
failed: permission_denied
The security settings for /users/$uid are as follows:
".read": "auth != null && auth.uid == $uid || auth != null && root.child('admins').child(auth.uid).val() == true",
".write": "auth != null && auth.uid == $uid || auth != null && root.child('admins').child(auth.uid).val() == true"
What am I doing wrong?
My code:
ref.child("users/\(Auth.auth().currentUser!.uid)/test/abc").setValue("true")

Related

Error running simulation — An unknown error occurred

{
"__name__":"/databases/(default)/documents/users/mfBBtfSX91YS9mXrkmEDTksA0712/sold/mfBBtfSX91YS9mXrkmEDTksA0712-hanSW0s5MxOOENWnc5T1cLinekj2/invoice/WhdEi2W83GtjDjowi6MV",
"id":"WhdEi2W83GtjDjowi6MV",
"data":{
"id":"db1a01a0-4da5-11ec-a579-c31f34aa6119",
"total":80,
"quotations":[
{
"id":"c595e610-46ab-11ec-b8ad-d56159e1778c",
"title":"anda",
"measuremntUnit":"kg",
"rate":35,
"quantity":2,
"isSelected":false,
"index":1
},
{
"id":"c595e610-46ab-11ec-b8ad-d56159e1778c",
"title":"biscuit",
"measuremntUnit":"packet",
"rate":10,
"quantity":1,
"isSelected":true,
"index":7}
],
"sellerEmail":"ku1438693#gmail.com",
"sellerUserId":"mfBBtfSX91YS9mXrkmEDTksA0712",
"sellerDisplayName":" Lila Kunwar",
"buyerUserId":"hanSW0s5MxOOENWnc5T1cLinekj2",
"buyerDisplayName":"saugat thapa",
"buyerEmail":"nexus.saugat26#gmail.com",
"sellerPhotoUrl":"https://lh3.googleusercontent.com/a/AATXAJ",
"buyerPhotUrl":"https://lh3.googleusercontent.com/a/AATXAJ"
}}
i am trying to check whether above data can insert into firestore given that the following is the security rule. however i get error Error running simulation — An unknown error occurred.
match /users/{userId}{
match /sold/{soldId}/{document=**}{
function isAuthed(){
return request.auth != null && request.auth.uid == userId;
}
function isSeller(){
return request.auth.uid == resource.data.sellerUserId ||
request.auth.uid == resource.data.buyerUserId;
}
function isApproved() {
return request.resource.data.isApproved == false;
// || request.resource.data.reviewed == "false"
}
function quotations(){
return request.resource.data.quotations;
}
function isOtherThanQuotations(){
return
isValidStringInput(request.resource.data.buyerUserId) &&
isValidStringInput(request.resource.data.buyerEmail) &&
isValidStringInput(request.resource.data.buyerPhotoUrl) &&
isValidStringInput(request.resource.data.buyerDisplayName) &&
isValidStringInput(request.resource.data.sellerUserId)&&
isValidStringInput(request.resource.data.buyerEmail) &&
isValidStringInput(request.resource.data.buyerPhotoUrl) &&
isValidStringInput(request.resource.data.buyerDisplayName) &&
isValidNumber(request.resource.data.total) &&
// isApproved() &&
request.resource.data.createdAt == request.time;
}
function isSelected() {
return request.resource.data.isSelected == false;
// || request.resource.data.reviewed == "false"
}
function isValidQuotation(item) {
return quotations()[item].keys().hasAll(
[
'title',
'rate',
'quantity',
'measuremntUnit',
'isSelected',
'index',
])
&& isValidStringInput(quotations()[item].title, 200)
&& isValidNumber(quotations()[item].rate)
&& isValidNumber(quotations()[item].quantity)
&& isValidStringInput(quotations()[item].measuremntUnit, 200)
&& isValidNumber(quotations()[item].index)
&& isSelected();
}
function isValidStringInput(input, maxSize) {
return input is string
&& input.size() > 0
&& input.size() <= maxSize;
}
function isValidNumber(input) {
return input is int
|| input.matches('^[0-9]+$');
}
allow read: if isSeller();
allow write: if isAuthed()
&& quotations().size() >= 1
&& quotations().size() <= 10
&& isOtherThanQuotations()
&& isValidQuotation(0)
&& (quotations().size() < 2 || isValidquotation(1) )
&& (quotations().size() < 3 || isValidquotation(2) )
&& (quotations().size() < 4 || isValidquotation(3) )
&& (quotations().size() < 5 || isValidquotation(4) )
&& (quotations().size() < 6 || isValidquotation(5) )
&& (quotations().size() < 7 || isValidquotation(6) )
&& (quotations().size() < 8 || isValidquotation(7) )
&& (quotations().size() < 9 || isValidquotation(8) )
&& (quotations().size() < 10 || isValidquotation(9) )
&& (quotations().size() < 11 || isValidquotation(10) );
}
}
is it becase i have too too many functions? right now myconcern is with create only.
is it becasue the id doesnt have security rule to validate? i tried providing validation for id as well but the error still persist. i need more than 10 map in an array but this is for simplicity of testing process.

SQL to LINQ (Entity Framework Core)

I am trying to write SQL to LINQ. I tried Linqpad, but this feature is not available (only LINQ to SQL conversion).
I have this SQL query
SELECT
count(*),
count(TireWidth),
count(TireRimDiameter),
count(TireSeason),
count(PriceCzk)
FROM(
SELECT
CASE WHEN TireWidth = 255 THEN 1 END TireWidth,
CASE WHEN TireRimDiameter = 16 AND TireWidth = 255 THEN 1 END TireRimDiameter,
CASE WHEN TireSeason = 1 THEN 1 END TireSeason,
CASE WHEN PriceCzk > 50000 THEN 1 END PriceCzk
FROM Products
)
Products
How can I write this query in LINQ? I am using Entity Framework Core, .NET Core 5.
Thank you advance
Trick is in using group by by constant. EF will create just plain aggregation query.
var query =
from p in ctx.Products
group p by 1 into g
select new
{
Count = g.Count(),
TireWidth = g.Sum(x => x.TireWidth == 255 ? 1 : 0),
TireRimDiameter = g.Sum(x => x.TireRimDiameter == 16 && x.TireWidth == 255 ? 1 : 0),
TireSeason = g.Sum(x => x.TireSeason == 1 ? 1 : 0),
PriceCzk = g.Sum(x => x.PriceCzk > 50000 ? 1 : 0)
};
I didn't find LINQ solution, but this solution is working and very fast:
var MySqlParameters = new List<MySqlParameter>();
MySqlParameters.Add(new MySqlParameter("p_date", DateTime.UtcNow.Date));
MySqlParameters.Add(new MySqlParameter("p_product_type", (int)(parameters.ProductType ?? 0)));
MySqlParameters.AddRange(BuildInParameters("p_condition",
parameters.Conditions.Select(c => (int)c), out var conditionParameters));
MySqlParameters.AddRange(BuildInParameters("p_brand",
parameters.Brands.Select(c => (int)c), out var brandParameters));
MySqlParameters.AddRange(BuildInParameters("p_seller",
parameters.UserTypes.Select(c => (int)c), out var sellerParameters));
if (parameters.ToMileage != null)
MySqlParameters.Add(new MySqlParameter("p_mileage", parameters.ToMileage));
if (parameters.FromPrice != null)
MySqlParameters.Add(new MySqlParameter("p_from_price", parameters.FromPrice));
if (parameters.ToPrice != null)
MySqlParameters.Add(new MySqlParameter("p_to_price", parameters.ToPrice));
if (parameters.TireSeason != null)
MySqlParameters.Add(new MySqlParameter("p_tire_ses", parameters.TireSeason));
if (parameters.TireVehicleType != null)
MySqlParameters.Add(new MySqlParameter("p_veh_type", parameters.TireVehicleType));
if (parameters.TireWidth != null)
MySqlParameters.Add(new MySqlParameter("p_tire_wid", parameters.TireWidth));
if (parameters.TireHeight != null)
MySqlParameters.Add(new MySqlParameter("p_tire_hei", parameters.TireHeight));
if (parameters.TireRimDiameter != null)
MySqlParameters.Add(new MySqlParameter("p_tire_rim", parameters.TireRimDiameter));
if (parameters.TireLoadIndex != null)
MySqlParameters.Add(new MySqlParameter("p_tire_loa", parameters.TireLoadIndex));
if (parameters.TireSpeedRating != null)
MySqlParameters.Add(new MySqlParameter("p_tire_spd", parameters.TireSpeedRating));
if (parameters.FromYear != null)
MySqlParameters.Add(new MySqlParameter("p_car_fromYear", parameters.FromYear));
if (parameters.ToYear != null)
MySqlParameters.Add(new MySqlParameter("p_car_toYear", parameters.ToYear));
var builder = new StringBuilder();
if (isTireWidth)
{
builder.AppendLine($#"UNION ALL
SELECT 'TireWidth' FilterName, TireWidth FilterValue, COUNT(1) as FilterCount FROM
`products` P LEFT OUTER JOIN `aspnetusers` U ON P.UserId=U.Id
WHERE P.Deleted=0 AND TireWidth IS NOT NULL
AND (P.ExpirationDateUtc='0001-01-01' OR P.ExpirationDateUtc >= #p_date)
AND P.ProductType = #p_product_type");
if (conditionParameters != "") builder.AppendLine($"AND P.ProductCondition IN ({conditionParameters})");
if (brandParameters != "") builder.AppendLine($"AND P.Brand IN ({brandParameters})");
if (sellerParameters != "") builder.AppendLine($"AND U.Type IN ({sellerParameters})");
if (parameters.ToMileage != null) builder.AppendLine($"AND P.Mileage <= #p_mileage");
if (parameters.FromPrice != null) builder.AppendLine($"AND P.PriceCzk >= #p_from_price");
if (parameters.ToPrice != null) builder.AppendLine($"AND P.PriceCzk <= #p_to_price");
if (parameters.TireSeason != null) builder.AppendLine($"AND P.TireSeason = #p_tire_ses");
if (parameters.TireVehicleType != null) builder.AppendLine($"AND P.TireVehicleType = #p_veh_type");
//if (parameters.TireWidth != null) builder.AppendLine($"AND P.TireWidth = #p_tire_wid");
if (parameters.TireHeight != null) builder.AppendLine($"AND P.TireHeight = #p_tire_hei");
if (parameters.TireRimDiameter != null) builder.AppendLine($"AND P.TireRimDiameter = #p_tire_rim");
if (parameters.TireLoadIndex != null) builder.AppendLine($"AND P.TireLoadIndex = #p_tire_loa");
if (parameters.TireSpeedRating != null) builder.AppendLine($"AND P.TireSpeedRating = #p_tire_spd");
builder.AppendLine("GROUP BY TireWidth");
}
if (isTireVehicleType)
{
builder.AppendLine($#"UNION ALL
SELECT 'TireVehicleType' FilterName, TireVehicleType FilterValue, COUNT(1) as FilterCount FROM
`products` P LEFT OUTER JOIN `aspnetusers` U ON P.UserId=U.Id
WHERE P.Deleted=0 AND TireVehicleType IS NOT NULL
AND (P.ExpirationDateUtc='0001-01-01' OR P.ExpirationDateUtc >= #p_date)
AND P.ProductType = #p_product_type");
if (conditionParameters != "") builder.AppendLine($"AND P.ProductCondition IN ({conditionParameters})");
if (brandParameters != "") builder.AppendLine($"AND P.Brand IN ({brandParameters})");
if (sellerParameters != "") builder.AppendLine($"AND U.Type IN ({sellerParameters})");
if (parameters.ToMileage != null) builder.AppendLine($"AND P.Mileage <= #p_mileage");
if (parameters.FromPrice != null) builder.AppendLine($"AND P.PriceCzk >= #p_from_price");
if (parameters.ToPrice != null) builder.AppendLine($"AND P.PriceCzk <= #p_to_price");
if (parameters.TireSeason != null) builder.AppendLine($"AND P.TireSeason = #p_tire_ses");
//if (parameters.TireVehicleType != null) builder.AppendLine($"AND P.TireVehicleType = #p_veh_type");
if (parameters.TireWidth != null) builder.AppendLine($"AND P.TireWidth = #p_tire_wid");
if (parameters.TireHeight != null) builder.AppendLine($"AND P.TireHeight = #p_tire_hei");
if (parameters.TireRimDiameter != null) builder.AppendLine($"AND P.TireRimDiameter = #p_tire_rim");
if (parameters.TireLoadIndex != null) builder.AppendLine($"AND P.TireLoadIndex = #p_tire_loa");
if (parameters.TireSpeedRating != null) builder.AppendLine($"AND P.TireSpeedRating = #p_tire_spd");
builder.AppendLine("GROUP BY TireVehicleType");
}
var result = _context.FilterQueries.FromSqlRaw(builder.ToString(), MySqlParameters.ToArray());
return result;

Check null in Entity framework

string password = Infoware.Students.FirstOrDefault(x=>x.AdmissionNo == AdmissionNo && x.AdmissionNo != null).Password;
Here students is a table where I have AdmissionNo and Password columns, Need to get password if admissionNo exists in the table. This expression is fetching Password for given AdmissionNo if the admissionNo is present in the records but if the entered admissionNo is not found in the records then it throwing NullReference Exception.
How can I check that given AdmissionNo is not a Valid AdmissionNO ??
You can simply check for null using an if statement"
if ( AdmissionNo != null )
{
//your code
}
You can also check ObjectContextOptions.UseCSharpNullComparisonBehavior Property which was introduced in EF 5.0
You can check it using Any:
.Any(a => a.AdmissionNo == null)
Or using if statement:
string user = Infoware.Students.FirstOrDefault(x=>x.AdmissionNo == AdmissionNo && x.AdmissionNo != null);
if(user != null)
{
password = user.Password;
}
You just need to put your tests the other way around (as they will shortcut):
string password = Infoware.Students.FirstOrDefault(x=> x.AdmissionNo != null
&& x.AdmissionNo == AdmissionNo ).Password;
And re-reading your question, you issue is when nothing is found...
So:
string password;
var result = Infoware.Students.FirstOrDefault(x=> x.AdmissionNo != null
&& x.AdmissionNo == AdmissionNo );
if (result != null) {
password = result.Password;
}

Swift: Error when updated Xcode: 'could not find overload for '&&' that accepts the supplied arguments'

I just updated to the non beta version of Xcode 6 (finally) and, coming from beta 5, got a few errors that I didn't get before, one being "could not find overload for '&&' that accepts the supplied arguments"
I am following a tutorial here and from another question, I know that this error is because "the expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions."
I'm a beginner; how do I break up an expression into sub-expressions?
My code:
func checkForWin(){
//first row across
var youWin = 1
var theyWin = 0
var whoWon = ["Lost":0,"Won":1]
for (key,value) in whoWon {
if ((plays[6] == value && plays[7] == value && plays[8] == value) || //across the bottom
(plays[3] == value && plays[4] == value && plays[5] == value) || //across the middle
(plays[0] == value && plays[1] == value && plays[2] == value) || //across the top
(plays[6] == value && plays[3] == value && plays[0] == value) || //down the left side
(plays[7] == value && plays[4] == value && plays[1] == value) || //down the middle
(plays[8] == value && plays[5] == value && plays[2] == value) || //down the right side
(plays[6] == value && plays[4] == value && plays[2] == value) || //diagonal
(plays[8] == value && plays[4] == value && plays[0] == value)){//diagonal
userMessage.hidden = false
youLabel.hidden = false
userMessage.text = "\(key)!"
done = true;
}
}
}
To break it up, you just need to add more ()s. Like this:
if (((plays[6] == value) && (plays[7] == value) && (plays[8] == value)) ||
((plays[3] == value) && (plays[4] == value) && (plays[5] == value)) ||
((plays[0] == value) && (plays[1] == value) && (plays[2] == value)) ||
((plays[8] == value) && (plays[4] == value) && (plays[0] == value)))

Entity framework better way to reduce lambda condition WHERE?

I had the code below that provide search, 4 field, the
lambda will increase huge lot for each field included in, and it is time consuming and not efficient to have such a combination search, any better way to write a proper search?
var result = db.ValuePairs
.Where(c=>
(isStatus && isText && isCode && isGroupCd)
?
c.Status == status && c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText && isCode)
?
c.Status == status && c.Text == text && c.Code == code
:
(isStatus && isText && isGroupCd)
?
c.Status == status && c.Text == text && c.GroupCode == groupCd
:
(isStatus && isCode && isGroupCd)
?
c.Status == status && c.Code == code && c.GroupCode == groupCd
:
(isText && isCode && isGroupCd)
?
c.Text == text && c.Code == code && c.GroupCode == groupCd
:
(isStatus && isText)
?
c.Status == status && c.Text == text
:
(isStatus && isCode)
?
c.Status == status && c.Code == code
:
(isStatus && isGroupCd)
?
c.Status == status && c.GroupCode == groupCd
:
(isText && isCode)
?
c.Text == text && c.Code == code
:
(isText && isGroupCd)
?
c.Text == text && c.GroupCode == groupCd
:
(isCode && isGroupCd)
?
c.Code == code && c.GroupCode == groupCd
:
(isStatus)
?
c.Status == status
:
(isText)
?
c.Text == text
:
(isCode)
?
c.Code == code
:
(isGroupCd)
?
c.GroupCode == groupCd
:
groupCd != ""
);
Pretty simple boolean logic there...
.Where(c =>
(!isStatus || c.Status == status)
&& (!isCode || c.Code == code)
&& (!isGroupCd || c.GroupCode == groupCd)
);