auth: admin subpermission check for edit pages
[minimedit.git] / auth.inc.php
index 52cd4c1e29ababbce4ae3729f652617a247935d0..7ece6350219902345e504b9d253156200f2d982d 100644 (file)
@@ -3,9 +3,9 @@ date_default_timezone_set('Europe/Amsterdam');
 
 class User
 {
-       function __construct($dir)
+       function __construct($dir, $existing = TRUE)
        {
-               if (!file_exists($dir)) {
+               if (!file_exists($dir) and $existing) {
                        throw new Exception("Gebruiker niet gevonden in $dir");
                }
                $this->dir = $dir;
@@ -32,9 +32,27 @@ class User
                return $this->name ?: $this->login;
        }
 
-       function admin()
+       function email()
        {
-               return @file_exists("{$this->dir}/.admin");
+               return rtrim(@file_get_contents("{$this->dir}/email.txt"));
+       }
+
+       function admin($permission = NULL)
+       {
+               if (isset($permission)) {
+                       if (!$this->admin) {
+                               return FALSE;  # empty results
+                       }
+                       @list ($rootlevel, $sublevel) = explode(' ', $permission);
+                       if ($sublevel and isset($this->admin[$rootlevel])) {
+                               return TRUE;  # root match
+                       }
+                       return isset($this->admin[$permission]);  # check level
+               }
+               if (!@file_exists("{$this->dir}/.admin")) {
+                       return FALSE;  # not an admin
+               }
+               return array_fill_keys(explode("\n", file_get_contents("{$this->dir}/.admin")), TRUE);
        }
 
        function seen()