Error hiding menu based on user permissions

Asked

Viewed 1,229 times

0

Hello, I had a method to control access to menus, based on permissions. But the nothing stopped working and I get the error: "Unable to link at runtime to a null reference"

I don’t know what was changed for that to happen.

Follow the code where I make the comparison:

In my model I have an ENUM, with possible profiles:

public enum Perfil
{
    /// <summary>
    /// Permissão para Envio, Listagem e Visualização dos arquivos.
    /// </summary>
    FUNCIONARIO,

    /// <summary>
    /// Permissão para Envio, Listagem, Visualização, Análise e Sincronização dos arquivos.
    /// </summary>
    SERVIDOR,

    /// <summary>
    /// Todas as permissões, inclusive inclusão de novos usuários para acesso ao sistema.
    /// </summary>
    ADMINISTRADOR
}

And in the model I have an attribute with the profile of each user:

 [Display(Name = "Perfil")]
    public string sPerfil { get; set; }

In my authentication class, I have the following code, to see whether or not the user is with permission:

public class CustomAutenticacaoAttribute : AuthorizeAttribute
{
    [Inject]
    public IAutenticacaoProvider autenticacaoProvider { get; set; }

    private string msgErro;

    private string[] perfilComPermissao;

    public CustomAutenticacaoAttribute(Perfil[] perfil)
    {
        perfilComPermissao = new string[perfil.Length];

        for (int x = 0; x < perfil.Length; x++)
        {
            perfilComPermissao.SetValue(perfil[x].ToString(), x);
        }
    }


    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (!autenticacaoProvider.Autenticado)
        {
            msgErro = "Você precisa estar autenticado para acessar essa página";
            return false;
        }
        if (perfilComPermissao.Length > 0 && !perfilComPermissao.Contains<string>(autenticacaoProvider.UsuarioAutenticado.Perfil))
        {
            msgErro = "Você não tem permissão para acessar essa página com suas credenciais.";
            return false;
        }
        return true;
    }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        base.HandleUnauthorizedRequest(filterContext);
        filterContext.Controller.TempData["Mensagem"] = msgErro;


    }
}

}

And in the view I just make the following mark:

 @if (ViewBag.UsuarioAutenticado.Perfil.Equals(PortalRH.DomainModel.Entities.Perfil.FUNCIONARIO.ToString())
                             || (ViewBag.UsuarioAutenticado.Perfil.Equals(PortalRH.DomainModel.Entities.Perfil.ADMINISTRADOR.ToString())))
                        {
                            <li>@Html.ActionLink("Todos", "Index", "Requerimento")</li>
                        }

It was working perfectly, but I made some changes to the code, to my controllers, and I don’t know what could have caused it. Someone would know how to help me?

  • Use the Visual Studio Debug and insert Breakpoints into your methods to identify where the error occurs. It’ll make it easier to help him.

  • I looked at Debug, and warned that it was null. I treated this error, but the error persisted.

1 answer

1


I switched the View authentication from:

 @if (ViewBag.UsuarioAutenticado.Perfil.Equals(PortalRH.DomainModel.Entities.Perfil.FUNCIONARIO.ToString())
                         || (ViewBag.UsuarioAutenticado.Perfil.Equals(PortalRH.DomainModel.Entities.Perfil.ADMINISTRADOR.ToString())))
                    {

For:

 @if (ViewBag.Perfil == "ADMINISTRADOR" || ViewBag.Perfil == "FUNCIONARIO")
                        {
                            <li>@Html.ActionLink("Todos", "Index", "Requerimento")</li>
                        }

And it worked correctly. I don’t know if this is the best way, or if it’s the right way, but it’s working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.