jeudi 30 avril 2015

Implement user permissions in a App

I'm working in a app where I get some user permissions from the back-end.

each of these permissions are relative to one or more Views in my App.

For this I'm creating a map with all possible permissions and their relative views.

public static final Map<String, List> ALL_PERMISSIONS = new HashMap<String, List>() {{
        put("Change-maps", new ArrayList<Integer>() {{
            add(R.id.button_change_view);
        }});
        put("Stores-info-view", new ArrayList<Integer>() {{
            add(R.id.details_fragment);
        }});
        put("Competitors-layer", new ArrayList<Integer>() {{
            add(R.id.switch_concorrentes);
        }});
        put("Context-routes", new ArrayList<Integer>(){{
            add(R.string.route_end);
            add(R.string.route_origin);
            add(R.string.route_remove);
        }});
    }};

Right now I'm having all the Views visible then I iterate for all views and disable then.

private void runPermissions() {
    Resources resources = getResources();
    for(Map.Entry<String, List> entry : disableFunc.entrySet()){

        if(entry.getValue() instanceof List) {
            for(Object itemToDisable : entry.getValue()){
                String resorceType = resources.getResourceTypeName((int)itemToDisable);
                if(itemToDisable instanceof Integer && !resorceType.equals("string")){
                    disableView((int) itemToDisable);
                }
            }
        }
    }
}

One thing that I should be doing different is that would be better to disable everything, than enable just the Views relative to the keys that I received from the back-end;

Another thing is about the Strings. For a dialog that I use in other part of the application, I use to pass an array of string to and ArrayListAdapter to then generate the Dialog items.

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final Resources resource = getResources();

    builder.setItems(mOptions, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            String selectedOption = mOptions[i];

            if(selectedOption.equals( resource.getString(R.string.coordenates) )){
                    mListener.onGetCoordenateListener(clickedLocation);
            } else if(selectedOption.equals( resource.getString(R.string.report_point) )) {
                    mListener.onReportPointListerner(clickedLocation);
            } else if(selectedOption.equals( resource.getString(R.string.route_origin) )) {
                    mListener.onPutOrigemDaRotaListener(clickedLocation);
            } else if(selectedOption.equals( resource.getString(R.string.route_end) )) {
                    mListener.onPutFimDaRotaListener(clickedLocation);
            } else if(selectedOption.equals( resource.getString(R.string.route_remove) )) {
                    mListener.onClearRote();
            }


        }
    });

    return builder.create();
}

Right now I'm not controlling these String's.

I thought too about implement a Functional interface, (just a interface with a method), than instead of using a Map of <String, List> I would use a Map of <String, FunctionalInterface>. So for each key I would implement the method (run or something like this) that would be called when iterating the map.

Is there some tips about how to control user access in a App ?

Resuming: I get a list of Keys from the back-end, to each key I'll have one or more view to enable, other than View there are some Strings that I use in ArraysAdapter's

PS: I'm really interesting about Patterns or Solutions to controlling user permissions in android application.

Aucun commentaire:

Enregistrer un commentaire