6,50 → 6,83 |
using System.IO; |
using System.Windows.Forms; |
using System.Security.AccessControl; |
using System.DirectoryServices.AccountManagement; |
|
namespace SWAT_Office_App |
{ |
class ManageUserAccounts |
{ |
public static List<string> UserAccountsList = new List<string>(); |
private static string studentGroupName = "StudentAccounts"; |
public static List<UserPrincipal> UserAccountsList = new List<UserPrincipal>(); |
public static bool AddUser(string username, string password) |
{ |
try |
{ |
// Returns true if user was added successfully |
foreach (string user in UserAccountsList) |
PrincipalContext localSystem = new PrincipalContext(ContextType.Machine); |
// Creates a new user after checking if it already exists or not |
UserPrincipal newUser = new UserPrincipal(localSystem); |
newUser.Name = username; |
PrincipalSearcher searcher = new PrincipalSearcher(newUser); |
Principal result = searcher.FindOne(); |
if (result == null) |
{ |
if (username.ToLower() == user.ToLower()) |
newUser = new UserPrincipal(localSystem); |
newUser.Name = username; |
newUser.SetPassword(password); |
newUser.PasswordNeverExpires = true; |
newUser.PasswordNotRequired = false; |
newUser.UserCannotChangePassword = true; |
newUser.Description = DateTime.Now.ToShortDateString(); |
newUser.Save(); |
|
// Creates a new group after checking if it already exists or not |
GroupPrincipal studentGroup = new GroupPrincipal(localSystem); |
studentGroup.Name = studentGroupName; |
searcher = new PrincipalSearcher(studentGroup); |
result = searcher.FindOne(); |
// Creates group if it doesnt already exist |
if (result == null) |
{ |
MessageBox.Show("Username already exists", "Error"); |
return false; |
studentGroup = new GroupPrincipal(localSystem, studentGroupName); |
studentGroup.Name = studentGroupName; |
studentGroup.IsSecurityGroup = true; |
studentGroup.Members.Add(newUser); |
studentGroup.Save(); |
} |
// Otherwise add to existing group |
else |
{ |
studentGroup = (GroupPrincipal)result; |
studentGroup.Members.Add(newUser); |
studentGroup.Save(); |
} |
DebugText.appendText("Account " + username + " has been created"); |
return true; |
} |
Process netProcess = new Process(); |
netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory; |
netProcess.StartInfo.FileName = "net.exe"; |
netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
netProcess.StartInfo.CreateNoWindow = true; |
// Arguments for user account creation. Run NET HELP USER from the command prompt for more info. |
netProcess.StartInfo.Arguments = "USER \"" + username + "\" \"" + password + |
"\" /ADD /ACTIVE:YES /PASSWORDCHG:NO /PASSWORDREQ:YES /EXPIRES:NEVER /COMMENT:" + DateTime.Now.ToShortDateString(); |
netProcess.Start(); |
netProcess.WaitForExit(); |
netProcess.Close(); |
return true; |
else |
{ |
// local account already exists, return with error |
return false; |
} |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return false; |
} |
} |
public static void DeleteUser(List<string> usernames) |
public static bool DeleteUser(List<string> usernames) |
{ |
try |
{ |
bool Success = true; |
PrincipalContext localSystem = new PrincipalContext(ContextType.Machine); |
// Iterates through and deletes selected users |
foreach (string user in usernames) |
{ |
bool deleteShare = false; |
if (Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + user)) |
{ |
// Prompts for deletion of folder as well as the user account |
57,116 → 90,59 |
"\nDelete the folder and all data within the folder?", |
"Warning!", MessageBoxButtons.YesNo); |
if (result == DialogResult.Yes) |
{ |
Process netProcess = new Process(); |
netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory; |
netProcess.StartInfo.FileName = "net.exe"; |
netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
netProcess.StartInfo.CreateNoWindow = true; |
// Arguments for user account deletion. Run NET HELP USER from the command prompt for more info. |
netProcess.StartInfo.Arguments = "USER \"" + user + "\" /DELETE"; |
netProcess.Start(); |
netProcess.WaitForExit(); |
netProcess.Close(); |
|
DeleteShareFolder(user); |
} |
deleteShare = true; |
} |
// If no folders exist for the user, account is deleted without prompting |
else |
{ |
Process netProcess = new Process(); |
netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory; |
netProcess.StartInfo.FileName = "net.exe"; |
netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
netProcess.StartInfo.CreateNoWindow = true; |
// Arguments for user account deletion. Run NET HELP USER from the command prompt for more info. |
netProcess.StartInfo.Arguments = "USER \"" + user + "\" /DELETE"; |
netProcess.Start(); |
netProcess.WaitForExit(); |
netProcess.Close(); |
} |
|
// Finds and deletes user and share |
UserPrincipal toDelete = new UserPrincipal(localSystem); |
toDelete.Name = user; |
PrincipalSearcher searcher = new PrincipalSearcher(toDelete); |
Principal found = searcher.FindOne(); |
toDelete = (UserPrincipal)found; |
toDelete.Delete(); |
DebugText.appendText("Account " + user + " has been deleted"); |
// Deletes share if selected |
if (deleteShare) |
if (!DeleteShareFolder(user)) |
Success = false; |
} |
return Success; |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return false; |
} |
} |
public static void QueryUserAccounts() |
public static bool QueryUserAccounts() |
{ |
// Function that reads the user accounts on the local computer to UserAccountsList |
UserAccountsList.Clear(); |
try |
{ |
Process netProcess = new Process(); |
netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory; |
netProcess.StartInfo.FileName = "net.exe"; |
netProcess.StartInfo.UseShellExecute = false; |
netProcess.StartInfo.RedirectStandardOutput = true; |
netProcess.StartInfo.CreateNoWindow = true; |
netProcess.StartInfo.Arguments = "USER"; |
netProcess.Start(); |
string netOutput = netProcess.StandardOutput.ReadToEnd(); |
// Splits the output into seperate strings for further processing |
string[] tempSplit = netOutput.Split(new string[] { " ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); |
// Roundabout way of trimming each string in the array |
string[] netOutputSplit = new string[tempSplit.Length]; |
for (int i = 0; i < tempSplit.Length; i++) |
netOutputSplit[i] = tempSplit[i].Trim(); |
// Imports the string array into the UserAccountsList List |
UserAccountsList.AddRange(netOutputSplit); |
// Removes the header and footer from the List |
UserAccountsList.RemoveRange(0, 2); |
UserAccountsList.RemoveRange(UserAccountsList.Count() - 1, 1); |
// Removes any additional empty entries |
UserAccountsList.RemoveAll(IsStringBlank); |
netProcess.WaitForExit(); |
netProcess.Close(); |
PrincipalContext localSystem = new PrincipalContext(ContextType.Machine); |
UserPrincipal user = new UserPrincipal(localSystem); |
user.Name = "*"; |
PrincipalSearcher searcher = new PrincipalSearcher(user); |
PrincipalSearchResult<Principal> result = searcher.FindAll(); |
foreach (Principal p in result) |
UserAccountsList.Add((UserPrincipal)p); |
return true; |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return false; |
} |
} |
public static string [] QueryUserAccountExtraInformation(string username) |
{ |
// Returns a string array with password requirement and date of account creation |
string[] stringArray = { "", "" }; |
try |
{ |
Process netProcess = new Process(); |
netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory; |
netProcess.StartInfo.FileName = "net.exe"; |
netProcess.StartInfo.UseShellExecute = false; |
netProcess.StartInfo.RedirectStandardOutput = true; |
netProcess.StartInfo.CreateNoWindow = true; |
netProcess.StartInfo.Arguments = "USER " + "\"" + username + "\""; |
netProcess.Start(); |
string netOutput = netProcess.StandardOutput.ReadToEnd(); |
// Splits the output into seperate strings for further processing |
string[] tempSplit = netOutput.Split(new string[] { " ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); |
// Roundabout way of trimming each string in the array |
string[] netOutputSplit = new string[tempSplit.Length]; |
for (int i = 0; i < tempSplit.Length; i++) |
netOutputSplit[i] = tempSplit[i].Trim(); |
stringArray[0] = netOutputSplit[20]; |
stringArray[1] = netOutputSplit[4]; |
netProcess.WaitForExit(); |
netProcess.Close(); |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
} |
return stringArray; |
} |
public static bool QueryUserSharedFolderExist(string username) |
{ |
return Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + username); |
} |
public static void CreateShareFolder(string username) |
public static bool CreateShareFolder(string username) |
{ |
|
try |
173,41 → 149,52 |
{ |
// Creates the directory with only the specific NTFS and share permissions for the user. |
DirectorySecurity dSecurity = new DirectorySecurity(); |
// Adds NTFS permissions for system accounts |
foreach (string systemUser in Settings_Form.systemAccounts) |
{ |
dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + systemUser, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)); |
dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + systemUser, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); |
} |
//dSecurity.AddAccessRule(new FileSystemAccessRule("\\Administrator", FileSystemRights.FullControl, AccessControlType.Allow)); |
//dSecurity.AddAccessRule(new FileSystemAccessRule("\\SWAT", FileSystemRights.FullControl, AccessControlType.Allow)); |
//dSecurity.AddAccessRule(new FileSystemAccessRule("\\Administrators", FileSystemRights.FullControl, AccessControlType.Allow)); |
// Adds NTFS permissions for the user |
dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)); |
dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); |
Directory.CreateDirectory(Settings_Form.sharedFolderLocation + @"\" + username, dSecurity); |
SetSharePermissions(username); |
DebugText.appendText("Shared folder for " + username + " has been created"); |
return SetSharePermissions(username); |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return false; |
} |
} |
public static void DeleteShareFolder(string username) |
public static bool DeleteShareFolder(string username) |
{ |
try |
{ |
bool Success = true; |
// Removes the share BEFORE deleting the folder. Otherwise share will error on remove. |
RemoveSharePermissions(username); |
if (!RemoveSharePermissions(username)) |
Success = false; |
Directory.Delete(Settings_Form.sharedFolderLocation + @"\" + username, true); |
DebugText.appendText("Shared folder for " + username + " has been deleted"); |
return Success; |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return false; |
} |
} |
public static void ToggleShare(string username) |
public static bool ToggleShare(string username) |
{ |
try |
{ |
// Checks if a share already exists for the user |
if (Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + username)) |
{ |
DialogResult result = MessageBox.Show("A shared folder exists for the user " + username + "!" + |
214,18 → 201,19 |
"\nDelete the folder and all data within the folder?", |
"Warning!", MessageBoxButtons.YesNo); |
if (result == DialogResult.Yes) |
{ |
DeleteShareFolder(username); |
} |
return DeleteShareFolder(username); |
} |
// Otherwise creates the share for the user |
else |
{ |
CreateShareFolder(username); |
} |
return CreateShareFolder(username); |
return true; |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return false; |
} |
} |
public static long GetShareSize(string username) |
234,29 → 222,62 |
long size = GetDirSize(dir); |
return size; |
} |
public static void ChangeUserPassword(string username, string password) |
private static long GetDirSize(DirectoryInfo input) |
{ |
try |
{ |
Process netProcess = new Process(); |
netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory; |
netProcess.StartInfo.FileName = "net.exe"; |
netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
netProcess.StartInfo.CreateNoWindow = true; |
// Arguments for changing user password. Run NET HELP USER from the command prompt for more info. |
netProcess.StartInfo.Arguments = "USER \"" + username + "\" \"" + password + "\""; |
netProcess.Start(); |
netProcess.WaitForExit(); |
netProcess.Close(); |
if (input.Exists) |
{ |
long size = 0; |
FileInfo[] files = input.GetFiles(); |
foreach (FileInfo file in files) |
{ |
size += file.Length; |
} |
DirectoryInfo[] dirs = input.GetDirectories(); |
foreach (DirectoryInfo dir in dirs) |
{ |
size += GetDirSize(dir); |
} |
return size; |
} |
else |
return 0; |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return 0; |
} |
} |
public static bool ChangeUserPassword(string username, string password) |
{ |
try |
{ |
// Queries for the specified user password |
PrincipalContext localSystem = new PrincipalContext(ContextType.Machine); |
UserPrincipal user = new UserPrincipal(localSystem); |
user.Name = username; |
PrincipalSearcher searcher = new PrincipalSearcher(user); |
Principal result = searcher.FindOne(); |
user = (UserPrincipal)result; |
user.SetPassword(password); |
user.Save(); |
DebugText.appendText("Password for " + username + " has been changed"); |
return true; |
} |
catch (Exception e) |
{ |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return false; |
} |
} |
public static bool SessionsOpen() |
{ |
bool sessionsOpen = true; |
try |
{ |
Process netProcess = new Process(); |
274,43 → 295,19 |
netProcess.Close(); |
|
if (tempSplit[0] == "There are no entries in the list.") |
sessionsOpen = false; |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
} |
return sessionsOpen; |
} |
private static long GetDirSize(DirectoryInfo entry) |
{ |
try |
{ |
if (entry.Exists) |
{ |
long size = 0; |
FileInfo[] files = entry.GetFiles(); |
foreach (FileInfo file in files) |
{ |
size += file.Length; |
} |
DirectoryInfo[] dirs = entry.GetDirectories(); |
foreach (DirectoryInfo dir in dirs) |
{ |
size += GetDirSize(dir); |
} |
return size; |
} |
return false; |
else |
return 0; |
return true; |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
return 0; |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return true; |
} |
} |
private static void SetSharePermissions(string username) |
private static bool SetSharePermissions(string username) |
{ |
string combinationString = ""; |
foreach (string systemUser in Settings_Form.systemAccounts) |
329,13 → 326,18 |
netProcess.Start(); |
netProcess.WaitForExit(); |
netProcess.Close(); |
DebugText.appendText("Share permissions for " + username + " has been set"); |
return true; |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return false; |
} |
} |
private static void RemoveSharePermissions(string username) |
private static bool RemoveSharePermissions(string username) |
{ |
try |
{ |
348,18 → 350,16 |
netProcess.Start(); |
netProcess.WaitForExit(); |
netProcess.Close(); |
DebugText.appendText("Share permissions for " + username + " has been removed"); |
return true; |
} |
catch (Exception e) |
{ |
MessageBox.Show(e.ToString(), "Error"); |
//MessageBox.Show(e.ToString(), "Error"); |
DebugText.appendText(e.ToString()); |
MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error"); |
return false; |
} |
} |
private static bool IsStringBlank(string s) |
{ |
if (s == "") |
return true; |
else |
return false; |
} |
} |
} |