I’m writing a command line tool based on Python’s cmd module (actually, cmd2, but they’re pretty similar). This tool connects to an REST API which requires authentication; each user has his own privilege level (think admin and regular user).
In my Python code I’m creating a do_* method corresponding to each method of the API. That means I’ll end up with things like do_an_admin_command and do_regular_user_command which will be callable by anyone running the tool, no matter of the privilege level of his user.
What I need is a way to hide every admin command available in the Python tool, if the user is not privileged. You can assume the tool knows at start up what credentials to use to authenticate to the API and whether the user is privileged or not; also, the administrative commands are a superset of the regular ones.
Any ideas?
Use subclassing to your advantage:
With the REST API, connect privileged users to the AdminShell and others to the RegularShell.
Another alternative to consider is using the precmd hook to see check for authorization.