#!/usr/bin/perl -w
# --
# bin/otrs.addUser2Group - Add User to a Group
# Copyright (C) 2002 Atif Ghaffar <aghaffar@developer.ch>
# Copyright (C) 2001-2006 OTRS AG, http://otrs.org/
# --
# $Id: otrs.addUser2Group,v 1.18 2008/05/08 09:36:57 mh Exp $
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# --

use strict;
use warnings;

use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";

use Kernel::Config;
use Kernel::System::Log;
use Kernel::System::Time;
use Kernel::System::DB;
use Kernel::System::Main;
use Kernel::System::User;
use Kernel::System::Group;

my %Param        = ();
my %CommonObject = ();
my %Opts         = ();

use Getopt::Std;
getopt( 'guph', \%Opts );

if ( $Opts{h} ) {
    print STDERR "Usage: bin/otrs.addUser2Group -g groupname -u username -p ro|rw\n";
    exit;
}

if ( !$Opts{g} ) {
    print STDERR "ERROR: Need -g groupname\n";
    exit 1;
}
if ( !$Opts{u} ) {
    print STDERR "ERROR: Need -u username\n";
    exit 1;
}
if ( !$Opts{p} ) {
    print STDERR "ERROR: Need -p permission\n";
    exit 1;
}

# create common objects
$CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject);
$CommonObject{LogObject}
    = Kernel::System::Log->new( %CommonObject, LogPrefix => 'otrs.addUser2Group' );
$CommonObject{TimeObject}  = Kernel::System::Time->new(%CommonObject);
$CommonObject{MainObject}  = Kernel::System::Main->new(%CommonObject);
$CommonObject{DBObject}    = Kernel::System::DB->new(%CommonObject);
$CommonObject{UserObject}  = Kernel::System::User->new(%CommonObject);
$CommonObject{GroupObject} = Kernel::System::Group->new(%CommonObject);

# user id
$Param{UserID} = '1';

# valid id
$Param{ValidID} = '1';

$Param{Permission}->{ $Opts{p} } = 1;
$Param{UserLogin}                = $Opts{u};
$Param{Group}                    = $Opts{g};

unless ( $Param{UID} = $CommonObject{UserObject}->UserLookup( UserLogin => $Param{UserLogin} ) ) {
    print STDERR "ERROR: Failed to get User ID. Perhaps non-existent user..\n";
    exit 1;
}

unless ( $Param{GID} = $CommonObject{GroupObject}->GroupLookup(%Param) ) {
    print STDERR "ERROR: Failed to get Group ID. Perhaps non-existent group..\n";
    exit;
}

print "GID: $Param{Group}/$Param{GID} \n";
print "UID: $Param{User}/$Param{UID} \n";
print "Perrmission: $Opts{p} \n";

if ( !$CommonObject{GroupObject}->GroupMemberAdd(%Param) ) {
    print STDERR "ERROR: Can't add user to group\n";
    exit 1;
}
else {
    exit(0);
}
