From 93a958c9f5cd178adc07a2c91a333b283a7fe872 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@spreadspace.org>
Date: Sun, 26 Jul 2015 07:05:43 +0200
Subject: added script to handle rivenell users


diff --git a/Makefile.PL b/Makefile.PL
index de9de18..d9d78ee 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -1,7 +1,7 @@
 use ExtUtils::MakeMaker;
 use 5.004;
 
-my @utils = qw(get-rd-token get-rd-week update-rd-tokens);
+my @utils = qw(get-rd-token get-rd-week update-rd-tokens rd-user);
 
 WriteMakefile(
   NAME       => 'RHRD',
diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index ee49444..b887c66 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -110,6 +110,93 @@ sub check_token
   return (0, 'ERROR', "wrong password");
 }
 
+sub add_user
+{
+  my ($dbh, $username, $token) = @_;
+
+  my $sql = qq{insert into USERS (LOGIN_NAME, FULL_NAME, PHONE_NUMBER, DESCRIPTION, PASSWORD, ENABLE_WEB, ADMIN_USERS_PRIV, ADMIN_CONFIG_PRIV, CREATE_CARTS_PRIV, DELETE_CARTS_PRIV, MODIFY_CARTS_PRIV, EDIT_AUDIO_PRIV, ASSIGN_CART_PRIV, CREATE_LOG_PRIV, DELETE_LOG_PRIV, DELETE_REC_PRIV, PLAYOUT_LOG_PRIV, ARRANGE_LOG_PRIV, MODIFY_TEMPLATE_PRIV, ADDTO_LOG_PRIV, REMOVEFROM_LOG_PRIV, CONFIG_PANELS_PRIV, VOICETRACK_LOG_PRIV, EDIT_CATCHES_PRIV, ADD_PODCAST_PRIV, EDIT_PODCAST_PRIV, DELETE_PODCAST_PRIV) values ( ?, "", "", "", ? , "N", "N", "N", "Y", "Y", "N", "Y", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N");};
+  my $sth = $dbh->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+
+  my $cnt = $sth->execute($username, $token)
+    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+  $sth->finish();
+  return ($cnt, 'OK', "success");
+}
+
+sub remove_user
+{
+  my ($dbh, $username) = @_;
+
+  my @actions = ({
+      # Delete RSS Feed Perms
+      sql => qq{delete from FEED_PERMS where USER_NAME = ?;},
+      name => 'podcast feed assignments',
+      cnt => 0
+    }, {
+      # Delete Member User Perms
+      sql => qq{delete from USER_PERMS where USER_NAME = ?;},
+      name => 'group assignments',
+      cnt => 0
+    }, {
+      # Delete from User List
+      sql => qq{delete from USERS where LOGIN_NAME = ?;},
+      name => 'user entries',
+      cnt => 0
+    }, {
+      # Delete from Cached Web Connections
+      sql => qq{delete from WEB_CONNECTIONS where LOGIN_NAME = ?;},
+      name => 'cached web connections',
+      cnt => 0
+    });
+
+  for my $href (@actions) {
+    my $sth = $dbh->prepare($href->{sql})
+      or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+    delete($href->{sql});
+
+    $href->{cnt} = $sth->execute($username)
+      or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+    $sth->finish();
+  }
+
+  return @actions;
+}
+
+sub check_user
+{
+  my ($dbh, $username) = @_;
+
+  my $sql = qq{select count(*) from USERS where LOGIN_NAME = ?;};
+  my $sth = $dbh->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+
+  $sth->execute($username)
+    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+  my ($cnt) = $sth->fetchrow_array();
+  $sth->finish();
+
+  if ($cnt != 0) {
+    $sql = qq{select count(*) from STATIONS where DEFAULT_NAME = ?;};
+    my $sth = $dbh->prepare($sql)
+      or return (undef, 'ERROR', "Database Error: " . $dbh->errstr);
+
+    $sth->execute($username)
+      or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+    ($cnt) = $sth->fetchrow_array();
+    $sth->finish();
+
+    if($cnt) { return (2, 'OK', "user '" . $username . "' is known by rivendell and is the default user of at least one station"); }
+    else { return (1, 'OK', "user '" . $username . "' is known by rivendell and isn't the default user of any station"); }
+  }
+
+  return (0, 'OK', "user '" . $username . "' not known by rivendell");
+}
+
 sub get_users
 {
   my ($dbh) = @_;
diff --git a/utils/rd-user b/utils/rd-user
new file mode 100755
index 0000000..22a41ec
--- /dev/null
+++ b/utils/rd-user
@@ -0,0 +1,73 @@
+#!/usr/bin/perl -w
+#
+#  rhrdlibs
+#
+#  Copyright (C) 2015 Christian Pointner <equinox@helsinki.at>
+#
+#  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 <http://www.gnu.org/licenses/>.
+#
+
+use strict;
+use RHRD::rddb;
+use String::MkPasswd qw(mkpasswd);
+
+# this is ridiculous but makes it a little harder to create/remove users...
+if ($> != 0 ) {
+  print STDERR "this must be run as root!\n";
+  exit 1;
+}
+
+my $num_args = $#ARGV + 1;
+if ($num_args != 2) {
+  print STDERR "Usage: rd-user (check|add|remove) <username>\n";
+  exit 1;
+}
+
+my $cmd = $ARGV[0];
+my $username = $ARGV[1];
+
+my ($dbh, undef, $errorstring) = RHRD::rddb::opendb();
+if(defined $dbh) {
+  if($cmd eq "check") {
+    (my $result, my $status, $errorstring) = RHRD::rddb::check_user($dbh, $username);
+    print "$result, $status: $errorstring\n";
+  } elsif($cmd eq "add") {
+    my $token = mkpasswd(-length => 16, -minnum => 3, -minupper => 3, -minspecial => 2);
+    (my $cnt, undef, $errorstring) = RHRD::rddb::add_user($dbh, $username, $token);
+    unless(defined $cnt) {
+      print "$errorstring\n";
+      RHRD::rddb::closedb($dbh);
+      exit 1;
+    }
+    print int($cnt) . " rows affected\n";
+  } elsif($cmd eq "remove") {
+    my @results = RHRD::rddb::remove_user($dbh, $username);
+    if(!defined $results[0] && defined $results[2]) {
+      print "$results[2]\n";
+    } else {
+      for my $href (@results) {
+        print int($href->{cnt}) . " " . $href->{name} . " deleted\n";
+      }
+    }
+  }
+
+  RHRD::rddb::closedb($dbh);
+} else {
+  print STDERR "$errorstring\n";
+  exit 1;
+}
+
+exit 0
-- 
cgit v0.10.2