- add functionality to verify admins and operators
authorAndreas Scherbaum <andreas@scherbaum.biz>
Sun, 27 May 2012 21:04:38 +0000 (23:04 +0200)
committerAndreas Scherbaum <andreas@scherbaum.biz>
Sun, 27 May 2012 21:04:38 +0000 (23:04 +0200)
- is_nick_allowed_operator_command()
- is_nick_allowed_admin_command()

docbot.pl

index 49bc83c5a3a16b45ca4555f3372d87e12c64e6e7..792f789747fe6f9b948f77ef4e311bf34eb935ab 100755 (executable)
--- a/docbot.pl
+++ b/docbot.pl
@@ -1426,26 +1426,74 @@ sub translate_text_for_channel {
 }
 
 
-# FIXME: implement this function
+# is_nick_allowed_admin_command()
+#
+# verify if a user (nick name) is allowed to execute admin commands
+#
+# parameter:
+#  - nick name
+# return:
+#  - 0/1 (1 = allowed)
 sub is_nick_allowed_admin_command {
     my $nick = shift;
 
-    if ($nick eq 'ads') {
+    my $query = "SELECT 1
+                   FROM docbot_user
+                  WHERE LOWER(u_nick) = LOWER(?)
+                    AND u_role = 'admin'";
+    my $st = $main::db->query($query, $nick);
+    if (!defined($st)) {
+        print_msg("Database error", ERROR);
+        return 0;
+    }
+
+    my $rows = $st->rows;
+    $st->finish;
+
+    if ($rows > 0) {
+        # user is authorized as admin
+        print_msg("User '$nick' is authed as admin", DEBUG);
         return 1;
     }
 
+    print_msg("User '$nick' is not authed as admin", DEBUG);
+
     return 0;
 }
 
 
-# FIXME: implement this function
+# is_nick_allowed_operator_command()
+#
+# verify if a user (nick name) is allowed to execute operator commands
+#
+# parameter:
+#  - nick name
+# return:
+#  - 0/1 (1 = allowed)
 sub is_nick_allowed_operator_command {
     my $nick = shift;
 
-    if ($nick eq 'ads') {
+    my $query = "SELECT 1
+                   FROM docbot_user
+                  WHERE LOWER(u_nick) = LOWER(?)
+                    AND (u_role = 'op' OR u_role = 'admin')";
+    my $st = $main::db->query($query, $nick);
+    if (!defined($st)) {
+        print_msg("Database error", ERROR);
+        return 0;
+    }
+
+    my $rows = $st->rows;
+    $st->finish;
+
+    if ($rows > 0) {
+        # user is authorized as operator
+        print_msg("User '$nick' is authed as operator", DEBUG);
         return 1;
     }
 
+    print_msg("User '$nick' is not authed as operator", DEBUG);
+
     return 0;
 }