In an application for user authentication via Radius I thought it would be interesting to use Design Patter Strategy with Enum.
So the code looks like this:
public enum TipoAutenticacao {
LIVRE("Acesso Livre"){
@Override
public String autentica(LoginService service, String user, String mac) throws NoResultException, AccessException{
Login login = service.findByUser(user);
return login.autentica(user, login.getPass());
}
},
MAC("Filtro de MAC"){
@Override
public String autentica(LoginService service, String user, String mac) throws NoResultException, AccessException{
Login login = service.findByUser(user);
return login.autentica(user, login.getPass(), mac);
}
};
private String descricao;
TipoAutenticacao(String descricao){
this.descricao = descricao;
}
public String getDescricao(){
return this.descricao;
}
public abstract String autentica(LoginService service , String user, String mac) throws NoResultException, AccessException;
}
Method autentica
not Login
public String autentica(String user, String pass, String mac) throws AccessException {
if(!this.mac.equals(mac))
throw new AccessException(TipoLoginResposta.MAC_INVALID);
return autentica(user, pass);
}
public String autentica(String user, String pass) throws AccessException {
if(!this.user.equals(user) || !this.pass.equals(pass))
throw new AccessException(TipoLoginResposta.USER_PASS_INVALID);
if(bloqueado)
throw new AccessException(TipoLoginResposta.BLOQUEADO);
if(!this.getUsuario().isPendenciaFinanceira())
throw new AccessException(TipoLoginResposta.PENDENCIA_FINANCEIRA);
return this.pass;
}
TipoLoginResposta
public enum TipoLoginResposta {
USER_PASS_INVALID("Login/Senha Inválidos"),
MAC_INVALID("MAC Inválido"),
PENDENCIA_FINANCEIRA("Pendência Financeira"),
BLOQUEADO("Bloqueado");
private String descricao;
TipoLoginResposta(String descricao){
this.descricao = descricao;
}
public String getDescricao(){
return this.descricao;
}
}
The idea of using AccessException
passing in the TipoLoginResposta
constructor is so that the reason why the user could not connect is later specified in the database.
Using this implementation, am I tying the code somehow?