How to create administrator profile from that code?

Asked

Viewed 900 times

0

How to create an administrator profile from that code, and when logging in a check is made whether you are an ordinary user or an administrator?

Daousuario.java

package br.edu.facema.model.dao;

import br.edu.facema.model.bean.Usuario;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DaoUsuario {

public Connection getConnection() {
    Connection connection = null;
    try {
        Class.forName("org.postgresql.Driver");
        connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "123");
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return connection;
}

public Usuario getUsuario(String login, String senha) {
    Connection c = this.getConnection();
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        ps = c.prepareStatement("select id, nome from usuario where login = ? and senha = ?");
        ps.setString(1, login);
        ps.setString(2, senha);

        rs = ps.executeQuery();

        if (rs.next()) {
            Usuario user = new Usuario();
            user.setId(rs.getInt("id"));
            user.setLogin(login);
            user.setSenha(senha);
            user.setNome(rs.getString("nome"));

            return user;

        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {;
            }
            rs = null;
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {;
            }
            ps = null;
        }
        if (c != null) {
            try {
                c.close();
            } catch (SQLException e) {;
            }
            c = null;
        }
    }
    return null;
}

}

package br.edu.facema.model.bean;

import java.io.Serializable;

public class Usuario implements Serializable {

private int id;

private String nome;

private String login;

private String senha;

private int tipoUsuario;

public int getTipoUsuario() {
    return tipoUsuario;
}

public void setTipoUsuario(int tipoUsuario) {
    this.tipoUsuario = tipoUsuario;
}

public int getId() {

    return id;

}

public void setId(int id) {

    this.id = id;

}

public String getLogin() {

    return login;

}

public void setLogin(String login) {

    this.login = login;

}

public String getNome() {

    return nome;

}

public void setNome(String nome) {

    this.nome = nome;

}

public String getSenha() {

    return senha;

}

public void setSenha(String senha) {

    this.senha = senha;

}

}
  • JSP itself has no session or authentication feature? Would it be cool to use something standard than reinventing the wheel.

  • It’s a college job, for learning purposes.

  • Then the easiest would be to implement an Administrator class, which inherits the User characteristics, and check on the page itself, if the user who is arriving is not from the Administrator instance, redirects the page. I think it would be the easiest. Or to get better at designing, create a Pessoa interface, which would facilitate in the hr of the check, since both user and administrator would implement this interface.

  • I’ll try to make.

1 answer

1

Make another one of this model, for example "Administrator", allowing you access to it.

boolean permite;
public void autentifica(DaoUsuario user, DaoAdministrador admin) {
    if (permite) {
        user.Usuario(login,senha);
    } else {
        admin.Administrador(login,senha);
    }
}

Browser other questions tagged

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