Group.pm addGroup, getGroupID, getUserId, addUser2Group setPassword

hi guys. Here is a Group.pm and also some scripts. You may be wondering why all scripts are prefixed with otrs. I have /opt/OpenTRS/bin in my path. Other admins may also have it like that, so I dont with that a script overrided another system command. if an admin types otrs.something, he obviously wanted to run the otrs command. I follow the same scheme in ISPMan. So here are the scripts. otrs.addGroup: Updated. Using Group.pm Adds a group and returns the group id otrs.getGroupID: Returns numeric group id for the group Usage: otrs.getGroupID group_name otrs.getUserID: Returns numeric user id for the user otrs.addUser2Group: Usage: otrs.addUser2Group username groupname Adds the user to the group. If the user already existed in the group, it wont add again. otrs.addUser : Revised. It will accept -g groupname If a goupname is given it will also add the user to the group otrs.setPassword Changes the passord of a user Usage: otrs.setPassword username password Example: otrs.setPassword root@localhost some-pass Changed files: Group.pm (New file) User.pm (Added method GetUserIdByName best regards -- Atif Ghaffar ---------------------------. +41 78 845 31 64 ¦ tel aghaffar@developer.ch ¦ email http://atifghaffar.com ¦ www 8206786 ¦ icq #!/usr/bin/perl -w # -- # otrs.addGroup - Add Group from CLI package main; BEGIN{ use FindBin; unshift @INC, ($FindBin::Bin , "$FindBin::Bin/../"); use vars qw (%opts); use Getopt::Std; getopt('c', \%opts); unless ($ARGV[0]){ print "$FindBin::Script [-c comment] groupname"; print "\n"; exit; } } package main; use lib '/opt/OpenTRS/'; use strict; use Kernel::Config; use Kernel::System::Syslog; use Kernel::System::DB; use Kernel::System::Group; # -- # create common objects # -- my %CommonObject = (); $CommonObject{LogObject} = Kernel::System::Syslog->new(); $CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject); $CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject); $CommonObject{GroupObject} = Kernel::System::Group->new(%CommonObject); my %Param; undef %Param; #user id of the person adding the record $Param{UserID}='1'; #Validrecord $Param{ValidID}='1'; $Param{Comment}=$opts{'c'}; $Param{Name}=$ARGV[0]; if ( my $gid=$CommonObject{GroupObject}->GroupAdd(%Param) ){ print "Group added. group id is $gid\n"; } # -- exit (0); #!/usr/bin/perl -w # -- # otrs.addUser - Add User from CLI BEGIN{ use FindBin; unshift @INC, ($FindBin::Bin , "$FindBin::Bin/../"); use vars qw (%opts); use Getopt::Std; getopt('flpg', \%opts); unless ($ARGV[0]){ print "$FindBin::Script [-f firstname] [-l lastname] [-p password] [-g groupname] username\n"; print "\tif you define -g with a valid group name then the user will be added that group\n"; print "\n"; exit; } } use lib '/opt/OpenTRS/'; use strict; use Kernel::Config; use Kernel::System::Syslog; use Kernel::System::DB; use Kernel::System::User; # -- # create common objects # -- my %CommonObject = (); $CommonObject{LogObject} = Kernel::System::Syslog->new(); $CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject); $CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject); $CommonObject{UserObject} = Kernel::System::User->new(%CommonObject); my %Param; undef %Param; #user id of the person adding the record $Param{UserID}='1'; #Validrecord $Param{ValidID}='1'; $Param{Firstname}=$opts{'f'}; $Param{Lastname}=$opts{'l'}; $Param{Pw}=$opts{'p'}; $Param{Login}=$ARGV[0]; if ( $Param{UID}=$CommonObject{UserObject}->UserAdd(%Param) ){ print "User added. user id is $Param{UID}\n"; } if ($opts{'g'}) { # bring in the groups module require Kernel::System::Group; import Kernel::System::Group; $CommonObject{GroupObject} = Kernel::System::Group->new(%CommonObject); $Param{Group}=$opts{'g'}; if ($Param{GID}=$CommonObject{GroupObject}->GetGroupIdByName(%Param)){ print "Found Group.. GID is $Param{GID}" , "\n"; } else { print "Failed to get Group ID. Perhaps non-existent group..\n"; } if ($CommonObject{GroupObject}->MemberAdd(%Param)) { print "User added to group\n"; } else { print "Failed to add user to group\n"; } } # -- exit (0); #!/usr/bin/perl -w # -- # otrs.addUser - Add User from CLI use lib '/opt/OpenTRS/'; use strict; use Kernel::Config; use Kernel::System::Syslog; use Kernel::System::DB; use Kernel::System::User; use Kernel::System::Group; # -- # create common objects # -- my %CommonObject = (); $CommonObject{LogObject} = Kernel::System::Syslog->new(); $CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject); $CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject); $CommonObject{UserObject} = Kernel::System::User->new(%CommonObject); $CommonObject{GroupObject} = Kernel::System::Group->new(%CommonObject); my %Param; undef %Param; #user id of the person adding the record? #root@loclhost? right? $Param{UserID}='1'; #Validrecord $Param{ValidID}='1'; $Param{User}=$ARGV[0]; unless ($Param{UID}=$CommonObject{UserObject}->GetUserIdByName(%Param)){ print "Failed to get User ID. Perhaps non-existent user..\n"; exit; } $Param{Group}=$ARGV[1]; unless ($Param{GID}=$CommonObject{GroupObject}->GetGroupIdByName(%Param)){ print "Failed to get Group ID. Perhaps non-existent group..\n"; exit; } print "GID $Param{GID} \n"; print "UID $Param{UID} \n"; $CommonObject{GroupObject}->MemberAdd(%Param); # -- exit (0); #!/usr/bin/perl -w # -- # otrs.getGroupID - return numeric group id use lib '/opt/OpenTRS/'; use strict; use Kernel::Config; use Kernel::System::Syslog; use Kernel::System::DB; use Kernel::System::Group; # -- # create common objects # -- my %CommonObject = (); $CommonObject{LogObject} = Kernel::System::Syslog->new(); $CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject); $CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject); $CommonObject{GroupObject} = Kernel::System::Group->new(%CommonObject); my %Param; undef %Param; #user id of the person making the query $Param{UserID}='1'; $Param{Group}=$ARGV[0]; if (my $gid=$CommonObject{GroupObject}->GetGroupIdByName(%Param)){ print $gid , "\n"; } else { print "Failed to get Group ID. Perhaps non-existent group..\n"; } # -- exit (0); #!/usr/bin/perl -w # -- # otrs.getUserId - Returns numeric user id use lib '/opt/OpenTRS/'; use strict; use Kernel::Config; use Kernel::System::Syslog; use Kernel::System::DB; use Kernel::System::User; # -- # create common objects # -- my %CommonObject = (); $CommonObject{LogObject} = Kernel::System::Syslog->new(); $CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject); $CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject); $CommonObject{UserObject} = Kernel::System::User->new(%CommonObject); my %Param; undef %Param; #user id of the person making the query $Param{UserID}='1'; $Param{User}=$ARGV[0]; if (my $uid=$CommonObject{UserObject}->GetUserIdByName(%Param)){ print $uid , "\n"; } else { print "Failed to get User ID. Perhaps non-existent user..\n"; } # -- exit (0); #!/usr/bin/perl -w # -- # otrs.setPassword - Changes or Sets password for a user BEGIN{ use FindBin; unshift @INC, ($FindBin::Bin , "$FindBin::Bin/../"); unless ($ARGV[1]){ print "$FindBin::Script username password"; print "\n"; exit; } } use lib '/opt/OpenTRS/'; use strict; use Kernel::Config; use Kernel::System::Syslog; use Kernel::System::DB; use Kernel::System::User; # -- # create common objects # -- my %CommonObject = (); $CommonObject{LogObject} = Kernel::System::Syslog->new(); $CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject); $CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject); $CommonObject{UserObject} = Kernel::System::User->new(%CommonObject); my %Param; undef %Param; # user id of the person Changing the record $Param{UserID}='1'; $Param{UserLogin}=$ARGV[0]; $Param{PW}=$ARGV[1]; $CommonObject{UserObject}->SetPassword(%Param); # -- exit (0);
participants (1)
-
Atif Ghaffar