This logic checks accessGroup and if accessGroup is equal to "Admin" then it only checks if result.Admin or baccess is true but if accessGroup is anthing else it will need to check two other objects result.Admin == true || result.PowerUser. Is there any other way to do this if condition?
if (accessGroup == "Admin")
{
if (baccess == true || result.Admin == true)
{
var FileInfo = GetFile(fileManagerGuidId);
if (FileInfo != null)
{
FileManagerLog _filemanagerLog = new FileManagerLog();
_filemanagerLog.CustomerId =Request.Cookies["customerid"] != null ? Convert.ToInt32(Request.Cookies["customerid"].Value) : 0;
_filemanagerLog.FileManagerGuid = new Guid(fileManagerGuidId);
SaveFileManagerLog(_filemanagerLog);
byte[] fileBytes = FileInfo.FileData;
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, FileInfo.FileName);
}
else
{
return null;
}
}
}
else
{
if (baccess == true || result.Admin == true || result.PowerUser)
{
var FileInfo = GetFile(fileManagerGuidId);
if (FileInfo != null)
{
FileManagerLog _filemanagerLog = new FileManagerLog();
_filemanagerLog.CustomerId =Request.Cookies["customerid"] != null ? Convert.ToInt32(Request.Cookies["customerid"].Value) : 0;
_filemanagerLog.FileManagerGuid = new Guid(fileManagerGuidId);
SaveFileManagerLog(_filemanagerLog);
byte[] fileBytes = FileInfo.FileData;
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, FileInfo.FileName);
}
else
{
return null;
}
}
}
2 Answers 2
here is a clearer view of the current code :
if (accessGroup == "Admin")
{
if (baccess == true || result.Admin == true)
{
var FileInfo = GetFile(fileManagerGuidId);
if (FileInfo != null)
{
// return File(...);
}
else
{
return null;
}
}
}
else
{
if (baccess == true || result.Admin == true || result.PowerUser)
{
var FileInfo = GetFile(fileManagerGuidId);
if (FileInfo != null)
{
// return File(...);
}
else
{
return null;
}
}
}
Can't you see both return the same file ? If this works very well with current project, then you can simplify it to :
if (accessGroup == "Admin" || baccess == true || result.Admin == true || result.PowerUser)
{
var FileInfo = GetFile(fileManagerGuidId);
if (FileInfo != null)
{
// return File(...);
}
}
else
{
return null;
}
if your actual file log is different than the code, and it has different logic based on the conditions above, then you can translate it to this :
if(baccess == false && result.Admin == false)
{
return null;
}
var FileInfo = GetFile(fileManagerGuidId);
if(FileInfo == null)
{
return null;
}
if(accessGroup == "Admin")
{
// Admin file logic
// return File(...);
}
if(result.PowerUser)
{
// PowerUser file logic
// return File(...);
}
-
\$\begingroup\$ i made the same mistake when i first read his code accessGroup != "Admin" And's with powerUsers not Or's \$\endgroup\$MikeT– MikeT2021年10月15日 09:16:08 +00:00Commented Oct 15, 2021 at 9:16
this looks like it will have the same effect
if (baccess || result.Admin || (accessGroup != "Admin" && Result.PowerUser))
though i suspect what you mean to do is AND (&&)
if (baccess && (result.Admin || (accessGroup != "Admin" && result.PowerUser))
ie if they don't have access or the right access level don't run the code
if (baccess || (accessGroup == "Admin" ? result.Admin : (result.Admin || result.PowerUser)))
\$\endgroup\$if..else
. \$\endgroup\$Request.Cookies["customerid"] != null ? Convert.ToInt32(Request.Cookies["customerid"].Value) : 0;
can be much simplified toConvert.ToInt32(Request.Cookies["customerid"]?.Value ?? 0);
\$\endgroup\$