From f0ea9667eaca528221a87dc455cdd1900f50c4c1 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 8 Dec 2016 07:16:01 +0100 Subject: ask for confirmation on user/group removal diff --git a/lib/RHRD/utils.pm b/lib/RHRD/utils.pm index 756707b..ad8e7b8 100755 --- a/lib/RHRD/utils.pm +++ b/lib/RHRD/utils.pm @@ -213,6 +213,20 @@ sub cmdline_dow return (undef, 'invalid day-of-week', CMDLINE_DOW_HINT); } +sub cmdline_ask_yn +{ + my ($question) = @_; + + print "$question (y/N)? "; + my $answer = ; + chomp($answer) if(defined $answer); + unless(defined $answer && lc($answer) eq 'y') { + print "aborting.\n"; + return 0; + } + return 1; +} + sub fetch_parse_json { my ($url, $ua_str, $headers, $queries) = @_; diff --git a/test/ask b/test/ask new file mode 100755 index 0000000..6810c65 --- /dev/null +++ b/test/ask @@ -0,0 +1,44 @@ +#!/usr/bin/perl -w +# +# rhrdlibs +# +# Copyright (C) 2015-2016 Christian Pointner +# +# This file is part of rhrdlibs. +# +# rhrdlibs is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# any later version. +# +# rhrdlibs 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with rhrdlibs. If not, see . +# + +use strict; +use Data::Dumper::Simple; + +use lib "../lib/"; +use RHRD::rddb; + +my ($ctx, undef, $errorstring) = RHRD::rddb::init(); +if(defined $ctx) { + + if(!RHRD::utils::cmdline_ask_yn("do you wan't to print context")) { + exit 0; + } + + print Dumper($ctx); + + RHRD::rddb::destroy($ctx); +} else { + print "$errorstring\n"; + exit 1; +} + +exit 0 diff --git a/utils/rhrd-group b/utils/rhrd-group index a66597e..2b12c82 100755 --- a/utils/rhrd-group +++ b/utils/rhrd-group @@ -26,8 +26,9 @@ use RHRD::rddb; sub print_usage { print STDERR "Usage: rhrd-group list [ (shows|jingles|pools) ]\n" . - " rhrd-group (check|remove|get-members|get-carts|get-reports) \n" . + " rhrd-group (check|get-members|get-carts|get-reports) \n" . " rhrd-group add [ ]\n" . + " rhrd-group remove [ -f ] \n" . " rhrd-group (add-member|remove-member|is-member) \n" . " rhrd-group set-carts [ [ ]]\n" . " rhrd-group set-reports \n"; @@ -77,7 +78,13 @@ sub add sub remove { - my ($ctx, $groupname) = @_; + my ($ctx, $groupname, $force) = @_; + + if(!$force) { + return 1 if(!RHRD::utils::cmdline_ask_yn("do you really wan't to delete group '$groupname'")); + print("\n"); + } + print("removing user: $groupname\n"); my @carts = RHRD::rddb::get_show_group_carts_used($ctx, $groupname); if(!defined $carts[0] && defined $carts[1]) { @@ -329,11 +336,20 @@ if(defined $ctx) { } } elsif($cmd eq "remove") { - if($num_args != 2) { + if($num_args < 2 || $num_args > 3) { print_usage(); $ret = 1; } else { - $ret = remove($ctx, $groupname); + if($num_args == 3) { + if($ARGV[1] ne '-f') { + print_usage(); + $ret = 1; + } else { + $ret = remove($ctx, $ARGV[2], 1); + } + } else { + $ret = remove($ctx, $groupname, 0); + } } } elsif($cmd eq "get-members") { diff --git a/utils/rhrd-user b/utils/rhrd-user index 4d613b6..b6d5ac6 100755 --- a/utils/rhrd-user +++ b/utils/rhrd-user @@ -27,8 +27,9 @@ use String::MkPasswd qw(mkpasswd); sub print_usage { print STDERR "Usage: rhrd-user list\n" . - " rhrd-user (check|remove|groups|is-grids) \n" . + " rhrd-user (check|groups|is-grids) \n" . " rhrd-user add [ ]\n" . + " rhrd-user remove [ -f ] \n" . " rhrd-user set-grids (1|0)\n"; } @@ -72,7 +73,13 @@ sub add sub remove { - my ($ctx, $username) = @_; + my ($ctx, $username, $force) = @_; + + if(!$force) { + return 1 if(!RHRD::utils::cmdline_ask_yn("do you really want to delete user '$username'")); + print("\n"); + } + print("removing user: $username\n"); my @results = RHRD::rddb::remove_user($ctx, $username); if(!defined $results[0] && defined $results[2]) { @@ -166,11 +173,20 @@ if(defined $ctx) { $ret = add($ctx, $username, $ARGV[2]); } } elsif($cmd eq "remove") { - if($num_args != 2) { + if($num_args < 2 || $num_args > 3) { print_usage(); $ret = 1; } else { - $ret = remove($ctx, $username); + if($num_args == 3) { + if($ARGV[1] ne '-f') { + print_usage(); + $ret = 1; + } else { + $ret = remove($ctx, $ARGV[2], 1); + } + } else { + $ret = remove($ctx, $username, 0); + } } } elsif($cmd eq "groups") { if($num_args != 2) { -- cgit v0.10.2