[Jaws-commits] r13330 - trunk/jaws/html/include/Jaws

afz at jaws-project.com afz at jaws-project.com
Thu Aug 9 12:41:14 UTC 2012


Author: afz
Date: 2012-08-09 12:41:14 +0000 (Thu, 09 Aug 2012)
New Revision: 13330

Modified:
   trunk/jaws/html/include/Jaws/User.php
Log:
- Replaced GetUserInfoById and GetUserInfoByName methods with GetUser method
- Added log-on hours and expiry date to AddUser and UpdateUser methods
- cosmetics

Modified: trunk/jaws/html/include/Jaws/User.php
===================================================================
--- trunk/jaws/html/include/Jaws/User.php	2012-08-09 12:37:35 UTC (rev 13329)
+++ trunk/jaws/html/include/Jaws/User.php	2012-08-09 12:41:14 UTC (rev 13330)
@@ -137,23 +137,22 @@
     }
 
     /**
-     * Get the info of an user
+     * Get the info of an user by the username or ID
      *
      * @access  public
-     * @param   int     $id  The user ID
+     * @param   mixed   $user  The username or ID
      * @return  mixed   Returns an array with the info of the user and false on error
      */
-    function GetUserInfoById($id, $account = true, $personal = false, $preferences = false, $extra = false)
+    function GetUser($user, $account = true, $personal = false, $preferences = false, $extra = false)
     {
-        $params = array();
-        $params['id'] = $id;
-
         $sql = 'SELECT [id]';
         $types = array('integer');
 
         if ($account) {
-            $sql .= ', [username], [nickname], [email], [superadmin], [concurrent_logins], [status]';
-            $types = array_merge($types, array('text', 'text', 'text', 'boolean', 'integer', 'integer'));
+            $sql .= ', [username], [nickname], [email], [superadmin], [concurrent_logins], [logon_hours],
+                       [expiry_date], [status]';
+            $types = array_merge($types, array('text', 'text', 'text', 'boolean', 'integer', 'text',
+                                               'integer', 'integer'));
         }
         if ($personal) {
             $sql .= ', [fname], [lname], [gender], [dob], [url]';
@@ -168,82 +167,21 @@
             // $types = array_merge($types, array('timestamp', 'timestamp', 'timestamp'));
         }
 
-        $sql .= '
-            FROM [[users]]
-            WHERE [id] = {id}';
-
-        $result = $GLOBALS['db']->queryRow($sql, $params, $types);
-        if (Jaws_Error::IsError($result)) {
-            return false;
-        }
-
-        return $result;
-    }
-
-    /**
-     * Get the info of a group
-     *
-     * @access  public
-     * @param   int     $id  The group ID
-     * @return  mixed   Returns an array with the info of the group and false on error
-     */
-    function GetGroupInfoById($id)
-    {
-        $params       = array();
-        $params['id'] = $id;
-        $sql = '
-            SELECT
-                [id], [name], [title], [description]
-            FROM [[groups]]
-            WHERE [id] = {id}';
-
-        $result = $GLOBALS['db']->queryRow($sql, $params);
-        if (Jaws_Error::IsError($result)) {
-            return false;
-        }
-
-        return $result;
-    }
-
-    /**
-     * Get the info of an user by the username
-     *
-     * @access  public
-     * @param   int     $user  The username
-     * @return  mixed   Returns an array with the info of the user and false on error
-     */
-    function GetUserInfoByName($user, $account = true, $personal = false, $preferences = false, $extra = false)
-    {
         $params = array();
-        $params['user'] = Jaws_UTF8::strtolower($user);
-
-        $sql = 'SELECT [id]';
-        $types = array('integer');
-
-        if ($account) {
-            $sql .= ', [username], [nickname], [email], [superadmin], [concurrent_logins], [status]';
-            $types = array_merge($types, array('text', 'text', 'text', 'boolean', 'integer', 'integer'));
+        if (is_int($user)) {
+            $params['id'] = $user;
+            $sql .= '
+                FROM [[users]]
+                WHERE [id] = {id}';
+        } else {
+            $params['user'] = Jaws_UTF8::strtolower($user);
+            $GLOBALS['db']->dbc->loadModule('Function', null, true);
+            $username = $GLOBALS['db']->dbc->function->lower('[username]');
+            $sql .= "
+                FROM [[users]]
+                WHERE $username = {user}";
         }
-        if ($personal) {
-            $sql .= ', [fname], [lname], [gender], [dob], [url]';
-            $types = array_merge($types, array('text', 'text', 'integer', 'timestamp', 'text'));
-        }
-        if ($preferences) {
-            $sql .= ', [language], [theme], [editor], [timezone]';
-            $types = array_merge($types, array('text', 'text', 'text', 'text'));
-        }
-        if ($extra) {
-            // $sql .= ', [last_login], [createtime], [updatetime]';
-            // $types = array_merge($types, array('timestamp', 'timestamp', 'timestamp'));
-        }
 
-        $GLOBALS['db']->dbc->loadModule('Function', null, true);
-        $username = $GLOBALS['db']->dbc->function->lower('[username]');
-
-        $sql .= "
-            FROM [[users]]
-            WHERE $username = {user}";
-
         return $GLOBALS['db']->queryRow($sql, $params, $types);
     }
 
@@ -254,7 +192,7 @@
      * @param   int     $email  The email address
      * @return  mixed   Returns an array with the info of the user(s) and false on error
      */
-    function GetUserInfoByEMail($email)
+    function GetUserInfoByEmail($email)
     {
         $params = array();
         $params['email'] = Jaws_UTF8::strtolower($email);
@@ -319,6 +257,31 @@
     }
 
     /**
+     * Get the info of a group
+     *
+     * @access  public
+     * @param   int     $id  The group ID
+     * @return  mixed   Returns an array with the info of the group and false on error
+     */
+    function GetGroupInfoById($id)
+    {
+        $params       = array();
+        $params['id'] = $id;
+        $sql = '
+            SELECT
+                [id], [name], [title], [description]
+            FROM [[groups]]
+            WHERE [id] = {id}';
+
+        $result = $GLOBALS['db']->queryRow($sql, $params);
+        if (Jaws_Error::IsError($result)) {
+            return false;
+        }
+
+        return $result;
+    }
+
+    /**
      * Get the info of a group by its name
      *
      * @access  public
@@ -578,12 +541,14 @@
      * @param   string  $email      User's email
      * @param   string  $password   User's password
      * @param   string  $superadmin Is superadmin (superadmin or normal)
-     * @param   int     $concurrent_logins  Concurrent logins limitation
      * @param   int     $status     User's status
+     * @param   int     $concurrent_logins  Concurrent logins limitation
+     * @param   string  $logon_hours        Logon hours
+     * @param   string  $expiry_date        Expiry date
      * @return  boolean Returns true if user was successfully added, false if not
      */
     function AddUser($username, $nickname, $email, $password, $superadmin = false,
-                     $concurrent_logins = 0, $status = 1)
+                     $status = 1, $concurrent_logins = 0, $logon_hours = null, $expiry_date = null)
     {
         $username = trim($username);
         if (!preg_match('/^[[:alnum:]-_.@]{3,24}$/', $username)) {
@@ -621,24 +586,30 @@
                                               JAWS_ERROR_NOTICE);
             }
         }
