I already have the 'ROLE_ADMIN', 'ROLE_SUPERUSER', 'ROLE_USER' roles set up and they are working perfectly. Now, I need to give access to 3 particular users to a certain page (with user names: admin1, admin2, admin3).
I tried the controller-centric approach, as follow...
import grails.plugin.springsecurity.annotation.Secured
#Secured(["authentication.name == 'admin1'", "authentication.name == 'admin2'", "authentication.name == 'admin3'"])
def transferMoney() {...}
But that only gave access to the first user, the admin1, the rest 2 didn't have the access.
Then I tried to give access from static mapping...
grails.plugin.springsecurity.interceptUrlMap = [
'/hostAdmin/account/transferMoney': ["authentication.name == 'admin1'", "authentication.name == 'admin2'", "authentication.name == 'admin3'"]
]
or
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
[pattern: '/hostAdmin/account/transferMoney', access: ["authentication.name == 'admin1'", "authentication.name == 'admin2'", "authentication.name == 'admin3'"]]
]
But both of them had no effect, the /hostAdmin/account/transferMoney page was open to ALL.
Can someone please help me? I really have to figure out how to give access to these 3 users only. Thank you in advance!
The solution was actually pretty simple ^_^
I went with the controller-centric approach. And both of these solutions worked....
#Secured(["authentication.name == 'admin1' || authentication.name == 'admin2' || authentication.name == 'admin3'"])
or
#Secured(closure = {
assert request
assert ctx
authentication.name == 'admin1' || authentication.name == 'admin2' || authentication.name == 'admin3'
})
Hope someone finds this helpful :)
Related
I know that user.status should be used to return the user status. But it always returns the status of offline, regardless of the fact that I or other participants change the status.
Code:
#app_commands.command(name='user', description=f'Детальна інформація про користувача')
async def user_(self, interaction: discord.Interaction, user: discord.Member = None):
ctx = await self.bot.get_context(interaction)# Don't pay attention to it
if user is None:
user = ctx.author
print(user.status)
global user_status
user_status = None
if user.status == discord.Status.online:
user_status = "<:online:1038376483758030898>В мережі"
elif user.status == discord.Status.offline or user.status == discord.Status.invisible:
user_status = "<:ofline:1038376481774120970>Не в мережі"
elif user.status == discord.Status.idle:
user_status = "<:idle:1038376474958381056>Відійшов"
elif user.status == discord.Status.dnd or user.status == discord.Status.do_not_disturb:
user_status = "<:dnds:1048347246556626954>Не турбувати"
await interaction.response.send_message(embed=embed, ephemeral=True)
In another command (not a slash command), this status check is working properly. Intentions I have set up that
bot = commands.Bot(commands.when_mentioned_or('.'), intents = discord.Intents.all()).
And in the portal of the developers, too, seems to be right
It seems to be a bug that it shows up as offline everytime, but here's something you can do:
userstatus = interaction.guild.get_member(user.id).status
if userstatus == discord.Status.online:
user_status = "<:online:1038376483758030898>В мережі"
#rest of your checks here
ps: You don't need or user.status == discord.Status.do_not_disturb and or user.status == discord.Status.invisible because they mean the same thing as discord.Status.offline and discord.Status.dnd respectively.
I hope you are well. Is it possible to query the LazyCache cache using Linq?
// Initialize Class
retVal = (ModelXyz)MemoryCache.Default.Where(u => (u.Value is ModelXyz) &&
(u.Value as ModelXyz).Property1 == abc &&
(u.Value as ModelXyz).Property2 == cde &&
(u.Value as ModelXyz).Property3 == true).Select(x => x.Value).FirstOrDefault();
I'm not sure if I'm barking up the wrong tree but I wanted to create a function to check if an account exists based on all optional parameters so it can be used to pull data depending on whatever you'd like to check on.
Basically the query should be:
where loginName = p1 or loginName = p2 or loginName = p3 but with the paramters all being optional, however at least one will be provided.
This is what I tried so far:
public async Task<bool> CheckAccountExistsAsync(string loginName = "", string authenticatorId = "", string eId = "")
{
if (string.IsNullOrWhiteSpace(loginName) && string.IsNullOrWhiteSpace(authenticatorId) && string.IsNullOrWhiteSpace(eId))
throw new InvalidOperationException("You must pass at least one parameter");
return await _context.Accounts.AnyAsync(a =>
(string.IsNullOrWhiteSpace(loginName) || a.LoginName == loginName)
|| (string.IsNullOrWhiteSpace(authenticatorId) || a.AuthenticatorId == authenticatorId)
|| (string.IsNullOrWhiteSpace(eId) || a.EmployeeId == eId));
}
Problem with this approach is that if I just pass the loginName, then the query is as follows with the condition completely omitted.:
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Accounts] AS [a]) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END
I'm sure I'm missing something, is there a better approach?
What you are using is applicable for optional and expressions, e.g.
return await _context.Accounts.AnyAsync(a =>
(string.IsNullOrWhiteSpace(loginName) || a.LoginName == loginName)
&& (string.IsNullOrWhiteSpace(authenticatorId) || a.AuthenticatorId == authenticatorId)
&& (string.IsNullOrWhiteSpace(eId) || a.EmployeeId == eId));
For optional or you have to use optional and sub conditions and add additional check for all optional parameters missing, e.g.
return await _context.Accounts.AnyAsync(a =>
(string.IsNullOrWhiteSpace(loginName)
&& string.IsNullOrWhiteSpace(authenticatorId)
&& string.IsNullOrWhiteSpace(eId))
|| (!string.IsNullOrWhiteSpace(loginName) && a.LoginName == loginName)
|| (!string.IsNullOrWhiteSpace(authenticatorId) && a.AuthenticatorId == authenticatorId)
|| (!string.IsNullOrWhiteSpace(eId) && a.EmployeeId == eId));
Can I use if clause with Linq where?
for example: I have a table with 4 filed(Id,UserId,Type,Sing).
I want select recorde that userid is 1 and if Type="None" , Sing should True.
(from d in db.Receive
where ((((d.SendType == "None") ? ((d.Signed) ? true : false) : true)) && userid==1)
select d).ToList();
when i use
((d.SendType == "None") ? ((d.Signed) ? true : false) : true)
it is select records that if Type="None" , Sing should True. but when add userid condition , don't return any records.
Isn't it better:
from d in db.Receive
where ((d.SendType == "None" && d.Signed) || d.SendType != "None") && userid == 1)
select d
and so?
(((d.SendType == "None") && (userid == 1)) ? ((d.Signed) ? true : false) : true)
Theres lots of brackets there...if I understand you correctly userid == 1 is used regardless so why not just have
where (((d.SendType == "None" && userid == 1) ? ((d.Signed) ? true : false) : true)))
select d).ToList();
I am trying to do a search with LINQ to NHibernate.
I have this code:
from d in rep.QueryAll<Document>()
where
d.Plata != null && d.Contractant != null && d.Stadiu == StadiuDocument.Polita
&& (d.NrPolita.Contains(query) ||
d.Contractant.CodUnic.Contains(query) ||
d.Contractant.Denumire.Contains(query) ||
d.Plata.IdTranzactie.Contains(query)) &&
((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))
select new
{
The problem is that I have some select inputs that have general values. Something like this:
<select id="tippolita" >
<option value = "-1">Any value</option>
<option value = "1">Value 1</option>
<option value = "2">Value 2</option>
<option value = "3">Value 3</option>
</select>
So when "Any value" is selected the where statement should be true like I wrote here:
((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))
This is almost the same as what I would write in SQL.
An error occurs inside the Nhibernate source code at line 33 in the file "Linq\NHLinqExpression.cs"
_expression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);
This error actually comes from the re-linq library.
One obvious workaround is to just write 3 if statements and put the appropriate LINQ queries in each of them but that means writing a lot more code.
Is there any way to make this kind of query work without copy-pasting the entire query and modifying just a little of it?
P.S.
This is the inner exception:
InnerException: System.NullReferenceException
Message=Object reference not set to an instance of an object.
Source=Anonymously Hosted DynamicMethods Assembly
StackTrace:
at lambda_method(Closure
)
I would rewrite this:
((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))
as
(TipPolita == null || d.Tip == (TipProdus)TipPolita) &&
(StareDocument == null || d.Stare == (StareDocument)StareDocument)
I don't know whether it'll work in NHibernate or not, but it's more idiomatic C# at least, so I would expect that it's more likely to be supported.
As an alternative, you could just replace "1 == 1" with "true".
Well, figured out how to do this the proper way
var date = rep.QueryAll<Document>().Where(d => d.Plata != null && d.Contractant != null && d.Stadiu == StadiuDocument.Polita);
if (!string.IsNullOrEmpty(query))
date = date.Where(d => (d.NrPolita.Contains(query) ||
d.Contractant.CodUnic.Contains(query) ||
d.Contractant.Denumire.Contains(query)));
I just move the ifs into the code and build the query(or rather the IQueryable) bit by bit