[vhffs-dev] [971] Add a robot to automatically cleanup unused users.

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


Revision: 971
Author:   gradator
Date:     2007-10-08 13:02:00 +0000 (Mon, 08 Oct 2007)

Log Message:
-----------
Add a robot to automatically cleanup unused users.

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Object.pm
    trunk/vhffs-api/src/Vhffs/User.pm
    trunk/vhffs-robots/Makefile.am

Added Paths:
-----------
    trunk/vhffs-robots/src/user_cleanup.pl


Modified: trunk/vhffs-api/src/Vhffs/Object.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Object.pm	2007-10-06 14:07:25 UTC (rev 970)
+++ trunk/vhffs-api/src/Vhffs/Object.pm	2007-10-08 13:02:00 UTC (rev 971)
@@ -416,7 +416,10 @@
 	setlocale( LC_ALL , $user->get_lang );
 
 	my $mail = sprintf(
-		gettext( "Hello,\n\nYour request for a %s (%s) on %s was accepted.\nPlease wait while we are creating your object.\n\nCheers,\nThe moderator team\n\n---------\n%s\n%s" ) ,
+		gettext( "Hello %s %s,\n\nYour request for a %s (%s) on %s was accepted.\nPlease wait while we are creating your object.\n\nCheers,\nThe moderator team\n\n---------\n%s\n%s" ) ,
+
+		$user->get_firstname,
+		$user->get_lastname,
 		Vhffs::Functions::type_string_from_type_id( $self->get_type ),
 		$self->get_label,
 		$vhffs->get_config->get_host_name,
@@ -462,7 +465,10 @@
 	setlocale( LC_ALL , $user->get_lang );
 
 	my $mail = sprintf(
-		gettext( "Hello,\n\nYour request for a %s (%s) on %s was refused.\n\nThe reason of refusal given by moderators is:\n%s\n\nYou can change the description and submit it again for moderation on the\npanel. You can delete this object on the panel if you have made\na mistake. Don't be upset, if you don't understand why your request has\nbeen refused, just reply to this email !\n\nCheers,\nThe moderator team\n\n---------\n%s\n%s" ) ,
+		gettext( "Hello %s %s,\n\nYour request for a %s (%s) on %s was refused.\n\nThe reason of refusal given by moderators is:\n%s\n\nYou can change the description and submit it again for moderation on the\npanel. You can delete this object on the panel if you have made\na mistake. Don't be upset, if you don't understand why your request has\nbeen refused, just reply to this email !\n\nCheers,\nThe moderator team\n\n---------\n%s\n%s" ) ,
+
+		$user->get_firstname,
+		$user->get_lastname,
 		Vhffs::Functions::type_string_from_type_id( $self->get_type ),
 		$self->get_label,
 		$vhffs->get_config->get_host_name,
@@ -505,8 +511,10 @@
 	setlocale( LC_ALL , $user->get_lang );
 
 	my $mail = sprintf(
-		gettext( "Hello,\n\nYour %s (%s) on %s has been deleted.\n\nThis is because it have been refused a long time ago and you didn't\nsubmit an update since.\n\nFor reminder, the reason of refusal was:\n%s\n\nDon't be upset, submit it again if you forgot to update it in time.\nIf you need further information, just reply to this email !\n\nCheers,\nThe moderator team\n\n---------\n%s\n%s" ) ,
+		gettext( "Hello %s %s,\n\nYour %s (%s) on %s has been deleted.\n\nThis is because it have been refused a long time ago and you didn't\nsubmit an update since.\n\nFor reminder, the reason of refusal was:\n%s\n\nDon't be upset, submit it again if you forgot to update it in time.\nIf you need further information, just reply to this email !\n\nCheers,\nThe moderator team\n\n---------\n%s\n%s" ) ,
 
+		$user->get_firstname,
+		$user->get_lastname,
 		Vhffs::Functions::type_string_from_type_id( $self->get_type ),
 		$self->get_label,
 		$vhffs->get_config->get_host_name,

Modified: trunk/vhffs-api/src/Vhffs/User.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/User.pm	2007-10-06 14:07:25 UTC (rev 970)
+++ trunk/vhffs-api/src/Vhffs/User.pm	2007-10-08 13:02:00 UTC (rev 971)
@@ -38,6 +38,9 @@
 use base qw(Vhffs::Object);
 use strict;
 use DBI;
+use POSIX qw(locale_h);
+use locale;
+use Locale::gettext;
 use Vhffs::Group;
 use Vhffs::Functions;
 use Vhffs::Acl;
@@ -324,6 +327,46 @@
 }
 
 
+# Delete this user with a notice mail
+sub pendingdeletion_withmail
+{
+	my $self = shift;
+
+	my $vhffs = $self->{main};
+
+	# TODO: write a beautiful module for INTL
+	bindtextdomain('vhffs', $vhffs->get_config->get_intldir);
+	textdomain('vhffs');
+
+	my $prevlocale = setlocale( LC_ALL );
+	setlocale( LC_ALL , $self->get_lang );
+
+	my $mail = sprintf(
+		gettext( "Hello %s %s,\n\nYour account (%s) on %s has been deleted.\n\nThis is because it was left unused for a long time\n\nDon't be upset, create it again and reply to this email to tell us\nwhy you really need to keep your account.\nIf you need further information, just reply to this email !\n\nCheers,\nThe moderator team\n\n---------\n%s\n%s\n" ) ,
+
+		$self->get_firstname,
+		$self->get_lastname,
+		$self->get_username,
+		$vhffs->get_config->get_host_name,
+		$vhffs->get_config->get_host_name,
+		$vhffs->get_config->get_panel->{url}
+		);
+
+	my $subject = sprintf(
+		gettext('Your account (%s) on %s has been deleted because it was unused for a long time'),
+		$self->get_username,
+		$vhffs->get_config->get_host_name
+		);
+
+	Vhffs::Functions::send_mail( $vhffs->get_config->get_moderator_mail , $self->get_mail , $vhffs->get_config->get_mailtag , $subject , $mail );
+
+	setlocale( LC_ALL , $prevlocale );
+
+	$self->set_status( Vhffs::Constants::TO_DELETE );
+	$self->commit;
+}
+
+
 sub uid_exists
 {
     my ( $dbh , $uid ) = @_;
@@ -654,41 +697,60 @@
 
 }
 
+
 sub getall
 {
 	my $vhffs = shift;
-	my $state = shift ;
-	my $name = shift ;
+	my $state = shift;
+	my $name = shift;
 	my @users;
-    my @params;
-	return if( ! defined $vhffs );
+	my @params;
+	return unless defined $vhffs;
 
-	my $query;
-	$query = 'SELECT username FROM vhffs_users vu INNER JOIN vhffs_object o ON o.object_id = vu.object_id WHERE o.object_id=vu.object_id ';
-    if(defined $name) {
-        $name = "\%$name\%";
-        $query.= ' AND ( vu.username LIKE ? OR vu.firstname LIKE ? OR vu.lastname LIKE ? )';
-        push @params, $name, $name, $name;
-    }
+	my $query = 'SELECT username FROM vhffs_users vu INNER JOIN vhffs_object o ON o.object_id=vu.object_id WHERE o.object_id=vu.object_id ';
+
+	if(defined $name) {
+		$name = '%'.$name.'%';
+		$query .= ' AND ( vu.username LIKE ? OR vu.firstname LIKE ? OR vu.lastname LIKE ? )';
+		push @params, $name, $name, $name;
+	}
 	(push(@params, $state), $query.= ' AND o.state=?') if( defined $state );
-	$query.="ORDER BY username";
 	
+	$query .= 'ORDER BY username';
 	
 	my $request = $vhffs->{'db'}->prepare( $query );
 	$request->execute(@params);
 	while( my ($name) = $request->fetchrow_array )
 	{   
-	    my $user = Vhffs::User::get_by_username( $vhffs , $name );
-	    if( defined $user )
-	    {   
-		push @users , $user;
-	    }
+		my $user = Vhffs::User::get_by_username( $vhffs , $name );
+		push( @users , $user ) if( defined $user );
 	}
+
 	return \@users;
-	
 }
 
 
+sub get_unused_accounts
+{
+	my $vhffs = shift;
+	my $age = shift;
+	my @users;
+	return unless defined $vhffs;
+
+	my $query = 'SELECT u.uid FROM vhffs_users u INNER JOIN vhffs_object o ON o.object_id=u.object_id WHERE u.admin=? AND o.state=? AND o.date_creation<? AND u.uid NOT IN (SELECT u.uid FROM vhffs_users u INNER JOIN vhffs_user_group ug ON u.uid=ug.uid)';
+
+	my $request = $vhffs->{'db'}->prepare( $query );
+	$request->execute( Vhffs::Constants::USER_NORMAL , Vhffs::Constants::ACTIVATED , time() - $age );
+	while( my $uid = $request->fetchrow_array )
+	{   
+		my $user = Vhffs::User::get_by_uid( $vhffs , $uid );
+		push( @users , $user ) if( defined $user );
+	}
+
+	return \@users;
+}
+
+
 sub get_last_users
 {
 	my $vhffs = shift;

Modified: trunk/vhffs-robots/Makefile.am
===================================================================
--- trunk/vhffs-robots/Makefile.am	2007-10-06 14:07:25 UTC (rev 970)
+++ trunk/vhffs-robots/Makefile.am	2007-10-08 13:02:00 UTC (rev 971)
@@ -58,6 +58,7 @@
 	src/user_group.pl \
 	src/user_setquota.pl \
 	src/user_getquota.pl \
+	src/user_cleanup.pl \
 	src/web_create.pl \
 	src/web_delete.pl \
 	src/web_stats.pl

Added: trunk/vhffs-robots/src/user_cleanup.pl
===================================================================
--- trunk/vhffs-robots/src/user_cleanup.pl	                        (rev 0)
+++ trunk/vhffs-robots/src/user_cleanup.pl	2007-10-08 13:02:00 UTC (rev 971)
@@ -0,0 +1,53 @@
+#!%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 lib '%VHFFS_LIB_DIR%';
+use Vhffs::Main;
+use Vhffs::Robots;
+use Vhffs::User;
+use Vhffs::Constants;
+
+my $vhffs = init Vhffs::Main;
+exit 1 unless $vhffs;
+
+Vhffs::Robots::lock( $vhffs , 'user' );
+
+my $users = Vhffs::User::get_unused_accounts( $vhffs , 1296000 );   # 15 days
+foreach my $user ( @{$users} )
+{
+	Vhffs::Robots::vhffs_log( sprintf( 'deleted user %s because it was unused' , $user->get_username ) , $vhffs );
+	$user->pendingdeletion_withmail;
+}
+
+Vhffs::Robots::unlock( $vhffs , 'user' );
+
+exit 0;


Property changes on: trunk/vhffs-robots/src/user_cleanup.pl
___________________________________________________________________
Name: svn:executable
   + *


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