Subversion Repositories Code-Repo

Rev

Rev 57 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 57 Rev 63
Line 8... Line 8...
8
using System.Security.AccessControl;
8
using System.Security.AccessControl;
9
using System.DirectoryServices.AccountManagement;
9
using System.DirectoryServices.AccountManagement;
10
 
10
 
11
namespace SWAT_Office_App
11
namespace SWAT_Office_App
12
{
12
{
13
    class ManageUserAccounts
13
    class Manage_User_Accounts
14
    {
14
    {
15
        private static string studentGroupName = "StudentAccounts";
15
        private static string studentGroupName = "StudentAccounts";
16
        public static List<UserPrincipal> UserAccountsList = new List<UserPrincipal>();
16
        public static List<UserPrincipal> UserAccountsList = new List<UserPrincipal>();
17
        public static bool AddUser(string username, string password)
17
        public static bool AddUser(string username, string password)
18
        {
18
        {
Line 54... Line 54...
54
                    {
54
                    {
55
                        studentGroup = (GroupPrincipal)result;
55
                        studentGroup = (GroupPrincipal)result;
56
                        studentGroup.Members.Add(newUser);
56
                        studentGroup.Members.Add(newUser);
57
                        studentGroup.Save();
57
                        studentGroup.Save();
58
                    }
58
                    }
59
                    DebugText.appendText("Account " + username + " has been created");
59
                    Debug.appendText("Account " + username + " has been created");
60
                    StatLogging.AccountsCreated += 1;
60
                    Stat_Logging.AccountsCreated += 1;
61
                    return true;
61
                    return true;
62
                }
62
                }
63
                else
63
                else
64
                {
64
                {
65
                    // local account already exists, return with error
65
                    // local account already exists, return with error
Line 67... Line 67...
67
                }
67
                }
68
            }
68
            }
69
            catch (Exception e)
69
            catch (Exception e)
70
            {
70
            {
71
                //MessageBox.Show(e.ToString(), "Error");
71
                //MessageBox.Show(e.ToString(), "Error");
72
                DebugText.appendText(e.ToString());
72
                Debug.appendText(e.ToString());
73
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
73
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
74
                return false;
74
                return false;
75
            }
75
            }
76
        }
76
        }
77
        public static bool DeleteUser(List<string> usernames)
77
        public static bool DeleteUser(List<string> usernames)
Line 98... Line 98...
98
                    toDelete.Name = user;
98
                    toDelete.Name = user;
99
                    PrincipalSearcher searcher = new PrincipalSearcher(toDelete);
99
                    PrincipalSearcher searcher = new PrincipalSearcher(toDelete);
100
                    Principal found = searcher.FindOne();
100
                    Principal found = searcher.FindOne();
101
                    toDelete = (UserPrincipal)found;
101
                    toDelete = (UserPrincipal)found;
102
                    toDelete.Delete();
102
                    toDelete.Delete();
103
                    DebugText.appendText("Account " + user + " has been deleted");
103
                    Debug.appendText("Account " + user + " has been deleted");
104
                    // Deletes share if selected
104
                    // Deletes share if selected
105
                    if (deleteShare)
105
                    if (deleteShare)
106
                        if (!DeleteShareFolder(user))
106
                        if (!DeleteShareFolder(user))
107
                            Success = false;
107
                            Success = false;
108
                }
108
                }
109
                return Success;
109
                return Success;
110
            }
110
            }
111
            catch (Exception e)
111
            catch (Exception e)
112
            {
112
            {
113
                //MessageBox.Show(e.ToString(), "Error");
113
                //MessageBox.Show(e.ToString(), "Error");
114
                DebugText.appendText(e.ToString());
114
                Debug.appendText(e.ToString());
115
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
115
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
116
                return false;
116
                return false;
117
            }
117
            }
118
        }
118
        }
119
        public static bool QueryUserAccounts()
119
        public static bool QueryUserAccounts()
Line 132... Line 132...
132
                return true;
132
                return true;
133
            }
133
            }
134
            catch (Exception e)
134
            catch (Exception e)
135
            {
135
            {
136
                //MessageBox.Show(e.ToString(), "Error");
136
                //MessageBox.Show(e.ToString(), "Error");
137
                DebugText.appendText(e.ToString());
137
                Debug.appendText(e.ToString());
138
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
138
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
139
                return false;
139
                return false;
140
            }
140
            }
