From 70f406e96531f6f8255a12c939e611bbc631c9c9 Mon Sep 17 00:00:00 2001 From: Robert Anderson <randerson@lbsd.net> Date: Tue, 30 Jun 2009 14:45:34 +0000 Subject: [PATCH] Added multi-user add --- webgui/include/ajax/functions/WiSPUsers.php | 255 +++++++++++++------- webgui/js/app/windows/WiSPUsers.js | 13 +- 2 files changed, 169 insertions(+), 99 deletions(-) diff --git a/webgui/include/ajax/functions/WiSPUsers.php b/webgui/include/ajax/functions/WiSPUsers.php index c6d63dd4..16f214f7 100644 --- a/webgui/include/ajax/functions/WiSPUsers.php +++ b/webgui/include/ajax/functions/WiSPUsers.php @@ -195,113 +195,184 @@ function createWiSPUser($params) { global $db; DBBegin(); - # Insert username - $res = DBDo("INSERT INTO users (Username) VALUES (?)",array($params[0]['Username'])); - - # Continue with others if successful - if ($res !== FALSE) { - $userID = DBLastInsertID(); - $res = DBDo(" - INSERT INTO - user_attributes (UserID,Name,Operator,Value) - VALUES - (?,?,?,?)", - array($userID, - 'User-Password', - '==', - $params[0]['Password']) - ); - } - - # Link users ID to make user a wisp user - if ($res !== FALSE) { - $res = DBDo("INSERT INTO wisp_userdata (UserID) VALUES (?)",array($userID)); - } + $res = "Username & Password required for single user. For adding multiple users an integer is required."; + # If we adding single user + if (empty($params[0]['Number']) && !empty($params[0]['Password']) && !empty($params[0]['Username'])) { + # Insert username + $res = DBDo("INSERT INTO users (Username) VALUES (?)",array($params[0]['Username'])); + + # Continue with others if successful + if ($res !== FALSE) { + $userID = DBLastInsertID(); + $res = DBDo(" + INSERT INTO + user_attributes (UserID,Name,Operator,Value) + VALUES + (?,?,?,?)", + array($userID, + 'User-Password', + '==', + $params[0]['Password']) + ); + } - # Add personal information - if ($res !== FALSE && isset($params[0]['Firstname'])) { - $res = DBDo("UPDATE wisp_userdata SET FirstName = ? WHERE UserID = ?",array($params[0]['Firstname'],$userID)); - } - if ($res !== FALSE && isset($params[0]['Lastname'])) { - $res = DBDo("UPDATE wisp_userdata SET LastName = ? WHERE UserID = ?",array($params[0]['Lastname'],$userID)); - } - if ($res !== FALSE && isset($params[0]['Phone'])) { - $res = DBDo("UPDATE wisp_userdata SET Phone = ? WHERE UserID = ?",array($params[0]['Phone'],$userID)); - } - if ($res !== FALSE && isset($params[0]['Email'])) { - $res = DBDo("UPDATE wisp_userdata SET Email = ? WHERE UserID = ?",array($params[0]['Email'],$userID)); - } - if ($res !== FALSE && isset($params[0]['LocationID'])) { - $res = DBDo("UPDATE wisp_userdata SET LocationID = ? WHERE UserID = ?",array($params[0]['LocationID'],$userID)); - } + # Link users ID to make user a wisp user + if ($res !== FALSE) { + $res = DBDo("INSERT INTO wisp_userdata (UserID) VALUES (?)",array($userID)); + } + # Add personal information + if ($res !== FALSE && isset($params[0]['Firstname'])) { + $res = DBDo("UPDATE wisp_userdata SET FirstName = ? WHERE UserID = ?",array($params[0]['Firstname'],$userID)); + } + if ($res !== FALSE && isset($params[0]['Lastname'])) { + $res = DBDo("UPDATE wisp_userdata SET LastName = ? WHERE UserID = ?",array($params[0]['Lastname'],$userID)); + } + if ($res !== FALSE && isset($params[0]['Phone'])) { + $res = DBDo("UPDATE wisp_userdata SET Phone = ? WHERE UserID = ?",array($params[0]['Phone'],$userID)); + } + if ($res !== FALSE && isset($params[0]['Email'])) { + $res = DBDo("UPDATE wisp_userdata SET Email = ? WHERE UserID = ?",array($params[0]['Email'],$userID)); + } + if ($res !== FALSE && isset($params[0]['LocationID'])) { + $res = DBDo("UPDATE wisp_userdata SET LocationID = ? WHERE UserID = ?",array($params[0]['LocationID'],$userID)); + } - # Grab each attribute and add it's details to the database - if ($res !== FALSE && isset($params[0]['Attributes'])) { - foreach ($params[0]['Attributes'] as $attr) { - - # Default value without modifier - $attrValue = $attr['Value']; - - if ($attr['Name'] == 'SMRadius-Capping-Traffic-Limit' || $attr['Name'] == 'SMRadius-Capping-Uptime-Limit') { - # If modifier is set we need to work out attribute value - if (isset($attr['Modifier'])) { - switch ($attr['Modifier']) { - case "Seconds": - $attrValue = $attr['Value'] / 60; - case "Minutes": - $attrValue = $attr['Value']; - case "Hours": - $attrValue = $attr['Value'] * 60; - case "Days": - $attrValue = $attr['Value'] * 1440; - case "Weeks": - $attrValue = $attr['Value'] * 10080; - case "Months": - $attrValue = $attr['Value'] * 44640; - case "MBytes": - $attrValue = $attr['Value']; - case "GBytes": - $attrValue = $attr['Value'] * 1024; - case "TBytes": - $attrValue = $attr['Value'] * 1048576; + # Grab each attribute and add it's details to the database + if ($res !== FALSE && isset($params[0]['Attributes'])) { + foreach ($params[0]['Attributes'] as $attr) { + + # Default value without modifier + $attrValue = $attr['Value']; + + if ($attr['Name'] == 'SMRadius-Capping-Traffic-Limit' || $attr['Name'] == 'SMRadius-Capping-Uptime-Limit') { + # If modifier is set we need to work out attribute value + if (isset($attr['Modifier'])) { + switch ($attr['Modifier']) { + case "Seconds": + $attrValue = $attr['Value'] / 60; + case "Minutes": + $attrValue = $attr['Value']; + case "Hours": + $attrValue = $attr['Value'] * 60; + case "Days": + $attrValue = $attr['Value'] * 1440; + case "Weeks": + $attrValue = $attr['Value'] * 10080; + case "Months": + $attrValue = $attr['Value'] * 44640; + case "MBytes": + $attrValue = $attr['Value']; + case "GBytes": + $attrValue = $attr['Value'] * 1024; + case "TBytes": + $attrValue = $attr['Value'] * 1048576; + } } } + + # Add attribute + $res = DBDo(" + INSERT INTO + user_attributes (UserID,Name,Operator,Value) + VALUES + (?,?,?,?)", + array( + $userID, + $attr['Name'], + $attr['Operator'], + $attrValue + ) + ); } + } - # Add attribute - $res = DBDo(" - INSERT INTO - user_attributes (UserID,Name,Operator,Value) - VALUES - (?,?,?,?)", - array( - $userID, - $attr['Name'], - $attr['Operator'], - $attrValue - ) - ); + # Link user to groups if any selected + if ($res !== FALSE && isset($params[0]['Groups'])) { + $refinedGroups = array(); + + # Filter out unique group ID's + foreach ($params[0]['Groups'] as $group) { + foreach ($group as $ID=>$value) { + $refinedGroups[$value] = $value; + } + } + # Loop through groups + foreach ($refinedGroups as $groupID) { + $res = DBDo("INSERT INTO users_to_groups (UserID,GroupID) VALUES (?,?)",array($userID,$groupID)); + } } - } - # Link user to groups if any selected - if ($res !== FALSE && isset($params[0]['Groups'])) { - $refinedGroups = array(); + # We adding multiple users + } elseif (!empty($params[0]['Number']) && $params[0]['Number'] > 1) { + $wispUser = array(); + # Loop for number of chosen numbers + for ($i = 0; $i < $params[0]['Number']; $i++) { + + # Check for duplicates and add + $usernameReserved = 1; + $characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; + while ($usernameReserved == 1) { + + # Generate random username + $string = ''; + for ($c = 0; $c < 7; $c++) { + $string .= $characters[rand(0, strlen($characters) - 1)]; + } + + $thisUsername = $string; + # Add prefix to string + if (!empty($params[0]['Prefix'])) { + $thisUsername = $params[0]['Prefix'].$string; + } + + # Check if username used + $res = DBSelect(" + SELECT + users.Username + FROM + users + WHERE + users.Username = ? + ",array($thisUsername) + ); + + # If there are no rows we may continue + if ($res->rowCount() == 0 && !defined($wispUser[$thisUsername])) { + $usernameReserved = 0; + + # Generate random username + $string = ''; + for ($c = 0; $c < 7; $c++) { + $string .= $characters[rand(0, strlen($characters) - 1)]; + } - # Filter out unique group ID's - foreach ($params[0]['Groups'] as $group) { - foreach ($group as $ID=>$value) { - $refinedGroups[$value] = $value; + # Add username and password onto array + $wispUser[$thisUsername] = $string; + } } } - # Loop through groups - foreach ($refinedGroups as $groupID) { - $res = DBDo("INSERT INTO users_to_groups (UserID,GroupID) VALUES (?,?)",array($userID,$groupID)); + + # Insert users from array into database + foreach ($wispUser as $username => $password) { + $res = DBDo("INSERT INTO users (Username) VALUES (?)",array($username)); + if ($res !== FALSE) { + $id = DBLastInsertID(); + $res = DBDo("INSERT INTO user_attributes (UserID,Name,Operator,Value) VALUES (?,?,?,?)", + array($id,'User-Password','==',$password) + ); + + # Link to wisp users + if ($res !== FALSE) { + $res = DBDo("INSERT INTO wisp_userdata (UserID) VALUES (?)", + array($id) + ); + } + } } } - # Commit changes if all was successful, else break + # Commit changes if all was successful, else rollback if ($res !== FALSE) { DBCommit(); return $res; diff --git a/webgui/js/app/windows/WiSPUsers.js b/webgui/js/app/windows/WiSPUsers.js index 5f3365f3..1d25b10a 100644 --- a/webgui/js/app/windows/WiSPUsers.js +++ b/webgui/js/app/windows/WiSPUsers.js @@ -271,7 +271,9 @@ function showWiSPUserAddEditWindow(id) { '0:Email,'+ '0:LocationID,'+ '0:Attributes,'+ - '0:Groups' + '0:Groups,'+ + '0:Number,'+ + '0:Prefix' }, hook: function() { @@ -556,14 +558,14 @@ function showWiSPUserAddEditWindow(id) { name: 'Username', vtype: 'usernamePart', maskRe: usernamePartRe, - allowBlank: false, + allowBlank: true, }, { fieldLabel: 'Password', name: 'Password', vtype: 'usernamePart', maskRe: usernamePartRe, - allowBlank: false, + allowBlank: true, }, { xtype: 'tabpanel', @@ -658,15 +660,12 @@ function showWiSPUserAddEditWindow(id) { { fieldLabel: 'Prefix', name: 'Prefix', - vtype: 'usernamePart', - maskRe: usernamePartRe, allowBlank: true, }, { fieldLabel: 'Number', name: 'Number', - vtype: 'usernamePart', - maskRe: usernamePartRe, + vtype: 'number', allowBlank: true, }, ] -- GitLab