[vhffs-dev] [2077] first pass of UserGroup reworking, added Vhffs::UserGroup and Vhffs: :Robots::UserGroup

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 2077
Author:   gradator
Date:     2012-02-28 01:24:13 +0100 (Tue, 28 Feb 2012)
Log Message:
-----------
first pass of UserGroup reworking, added Vhffs::UserGroup and Vhffs::Robots::UserGroup

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Makefile.am
    trunk/vhffs-api/src/Vhffs/Object.pm
    trunk/vhffs-api/src/Vhffs/Robots/Group.pm
    trunk/vhffs-api/src/Vhffs/Robots/Repository.pm
    trunk/vhffs-api/src/Vhffs/Robots/User.pm
    trunk/vhffs-robots/Makefile.am
    trunk/vhffs-robots/src/user_group.pl

Added Paths:
-----------
    trunk/vhffs-api/src/Vhffs/Robots/UserGroup.pm
    trunk/vhffs-api/src/Vhffs/UserGroup.pm

Modified: trunk/vhffs-api/src/Vhffs/Makefile.am
===================================================================
--- trunk/vhffs-api/src/Vhffs/Makefile.am	2012-02-27 00:29:11 UTC (rev 2076)
+++ trunk/vhffs-api/src/Vhffs/Makefile.am	2012-02-28 00:24:13 UTC (rev 2077)
@@ -19,6 +19,7 @@
 	Stats.pm \
 	Tag.pm \
 	User.pm \
+	UserGroup.pm \
 	Panel/Acl.pm \
 	Panel/Admin.pm \
 	Panel/Auth.pm \
@@ -63,6 +64,7 @@
 	Robots/Svn.pm \
 	Robots/Git.pm \
 	Robots/User.pm \
+	Robots/UserGroup.pm \
 	Robots/Web.pm \
 	Robots/Cron.pm \
 	Services/Bazaar.pm \

Modified: trunk/vhffs-api/src/Vhffs/Object.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Object.pm	2012-02-27 00:29:11 UTC (rev 2076)
+++ trunk/vhffs-api/src/Vhffs/Object.pm	2012-02-28 00:24:13 UTC (rev 2077)
@@ -379,7 +379,6 @@
 	return $self->{'owner_gid'};
 }
 
-
 =pod
 
 =head2 get_type

Modified: trunk/vhffs-api/src/Vhffs/Robots/Group.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2012-02-27 00:29:11 UTC (rev 2076)
+++ trunk/vhffs-api/src/Vhffs/Robots/Group.pm	2012-02-28 00:24:13 UTC (rev 2077)
@@ -32,6 +32,7 @@
 use strict;
 use utf8;
 use POSIX;
+use Quota;
 use File::Path;
 use File::Basename;
 
@@ -142,7 +143,6 @@
 }
 
 sub quota {
-	require Quota;
 	my $group = shift;
 	return undef unless defined $group;
 
@@ -178,7 +178,6 @@
 }
 
 sub quota_zero {
-	require Quota;
 	my $group = shift;
 	my $path = shift;
 	return undef unless defined $group and defined $path and -d $path;
@@ -201,99 +200,4 @@
 	return 1;
 }
 
-#TODO: Rework everything below this line
-
-=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::WAITING_FOR_DELETION;
-
-	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 $groupdir = $group->get_dir;
-		my $path = $user->get_home.'/'.$group->get_groupname;
-		symlink( $groupdir, $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;
-}
-
 1;

Modified: trunk/vhffs-api/src/Vhffs/Robots/Repository.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Repository.pm	2012-02-27 00:29:11 UTC (rev 2076)
+++ trunk/vhffs-api/src/Vhffs/Robots/Repository.pm	2012-02-28 00:24:13 UTC (rev 2077)
@@ -32,6 +32,7 @@
 use strict;
 use utf8;
 use POSIX;
+use Quota;
 use File::Path;
 use File::Basename;
 