141
        }
141
        }
142
        public static bool QueryUserSharedFolderExist(string username)
142
        public static bool QueryUserSharedFolderExist(string username)
Line 158... Line 158...
158
                }
158
                }
159
                // Adds NTFS permissions for the user
159
                // Adds NTFS permissions for the user
160
                dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
160
                dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
161
                dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
161
                dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
162
                Directory.CreateDirectory(Settings_Form.sharedFolderLocation + @"\" + username, dSecurity);
162
                Directory.CreateDirectory(Settings_Form.sharedFolderLocation + @"\" + username, dSecurity);
163
                DebugText.appendText("Shared folder for " + username + " has been created");
163
                Debug.appendText("Shared folder for " + username + " has been created");
164
                if (SetSharePermissions(username))
164
                if (SetSharePermissions(username))
165
                {
165
                {
166
                    StatLogging.SharesCreated += 1;
166
                    Stat_Logging.SharesCreated += 1;
167
                    return true;
167
                    return true;
168
                }
168
                }
169
                else
169
                else
170
                    return false;
170
                    return false;
171
            }
171
            }
172
            catch (Exception e)
172
            catch (Exception e)
173
            {
173
            {
174
                //MessageBox.Show(e.ToString(), "Error");
174
                //MessageBox.Show(e.ToString(), "Error");
175
                DebugText.appendText(e.ToString());
175
                Debug.appendText(e.ToString());
176
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
176
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
177
                return false;
177
                return false;
178
            }
178
            }
179
        }
179
        }
180
        public static bool DeleteShareFolder(string username)
180
        public static bool DeleteShareFolder(string username)
Line 184... Line 184...
184
                bool Success = true;
184
                bool Success = true;
185
                // Removes the share BEFORE deleting the folder. Otherwise share will error on remove.
185
                // Removes the share BEFORE deleting the folder. Otherwise share will error on remove.
186
                if (!RemoveSharePermissions(username))
186
                if (!RemoveSharePermissions(username))
187
                    Success = false;
187
                    Success = false;
188
                Directory.Delete(Settings_Form.sharedFolderLocation + @"\" + username, true);
188
                Directory.Delete(Settings_Form.sharedFolderLocation + @"\" + username, true);
189
                DebugText.appendText("Shared folder for " + username + " has been deleted");
189
                Debug.appendText("Shared folder for " + username + " has been deleted");
190
                return Success;
190
                return Success;
191
            }
191
            }
192
            catch (Exception e)
192
            catch (Exception e)
193
            {
193
            {
194
                //MessageBox.Show(e.ToString(), "Error");
194
                //MessageBox.Show(e.ToString(), "Error");
195
                DebugText.appendText(e.ToString());
195
                Debug.appendText(e.ToString());
196
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
196
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
197
                return false;
197
                return false;
198
            }
198
            }
199
        }
199
        }
200
        public static bool ToggleShare(string username)
200
        public static bool ToggleShare(string username)
Line 216... Line 216...
216
                return true;
216
                return true;
217
            }
217
            }
218
            catch (Exception e)
218
            catch (Exception e)
219
            {
219
            {
220
                //MessageBox.Show(e.ToString(), "Error");
220
                //MessageBox.Show(e.ToString(), "Error");
221
                DebugText.appendText(e.ToString());
221
                Debug.appendText(e.ToString());
222
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
222
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
223
                return false;
223
                return false;
224
            }
224
            }
225
        }
225
        }
226
        public static long GetShareSize(string username)
226
        public static long GetShareSize(string username)
Line 252... Line 252...
252
                    return 0;
252
                    return 0;
253
            }
253
            }
254
            catch (Exception e)
254
            catch (Exception e)
255
            {
255
            {
256
                //MessageBox.Show(e.ToString(), "Error");
256
                //MessageBox.Show(e.ToString(), "Error");
257
                DebugText.appendText(e.ToString());
257
                Debug.appendText(e.ToString());
258
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
258
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
259
                return 0;
259
                return 0;
260
            }
260
            }
261
        }
261
        }
262
        public static bool ChangeUserPassword(string username, string password)
262
        public static bool ChangeUserPassword(string username, string password)
Line 270... Line 270...
270
                PrincipalSearcher searcher = new PrincipalSearcher(user);
