How to make only one user access to activity?

0

I need your help because I'm having a hard time making only one user access to a certain activity.

Ex: I am developing an App where settings screen only I will have access with my user, no other. I want to leave this set up in code just my userId with access to the screen.

private void openRestrictedSettings() {
    if(userId.equals("id")){
        Intent intentRestrictedSettings = new Intent(TelaPrincipalActivity.this, ActivityRestrictedSettings.class);
        startActivity(intentRestrictedSettings);
    }else {
        Toast.makeText(this, "Apenas usuários autorizados podem acessar as configurações", Toast.LENGTH_SHORT).show();
    }
}

"userPermissions" exists only in my user, no other user will have this.

How can I make an activity open only by who owns userPermissions?

    
asked by anonymous 07.11.2018 / 11:57

1 answer

1

Some things you can try:

Constants.java

public final class Constantes {
    // TODO: Revisar o ID 
    public static final String UID_ADMIN = "dGVzdGVAdGVzdGUuY29t";

    private Constantes() {
        // Sem instâncias
    }
}

Function to open the screen

private void openRestrictedSettings() {
    if (allowRestrictedSettings()) {
        Intent intentRestrictedSettings = new Intent(TelaPrincipalActivity.this, ActivityRestrictedSettings.class);
        startActivity(intentRestrictedSettings);
    }
}

private boolean allowRestrictedSettings() {
    /*
        Supondo que há um sistema de login com firebase
        e que você já tem uma classe UserEmail com o campo "id"
        e um objeto dessa classe disponível para verificação
     */
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    if (currentUser == null) {
        Toast.makeText(this, "Por favor, faça login com sua conta", Toast.LENGTH_SHORT).show();
        return false;
    }

    String userId = userEmail.getUid();
    String userPermissions = userEmail.getuserPermissions();

    if (!userId.equals(Constantes.UID_ADMIN)) {
        Toast.makeText(this, "Apenas usuários autorizados podem acessar as configurações", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (TextUtils.isEmpty(userPermissions) || !userPermissions.equals("admin")) {
        Toast.makeText(this, "Apenas usuários autorizados podem acessar as configurações", Toast.LENGTH_SHORT).show();
        return false;
    }

    // Ou direto pelo firebase auth
    if (!currentUser.getUid().equals(Constantes.UID_ADMIN)) {
        Toast.makeText(this, "Apenas usuários autorizados podem acessar as configurações", Toast.LENGTH_SHORT).show();
        return false;
    }

    return true;
}
    
07.11.2018 / 15:37