Browse code

Fürs erste feature-complete

git-svn-id: https://svn.schokokeks.org/repos/tools/webinterface/trunk@2092 87cf0b9e-d624-0410-a070-f6ee81989793

bernd authored on21/12/2011 16:51:52
Showing4 changed files
... ...
@@ -19,7 +19,7 @@ if (isset($_GET['repo'])) {
19 19
   }
20 20
   elseif ($sure === true)
21 21
   {
22
-    delete_repository($_GET['repo']);
22
+    delete_repo($_GET['repo']);
23 23
     if (! $debugmode)
24 24
       header('Location: git');
25 25
     die();
... ...
@@ -5,6 +5,45 @@ include("git.php");
5 5
 
6 6
 $section = 'git_git';
7 7
 
8
+$repos = list_repos();
8 9
 
10
+$users = list_users();
9 11
 
12
+$action = '';
13
+$form = '';
14
+
15
+if (isset($_GET['repo']) && isset($repos[$_GET['repo']])) {
16
+  $action = 'editrepo';
17
+  title("Zugriff auf GIT-Repository ändern");
18
+  output("<p>Legen Sie hier fest, welche Berechtigungen für welche SSH-Keys gelten sollen.</p>");
19
+  $form .= '<table><tr><td>Name des Repository</td><td><input type="hidden" name="repo" value="'.filter_input_general($_GET['repo']).'" />'.filter_input_general($_GET['repo']).'</td></tr>';
20
+} else {
21
+  $action = 'newrepo';
22
+  title("Neues GIT-Repository anlegen");
23
+  output("<p>Geben Sie einen Namen für das neue Repository an und legen Sie fest, welche Berechtigungen für welche SSH-Keys gelten sollen.</p>");
24
+  $form .= '<table><tr><td><label for="repo">Name des Repository</label></td><td><input type="text" id="repo" name="repo" /></td></tr>';
25
+}
26
+
27
+$form .= '<tr><td>Berechtigungen</td><td>';
28
+foreach ($users as $user) {
29
+  $r = $rw = $rwplus = '';
30
+  if (isset($_GET['repo']) && isset($repos[$_GET['repo']])) {
31
+    $repo = $repos[$_GET['repo']];
32
+    if (isset($repo[$user])) {
33
+      switch ($repo[$user]) {
34
+        case 'RW+': $rwplus = ' selected="selected"';
35
+                    break;
36
+        case 'RW': $rw = ' selected="selected"';
37
+                   break;
38
+        case 'R': $r = ' selected="selected"';
39
+                  break;
40
+      }
41
+    }
42
+  }
43
+  $form .= $user.': <select name="'.$user.'"><option value="-">Zugriff verweigern</option><option value="r"'.$r.'>Lesezugriff erlauben</option><option value="rw"'.$rw.'>Lese- und Schreibzugriff</option><option value="rwplus"'.$rwplus.'>erweiterter Lese- und Schreibzugriff (inkl. &quot;rewind&quot;)</option></select><br />';
44
+}
45
+$form .= '</td></tr></table>';
46
+$form .= '<p><input type="submit" value="Speichern" /></p>';
47
+
48
+output(html_form('git_edit', 'save', 'action='.$action, $form));
10 49
 
... ...
@@ -9,6 +9,7 @@ DEBUG("gitolite-data_dir: ".$data_dir);
9 9
 $git_wrapper = $data_dir . '/git-wrapper.sh';
10 10
 
11 11
 
12
+
12 13
 function check_env() 
13 14
 {
14 15
   global $git_wrapper, $data_dir, $config_file, $config_dir, $key_dir;
... ...
@@ -76,15 +77,16 @@ function list_repos()
76 77
   $current_repo = NULL;
77 78
   $current_repo_users = array();
78 79
   foreach ($lines as $line) {
80
+    DEBUG("LINE: ".$line);
79 81
     $m = array();
80
-    if (preg_match('_^[ \t]*repo ([^]]+)_', $line, $m) != 0) {
82
+    if (preg_match('_^\s*repo (\S+)\s*$_', $line, $m) != 0) {
81 83
       if ($current_repo) {
82 84
         $repos[$current_repo] = $current_repo_users;
83 85
       }
84 86
       DEBUG("found repo ".$m[1]);
85 87
       $current_repo = chop($m[1]);
86 88
       $current_repo_users = array();
87
-    } else if (preg_match('/^\s*(R|RW|RW+)\s*=\s*([[:alnum:]][[:alnum:]._-]*)\s*/', $line, $m) != 0) {
89
+    } else if (preg_match('/^\s*(R|RW|RW\+)\s*=\s*([[:alnum:]][[:alnum:]._-]*)\s*$/', $line, $m) != 0) {
88 90
       DEBUG("found access rule: ".$m[1]." for ".$m[2]);
89 91
       $current_repo_users[chop($m[2])] = chop($m[1]);
90 92
     }
... ...
@@ -224,3 +226,97 @@ function delete_key($handle)
224 226
 
225 227
 
226 228
 }
229
+
230
+
231
+function remove_repo_from_array($data, $repo) {
232
+  DEBUG("Request to remove repo »{$repo}«...");
233
+  $inside = false;
234
+  $outdata = array();
235
+  foreach ($data as $line) {
236
+    $m = array();
237
+    if (preg_match('_^\s*repo (\S+)\s*$_', $line, $m) != 0) {
238
+      $inside = ($m[1] == $repo);
239
+    }
240
+    if (! $inside) {
241
+      $outdata[] = $line;
242
+    }
243
+  }
244
+  DEBUG($outdata);
245
+  return $outdata;
246
+}
247
+
248
+
249
+function repo_exists_globally($repo) 
250
+{
251
+  global $config_dir;
252
+  $files = scandir($config_dir);
253
+  foreach ($files as $f) {
254
+    if (is_file(realpath($config_dir.'/'.$f))) {
255
+      $data = file(realpath($config_dir.'/'.$f));
256
+      foreach ($data as $line) {
257
+        if (preg_match('/^\s*repo '.$repo.'\s*$/', $line) != 0) {
258
+          return true;
259
+        }
260
+      }
261
+    }
262
+  }
263
+  return false;
264
+}
265
+
266
+
267
+function delete_repo($repo) 
268
+{
269
+  $repos = list_repos();
270
+  if (!array_key_exists($repo, $repos)) {
271
+    system_failure("Ein solches Repository existiert nicht!");
272
+  }
273
+  
274
+  global $config_dir;
275
+  $username = $_SESSION['userinfo']['username'];
276
+  $userconfig = $config_dir . '/' . $username . '.conf';
277
+  DEBUG("using config file ".$userconfig);
278
+  $data = file($userconfig);
279
+  $data = remove_repo_from_array($data, $repo);
280
+  file_put_contents($userconfig, implode('', $data));
281
+  git_wrapper('add '.$userconfig);
282
+  
283
+  git_wrapper('commit --allow-empty -m "deleted repo '.$repo.'"');
284
+  git_wrapper('push');
285
+}
286
+
287
+function save_repo($repo, $permissions) 
288
+{
289
+  if (!validate_name($repo)) {
290
+    system_failure("Der gewählte name entspricht nicht den Konventionen!");
291
+  }
292
+  if (!array_key_exists($repo, list_repos()) && repo_exists_globally($repo)) {
293
+    system_failure("Der gewählte Name existiert bereits auf diesem Server. Bitte wählen Sie einen spezifischeren Namen.");
294
+  } 
295
+  global $config_dir;
296
+  $username = $_SESSION['userinfo']['username'];
297
+  $userconfig = $config_dir . '/' . $username . '.conf';
298
+  DEBUG("using config file ".$userconfig);
299
+  $data = array();
300
+  if (! is_file($userconfig)) {
301
+    DEBUG("user-config does not exist, creating new one");
302
+  } else {
303
+    $data = file($userconfig);
304
+  }
305
+
306
+  $repos = list_repos();
307
+  if (array_key_exists($repo, $repos)) {
308
+    $data = remove_repo_from_array($data, $repo);
309
+  }
310
+
311
+  $data[] = "\n";
312
+  $data[] = 'repo '.$repo."\n";
313
+  foreach ($permissions as $user => $perm) {
314
+    $data[] = '  '.$perm.' = '.$user."\n";
315
+  }
316
+  file_put_contents($userconfig, implode('', $data));
317
+  git_wrapper('add '.$userconfig);
318
+  
319
+  git_wrapper('commit --allow-empty -m "written repo '.$repo.'"');
320
+  git_wrapper('push');
321
+}
322
+
... ...
@@ -4,6 +4,7 @@ require_role(ROLE_SYSTEMUSER);
4 4
 include('git.php');
5 5
 
6 6
 if ($_GET['action'] == 'newuser') {
7
+  check_form_token('git_newkey');
7 8
   $handle = $_POST['handle'];
8 9
   if ($handle == '') {
9 10
     system_failure("Leere Benutzerbezeichnung!");
... ...
@@ -17,6 +18,7 @@ if ($_GET['action'] == 'newuser') {
17 18
     header('Location: git');
18 19
   die();
19 20
 } elseif ($_GET['action'] == 'newkey') {
21
+  check_form_token('git_newkey');
20 22
   $handle = $_POST['handle'];
21 23
   if ($handle == '') {
22 24
     system_failure("Leere Benutzerbezeichnung!");
... ...
@@ -25,6 +27,27 @@ if ($_GET['action'] == 'newuser') {
25 27
   if (! $debugmode)
26 28
     header('Location: git');
27 29
   die();
30
+} elseif ($_GET['action'] == 'newrepo' || $_GET['action'] == 'editrepo') {
31
+  check_form_token('git_edit');
32
+  $permissions = array();
33
+  $users = list_users();
34
+  foreach ($users as $u) {  
35
+    if (isset($_POST[$u])) {
36
+      switch ($_POST[$u]) {
37
+        case 'rwplus': $permissions[$u] = 'RW+';
38
+          break;
39
+        case 'rw': $permissions[$u] = 'RW';
40
+          break;
41
+        case 'r': $permissions[$u] = 'R';
42
+          break;
43
+      }
44
+    }
45
+  }
46
+  save_repo($_POST['repo'], $permissions);
47
+  if (! $debugmode)
48
+    header('Location: git');
49
+  die();
50
+  
28 51
 }
29 52
 
30 53