Permissions in the Login

Asked

Viewed 151 times

0

I’m making an application in C# where I created a database for the login system where I have the users created. In this login system I wanted to add permissions to certain users. For example, the program detects that it is the admin to log in and enables a certain button, while if it is another user this button is disabled.

Is that possible? And how?

1 answer

0

You can create a field in the table to know the type of user and/or hierarchy to know 'how it will' validate who will have rights to the components of each module.

The example below is just a base, because I do not know how your table is mounted. But initially this is how you can start thinking about your doubt.

private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                Usuario objUsuario = new Usuario();
                if (objUsuario.ConsultarUsuario(txtLogin.Text, txtSenha.Text) > 0)
                {
                    if (objUsuario.ChecarAcessoUsuario("opcCaixa", "", Metodos.codUsuario, Metodos.codGrupo))
                    {                        
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Usuário não possui acesso ao caixa loja!", "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("Login ou senha inválidos!","Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }







public bool ChecarAcessoUsuario(string menu, string acao, int usuario, string grupo)
        {
            try
            {
                var segnivel = ConsultarSegnivel1(menu);
                if (!string.IsNullOrEmpty(segnivel) && ConsultarDireitos(segnivel, usuario, grupo))
                {
                    return true;
                }
                segnivel = ConsultarSegnivel2(menu);
                if (!string.IsNullOrEmpty(segnivel) && ConsultarDireitos(segnivel, usuario, grupo))
                {
                    return true;
                }
                return false;
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Browser other questions tagged

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