Lock or release a controller according to some condition

Asked

Viewed 309 times

4

I have a controller that should only be accessed if an attribute in my table is "true". Is there any way at the time the user clicks on such a link, check if the attribute is true, and only then release the access?

  • Let me see if I understand: Vc wants that when the user clicks on a link the system check if the attribute of his table is true and if it leads to another page, this?

  • Yes. If not, display an error message!

  • Let me explain it to you. I have a table called publications, when the user posts a post on the site, it is entered as false, as it has not been validated. After the validation of this post, the user can add chapters to it and so on. I want that while the post is false, the user is not able to access the controller that allows him to add chapters.

  • When vc says table is database table or view html table?

  • In the same bank.

  • In this case it is a simple check that you do in your bank through the controller. You can do this via ajax or even conventional mode. Just in your controller you create a method that given the user id returns the value of the attribute you want to check, if the value is true vc continues if you do not send a Answer to view with the desired exception.

Show 1 more comment

1 answer

4


You can let the user access the Controller no problem. Just create in a business rule that does not allow you to continue:

public ActionResult AdicionarCapitulos(int id)
{
    var publicacao = contexto.Publicacoes.SingleOrDefault(p => p.Id == id);
    if (!publicacao.Validada) 
    {
        ModelState.AddModelError("", "Publicação ainda não validada. Não é permitido a edição.");
        return RedirectToAction("Index", "Publicacoes");
    }

    // Coloque aqui o resto da lógica
}

Browser other questions tagged

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