Skip to content
Snippets Groups Projects
Commit 70f406e9 authored by Robert Anderson's avatar Robert Anderson
Browse files

Added multi-user add

parent 068c1ec0
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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,
},
]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment