login/edit: apply tags input to target links
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 7 Jul 2018 13:30:19 +0000 (15:30 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 7 Jul 2018 16:50:48 +0000 (18:50 +0200)
Allow admins to change user presence in listed tags.

login/edit.php

index 759eba5f10e61a359cb61027d1ae1fcbb7696487..894264b336ac3cc638a5d8a6ab85b61838e8f8ab 100644 (file)
@@ -59,7 +59,18 @@ if (file_exists($tagdir)) {
        foreach (glob("$tagdir/*") as $tag) {
                $tagname = pathinfo($tag, PATHINFO_BASENAME);
                $target = "$tag/{$user['name']}";
-               $tags[$tagname] = file_exists($tagname);
+               $val = file_exists($target);
+               $tags[$tagname] = ['value' => $val];
+               if (empty($User['admin'])) {
+                       continue;  # forbidden
+               }
+               if (!is_writable($tag)) {
+                       continue;  # locked tag directory
+               }
+               if ($val and !is_writable($target)) {
+                       continue;  # existing file locked
+               }
+               $tags[$tagname]['target'] = $target;
        }
 
        if ($tags) {
@@ -70,8 +81,8 @@ if (file_exists($tagdir)) {
                                '<input type="hidden" name="tags[%1$s]" value="" />' .
                                '<input type="checkbox" name="tags[%s]" value="1"%s%s /> %s',
                                $tag,
-                               $val ? ' checked' : '',
-                               ' readonly',
+                               $val['value'] ? ' checked' : '',
+                               isset($val['target']) ? '' : ' readonly',
                                ucfirst($tag)
                        );
                }
@@ -79,6 +90,7 @@ if (file_exists($tagdir)) {
                $cols['tags'] = [
                        'label' => 'groepen',
                        'input' => $options,
+                       'values' => $tags,
                ];
        }
 }
@@ -120,6 +132,29 @@ if ($_POST) {
                }
        }
 
+       if (isset($cols['tags']) and !empty($_POST['tags'])) {
+               $tagok = [];
+               foreach ($_POST['tags'] as $col => $val) {
+                       $tag = $cols['tags']['values'][$col];
+                       if (!isset($tag['target'])) {
+                               $tagok[$col] = 'forbidden';
+                       }
+                       if ($tag['value'] === !empty($val)) {
+                               $tagok[$col] = NULL;  # unaltered
+                       }
+                       elseif (empty($val)) {
+                               $tagok[$col] = !@unlink($tag['target']);
+                       }
+                       else {
+                               $tagok[$col] = !@symlink("../../{$user['name']}", $tag['target']);
+                       }
+               }
+               if ($tagok = array_filter($tagok)) {
+                       $colwarn['tags'] = "Wijziging niet opgeslagen voor "
+                               . implode(', ', array_keys($tagok));
+               }
+       }
+
        foreach ($_FILES as $col => $val) {
                if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
                        continue; # unknown