#!%PERL%
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use utf8;

package Vhffs::Robots::Group;

use Vhffs::Group;
use Vhffs::User;
use Vhffs::Functions;
use Vhffs::Constants;

sub create_groupdir
{
	my $main = shift;
	my $group = shift;

	return undef unless ( defined $main && defined $group );

	my $user = Vhffs::User::get_by_uid( $main , $group->get_owner_uid );
	my $hash = Vhffs::Functions::hash_groupname( $group->get_groupname , $main );	
	$group->add_history('Ok, Robots is going to create the directory');
	Vhffs::Functions::create_dir( $hash );
	$group->add_history('Ok, Robots finished the creation of the directory');
	chown( $group->get_owner_uid , $group->get_gid , $hash );
	chmod( 02770 , $hash );
	add_user( $user , $group , $main );
	$group->set_status( Vhffs::Constants::ACTIVATED );
	$group->commit;
	return $hash;
}


sub delete_groupdir
{
	my $main = shift;
	my $group = shift;

	return undef unless ( defined $main && defined $group );

	my $user = Vhffs::User::get_by_uid( $main , $group->get_owner_uid );
	my $hash = Vhffs::Functions::hash_groupname( $group->get_groupname , $main );	
	if( -d $hash )
	{
		my $cmd = "rm -rf $hash";
		system( $cmd );
	}
	return 0;
}


=pod

=head2 getall_user_group_to_delete

    my $quits = Vhffs::Robots::Group::getall_user_group_to_delete($main);

Returns an array ref containing all users to delete from a group. Each rows is
itself an array ref wich contains group id from which user should be deleted
in [0] and uid in [1].

=cut

sub getall_user_group_to_delete
{
	my $main = shift;	
	my $query = 'SELECT gid, uid FROM vhffs_user_group WHERE state = '.Vhffs::Constants::TO_DELETE;

	my $request = $main->get_db_object()->prepare( $query ) or return -1;
	return -1 unless( $request->execute() );

	return( $request->fetchall_arrayref() );
}


=pod

=head2 getall_user_group_to_delete

    my $joins = Vhffs::Robots::Group::getall_user_group_to_create($main);

Returns an array ref containing all users to add to a group. Each rows is
itself an array ref wich contains group id to which user should be added
in [0] and uid in [1].

=cut

sub getall_user_group_to_create
{
	my $main = shift;	
	my $query = 'SELECT gid, uid FROM vhffs_user_group WHERE state='.Vhffs::Constants::WAITING_FOR_CREATION;

	my $request = $main->{'db'}->prepare( $query ) or return -1;
	return -1 unless ( $request->execute() );

	return( $request->fetchall_arrayref() );
}

sub del_user
{
	my $user = shift;
	my $group = shift;
	my $main = shift;

	return -1 unless defined $user;
	return -2 unless defined $group;

	unless( $main->get_config->use_vhffsfs )  {
		my $path = $user->get_home.'/'.$group->get_groupname;
		unlink( $path );
	}
	$group->add_history('The user '.$user->get_username.' left the group');
	$user->add_history('The user '.$user->get_username.' left the group');

	my $query = "DELETE FROM vhffs_user_group WHERE uid='".$user->get_uid."' AND gid='".$group->get_gid."'";
	my $request = $main->{'db'}->prepare( $query ) or return -2;
	$request->execute;
}


sub add_user
{
	my $user = shift;
	my $group = shift;
	my $main = shift;
	
	return -1 if( ! defined $user );
	return -1 if( ! defined $group );

	unless( $main->get_config->use_vhffsfs )  {
		my $path = $user->get_home . "/" . $group->get_groupname;
		symlink( Vhffs::Functions::hash_groupname( $group->get_groupname , $main ) , $path );
	}
	$group->add_history("The user ".$user->get_username." join the group");
	$user->add_history("The user ".$user->get_username." join the group");

	my $query = "UPDATE vhffs_user_group SET state='".Vhffs::Constants::ACTIVATED."' WHERE uid='".$user->get_uid."' AND gid='".$group->get_gid."'";
	my $request = $main->{'db'}->prepare( $query ) or return -2;
	$request->execute;
	
	return 1;
}

sub delete
{
	my $vhffs = shift;
	my $group = shift;
	my $users = $group->get_users;
	my $user;
	foreach $user ( @{$users} )
	{
		del_user( $user , $group , $vhffs );
	}

	delete_groupdir( $vhffs , $group );
	$group->delete;
}


1;

