From 6aa72c3b376c0223ae3b7fd819db246cd0a4c5b6 Mon Sep 17 00:00:00 2001
From: zarzuelo <zarzuelo@gmail.com>
Date: Wed, 4 Jan 2012 15:09:02 +0000
Subject: [PATCH] 2012-01-04  Sergio Martin <sergio.martin@artica.es>

	*  lib/PandoraFMS/DB.pm
	util/pandora_manage.pl: Added --enable_user and
	--disable_user options to CLI



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@5321 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_server/ChangeLog              |  6 +++
 pandora_server/lib/PandoraFMS/DB.pm   | 11 +++++
 pandora_server/util/pandora_manage.pl | 62 ++++++++++++++++++++++++++-
 3 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog
index ff51da19f5..8f833f9c99 100644
--- a/pandora_server/ChangeLog
+++ b/pandora_server/ChangeLog
@@ -1,3 +1,9 @@
+2012-01-04  Sergio Martin <sergio.martin@artica.es>
+
+	*  lib/PandoraFMS/DB.pm
+	util/pandora_manage.pl: Added --enable_user and
+	--disable_user options to CLI
+
 2011-12-29  Sergio Martin <sergio.martin@artica.es>
 
 	* lib/PandoraFMS/Core.pm
diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm
index 7b927918bf..7f8c9134a0 100644
--- a/pandora_server/lib/PandoraFMS/DB.pm
+++ b/pandora_server/lib/PandoraFMS/DB.pm
@@ -67,6 +67,7 @@ our @EXPORT = qw(
 		get_server_id
 		get_template_id
 		get_template_module_id
+		get_user_disabled
 		is_agent_address
 		is_group_disabled
 	);
@@ -279,6 +280,16 @@ sub get_module_id ($$) {
 	return defined ($rc) ? $rc : -1;
 }
 
+##########################################################################
+## Return disabled bit frin a user.
+##########################################################################
+sub get_user_disabled ($$) {
+	my ($dbh, $user_id) = @_;
+
+	my $rc = get_db_value ($dbh, "SELECT disabled FROM tusuario WHERE id_user = ?", safe_input($user_id));
+	return defined ($rc) ? $rc : -1;
+}
+
 ##########################################################################
 ## Return plugin ID given the plugin name.
 ##########################################################################
diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl
index 66c920ba79..905e44872f 100755
--- a/pandora_server/util/pandora_manage.pl
+++ b/pandora_server/util/pandora_manage.pl
@@ -434,6 +434,8 @@ sub help_screen{
     help_screen_line('--disable_policy_alerts', '<policy_name>', 'Disable all the alerts of a policy');
     help_screen_line('--create_group', '<group_name> [<parent_group_name>]', 'Create an agent group');
     help_screen_line('--add_agent_to_policy', '<agent_name> <policy_name>', 'Add an agent to a policy');
+    help_screen_line('--disable_user', '<user_id>', 'Disable a given user');
+    help_screen_line('--enable_user', '<user_id>', 'Enable a given user');
     print "\n";
 	exit;
 }
@@ -1463,6 +1465,56 @@ sub cli_enable_eacl ($$) {
     exit;
 }
 
+###############################################################################
+# Enable user
+# Related option: --enable_user
+###############################################################################
+sub cli_user_enable () {
+	my $user_id = @ARGV[2];
+
+	my $user_disabled = get_user_disabled ($dbh, $user_id);
+	
+	exist_check($user_disabled,'user',$user_id);
+
+	if($user_disabled == 0) {
+		print "[INFO] The user '$user_id' is already enabled. Nothing to do.\n\n";
+		exit;
+	}
+	
+	print "[INFO] Enabling user '$user_id'\n\n";
+
+	$user_id = safe_input($user_id);
+
+    db_do ($dbh, "UPDATE tusuario SET `disabled` = '0' WHERE `id_user` = '$user_id'");
+    	
+    exit;
+}
+
+###############################################################################
+# Disable user
+# Related option: --disable_user
+###############################################################################
+sub cli_user_disable () {
+	my $user_id = @ARGV[2];
+
+	my $user_disabled = get_user_disabled ($dbh, $user_id);
+	
+	exist_check($user_disabled,'user',$user_id);
+
+	if($user_disabled == 1) {
+		print "[INFO] The user '$user_id' is already disabled. Nothing to do.\n\n";
+		exit;
+	}
+	
+	print "[INFO] Disabling user '$user_id'\n\n";
+
+	$user_id = safe_input($user_id);
+	
+    db_do ($dbh, "UPDATE tusuario SET `disabled` = '1' WHERE `id_user` = '$user_id'");
+    	
+    exit;
+}
+
 ###############################################################################
 ###############################################################################
 # MAIN
@@ -1612,9 +1664,17 @@ sub pandora_manage_main ($$$) {
 			cli_create_group();
 		}
 		elsif ($param eq '--add_agent_to_policy') {
-			param_check($ltotal, 2, 0);
+			param_check($ltotal, 2);
 			cli_policy_add_agent();
 		}
+		elsif ($param eq '--enable_user') {
+			param_check($ltotal, 1);
+			cli_user_enable();
+		}
+		elsif ($param eq '--disable_user') {
+			param_check($ltotal, 1);
+			cli_user_disable();
+		}
 		else {
 			print "[ERROR] Invalid option '$param'.\n\n";
 			$param = '';