1
In the database I have a field called resources, in it I save the permissions of a user, in Json format:
[
{
"rota":"reserva",
"sref":"oQueFazer",
"ordem":"1",
"recursos":[
"index",
"show",
"store",
"update"
],
"descricao":"Reservar",
"controller":"ReservasController"
},
{
"rota":"reserva",
"sref":"oQueFazer",
"ordem":"2",
"recursos":[
"index",
"show"
],
"descricao":"Reservas",
"controller":"ReservasController"
},
{
"rota":"usuario",
"sref":"oQueFazer",
"ordem":"3",
"recursos":[
"index",
"show"
],
"descricao":"Usuários",
"controller":"UsuariosController"
},
{
"rota":"feriado",
"sref":"home.feriado",
"ordem":"4",
"recursos":[
"index",
"show",
"store",
"update",
"destroy"
],
"descricao":"Feriados",
"controller":"FeriadosController"
},
{
"rota":"sala",
"sref":"home.sala",
"ordem":"5",
"recursos":[
"index",
"show"
],
"descricao":"Salas",
"controller":"SalasController"
}
]
When I run the query below, it gives me the correct feedback from which user has the permission:
SELECT recursos from perfis
WHERE
JSON_CONTAINS(recursos, '{"controller": "FeriadosController"}');
Almost there, with the select
above I get the record line I’m looking for, the problem it shows me all the collection of json that is in the resource field. (Of course he was going to do it, that’s what I asked to be executed).
So for it to bring me the permissions that are authorized for a user in a given controller, I have selected below:
SELECT JSON_EXTRACT(recursos, '$[0].recursos') permite from perfis
WHERE
JSON_CONTAINS(recursos, '{"controller": "FeriadosController"}');
And the return was:
["index", "show", "store", "update"]
That is, everything OK, it shows the permissions that the user has in a given controller, THE PROBLEM is that it shows the permissions of the first line JSON collection '$[0].recursos'
but that’s not quite what I’m wanting, I want to return:
["index", "show", "store", "update", "destroy"]
Which is the permission the user has for the controller FeriadosController
I mean, I’m able to scan the controller to search the json, but I’m not able to show the part of the json that was searched, someone has a solution to this, jpa has gone through this?
@milrak-pear-person thanks for edits! tested here using the variable
@j
instead of creating a table and forgot to change...– nunks
You’re the guy, perfect! If you can edit the post explaining better...
– Milrak Pereira Pessoa
@Milrakpereirapessoa, have any specific questions? I edited trying to unlock better what each function does alone...
– nunks