270
                PrincipalSearcher searcher = new PrincipalSearcher(user);
271
                Principal result = searcher.FindOne();
271
                Principal result = searcher.FindOne();
272
                user = (UserPrincipal)result;
272
                user = (UserPrincipal)result;
273
                user.SetPassword(password);
273
                user.SetPassword(password);
274
                user.Save();
274
                user.Save();
275
                DebugText.appendText("Password for " + username + " has been changed");
275
                Debug.appendText("Password for " + username + " has been changed");
276
                return true;
276
                return true;
277
            }
277
            }
278
            catch (Exception e)
278
            catch (Exception e)
279
            {
279
            {
280
                //MessageBox.Show(e.ToString(), "Error");
280
                //MessageBox.Show(e.ToString(), "Error");
281
                DebugText.appendText(e.ToString());
281
                Debug.appendText(e.ToString());
282
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
282
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
283
                return false;
283
                return false;
284
            }
284
            }
285
        }
285
        }
286
        public static bool SessionsOpen()
286
        public static bool SessionsOpen()
Line 307... Line 307...
307
                    return true;
307
                    return true;
308
            }
308
            }
309
            catch (Exception e)
309
            catch (Exception e)
310
            {
310
            {
311
                //MessageBox.Show(e.ToString(), "Error");
311
                //MessageBox.Show(e.ToString(), "Error");
312
                DebugText.appendText(e.ToString());
312
                Debug.appendText(e.ToString());
313
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
313
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
314
                return true;
314
                return true;
315
            }
315
            }
316
        }
316
        }
317
        private static bool SetSharePermissions(string username)
317
        private static bool SetSharePermissions(string username)
Line 331... Line 331...
331
                netProcess.StartInfo.CreateNoWindow = true;
331
                netProcess.StartInfo.CreateNoWindow = true;
332
                netProcess.StartInfo.Arguments = "SHARE \"" + username + "\"=\"" + Settings_Form.sharedFolderLocation + "\\" + username + "\" " + combinationString;
332
                netProcess.StartInfo.Arguments = "SHARE \"" + username + "\"=\"" + Settings_Form.sharedFolderLocation + "\\" + username + "\" " + combinationString;
333
                netProcess.Start();
333
                netProcess.Start();
334
                netProcess.WaitForExit();
334
                netProcess.WaitForExit();
335
                netProcess.Close();
335
                netProcess.Close();
336
                DebugText.appendText("Share permissions for " + username + " has been set");
336
                Debug.appendText("Share permissions for " + username + " has been set");
337
                return true;
337
                return true;
338
            }
338
            }
339
            catch (Exception e)
339
            catch (Exception e)
340
            {
340
            {
341
                //MessageBox.Show(e.ToString(), "Error");
341
                //MessageBox.Show(e.ToString(), "Error");
342
                DebugText.appendText(e.ToString());
342
                Debug.appendText(e.ToString());
343
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
343
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
344
                return false;
344
                return false;
345
            }
345
            }
346
        }
346
        }
347
        private static bool RemoveSharePermissions(string username)
347
        private static bool RemoveSharePermissions(string username)
Line 355... Line 355...
355
                netProcess.StartInfo.CreateNoWindow = true;
355
                netProcess.StartInfo.CreateNoWindow = true;
356
                netProcess.StartInfo.Arguments = "SHARE \"" + username + "\" /Delete";
356
                netProcess.StartInfo.Arguments = "SHARE \"" + username + "\" /Delete";
357
                netProcess.Start();
357
                netProcess.Start();
358
                netProcess.WaitForExit();
358
                netProcess.WaitForExit();
359
                netProcess.Close();
359
                netProcess.Close();
360
                DebugText.appendText("Share permissions for " + username + " has been removed");
360
                Debug.appendText("Share permissions for " + username + " has been removed");
361
                return true;
361
                return true;
362
            }
362
            }
363
            catch (Exception e)
363
            catch (Exception e)
364
            {
364
            {
365
                //MessageBox.Show(e.ToString(), "Error");
365
                //MessageBox.Show(e.ToString(), "Error");
366
                DebugText.appendText(e.ToString());
366
                Debug.appendText(e.ToString());
367
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
367
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
368
                return false;
368
                return false;
369
            }
369
            }
370
        }
370
        }
371
    }
371
    }