/
GroupAdministration.cs
1081 lines (946 loc) · 46.6 KB
/
GroupAdministration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Threading;
using MGL.Data.DataUtilities;
using MGL.DomainModel;
using DataNirvana.Database;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
namespace MGL.Security {
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Summary description for GroupAdministration
/// </summary>
public class GroupAdministration {
#region !--- Properties ---!
private ConfigurationInfo lcf = null;
public ConfigurationInfo Lcf {
get { return lcf; }
set { lcf = value; }
}
public enum AssociationTypes {
Assign, UnAssign, NotAssigned
}
public enum GroupDeleteTableType {
Main, XRefContents, XRefDisplay, XRefFunctionality, XRefUsers
}
private DatabaseWrapper dbInfo;
public DatabaseWrapper DbInfo {
get { return dbInfo; }
set { dbInfo = value; }
}
#endregion
#region !--- Constructor ---!
public GroupAdministration(ConfigurationInfo lcf) {
this.Lcf = lcf;
}
#endregion
#region !--- Methods ---!
public List<MGGroup> GetAllGroups() {
bool isFilterOutSuperGroup = false;
return GetAllGroups(isFilterOutSuperGroup);
}
/// <summary>
/// Gets list of all groups from database.
/// </summary>
/// <returns>List of MG Groups</returns>
public List<MGGroup> GetAllGroups(bool isFilterOutSuperGroup) {
List<MGGroup> allGroups = null;
GroupOperations groupHelper = null;
try {
groupHelper = new GroupOperations(Lcf);
allGroups = groupHelper.GetAllGroups(isFilterOutSuperGroup);
if (allGroups == null) {
Logger.LogError(5, "Error getting all groups from system where Filter Super Group is: " + isFilterOutSuperGroup);
}
if (allGroups.Count == 0) {
Logger.LogWarning("No group found in system. Please check it...! ");
}
} catch (Exception ex) {
Logger.LogError(5, "Error getting all groups from database at: " + ex);
return null;
} finally {
if (groupHelper != null)
groupHelper.Finish();
}
return allGroups;
}
public MGGroup GetGroup(int id) {
MGGroup result = null;
GroupOperations groupHelper = null;
try {
groupHelper = new GroupOperations(Lcf);
result = groupHelper.GetGroup(id);
if (result == null) {
Logger.LogWarning("Null Group found in the database for group ID = " + id.ToString());
}
} catch (Exception ex) {
Logger.LogError(5, "Error getting a group given its ID from database at: " + ex);
return null;
} finally {
if (groupHelper != null)
groupHelper.Finish();
}
return result;
}
public MGGroup GetGroup(string name) {
MGGroup result = null;
GroupOperations groupHelper = null;
Logger.Log("Start getting a group from databas where name of group is '" + name + "'.");
try {
groupHelper = new GroupOperations(Lcf);
result = groupHelper.GetGroup(name);
if (result == null) {
Logger.LogWarning("Null Group found in the database for group Name = " + name);
}
} catch (Exception ex) {
Logger.LogError(5, "Error getting a group from databas where name of group is '" + name + "'." + "at: " + ex);
return null;
} finally {
if (groupHelper != null)
groupHelper.Finish();
}
return result;
}
public List<MGGroup> GetDefaultGroups(bool isFilterOutSuperGroup) {
List<MGGroup> result = null;
List<MGGroup> allGroups = null;
Logger.Log("Start getting default groups from system...");
try {
result = new List<MGGroup>();
allGroups = GetAllGroups(isFilterOutSuperGroup);
if (allGroups == null) {
Logger.LogError(5, "Error while trying to get all groups from system. Quitting..!");
return null;
}
if (allGroups.Count == 0) {
Logger.LogWarning("No group is found in the system.");
return new List<MGGroup>();
}
Logger.Log("Start looping all groups to check which one is default and adding to result list...");
result = new List<MGGroup>();
foreach (MGGroup group in allGroups) {
if (group.IsDefault) {
result.Add(group);
}
}
} catch (Exception ex) {
Logger.LogError(5, "Error getting default groups from system at: " + ex);
return null;
}
return result;
}
public List<MGGroup> GetAllNonDefaultGroups(bool isFilterOutSuperGroup) {
List<MGGroup> result = null;
List<MGGroup> allGroups = null;
Logger.Log("Start getting all non default groups from system...");
try {
result = new List<MGGroup>();
allGroups = GetAllGroups(isFilterOutSuperGroup);
if (allGroups == null) {
Logger.LogError(5, "Error while trying to get all groups from system. Quitting..!");
return null;
}
if (allGroups.Count == 0) {
Logger.LogWarning("No group is found in the system.");
return new List<MGGroup>();
}
Logger.Log("Start looping all groups to check which one is not default and adding to result list...");
result = new List<MGGroup>();
foreach (MGGroup group in allGroups) {
if (!group.IsDefault) {
result.Add(group);
}
}
} catch (Exception ex) {
Logger.LogError(5, "Error getting default groups from system at: " + ex);
return null;
}
return result;
}
public List<MGGroup> GetUnassignedGroups(MGUser SelectedUser, List<MGGroup> AssignedUser_Groups) {
bool isFilterOutSuperGroup = false;
return GetUnassignedGroups(SelectedUser, AssignedUser_Groups, isFilterOutSuperGroup);
}
public List<MGGroup> GetUnassignedGroups(MGUser SelectedUser, List<MGGroup> AssignedUser_Groups, bool isFilterOutSuperGroup) {
if (SelectedUser == null) {
Logger.LogError(5, "Null MG User is selected and passed. Quitting..");
return null;
}
if (AssignedUser_Groups == null) {
Logger.LogError(5, "Null list of assigned MG User is selected and passed. Quitting..");
return null;
}
List<MGGroup> unAssignedGroups = null;
try {
//First Get All Groups
List<MGGroup> allGroups = GetAllGroups();
if (allGroups == null) {
Logger.LogError(5, "Failed to get all groups from system. Quitting");
return null;
}
unAssignedGroups = new List<MGGroup>();
foreach (MGGroup group in allGroups) {
if (isFilterOutSuperGroup) {
if (group.Name != null && group.Name.Equals(GroupAdministration.SUPER_USER_GROUP_NAME, StringComparison.CurrentCultureIgnoreCase)) {
continue;
}
}
if (!AssignedUser_Groups.Contains(group)) {
unAssignedGroups.Add(group);
}
}
} catch (Exception ex) {
Logger.LogError(5, "Error getting unassigned groups for user = " + SelectedUser.Username + " at: " + ex);
return null;
}
return unAssignedGroups;
}
public string GetError() {
return MGLSessionSecurityInterface.Instance().SecurityError;
}
/// <summary>
/// Given a MG Group, Add to database
/// </summary>
/// <param name="group">Group to add</param>
/// <returns>Return true if success, false otherwidr</returns>
public bool AddGroup(MGGroup groupToAdd, out string message) {
bool isAddSuccess = false;
message = string.Empty;
try {
DbInfo = new DatabaseWrapper(Lcf);
//Check if group can be added
if (CheckIfGroupCanBeAdded(groupToAdd, out message)) {
//Insert
string sql = GroupQB.GetInsertGroupSql(groupToAdd);
DbInfo.Connect();
bool success = false;
if (DbInfo.ExecuteSQL(sql, ref success) == 1) {
isAddSuccess = true;
message = "Successfully added a group: '" + groupToAdd.Name + "'";
} else {
message = "Failed to add a group: '" + groupToAdd.Name + "'";
}
}
} catch (Exception ex) {
Logger.LogError(5, "Error adding a group at " + ex);
message = "Error adding a Group " + groupToAdd.Name + ". Contact MGL.";
isAddSuccess = false;
} finally {
if (isAddSuccess) {
SecureContentWrapper.SecurityHasBeenModifiedThisSession = true;
}
if (DbInfo != null)
DbInfo.Disconnect();
}
return isAddSuccess;
}
public bool EditGroup(MGGroup newGroup, out string message) {
bool isAddSuccess = false;
message = string.Empty;
try {
DbInfo = new DatabaseWrapper(Lcf);
if (CheckIfGroupCanBeEdited(newGroup, out message)) {
//Edit
string sql = GroupQB.GetEditGroupSql(newGroup);
DbInfo.Connect();
bool success = false;
if (DbInfo.ExecuteSQL(sql, ref success) == 1) {
isAddSuccess = true;
message = "Successfully edited group: '" + newGroup.Name + "'";
} else {
message = "Failed to edit group: '" + newGroup.Name + "'";
}
}
} catch (Exception ex) {
Logger.LogError(5, "Error editing a group at " + ex);
message = "Error editing a Group " + newGroup.Name + ". Contact MGL.";
isAddSuccess = false;
} finally {
if (isAddSuccess) {
SecureContentWrapper.SecurityHasBeenModifiedThisSession = true;
}
if (DbInfo != null)
DbInfo.Disconnect();
}
return isAddSuccess;
}
private bool CheckIfGroupCanBeEdited(MGGroup newGroup, out string message) {
bool canEdit = true;
message = "";
//For given ID, If the new name is same as old Name then check if any of other values is changed
//If the new Name is not same as that of existing name of same group, then check if the new name already exists for any other group
MGGroup existingGroup = GetGroup(newGroup.ID);
if (existingGroup.Name.Equals(newGroup.Name)) {
if (existingGroup.Description.Equals(newGroup.Description)) {
if (existingGroup.IsDefault == newGroup.IsDefault) {
message = "All new values are identical to existing values.";
canEdit = false;
}
}
} else if (!CheckIfNewGroupNameIsValidInSystem(newGroup.Name)) {
message = "The name '" + newGroup.Name + "' is not valid or already exists in the system. Please select another name.";
canEdit = false;
}
return canEdit;
}
/// <summary>
/// Checks if a group can be added.
/// </summary>
/// <param name="group"></param>
/// <returns></returns>
private bool CheckIfGroupCanBeAdded(MGGroup group, out string msg) {
bool canAdd = true;
msg = string.Empty;
try {
if (group == null || group.Name == null || group.Description == string.Empty) {
Logger.LogError(5, "Null or empty value for new group is supplied. Check the inputs.");
msg = "Null or empty input value. Please check that there is a valid value for gorup name and description ";
return false;
}
//Now check if Name is present in the database
if (!CheckIfNewGroupNameIsValidInSystem(group.Name)) {
msg = "The name '" + group.Name + "' is invalid or already exists in the system. Please select another name.";
return false;
}
} catch (Exception ex) {
Logger.LogError(5, "Error while checking if a group can be added at: " + ex);
canAdd = false;
}
return canAdd;
}
/// <summary>
/// Check if Group Name already exists in the database
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
private bool CheckIfNewGroupNameIsValidInSystem(string groupName) {
bool isValid = true;
string sql = "";
if (groupName == null) {
Logger.LogError(5, "Cannot check if a NULL groupname already exists in the system!");
return false;
}
if (groupName == String.Empty) {
Logger.LogError(5, "Cannot check if an empty groupname already exists in the system!");
return false;
}
string groupNameGiven = DatabaseHelper.SQL_INJECTION_CHECK_PARAMETER(true, groupName);
try {
DbInfo = new DatabaseWrapper(Lcf);
DbInfo.Connect();
sql = GroupQB.GetCheckIfAGroupISPresentSql(groupNameGiven);
IDataReader reader = DbInfo.RunSqlReader(sql);
while (reader.Read()) {
string groupNameFromDB = "";
if (reader[GroupQB.GROUP_NAME_COL] != System.DBNull.Value) {
groupNameFromDB = (string)reader[GroupQB.GROUP_NAME_COL];
} else {
Logger.LogError(5, "Got Null Group Name using Sql: " + sql);
return false;
}
groupNameFromDB = DatabaseHelper.SQL_INJECTION_CHECK_PARAMETER(true, groupNameFromDB);
if (groupNameFromDB.Equals(groupNameGiven, StringComparison.CurrentCultureIgnoreCase)) {
isValid = false;
} else {
isValid = true;
}
}
} catch (Exception ex) {
Logger.LogError(5, "Error checking if a group name already exists in the system at: " + ex);
isValid = false;
} finally {
if (DbInfo != null)
DbInfo.Disconnect();
}
return isValid;
}
///// <summary>
///// Convert ContentItems into a List of ListItems to enable to link to front end.
///// </summary>
///// <param name="contentItems">List of Content Items</param>
///// <returns>List of ListItems</returns>
//public List<ListItem> GetGroupContentsAsListItem(List<ContentItem> contentItems)
//{
// List<ListItem> result = null;
// try
// {
// if (contentItems == null)
// {
// Logger.LogWarning("Null of list of contents provided. Quitting !");
// return null;
// }
// if (contentItems.Count == 0)
// {
// Logger.LogWarning("Empty list of contents provided. Quitting");
// result = new List<ListItem>();
// }
// result = new List<ListItem>();
// foreach (ContentItem contentItem in contentItems)
// {
// ListItem li = new ListItem();
// li.Value = contentItem.ID.ToString();
// int parentID = contentItem.GetParentID();
// if (parentID > 0)
// {
// li.Value = parentID.ToString() + "_" + li.Value;
// }
// li.Text = contentItem.Name;
// result.Add(li);
// }
// }
// catch (Exception ex)
// {
// Logger.LogError("Error creating a List<ListItem> from a list of content items at" + ex);
// return null;
// }
// return result;
//}
/// <summary>
/// Given a list of groups, it create a List of ListItems, where each ListItems contains the ID and Name of group.
/// This is to allow the group names to be displayed onto a list control on web page.
/// </summary>
/// <param name="Groups"></param>
/// <returns></returns>
public List<ListItem> GetGroupInfoAsListItem(List<MGGroup> Groups) {
List<ListItem> result = null;
try {
if (Groups == null) {
Logger.LogWarning("Null of list of groups provided. Quitting !");
return null;
}
if (Groups.Count == 0) {
Logger.LogWarning("Empty list of groups provided.");
result = new List<ListItem>();
}
result = new List<ListItem>();
foreach (MGGroup group in Groups) {
ListItem li = new ListItem();
li.Value = group.ID.ToString();
li.Text = group.Name;
result.Add(li);
}
} catch (Exception ex) {
Logger.LogError(5, "Error creating a List<ListItem> from a list of groups at" + ex);
return null;
}
return result;
}
public bool AssignUserToDefaultGroups(int userID) {
List<int> defaultGroupIDs = GetDefaultGroupIDs();
if (defaultGroupIDs == null) {
Logger.LogError(5, "Cannot assign user with ID " + userID + " to default user groups as GetDefaultGroupIDs returned a NULL list!");
return false;
} else if (defaultGroupIDs.Count == 0) {
Logger.LogWarning("Skipping assigning user with ID " + userID + " to default user groups as GetDefaultGroupIDs returned a zero default groups ...");
return true;
}
return AssignUserToGroups(userID, defaultGroupIDs);
}
private List<int> GetDefaultGroupIDs() {
List<int> defaultGroupIDs = null;
string sql = "";
try {
DbInfo = new DatabaseWrapper(Lcf);
DbInfo.Connect();
if (!DbInfo.ColumnExists(GroupQB.GROUP_TBLE_NAME, GroupQB.GROUP_DEFAULT_COL)) {
Logger.LogError(5, "Column " + GroupQB.GROUP_DEFAULT_COL + " does not exist in table " + GroupQB.GROUP_TBLE_NAME + ". Cannot get default Group IDs!");
return null;
}
sql = GroupQB.GetSelectDefaultGroupIdsSql();
defaultGroupIDs = DbInfo.GetIntegerList(sql);
if (defaultGroupIDs == null) {
Logger.LogError(5, "Failed to get default group IDs!");
return null;
}
if (defaultGroupIDs.Count == 0) {
Logger.Log("No default group is found in the system when using SQL " + sql);
}
} catch (Exception ex) {
Logger.LogError(5, "Failed to get default group IDs at: " + ex);
defaultGroupIDs = null;
} finally {
if (DbInfo != null)
DbInfo.Disconnect();
}
return defaultGroupIDs;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// 30-Jan-2015 - added the bool toggle on whether or not to use the session wrapper. This causes an issue with threaded applications as in the
/// worker thread the session wrappers are not available.
/// </summary>
public bool AssignUserToGroups(int userID, List<int> groupsIDs, bool recordModificationInSessionWrapper) {
return ChangeGroupToUserAssociation(userID, groupsIDs, AssociationTypes.Assign, recordModificationInSessionWrapper);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
public bool AssignUserToGroups(int userID, List<int> groupsIDs) {
// the default case ...
return ChangeGroupToUserAssociation(userID, groupsIDs, AssociationTypes.Assign, true);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Change the group association for a given user.
/// It can assign the groups to a user and also can un assign groups linked to a user.
/// 30-Jan-2015 - added the bool toggle on whether or not to use the session wrapper. This causes an issue with threaded applications as in the
/// worker thread the session wrappers are not available.
/// </summary>
/// <param name="groupsIDs">Group Ids to Assign or UnAssign</param>
/// <param name="associationType">Assign or UnAssign</param>
/// <returns>True if successfull, false other wise</returns>
public bool ChangeGroupToUserAssociation(int userID, List<int> groupsIDs, AssociationTypes associationType, bool recordModificationInSessionWrapper) {
bool isChangeSuccess = true;
DbInfo = new DatabaseWrapper(Lcf);
string sql = "";
try {
DbInfo.Connect();
foreach (int groupID in groupsIDs) {
if (associationType == AssociationTypes.Assign) {
sql = GroupQB.GetAssignGroupForUserSql(userID, groupID);
} else {
sql = GroupQB.GetUnAssignGroupForUserSql(userID, groupID);
}
bool success = false;
int numChanged = DbInfo.ExecuteSQL(sql, ref success);
if (numChanged == 0) {
isChangeSuccess = false;
}
}
} catch (Exception ex) {
Logger.LogError(5, "Error in changing group association at: " + ex);
isChangeSuccess = false;
} finally {
if (isChangeSuccess && recordModificationInSessionWrapper) {
SecureContentWrapper.SecurityHasBeenModifiedThisSession = true;
if (DbInfo != null)
DbInfo.Disconnect();
}
}
return isChangeSuccess;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Removes all xref records that link the specified user to any groups.
/// 30-Jan-2015 - added the bool toggle on whether or not to use the session wrapper. This causes an issue with threaded applications as in the
/// worker thread the session wrappers are not available.
/// </summary>
/// <param name="userID">The ID of the user to remove from all groups.</param>
/// <returns>True if successfull, false otherwise.</returns>
public bool UnassignAllGroupsFromUser(int userID, bool recordModificationInSessionWrapper) {
if (userID < 1) {
Logger.LogError(5, "Cannot UnassignAllGroupsFromUser where userID is not specified!");
return false;
}
bool isSuccess = false;
DbInfo = new DatabaseWrapper(Lcf);
string sql = "";
try {
DbInfo.Connect();
sql = GroupQB.GetDeleteUserFromAllGroupsSql(userID);
if (sql == null) {
Logger.LogError(5, "Failed to get SQL to delete user from all groups! Abandoning UnassignAllGroupsFromUser ...");
return false;
}
bool success = false;
int numChanged = DbInfo.ExecuteSQL(sql, ref success);
if (numChanged == 0) {
isSuccess = false;
} else {
isSuccess = true;
}
} catch (Exception ex) {
Logger.LogError(5, "Error in changing group association at: " + ex);
return false;
} finally {
if (isSuccess && recordModificationInSessionWrapper) {
SecureContentWrapper.SecurityHasBeenModifiedThisSession = true;
}
if (DbInfo != null)
DbInfo.Disconnect();
}
return isSuccess;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public bool UnassignAllGroupsFromUser(int userID) {
// default case is true ....
return UnassignAllGroupsFromUser(userID, true);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public bool DeleteGroup(int groupID) {
bool isAllDeleted = false;
bool isMainDeleted = false;
bool isXrefDeleted = false;
try {
Logger.Log("Trying to delete the group entry from main table.");
isMainDeleted = DeleteGroupFromMain(groupID);
if (!isMainDeleted) {
Logger.LogError(5, "Failed to delete group with id = " + groupID + ". Quitting!");
return false;
}
isXrefDeleted = DeleteGroupEntryFromXrefTables(groupID);
if (!isXrefDeleted) {
Logger.LogError(5, "Failed to delete records from all Xref Tables for Group with id = " + groupID + ".");
return false;
}
isAllDeleted = isMainDeleted && isXrefDeleted;
} catch (Exception ex) {
Logger.LogError(5, "Error deleting group with id = " + groupID + " at: " + ex);
return false;
} finally {
if (isAllDeleted) {
SecureContentWrapper.SecurityHasBeenModifiedThisSession = true;
}
}
return isAllDeleted;
}
private bool DeleteGroupFromMain(int groupID) {
bool isDeleted = false;
string sql = "";
string tableName = "";
try {
Logger.Log("Trying to delete the group entry from main table '" + GroupQB.GROUP_TBLE_NAME + "'");
tableName = GetTableNameForGroup(GroupDeleteTableType.Main);
sql = GroupQB.GetDelteGroupSql(groupID, tableName, true);
DbInfo = new DatabaseWrapper(Lcf);
DbInfo.Connect();
bool success = false;
int numChanged = DbInfo.ExecuteSQL(sql, ref success);
if (numChanged == 0) {
isDeleted = false;
} else {
isDeleted = true;
}
} catch (Exception ex) {
Logger.LogError(5, "Error deleting the group entry from main table '" + GroupQB.GROUP_TBLE_NAME + "' at: " + ex);
return false;
} finally {
if (isDeleted) {
SecureContentWrapper.SecurityHasBeenModifiedThisSession = true;
}
if (DbInfo != null)
DbInfo.Disconnect();
}
return isDeleted;
}
private bool DeleteGroupEntryFromXrefTables(int groupID) {
bool isDeleted = false;
bool IsContentDelete = false;
bool IsDisplayDelete = false;
bool IsFunctionalityDelete = false;
bool IsUsersDelete = false;
try {
IsContentDelete = DeleteGroupEntryFromXrefTable(groupID, GroupDeleteTableType.XRefContents);
IsDisplayDelete = DeleteGroupEntryFromXrefTable(groupID, GroupDeleteTableType.XRefDisplay);
IsFunctionalityDelete = DeleteGroupEntryFromXrefTable(groupID, GroupDeleteTableType.XRefFunctionality);
IsUsersDelete = DeleteGroupEntryFromXrefTable(groupID, GroupDeleteTableType.XRefUsers);
isDeleted = IsContentDelete && IsDisplayDelete && IsFunctionalityDelete && IsUsersDelete;
} catch (Exception ex) {
Logger.LogError(5, "Error deleting the group entry from Xref tables for Group ID " + groupID + "' at: " + ex);
return false;
} finally {
if (isDeleted) {
SecureContentWrapper.SecurityHasBeenModifiedThisSession = true;
}
}
return isDeleted;
}
private bool DeleteGroupEntryFromXrefTable(int groupID, GroupDeleteTableType groupDeleteTableType) {
bool isDeleted = false;
int count = -1;
int countDeleted = -1;
string tableName = GetTableNameForGroup(groupDeleteTableType);
string sql = "";
Logger.Log("Start deleting records from table '" + tableName + "' for group id " + groupID);
try {
DbInfo = new DatabaseWrapper(Lcf);
DbInfo.Connect();
Logger.Log("Getting records from table '" + tableName + "' for group id " + groupID);
count = GetRecordsForAGroupFromXref(groupID, tableName);
if (count > 0) {
Logger.Log("Start deleting records from '" + tableName + "' for group id = " + groupID);
sql = GroupQB.GetDelteGroupSql(groupID, tableName, false);
bool success = false;
countDeleted = DbInfo.ExecuteSQL(sql, ref success);
if (count == countDeleted) {
Logger.Log("Successfully deleted " + count + " records from " + tableName + " for group id = " + groupID);
isDeleted = true;
} else {
Logger.Log("Failed to delte " + count + " records from " + tableName + " for group id = " + groupID);
}
} else if (count == 0) {
Logger.Log("No records was found in table '" + tableName + "' for group id " + groupID);
return true;
}
} catch (Exception ex) {
Logger.Log("Error deleting records from table '" + tableName + "' for group id " + groupID + " at: " + ex);
return false;
} finally {
if (isDeleted) {
SecureContentWrapper.SecurityHasBeenModifiedThisSession = true;
}
if (DbInfo != null)
DbInfo.Disconnect();
}
return isDeleted;
}
private int GetRecordsForAGroupFromXref(int groupID, string tbleName) {
int count = -1;
// string sql = "";
string whereClause = GroupQB.GROUP_ID_COL_XREFTBL + " = " + groupID;
try {
DbInfo = new DatabaseWrapper(Lcf);
DbInfo.Connect();
count = DbInfo.GetCount(tbleName, whereClause);
if (count == -1) {
Logger.LogError(5, "Error getting count from table " + tbleName + " Where " + whereClause);
}
} catch (Exception ex) {
Logger.LogError(5, "Error getting count of xref entires for a group with ID = " + groupID + " from table" + tbleName + "' at :" + ex);
count = -1;
} finally {
if (DbInfo != null)
DbInfo.Disconnect();
}
return count;
}
private string GetTableNameForGroup(GroupDeleteTableType groupDeleteTableType) {
string tbleName = "";
if (groupDeleteTableType == GroupAdministration.GroupDeleteTableType.Main) {
tbleName = GroupQB.GROUP_TBLE_NAME;
}
if (groupDeleteTableType == GroupAdministration.GroupDeleteTableType.XRefContents) {
tbleName = GroupQB.GROUP_CONTENT_XREF_TBL;
}
if (groupDeleteTableType == GroupAdministration.GroupDeleteTableType.XRefDisplay) {
tbleName = GroupQB.GROUP_DISPLAY_XREF_TBL;
}
if (groupDeleteTableType == GroupAdministration.GroupDeleteTableType.XRefFunctionality) {
tbleName = GroupQB.GROUP_FUNCTION_XREF_TBL;
}
if (groupDeleteTableType == GroupAdministration.GroupDeleteTableType.XRefUsers) {
tbleName = GroupQB.GROUP_USER_XREF_TBL;
}
return tbleName;
}
/// <summary>
/// Check if a group can be deleted. It check that it is not the last default group.
/// </summary>
/// <param name="groupID">Group Id</param>
/// <param name="msg">Output message</param>
/// <returns>True if can, false otherwise</returns>
public bool CanDeleteGroup(int groupID, out string msg) {
bool canDelete = false;
MGGroup group = null;
msg = "";
try {
group = new MGGroup();
group = GetGroup(groupID);
if (group == null) {
msg = "Failed to get group for ID (" + groupID + "). Quitting";
Logger.LogError(5, msg);
return false;
}
//Checking if group if not default
if (!group.IsDefault) {
msg = "Group '" + group.Name + "' is not a 'Default Group' therefore can be deleted.";
Logger.Log(msg);
return true;
}
canDelete = CanDeleteDefaultGroup(group, out msg);
} catch (Exception ex) {
msg = "Error checking if gorup with ID (" + groupID + ") can be deleted";
Logger.LogError(5, msg + " at: " + ex);
return false;
}
return canDelete;
}
private bool CanDeleteDefaultGroup(MGGroup group, out string msg) {
bool canDelte = false;
List<int> allDefaultGroupsIds = null;
msg = "";
try {
Logger.Log("Getting IDs of all default groups in the system....");
allDefaultGroupsIds = new List<int>();
allDefaultGroupsIds = GetDefaultGroupIDs();
if (allDefaultGroupsIds == null) {
msg = "Error getting IDs of all Default Groups in the system. Quitting!";
Logger.LogError(5, msg);
return false;
}
if (allDefaultGroupsIds.Count == 0) {
msg = "Can not delete. No default group entry is retreived from system. It is contradictory as the current selected group '" + group.Name + "'is a 'default group'.";
Logger.LogError(5, msg);
return false;
}
if (allDefaultGroupsIds.Count == 1 && allDefaultGroupsIds.Contains(group.ID)) {
msg = "Can not delete as the selected group is the only 'Default Group' in the system.";
Logger.Log(msg);
return false;
}
if (allDefaultGroupsIds.Count > 1 && allDefaultGroupsIds.Contains(group.ID)) {
msg = "Can delete the selected 'Default Group' as it is not 'the Only' in the system.";
Logger.Log(msg);
canDelte = true;
}
} catch (Exception ex) {
msg = "Error checking if default gorup '" + group.Name + "' can be deleted";
Logger.LogError(5, msg + " at: " + ex);
return false;
}
return canDelte;
}
/// <summary>
/// Get Users for a given Group. It populate only (3) three User Information (UserName, JobTitle, Email)
/// </summary>
/// <param name="group">Group for which to find users.</param>
/// <param name="associationTypes">Assigned and Unassigned user to group.</param>
/// <returns></returns>
public List<MGUser> GetUsersForAGroup(MGGroup group, string searchString, AssociationTypes associationTypes) {
List<MGUser> result = null;
IDataReader reader = null;
string strUserID = null;
int userID = -1;
string sql = "";
string msgPart = "getting users which are '" + associationTypes + "ed' to Group '" + group.Name + "'";
bool isLockAcquired = Monitor.TryEnter(UserAdministration.USER_ADMIN_LOCK_OBJ, UserAdministration.USER_ADMIN_LOCK_TIMEOUT);
if (isLockAcquired) {
try {
Logger.Log("Start " + msgPart);
DbInfo = new DatabaseWrapper(Lcf);
DbInfo.Connect();
sql = GroupQB.GetSelectUsersForAGroupSql(group.ID, searchString, associationTypes);
reader = DbInfo.RunSqlReader(sql);
if (reader == null) {
Logger.LogError(5, "Quitting, failed " + msgPart + " with sql : " + sql);
return null;
}
result = new List<MGUser>();
while (reader.Read()) {
strUserID = null;
userID = -1;
MGUser user = new MGUser();
//Get USER ID
if (reader[GroupQB.USER_ID_GENERAL_COL] != System.DBNull.Value) {
strUserID = reader[GroupQB.USER_ID_GENERAL_COL].ToString();
if (!int.TryParse(strUserID, out userID)) {
userID = -1;
Logger.LogError(5, "Error parsing user ID into integer. Quitting");
return null;
}
}
user.ID = userID;
//Get User Name
if (reader[GroupQB.USER_NAME_COL] != System.DBNull.Value) {
user.Username = SecureStringWrapper.Encrypt((string)reader[GroupQB.USER_NAME_COL]);
} else {
Logger.LogWarning("Null or empty User is found for ID =" + user.ID + ". Please check the database!");
user.Username = SecureStringWrapper.Encrypt("");
}
//Get User EMAIL
if (reader[GroupQB.USER_EMAIL_COL] != System.DBNull.Value) {
user.Email = SecureStringWrapper.Encrypt((string)reader[GroupQB.USER_EMAIL_COL]);
} else {
Logger.LogWarning("Null or empty Email is found for ID =" + user.ID + ". Please check the database!");
user.Email = SecureStringWrapper.Encrypt("");
}
//Get User Job Title
if (reader[GroupQB.USER_JOBTITLE_COL] != System.DBNull.Value) {
user.JobTitle = SecureStringWrapper.Encrypt((string)reader[GroupQB.USER_JOBTITLE_COL]);
} else {
//Logger.LogWarning("Null or empty job title is found for ID =" + user.ID + ". Please check the database!");
user.JobTitle = SecureStringWrapper.Encrypt("");
}
result.Add(user);
}
} catch (Exception ex) {
Logger.LogError(5, "Error " + msgPart + " at: " + ex);
return null;
} finally {
Monitor.Exit(UserAdministration.USER_ADMIN_LOCK_OBJ);
if (reader != null && !reader.IsClosed)
reader.Close();
if (DbInfo != null)
DbInfo.Disconnect();
}
} else {
Logger.LogError(5, "Failed to get exclusive lock in GetUsersForAGroup when " + msgPart);
return null;
}
return result;
}
public List<ListItem> GetUserInfoAsListItem(List<MGUser> users) {
List<ListItem> result = null;
try {
if (users == null) {
Logger.LogWarning("Null of list of users provided. Quitting !");
return null;
}
if (users.Count == 0) {
Logger.LogWarning("Empty list of users provided.");
result = new List<ListItem>();
}
result = new List<ListItem>();
foreach (MGUser user in users) {