Re: perl menubased user interface
- From: nospam@xxxxxxxxxxxxx (Jamie)
- Date: Sat, 18 Feb 2006 13:10:51 GMT
In <MF4Df.52493$tK4.376@xxxxxxxxxxxxxxxxxxxxxx>,
"Dave" <dmehler26@xxxxxxxxxx> mentions:
Hello,
I've got a project that i would appreciate some feedback on. I want to
design a perlbased user interface that i can drop in as a shell for one or
more users. My primary purpose is to allow trusted users to manage various
aspects of a unix machine, utilizing the sudo command to perform only
certain tasks.
Sounds to me like the classic case for a type of dispatch table. You can
actually use a perl package OR a hash for this, (or both) In a "perl package"
model, the package itself becomes like a database of functions.
Here is a general LAYOUT done in perl to do this, it's NOT perl code, rather
just a layout of an idea very much like some stuff I do. (though I never
intend to take mine web based, I kind of go out of my way for a 1/2 decent
terminal interface because I like terminals.)
package My::Commands;
use strict;
#................ Stuff ..............
# $cmd->run('ls');
sub run {
my($self,$cmd,@args) = @_;
# test to see if we have $cmd before trying to run it.
if(my $meth = $self->map_method($cmd) ) {
$disp = $self->meth(@args);
#
# $disp is likely an object that has some kind of "display" method,
# since you say you'll be going to a web based environment later,
# you MAY need to keep terminal IO at a minimum. (have fun with sudo...)
#
}
}
sub map_method {
my($self,$cmd) = (shift,shift);
my $meth = 'cmd_' . $cmd; # Here we convert a command (for example, "ls") to
if($self->can($meth)){ # a sub named 'cmd_ls' and return it if we have it.
return($meth);
}
return();
}
#
# as for the commands...
#
# Write yourself a little script to BUILD THESE if you have a number of them
# and they're all shell commands. (keep in mind, if you need input from the user
# and have any hope of going web-based later on, you'll need to be "param() compatible
# with input checking and all that stuff.
#
sub cmd_ls {
my($self) = shift;
return( $self->shell_wrapper('ls','-l') );
}
sub shell_wrapper {
.. Do whatever you want to wrap a shell command. Could be a
.. simple system() call or you could have special setup/teardown
.. routines..
return($MY_CUSTOM_DISPLAY_OBJECT_GOES_HERE);
}
As far as the menu, there are a wide variety of CPAN modules out there, but,
my favorite is to just design my own using Term::Cap and Term::ReadKey
Build up a base class, something with the scrollbar and other goodies, then,
inherit from it in various modules. You can rip mine if you want:
http://www.geniegate.com/other/lucy/
Do note that the whole package is very buggy. (and will likely remain some-what
buggy, it's a purely for-fun package)
The thing you'll want is the "Scroll" stuff. You can rip that out and re-apply
it to your project if you like, or, just build your own and use it for ideas.
(I play a lot of tricks on Term::Cap to get symbolic color names, you may or
may not want to do that)
By doing it this way, you don't NEED to have your list of commands in any
particular shape or form. Just inherit and implement these two methods:
list_size
getListValue
I'd avoid elaborate keymapping if you can, but do make sure and wrap your calls
to readkey in a centralized place, just in case you end up doing that in the
future. (You'll likely want to "map" up/down pgup/pgdn and friends, though,
implementing these as commands for your menu system)
user logs in, and the mainmenu comes up, i'm thinking it's numerical, 1 do
this 2 do that etc. and depending on the choice there may be submenus or it
could just execute a command. If this works i might also want to transfer
this in to a web environment, where the user wouldn't have to log in would
just pull up a web page. If anyone has anything like this or an idea how to
proceed i would welcome suggestions.
sudo won't work in web-land, so, I'd suggest turning on taint checking from
the very start. You'll need to implement your own sudo. (this was the leading
reason for the shell_wrapper() method above, rather then just using system(),
if the time comes, you'll be glad all calls to system() are done in one place)
Obviously, if you're putting this on the web... you'll need to enforce security,
your users may not be the same users who end up hacking in. Security on this
level will be important.
Jamie
--
http://www.geniegate.com Custom web programming
guhzo_42@xxxxxxxxx (rot13) User Management Solutions
.
- Prev by Date: Re: OT: Usefulness of Bats in Perl (Was: Why not Batch files??)
- Next by Date: Re: Text file splitter, date/time field
- Previous by thread: FAQ 6.3 How can I pull out lines between two patterns that are themselves on different lines?
- Next by thread: How to make Perl's regex engine "halt" after a match
- Index(es):
Relevant Pages
|