-        
+
         $params = array();
         $params['username']          = strtolower($username);
         $params['nickname']          = $nickname;
         $params['email']             = strtolower($email);
         $params['password']          = Jaws_User::GetHashedPassword($password);
         $params['superadmin']        = (bool)$superadmin;
-        $params['now']               = $GLOBALS['db']->Date();
-        $params['concurrent_logins'] = (int)$concurrent_logins;
         $params['status']            = (int)$status;
+        $params['concurrent_logins'] = (int)$concurrent_logins;
+        $params['logon_hours']       = empty($logon_hours)? str_pad('', 168 , '1') : $logon_hours;
+        $params['expiry_date']       = 0;
+        if (!empty($expiry_date)) {
+            $objDate = $GLOBALS['app']->loadDate();
+            $expiry_date = (int)$objDate->ToBaseDate(preg_split('/[- :]/', $expiry_date), 'U');
+            $params['expiry_date'] = $GLOBALS['app']->UserTime2UTC($expiry_date);
+        }
 
         $sql = '
             INSERT INTO [[users]]
                 ([username], [nickname], [email], [passwd], [superadmin],
-                 [concurrent_logins], [status])
+                 [concurrent_logins], [logon_hours], [expiry_date], [status])
             VALUES
                 ({username}, {nickname}, {email}, {password}, {superadmin},
-                 {concurrent_logins}, {status})';
+                 {concurrent_logins}, {logon_hours}, {expiry_date}, {status})';
 
         $result = $GLOBALS['db']->query($sql, $params);
         if (Jaws_Error::IsError($result)) {
@@ -671,12 +642,14 @@
      * @param   string  $email      User's email
      * @param   string  $password   User's password
      * @param   bool    $superadmin Is superadmin
-     * @param   int     $concurrent_logins   Concurrent logins limitation
      * @param   int     $status     User's status
+     * @param   int     $concurrent_logins  Concurrent logins limitation
+     * @param   string  $logon_hours        Logon hours
+     * @param   string  $expiry_date        Expiry date
      * @return  boolean Returns true if user was successfully updated, false if not
      */
     function UpdateUser($id, $username, $nickname, $email, $password = null, $superadmin = null,
-                        $concurrent_logins = null, $status = null)
+                        $status = null, $concurrent_logins = null, $logon_hours = null, $expiry_date = null)
     {
         $username = trim($username);
         if (!preg_match('/^[[:alnum:]-_.@]{3,24}$/', $username)) {
@@ -723,8 +696,15 @@
         $params['email']             = strtolower($email);
         $params['password']          = Jaws_User::GetHashedPassword($password);
         $params['superadmin']        = (bool)$superadmin;
-        $params['concurrent_logins'] = $concurrent_logins;
         $params['status']            = (int)$status;
+        $params['concurrent_logins'] = (int)$concurrent_logins;
+        $params['logon_hours']       = empty($logon_hours)? str_pad('', 168 , '1') : $logon_hours;
+        $params['expiry_date']       = 0;
+        if (!empty($expiry_date)) {
+            $objDate = $GLOBALS['app']->loadDate();
+            $expiry_date = (int)$objDate->ToBaseDate(preg_split('/[- :]/', $expiry_date), 'U');
+            $params['expiry_date'] = $GLOBALS['app']->UserTime2UTC($expiry_date);
+        }
 
         $sql = '
             UPDATE [[users]] SET
@@ -740,10 +720,15 @@
         if (!is_null($concurrent_logins)) {
             $sql .= ', [concurrent_logins] = {concurrent_logins} ';
         }
+        if (!is_null($expiry_date)) {
+            $sql .= ', [expiry_date] = {expiry_date} ';
+        }
+        if (!is_null($logon_hours)) {
+            $sql .= ', [logon_hours] = {logon_hours} ';
+        }
         if (!is_null($status)) {
             $sql .= ', [status] = {status} ';
         }
-
         $sql .= 'WHERE [id] = {id}';
 
         $result = $GLOBALS['db']->query($sql, $params);
@@ -947,53 +932,43 @@
      */
     function DeleteUser($id)
     {
-        $sql = 'SELECT COUNT([id]) FROM [[users]]';
-        $c = $GLOBALS['db']->queryOne($sql);
-        if (Jaws_Error::IsError($c)) {
+        $user = Jaws_User::GetUser((int)$id);
+        if (Jaws_Error::IsError($user) || empty($user)) {
             return false;
         }
 
-        if ($c > '1') {
-            $user = Jaws_User::GetUserInfoById($id);
-            if (!$user) {
-                return false;
-            }
+        $params = array();
+        $params['id'] = $id;
+        $sql = 'DELETE FROM [[users]] WHERE [id] = {id}';
+        $result = $GLOBALS['db']->query($sql, $params);
+        if (Jaws_Error::IsError($result)) {
+            return false;
+        }
 
-            $params = array();
-            $params['id'] = $id;
-            $sql = 'DELETE FROM [[users]] WHERE [id] = {id}';
-            $result = $GLOBALS['db']->query($sql, $params);
-            if (Jaws_Error::IsError($result)) {
-                return false;
-            }
+        $sql = 'DELETE FROM [[users_groups]] WHERE [user_id] = {id}';
+        $result = $GLOBALS['db']->query($sql, $params);
+        if (Jaws_Error::IsError($result)) {
+            return false;
+        }
 
-            $sql = 'DELETE FROM [[users_groups]] WHERE [user_id] = {id}';
-            $result = $GLOBALS['db']->query($sql, $params);
-            if (Jaws_Error::IsError($result)) {
-                return false;
-            }
+        $GLOBALS['app']->loadClass('ACL', 'Jaws_ACL');
+        $GLOBALS['app']->ACL->DeleteUserACL($user['username']);
 
-            $GLOBALS['app']->loadClass('ACL', 'Jaws_ACL');
-            $GLOBALS['app']->ACL->DeleteUserACL($user['username']);
-
-            if (isset($GLOBALS['app']->Session)) {
-                $res = $GLOBALS['app']->Session->_cache->DeleteUserSessions($id);
-                if (!$res) {
-                    return false;
-                }
-            }
-
-            // Let everyone know that a user has been deleted
-            $GLOBALS['app']->loadClass('Shouter', 'Jaws_EventShouter');
-            $res = $GLOBALS['app']->Shouter->Shout('onDeleteUser', $id);
-            if (Jaws_Error::IsError($res) || !$res) {
+        if (isset($GLOBALS['app']->Session)) {
+            $res = $GLOBALS['app']->Session->_cache->DeleteUserSessions($id);
+            if (!$res) {
                 return false;
             }
+        }
 
-            return true;
+        // Let everyone know that a user has been deleted
+        $GLOBALS['app']->loadClass('Shouter', 'Jaws_EventShouter');
+        $res = $GLOBALS['app']->Shouter->Shout('onDeleteUser', $id);
+        if (Jaws_Error::IsError($res) || !$res) {
+            return false;
         }
 
-        return false;
+        return true;
     }
 
 



More information about the Jaws-commits mailing list