]> andersk Git - sql-web.git/blobdiff - lib/security.lib.php
old admin stuff deleted
[sql-web.git] / lib / security.lib.php
index 8cd49db162ffa491f055afe0b9903cc378bd3c18..c898f766d7cfb243e2472024a278b2979082e403 100644 (file)
@@ -79,15 +79,31 @@ class User {
        var $dblist;
     function User($userId) {
                $this->userId = $userId;
-        $sql = sprintf("SELECT UserId, Username, Password, Name, Email, UL, bEnabled
+        $sql = sprintf("SELECT User.UserId, Username, Password, Name, Email, UL, bEnabled, nBytesSoft, nBytesHard, nBytes, nDatabases, nDatabasesHard
                         FROM User
-                        WHERE UserId = '%s'",
+                                               NATURAL JOIN UserQuota
+                                               NATURAL JOIN UserStat
+                        WHERE User.UserId = '%s'",
                         mysql_escape_string($userId));
         $r = fetchRows(DBSelect($sql),'UserId');
         $this->info = count($r)?array_shift($r):$r;
                $this->dblist = $this->getDBList();
 //             $this->pass = base64_decode($this->info['Password']);
     }
+       function refresh() {
+               unset($this->dblist);
+               $this->User($this->userId);
+               /*
+        $sql = sprintf("SELECT UserId, Username, Password, Name, Email, UL, bEnabled
+                        FROM User
+                        WHERE UserId = '%s'",
+                        mysql_escape_string($this->userId));
+        $r = fetchRows(DBSelect($sql),'UserId');
+        $this->info = count($r)?array_shift($r):$r;
+               unset($this->dblist);
+               $this->getDBList();
+               */
+       }
     function exists() {
         return count($this->info);
     }
@@ -96,6 +112,14 @@ class User {
     }
     function getUsername() {
         return $this->exists()?$this->info['Username']:'';
+    }
+    function getBytes() {
+        if($this->exists()) {
+                       $arr['nBytes'] = $this->info['nBytes'];
+                       $arr['nBytesSoft'] = $this->info['nBytesSoft'];
+                       $arr['nBytesHard'] = $this->info['nBytesHard'];
+                       return $arr;
+               }
     }
        function setPassword($pwd) {
                $arr['Password'] = base64_encode($pwd);
@@ -135,8 +159,7 @@ class User {
                $verb = $yes?'GRANT':'REVOKE';
                $prep = $yes?'TO':'FROM';
                if (is_null($db)) {
-                       $this->dblist = $this->getDBList();
-                       $dbs = $this->dblist;
+                       $dbs = $this->getDBList();
                } else {
                        $dbs[] = array('Name'=>$db);
                }
@@ -152,14 +175,20 @@ class User {
                }
        }
        function getDBList() {
-               $sql = sprintf("SELECT *
+               if (isset($this->dblist)) {
+                       return $this->dblist;
+               } else {
+                       //                      LEFT JOIN DBQuota ON DBQuota.DatabaseId = DBOwner.DatabaseId
+                       $sql = sprintf("SELECT *
                                                FROM DBOwner
-                                               INNER JOIN DB ON DB.DatabaseId = DBOwner.DatabaseId
-                                               LEFT JOIN DBQuota ON DBQuota.DatabaseId = DBOwner.DatabaseId
-                                               WHERE UserId = '%s' AND DB.bEnabled=1",
+                                               NATURAL JOIN DB
+                                               NATURAL JOIN DBQuota
+                                               WHERE DBOwner.UserId = '%s' AND DB.bEnabled=1",
                                                mysql_escape_string($this->getUserId()));
-               $r = fetchRows(DBSelect($sql),'DatabaseId');
-               return $r;
+//                     $r = fetchRows(DBSelect($sql),'DatabaseId');
+                       $r = fetchRows(DBSelect($sql),'Name');
+                       return $r;
+               }
        }
        function addDB($name) {
                if (!addDB($name, $this->getUserId())) return false;
@@ -177,6 +206,29 @@ function isLoggedIn($aLogin=null) {
     return !empty($aLogin) && is_a($aLogin, 'Login') && $aLogin->canLogin();
 }
 
+function isImpersonating() {
+       return isSess('_UserId') && isSess('UserId');
+}
+
+function impersonate($userId=null) {
+       $wasImpersonating = isImpersonating();
+       if ($wasImpersonating) {
+               if (is_null($userId)) {
+                       sess('UserId',sess('_UserId'));
+               } elseif ($userId>0) {
+                       sess('UserId',$userId);
+               } else {
+                       return false;
+               }
+       } elseif (isLoggedIn()) {
+               sess('_UserId',sess('UserId'));
+               sess('UserId',$userId);
+               return true;
+       } else {
+               return false;
+       }
+}
+
 function isSSL() {
        return $_SERVER['SERVER_PORT'] == 443;
 }
@@ -270,4 +322,4 @@ function addDB($dbname,$userid) {
        return $DBId;
 }
 
-?>
\ No newline at end of file
+?>
This page took 3.657523 seconds and 4 git commands to generate.