@@ -125,7 +126,6 @@
 }
 
 sub quota {
-	require Quota;
 	my $repository = shift;
 	return undef unless defined $repository;
 

Modified: trunk/vhffs-api/src/Vhffs/Robots/User.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/User.pm	2012-02-27 00:29:11 UTC (rev 2076)
+++ trunk/vhffs-api/src/Vhffs/Robots/User.pm	2012-02-28 00:24:13 UTC (rev 2077)
@@ -32,6 +32,7 @@
 use strict;
 use utf8;
 use POSIX;
+use Quota;
 use File::Path;
 use File::Basename;
 
@@ -130,13 +131,12 @@
 }
 
 sub quota {
-	require Quota;
 	my $user = shift;
 	return undef unless defined $user;
 
 	my $vhffs = $user->get_main;
 
-	my $dev = quota::getqcarg($vhffs->get_config->get_datadir);
+	my $dev = Quota::getqcarg($vhffs->get_config->get_datadir);
 
 	my $group = $user->get_group;
 	return undef unless defined $group;
@@ -171,7 +171,6 @@
 }
 
 sub quota_zero {
-	require Quota;
 	my $user = shift;
 	my $path = shift;
 	return undef unless defined $user and defined $path and -d $path;

Added: trunk/vhffs-api/src/Vhffs/Robots/UserGroup.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/UserGroup.pm	                        (rev 0)
+++ trunk/vhffs-api/src/Vhffs/Robots/UserGroup.pm	2012-02-28 00:24:13 UTC (rev 2077)
@@ -0,0 +1,99 @@
+#!%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;
+use POSIX;
+use File::Path;
+use File::Basename;
+
+use Vhffs::Functions;
+use Vhffs::Constants;
+use Vhffs::Robots;
+use Vhffs::UserGroup;
+
+package Vhffs::Robots::UserGroup;
+
+sub create {
+	my $usergroup = shift;
+	return undef unless defined $usergroup and $usergroup->get_status == Vhffs::Constants::WAITING_FOR_CREATION;
+
+	my $user = $usergroup->get_user;
+	my $group = $usergroup->get_group;
+	my $vhffs = $usergroup->get_vhffs;
+
+	unless( $vhffs->get_config->use_vhffsfs )  {
+		my $groupdir = $group->get_dir;
+		my $path = $user->get_home.'/'.$group->get_groupname;
+		symlink( $groupdir, $path );
+	}
+#	$group->add_history("The user ".$user->get_username." join the group");
+#	$user->add_history("The user ".$user->get_username." join the group");
+
+	$usergroup->set_status( Vhffs::Constants::ACTIVATED );
+	$usergroup->commit;
+
+#Vhffs::Robots::vhffs_log( $vhffs, sprintf( "Error while adding user %s to group %s" , $user->get_username , $group->get_groupname ));
+#Vhffs::Robots::vhffs_log( $vhffs, sprintf( "Added user %s to group %s" , $user->get_username , $group->get_groupname ));
+	return 1;
+}
+
+sub delete {
+	my $usergroup = shift;
+	return undef unless defined $usergroup and $usergroup->get_status == Vhffs::Constants::WAITING_FOR_DELETION;
+
+	my $user = $usergroup->get_user;
+	my $group = $usergroup->get_group;
+	my $vhffs = $usergroup->get_vhffs;
+
+	unless( $vhffs->get_config->use_vhffsfs )  {
+		my $path = $user->get_home.'/'.$group->get_groupname;
+		unlink( $path );
+	}
+
+	$usergroup->delete;
+#	$group->add_history('The user '.$user->get_username.' left the group');
+#	$user->add_history('The user '.$user->get_username.' left the group');
+
+#Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Removed user %s from group %s', $user->get_username, $group->get_groupname ));
+#Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Error while removing user %s from group %s', $user->get_username, $group->get_groupname ));
+	return 1;
+}
+
+sub modify {
+	my $usergroup = shift;
+	return undef unless defined $usergroup and $usergroup->get_status == Vhffs::Constants::WAITING_FOR_MODIFICATION;
+	$usergroup->set_status( Vhffs::Constants::ACTIVATED );
+	$usergroup->commit;
+	return 1;
+}
+
+1;

Added: trunk/vhffs-api/src/Vhffs/UserGroup.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/UserGroup.pm	                        (rev 0)
+++ trunk/vhffs-api/src/Vhffs/UserGroup.pm	2012-02-28 00:24:13 UTC (rev 2077)
@@ -0,0 +1,196 @@
+# 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.
+
+package Vhffs::UserGroup;
+
+use strict;
+use utf8;
+use DBI;
+use POSIX qw(locale_h);
+use locale;
+use Locale::gettext;
+use Vhffs::User;
+use Vhffs::Group;
+use Vhffs::Functions;
+
+=pod
+
+=head2 _new
+
+	Self constructor, almost private, please use getall methods instead.
+
+=cut
+sub _new {
+	my ($class, $vhffs, $user, $group, $state) = @_;
+
+	return undef unless defined $vhffs;
+
+	my $self = {};
+	bless($self, $class);
+
+	$self->{vhffs} = $vhffs;
+	$self->{group} = $group;
+	$self->{user} = $user;
+	$self->{state} = $state;
+
+	return $self;
+}
+
+=pod
+
+=head2 getall
+
+	my $usergroups = Vhffs::UserGroup::getall( $main, $state ;
+
+Returns an array of usergroups who matched $state.
+
+=cut
+sub getall {
+	my $vhffs = shift;
+	my $state = shift;
+	my @usergroups;
+	my @params;
+	return unless defined $vhffs;
+
+	my $query = 'SELECT uid, gid, state FROM vhffs_user_group';
+
+	if( defined $state ) {
+		$query.= ' WHERE state=?';
+		push(@params, $state);
+	}
+
+	my $request = $vhffs->{'db'}->prepare( $query );
+	$request->execute(@params);
+	while( my ($uid, $gid, $state) = $request->fetchrow_array ) {
+		my $user = Vhffs::User::get_by_uid( $vhffs, $uid );
+		my $group = Vhffs::Group::get_by_gid( $vhffs, $gid );
+		push( @usergroups, _new Vhffs::UserGroup($vhffs, $user, $group, $state) ) if defined $user and defined $group;
+	}
+
+	return \@usergroups;
+}
+
+=pod
+
+=head2 get_vhffs
+
+This method returns the Vhffs::Main object.
+
+=cut
+sub get_vhffs {
+	my $self = shift;
+	return $self->{'vhffs'};
+}
+=pod
+
+=head2 get_status
+
+Get the status of this user-group relation. The status are given in the Vhffs::Constants class.
+
+=cut
+sub get_status {
+	my $self = shift;
+	return $self->{'state'};
+}
+
+=pod
+
+=head2 set_status
+
+Change the status. The status are available as constants in Vhffs::Constants class.
+
+=cut
+sub set_status {
+	my ($self, $value) = @_;
+	$self->{'state'} = $value;
+}
+
+=pod
+
+=head2 get_user
+
+Returns the C<Vhffs::User> of this user-group relation.
+
+=cut
+sub get_user {
+	my $self = shift;
+	return $self->{'user'};
+}
+
+=pod
+
+=head2 get_group
+
+Returns the C<Vhffs::Group> of this user-group relation.
+
+=cut
+sub get_group {
+	my $self = shift;
+	return $self->{'group'};
+}
+
+=pod
+
+=head2 commit
+
+Apply all changes that were made on this user-group relation. Returns undef value if failed, true if success.
+
+=cut
+sub commit {
+	my $self = shift;
+
+	my $query = 'UPDATE vhffs_user_group SET state=? WHERE uid=? AND gid=?';
+	my $dbh = $self->get_vhffs->get_db_object;
+	my $result = $dbh->prepare($query);
+	$result->execute( $self->{'state'} , $self->{'user'}->get_uid , $self->{'group'}->get_gid ) or return undef;
+	return 1;
+}
+
+=pod
+
+=head2 delete
+
+Delete the user-group relation from the database.
+
+=cut
+sub delete {
+	my $self = shift;
+
+	# Foreign key constraints are in 'ON DELETE CASCADE' mode
+	# we don't have to bother with foreign tables deletion.
+	my $query = 'DELETE FROM vhffs_user_group WHERE uid=? AND gid=?';
+	my $dbh = $self->get_vhffs->get_db_object;
+	my $request = $dbh->prepare($query);
+	$request->execute( $self->{'user'}->get_uid , $self->{'group'}->get_gid ) or return undef;
+
+	return 1;
+}
+
+1;

Modified: trunk/vhffs-robots/Makefile.am
===================================================================
--- trunk/vhffs-robots/Makefile.am	2012-02-27 00:29:11 UTC (rev 2076)
+++ trunk/vhffs-robots/Makefile.am	2012-02-28 00:24:13 UTC (rev 2077)
@@ -44,6 +44,7 @@
 	src/user.pl \
 	src/user_quota.pl \
 	src/user_cleanup.pl \
+	src/user_group.pl \
 	src/web.pl \
 	src/web_stats.pl \
 	src/cron.pl

Modified: trunk/vhffs-robots/src/user_group.pl
===================================================================
--- trunk/vhffs-robots/src/user_group.pl	2012-02-27 00:29:11 UTC (rev 2076)
+++ trunk/vhffs-robots/src/user_group.pl	2012-02-28 00:24:13 UTC (rev 2077)
@@ -33,72 +33,28 @@
 use utf8;
 
 use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Main;
-use Vhffs::User;
-use Vhffs::Group;
-use Vhffs::Robots::Group;
-use Vhffs::Robots;
+use Vhffs::Robots::UserGroup;
 
-
-
-my $user;
-my $group;
-my $name;
 my $vhffs = init Vhffs::Main;
+exit 1 unless defined $vhffs;
 
 Vhffs::Robots::lock( $vhffs , "usergroup" );
 
-my $joins = Vhffs::Robots::Group::getall_user_group_to_create( $vhffs );
+my $usergroups = Vhffs::UserGroup::getall( $vhffs, Vhffs::Constants::WAITING_FOR_CREATION );
+foreach ( @{$usergroups} ) {
+	Vhffs::Robots::UserGroup::create( $_ );
+}
 
-if(!ref($joins)) {
-    Vhffs::Robots::vhffs_log( $vhffs, 'Database error while trying to add new users to groups' );
-} else {
-    foreach(@{$joins}) {
-        $group = Vhffs::Group::get_by_gid( $vhffs , $_->[0] );
-        if( defined $group ) {
-            $user = Vhffs::User::get_by_uid($vhffs, $_->[1] );
-    
-            if( defined $user ) {
-                if( Vhffs::Robots::Group::add_user( $user , $group , $vhffs ) < 0 ) {
-                    Vhffs::Robots::vhffs_log( $vhffs, sprintf( "Error while adding user %s to group %s" , $user->get_username , $group->get_groupname ));
-                } else {
-                    Vhffs::Robots::vhffs_log( $vhffs, sprintf( "Added user %s to group %s" , $user->get_username , $group->get_groupname ));
-                }			
-            } else {
-                Vhffs::Robots::vhffs_log( $vhffs, sprintf( "Cannot get user %s" , $_->[1] ));
-            }
-        } else {
-            Vhffs::Robots::vhffs_log( $vhffs, sprintf( "Cannot get group %s" , $_->[0] ));
-        }
-    }
+$usergroups = Vhffs::UserGroup::getall( $vhffs, Vhffs::Constants::WAITING_FOR_DELETION );
+foreach ( @{$usergroups} ) {
+	Vhffs::Robots::UserGroup::delete( $_ );
 }
 
-
-
-my $quits = Vhffs::Robots::Group::getall_user_group_to_delete( $vhffs );
-
-if(!ref($quits)) {
-    Vhffs::Robots::vhffs_log( $vhffs, 'Database error while trying to delete users from groups' );
-} else {
-    foreach(@{$quits}) {
-        $group = Vhffs::Group::get_by_gid( $vhffs , $_->[0] );
-        if( defined $group ) {
-            $user = Vhffs::User::get_by_uid( $vhffs , $_->[1] );
-            if( defined $user ) {
-                if( Vhffs::Robots::Group::del_user( $user , $group , $vhffs ) > 0 ) {
-                    Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Removed user %s from group %s', $user->get_username, $group->get_groupname ));
-                } else {
-                    Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Error while removing user %s from group %s', $user->get_username, $group->get_groupname ));
-                }
-            } else {
-                Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Cannot get user %d', $_->[1] ));
-            }
-        } else {
-            Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Cannot get group %d' , $->[0] ));
-        }
-    }
+$usergroups = Vhffs::UserGroup::getall( $vhffs, Vhffs::Constants::WAITING_FOR_MODIFICATION );
+foreach ( @{$usergroups} ) {
+	Vhffs::Robots::UserGroup::modify( $_ );
 }
 
-Vhffs::Robots::unlock( $vhffs , "usergroup" );
+Vhffs::Robots::unlock( $vhffs , 'usergroup' );
 
 exit 0;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/