Unified · Split
| @@ -2731,6 +2731,52 @@ namespace GSF.Identity · 2730 unmodified lines | ||
| 2731 | 2731 | #endif |
| 2732 | 2732 | } |
| 2733 | 2733 | |
| 2734 | + /// <summary> | |
| 2735 | + /// Determines whether the given security identifier identifies a user account. | |
| 2736 | + /// </summary> | |
| 2737 | + /// <param name="sid">The security identifier.</param> | |
| 2738 | + /// <returns>True if the security identifier identifies a user account; false otherwise.</returns> | |
| 2739 | + public static bool IsUserSID(string sid) | |
| 2740 | + { | |
| 2741 | +#if MONO | |
| 2742 | + throw new NotSupportedException("Not supported under Mono."); | |
| 2743 | +#else | |
| 2744 | + try | |
| 2745 | + { | |
| 2746 | + string accountName = SIDToAccountName(sid); | |
| 2747 | + DirectoryEntry entry = new DirectoryEntry(string.Format("WinNT://{0}", ValidateGroupName(accountName).Replace('\\', '/'))); | |
| 2748 | + return entry.SchemaClassName.Equals("User", StringComparison.OrdinalIgnoreCase); | |
| 2749 | + } | |
| 2750 | + catch (COMException) | |
| 2751 | + { | |
| 2752 | + return false; | |
| 2753 | + } | |
| 2754 | +#endif | |
| 2755 | + } | |
| 2756 | + | |
| 2757 | + /// <summary> | |
| 2758 | + /// Determines whether the given security identifier identifies a group. | |
| 2759 | + /// </summary> | |
| 2760 | + /// <param name="sid">The security identifier.</param> | |
| 2761 | + /// <returns>True if the security identifier identifies a group; false otherwise.</returns> | |
| 2762 | + public static bool IsGroupSID(string sid) | |
| 2763 | + { | |
| 2764 | +#if MONO | |
| 2765 | + throw new NotSupportedException("Not supported under Mono."); | |
| 2766 | +#else | |
| 2767 | + try | |
| 2768 | + { | |
| 2769 | + string accountName = SIDToAccountName(sid); | |
| 2770 | + DirectoryEntry entry = new DirectoryEntry(string.Format("WinNT://{0}", ValidateGroupName(accountName).Replace('\\', '/'))); | |
| 2771 | + return entry.SchemaClassName.Equals("Group", StringComparison.OrdinalIgnoreCase); | |
| 2772 | + } | |
| 2773 | + catch (COMException) | |
| 2774 | + { | |
| 2775 | + return false; | |
| 2776 | + } | |
| 2777 | +#endif | |
| 2778 | + } | |
| 2779 | + | |
| 2734 | 2780 | // DirectoryEntry will only resolve "BUILTIN\" groups with a dot ".\" |
| 2735 | 2781 | private static string ValidateGroupName(string groupName) |
| 2736 | 2782 | { |
| @@ -138,8 +138,6 @@ namespace GSF.PhasorProtocols.UI.UserControls · 137 unmodified lines | ||
| 138 | 138 | if (!CommonFunctions.CurrentPrincipal.IsInRole("Administrator,Editor")) |
| 139 | 139 | ButtonInputWizard.IsEnabled = false; |
| 140 | 140 | |
| 141 | − | |
| 142 | − | |
| 143 | 141 | m_windowsServiceClient = CommonFunctions.GetWindowsServiceClient(); |
| 144 | 142 | |
| 145 | 143 | if (m_windowsServiceClient == null || m_windowsServiceClient.Helper.RemotingClient.CurrentState != ClientState.Connected) |
| @@ -171,27 +169,39 @@ namespace GSF.PhasorProtocols.UI.UserControls · 25 unmodified lines | ||
| 171 | 169 | Version appVersion = AssemblyInfo.EntryAssembly.Version; |
| 172 | 170 | TextBlockManagerVersion.Text = appVersion.Major + "." + appVersion.Minor + "." + appVersion.Build + ".0"; |
| 173 | 171 | |
| 174 | − using (AdoDataConnection database = new AdoDataConnection(CommonFunctions.DefaultSettingsCategory)) | |
| 172 | + try | |
| 175 | 173 | { |
| 176 | − TextBlockDatabaseType.Text = database.DatabaseType.ToString(); | |
| 177 | − | |
| 178 | − if (database.DatabaseType == DatabaseType.SQLite || database.DatabaseType == DatabaseType.Access) | |
| 174 | + using (AdoDataConnection database = new AdoDataConnection(CommonFunctions.DefaultSettingsCategory)) | |
| 179 | 175 | { |
| 176 | + TextBlockDatabaseType.Text = database.DatabaseType.ToString(); | |
| 177 | + | |
| 180 | 178 | try |
| 181 | 179 | { |
| 182 | − // Extract database file name from connection string for file centric databases | |
| 183 | − TextBlockDatabaseName.Text = FilePath.GetFileName(ConfigurationFile.Current.Settings[CommonFunctions.DefaultSettingsCategory]["ConnectionString"].Value.ParseKeyValuePairs()["Data Source"]); | |
| 180 | + if (database.IsSqlite || database.IsJetEngine) | |
| 181 | + { | |
| 182 | + // Extract database file name from connection string for file centric databases | |
| 183 | + TextBlockDatabaseName.Text = FilePath.GetFileName(database.Connection.ConnectionString.ParseKeyValuePairs()["Data Source"]); | |
| 184 | + } | |
| 185 | + else if (database.IsOracle) | |
| 186 | + { | |
| 187 | + // Extract user name from connection string for Oracle databases | |
| 188 | + TextBlockDatabaseName.Text = database.Connection.ConnectionString.ParseKeyValuePairs()["User Id"]; | |
| 189 | + } | |
| 190 | + else | |
| 191 | + { | |
| 192 | + TextBlockDatabaseName.Text = database.Connection.Database; | |
| 193 | + } | |
| 184 | 194 | } |
| 185 | 195 | catch |
| 186 | 196 | { |
| 187 | − // Fall back on database name if file name extraction fails | |
| 197 | + // Fall back on database name if file anything fails | |
| 188 | 198 | TextBlockDatabaseName.Text = database.Connection.Database; |
| 189 | 199 | } |
| 190 | 200 | } |
| 191 | − else | |
| 192 | − { | |
| 193 | − TextBlockDatabaseName.Text = database.Connection.Database; | |
| 194 | − } | |
| 201 | + } | |
| 202 | + catch | |
| 203 | + { | |
| 204 | + TextBlockDatabaseName.Text = "Not Avaliable"; | |
| 195 | 205 | } |
| 196 | 206 | |
| 197 | 207 | try |
| @@ -135,7 +135,7 @@ namespace GSF.TimeSeries · 134 unmodified lines | ||
| 135 | 135 | { |
| 136 | 136 | sid = UserInfo.AccountNameToSID(accountName); |
| 137 | 137 | |
| 138 | − if (!ReferenceEquals(accountName, sid)) | |
| 138 | + if (!ReferenceEquals(accountName, sid) && UserInfo.IsUserSID(sid)) | |
| 139 | 139 | updateMap.Add(id, sid); |
| 140 | 140 | } |
| 141 | 141 | } |
| @@ -159,7 +159,7 @@ namespace GSF.TimeSeries · 17 unmodified lines | ||
| 159 | 159 | { |
| 160 | 160 | sid = UserInfo.AccountNameToSID(accountName); |
| 161 | 161 | |
| 162 | − if (!ReferenceEquals(accountName, sid)) | |
| 162 | + if (!ReferenceEquals(accountName, sid) && UserInfo.IsGroupSID(sid)) | |
| 163 | 163 | updateMap.Add(id, sid); |
| 164 | 164 | } |
| 165 | 165 | } |
| @@ -169,27 +169,39 @@ namespace GSF.TimeSeries.Transport.UI.UserControls · 168 unmodified lines | ||
| 169 | 169 | Version appVersion = AssemblyInfo.EntryAssembly.Version; |
| 170 | 170 | TextBlockManagerVersion.Text = appVersion.Major + "." + appVersion.Minor + "." + appVersion.Build + ".0"; |
| 171 | 171 | |
| 172 | − using (AdoDataConnection database = new AdoDataConnection(CommonFunctions.DefaultSettingsCategory)) | |
| 172 | + try | |
| 173 | 173 | { |
| 174 | − TextBlockDatabaseType.Text = database.DatabaseType.ToString(); | |
| 175 | − | |
| 176 | − if (database.DatabaseType == DatabaseType.SQLite || database.DatabaseType == DatabaseType.Access) | |
| 174 | + using (AdoDataConnection database = new AdoDataConnection(CommonFunctions.DefaultSettingsCategory)) | |
| 177 | 175 | { |
| 176 | + TextBlockDatabaseType.Text = database.DatabaseType.ToString(); | |
| 177 | + | |
| 178 | 178 | try |
| 179 | 179 | { |
| 180 | − // Extract database file name from connection string for file centric databases | |
| 181 | − TextBlockDatabaseName.Text = FilePath.GetFileName(ConfigurationFile.Current.Settings[CommonFunctions.DefaultSettingsCategory]["ConnectionString"].Value.ParseKeyValuePairs()["Data Source"]); | |
| 180 | + if (database.IsSqlite || database.IsJetEngine) | |
| 181 | + { | |
| 182 | + // Extract database file name from connection string for file centric databases | |
| 183 | + TextBlockDatabaseName.Text = FilePath.GetFileName(database.Connection.ConnectionString.ParseKeyValuePairs()["Data Source"]); | |
| 184 | + } | |
| 185 | + else if (database.IsOracle) | |
| 186 | + { | |
| 187 | + // Extract user name from connection string for Oracle databases | |
| 188 | + TextBlockDatabaseName.Text = database.Connection.ConnectionString.ParseKeyValuePairs()["User Id"]; | |
| 189 | + } | |
| 190 | + else | |
| 191 | + { | |
| 192 | + TextBlockDatabaseName.Text = database.Connection.Database; | |
| 193 | + } | |
| 182 | 194 | } |
| 183 | 195 | catch |
| 184 | 196 | { |
| 185 | − // Fall back on database name if file name extraction fails | |
| 197 | + // Fall back on database name if file anything fails | |
| 186 | 198 | TextBlockDatabaseName.Text = database.Connection.Database; |
| 187 | 199 | } |
| 188 | 200 | } |
| 189 | − else | |
| 190 | − { | |
| 191 | − TextBlockDatabaseName.Text = database.Connection.Database; | |
| 192 | − } | |
| 201 | + } | |
| 202 | + catch | |
| 203 | + { | |
| 204 | + TextBlockDatabaseName.Text = "Not Avaliable"; | |
| 193 | 205 | } |
| 194 | 206 | |
| 195 | 207 | try |
| @@ -62,7 +62,7 @@ namespace GSF.TimeSeries.UI.DataModels · 61 unmodified lines | ||
| 62 | 62 | |
| 63 | 63 | #endregion |
| 64 | 64 | |
| 65 | − #region [properties] | |
| 65 | + #region [ Properties ] | |
| 66 | 66 | |
| 67 | 67 | /// <summary> |
| 68 | 68 | /// Gets or sets the current SecurityGroup ID |
| @@ -240,17 +240,19 @@ namespace GSF.TimeSeries.UI.DataModels · 171 unmodified lines | ||
| 240 | 240 | |
| 241 | 241 | foreach (DataRow row in securityGroupTable.Rows) |
| 242 | 242 | { |
| 243 | + Guid id = database.Guid(row, "ID"); | |
| 244 | + | |
| 243 | 245 | securityGroupList.Add(new SecurityGroup() |
| 244 | 246 | { |
| 245 | − ID = database.Guid(row, "ID"), | |
| 247 | + ID = id, | |
| 246 | 248 | Name = UserInfo.SIDToAccountName(row.Field<string>("Name")), |
| 247 | 249 | Description = row.Field<object>("Description") == null ? string.Empty : row.Field<string>("Description"), |
| 248 | 250 | CreatedOn = Convert.ToDateTime(row.Field<object>("CreatedOn")), |
| 249 | 251 | CreatedBy = row.Field<string>("CreatedBy"), |
| 250 | 252 | UpdatedOn = Convert.ToDateTime(row.Field<object>("UpdatedOn")), |
| 251 | 253 | UpdatedBy = row.Field<string>("UpdatedBy"), |
| 252 | − CurrentUsers = GetCurrentUsers(database, Guid.Parse(row.Field<object>("ID").ToString())), | |
| 253 | − PossibleUsers = GetPossibleUsers(database, Guid.Parse(row.Field<object>("ID").ToString())) | |
| 254 | + CurrentUsers = GetCurrentUsers(database, id), | |
| 255 | + PossibleUsers = GetPossibleUsers(database, id) | |
| 254 | 256 | }); |
| 255 | 257 | } |
| 256 | 258 | |
| @@ -279,10 +281,10 @@ namespace GSF.TimeSeries.UI.DataModels · 22 unmodified lines | ||
| 279 | 281 | createdConnection = CreateConnection(ref database); |
| 280 | 282 | |
| 281 | 283 | Dictionary<Guid, string> currentUsers = new Dictionary<Guid, string>(); |
| 282 | − DataTable currentUsersTable = database.Connection.RetrieveData(database.AdapterType, database.ParameterizedQueryString("SELECT * FROM SecurityGroupUserAccountDetail WHERE SecurityGroupID = {0} ORDER BY UserName", "groupID"), groupID); | |
| 284 | + DataTable currentUsersTable = database.Connection.RetrieveData(database.AdapterType, database.ParameterizedQueryString("SELECT * FROM SecurityGroupUserAccountDetail WHERE SecurityGroupID = {0} ORDER BY UserName", "groupID"), database.Guid(groupID)); | |
| 283 | 285 | |
| 284 | 286 | foreach (DataRow row in currentUsersTable.Rows) |
| 285 | − currentUsers[database.Guid(row, "UserAccountID")] = row.Field<string>("UserName"); | |
| 287 | + currentUsers[database.Guid(row, "UserAccountID")] = UserInfo.SIDToAccountName(row.Field<string>("UserName")); | |
| 286 | 288 | |
| 287 | 289 | return currentUsers; |
| 288 | 290 | } |
| @@ -307,10 +309,10 @@ namespace GSF.TimeSeries.UI.DataModels · 18 unmodified lines | ||
| 307 | 309 | createdConnection = CreateConnection(ref database); |
| 308 | 310 | Dictionary<Guid, string> possibleGroupUsers = new Dictionary<Guid, string>(); |
| 309 | 311 | string query = database.ParameterizedQueryString("SELECT ID, Name FROM UserAccount WHERE ID NOT IN (SELECT UserAccountID FROM SecurityGroupUserAccount WHERE SecurityGroupID = {0}) ORDER BY Name", "groupID"); |
| 310 | − DataTable possibleUsersTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout, groupID); | |
| 312 | + DataTable possibleUsersTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout, database.Guid(groupID)); | |
| 311 | 313 | |
| 312 | 314 | foreach (DataRow row in possibleUsersTable.Rows) |
| 313 | − possibleGroupUsers[database.Guid(row, "ID")] = row.Field<string>("Name"); | |
| 315 | + possibleGroupUsers[database.Guid(row, "ID")] = UserInfo.SIDToAccountName(row.Field<string>("Name")); | |
| 314 | 316 | |
| 315 | 317 | return possibleGroupUsers; |
| 316 | 318 | } |
| @@ -340,8 +342,8 @@ namespace GSF.TimeSeries.UI.DataModels · 23 unmodified lines | ||
| 340 | 342 | createdConnection = CreateConnection(ref database); |
| 341 | 343 | foreach (Guid id in usersToBeAdded) |
| 342 | 344 | { |
| 343 | − userName = database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT Name FROM UserAccount WHERE ID = {0}", "userID"), id).ToNonNullString(); | |
| 344 | − securityGroupName = database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT Name FROM SecurityGroup WHERE ID = {0}", "securityGroupID"), groupID).ToNonNullString(); | |
| 345 | + userName = database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT Name FROM UserAccount WHERE ID = {0}", "userID"), database.Guid(id)).ToNonNullString(); | |
| 346 | + securityGroupName = database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT Name FROM SecurityGroup WHERE ID = {0}", "securityGroupID"), database.Guid(groupID)).ToNonNullString(); | |
| 345 | 347 | query = database.ParameterizedQueryString("INSERT INTO SecurityGroupUserAccount (SecurityGroupID, UserAccountID) VALUES ({0}, {1})", "groupID", "userID"); |
| 346 | 348 | database.Connection.ExecuteNonQuery(query, DefaultTimeout, database.Guid(groupID), database.Guid(id)); |
| 347 | 349 | CommonFunctions.LogEvent(string.Format("User \"{0}\" successfully added to security group \"{1}\" by user \"{2}\".", UserInfo.SIDToAccountName(userName), UserInfo.SIDToAccountName(securityGroupName), CommonFunctions.CurrentUser), 8); |
| @@ -375,8 +377,8 @@ namespace GSF.TimeSeries.UI.DataModels · 27 unmodified lines | ||
| 375 | 377 | createdConnection = CreateConnection(ref database); |
| 376 | 378 | foreach (Guid id in usersToBeDeleted) |
| 377 | 379 | { |
| 378 | − userName = database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT Name FROM UserAccount WHERE ID = {0}", "userID"), id).ToNonNullString(); | |
| 379 | − securityGroupName = database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT Name FROM SecurityGroup WHERE ID = {0}", "securityGroupID"), groupID).ToNonNullString(); | |
| 380 | + userName = database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT Name FROM UserAccount WHERE ID = {0}", "userID"), database.Guid(id)).ToNonNullString(); | |
| 381 | + securityGroupName = database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT Name FROM SecurityGroup WHERE ID = {0}", "securityGroupID"), database.Guid(groupID)).ToNonNullString(); | |
| 380 | 382 | query = database.ParameterizedQueryString("DELETE FROM SecurityGroupUserAccount WHERE SecurityGroupID = {0} AND UserAccountID = {1}", "groupID", "userID"); |
| 381 | 383 | database.Connection.ExecuteNonQuery(query, DefaultTimeout, database.Guid(groupID), database.Guid(id)); |
| 382 | 384 | CommonFunctions.LogEvent(string.Format("User \"{0}\" successfully removed from security group \"{1}\" by user \"{2}\".", UserInfo.SIDToAccountName(userName), UserInfo.SIDToAccountName(securityGroupName), CommonFunctions.CurrentUser), 9); |
| @@ -413,7 +415,7 @@ namespace GSF.TimeSeries.UI.DataModels · 30 unmodified lines | ||
| 413 | 415 | DataTable securityGroupTable = database.Connection.RetrieveData(database.AdapterType, "SELECT ID, Name FROM SecurityGroup ORDER BY Name"); |
| 414 | 416 | |
| 415 | 417 | foreach (DataRow row in securityGroupTable.Rows) |
| 416 | − securityGroupList[row.ConvertField<int>("ID")] = row.Field<string>("Name"); | |
| 418 | + securityGroupList[row.ConvertField<int>("ID")] = UserInfo.SIDToAccountName(row.Field<string>("Name")); | |
| 417 | 419 | |
| 418 | 420 | return securityGroupList; |
| 419 | 421 | } |
| @@ -432,29 +434,47 @@ namespace GSF.TimeSeries.UI.DataModels · 12 unmodified lines | ||
| 432 | 434 | /// <returns>String, for display use, indicating success.</returns> |
| 433 | 435 | public static string Save(AdoDataConnection database, SecurityGroup securityGroup) |
| 434 | 436 | { |
| 437 | + const string ErrorMessage = "Group name already exists."; | |
| 438 | + | |
| 435 | 439 | bool createdConnection = false; |
| 436 | 440 | string query; |
| 441 | + string groupSID; | |
| 442 | + int existing; | |
| 437 | 443 | |
| 438 | 444 | try |
| 439 | 445 | { |
| 440 | 446 | createdConnection = CreateConnection(ref database); |
| 447 | + groupSID = UserInfo.AccountNameToSID(securityGroup.Name); | |
| 448 | + | |
| 449 | + if (!UserInfo.IsGroupSID(groupSID)) | |
| 450 | + groupSID = securityGroup.Name; | |
| 441 | 451 | |
| 442 | 452 | if (securityGroup.ID == Guid.Empty) |
| 443 | 453 | { |
| 454 | + existing = Convert.ToInt32(database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT COUNT(*) FROM SecurityGroup WHERE Name = {0}", "name"), DefaultTimeout, groupSID)); | |
| 455 | + | |
| 456 | + if (existing > 0) | |
| 457 | + throw new InvalidOperationException(ErrorMessage); | |
| 458 | + | |
| 444 | 459 | query = database.ParameterizedQueryString("INSERT INTO SecurityGroup (Name, Description, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) VALUES ({0}, {1}, " + |
| 445 | 460 | "{2}, {3}, {4}, {5})", "name", "description", "updatedBy", "updatedOn", "createdBy", "createdOn"); |
| 446 | 461 | |
| 447 | − database.Connection.ExecuteNonQuery(query, DefaultTimeout, UserInfo.AccountNameToSID(securityGroup.Name), securityGroup.Description.ToNotNull(), | |
| 462 | + database.Connection.ExecuteNonQuery(query, DefaultTimeout, groupSID, securityGroup.Description.ToNotNull(), | |
| 448 | 463 | CommonFunctions.CurrentUser, database.UtcNow(), CommonFunctions.CurrentUser, database.UtcNow()); |
| 449 | 464 | |
| 450 | 465 | CommonFunctions.LogEvent(string.Format("Security group \"{0}\" created successfully by user \"{1}\".", securityGroup.Name, CommonFunctions.CurrentUser), 6); |
| 451 | 466 | } |
| 452 | 467 | else |
| 453 | 468 | { |
| 469 | + existing = Convert.ToInt32(database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT COUNT(*) FROM SecurityGroup WHERE Name = {0} AND ID <> {1}", "name", "id"), DefaultTimeout, groupSID, securityGroup.ID)); | |
| 470 | + | |
| 471 | + if (existing > 0) | |
| 472 | + throw new InvalidOperationException(ErrorMessage); | |
| 473 | + | |
| 454 | 474 | query = database.ParameterizedQueryString("UPDATE SecurityGroup SET Name = {0}, Description = {1}, UpdatedBy = {2}, UpdatedOn = {3} " + |
| 455 | 475 | "WHERE ID = {4}", "name", "description", "updatedBy", "updatedOn", "id"); |
| 456 | 476 | |
| 457 | − database.Connection.ExecuteNonQuery(query, DefaultTimeout, UserInfo.AccountNameToSID(securityGroup.Name), securityGroup.Description.ToNotNull(), | |
| 477 | + database.Connection.ExecuteNonQuery(query, DefaultTimeout, groupSID, securityGroup.Description.ToNotNull(), | |
| 458 | 478 | CommonFunctions.CurrentUser, database.UtcNow(), database.Guid(securityGroup.ID)); |
| 459 | 479 | |
| 460 | 480 | CommonFunctions.LogEvent(string.Format("Information about security group \"{0}\" updated successfully by user \"{1}\".", securityGroup.Name, CommonFunctions.CurrentUser), 7); |
| @@ -395,7 +395,7 @@ namespace GSF.TimeSeries.UI.DataModels · 394 unmodified lines | ||
| 395 | 395 | DataTable userAccountTable = database.Connection.RetrieveData(database.AdapterType, "SELECT ID, Name FROM UserAccount ORDER BY Name"); |
| 396 | 396 | |
| 397 | 397 | foreach (DataRow row in userAccountTable.Rows) |
| 398 | − userAccountList[database.Guid(row, "ID")] = row.Field<string>("Name"); | |
| 398 | + userAccountList[database.Guid(row, "ID")] = UserInfo.SIDToAccountName(row.Field<string>("Name")); | |
| 399 | 399 | |
| 400 | 400 | return userAccountList; |
| 401 | 401 | } |
| @@ -414,8 +414,12 @@ namespace GSF.TimeSeries.UI.DataModels · 12 unmodified lines | ||
| 414 | 414 | /// <returns>String, for display use, indicating success.</returns> |
| 415 | 415 | public static string Save(AdoDataConnection database, UserAccount userAccount) |
| 416 | 416 | { |
| 417 | + const string ErrorMessage = "User name already exists."; | |
| 418 | + | |
| 417 | 419 | bool createdConnection = false; |
| 418 | 420 | string query; |
| 421 | + string userAccountSID; | |
| 422 | + int existing; | |
| 419 | 423 | |
| 420 | 424 | try |
| 421 | 425 | { |
| @@ -427,19 +431,30 @@ namespace GSF.TimeSeries.UI.DataModels · 5 unmodified lines | ||
| 427 | 431 | pColumn = "[Password]"; |
| 428 | 432 | |
| 429 | 433 | object changePasswordOn = userAccount.ChangePasswordOn; |
| 434 | + | |
| 430 | 435 | if (userAccount.ChangePasswordOn == DateTime.MinValue) |
| 431 | 436 | changePasswordOn = (object)DBNull.Value; |
| 432 | 437 | else if (database.IsJetEngine) |
| 433 | 438 | changePasswordOn = userAccount.ChangePasswordOn.ToOADate(); |
| 434 | 439 | |
| 435 | − if (userAccount.ID == null || userAccount.ID == Guid.Empty) | |
| 440 | + userAccountSID = UserInfo.AccountNameToSID(userAccount.Name); | |
| 441 | + | |
| 442 | + if (!UserInfo.IsUserSID(userAccountSID)) | |
| 443 | + userAccountSID = userAccount.Name; | |
| 444 | + | |
| 445 | + if (userAccount.ID == Guid.Empty) | |
| 436 | 446 | { |
| 447 | + existing = Convert.ToInt32(database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT COUNT(*) FROM UserAccount WHERE Name = {0}", "name"), DefaultTimeout, userAccountSID)); | |
| 448 | + | |
| 449 | + if (existing > 0) | |
| 450 | + throw new InvalidOperationException(ErrorMessage); | |
| 451 | + | |
| 437 | 452 | query = database.ParameterizedQueryString("INSERT INTO UserAccount (Name, " + pColumn + ", FirstName, LastName, DefaultNodeID, Phone, Email, " + |
| 438 | 453 | "LockedOut, UseADAuthentication, ChangePasswordOn, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, " + |
| 439 | 454 | "{9}, {10}, {11}, {12}, {13})", "name", "password", "firstName", "lastName", "defaultNodeID", "phone", "email", "lockedOut", "useADAuthentication", |
| 440 | 455 | "changePasswordOn", "updatedBy", "updatedOn", "createdBy", "createdOn"); |
| 441 | 456 | |
| 442 | − database.Connection.ExecuteNonQuery(query, DefaultTimeout, UserInfo.AccountNameToSID(userAccount.Name), | |
| 457 | + database.Connection.ExecuteNonQuery(query, DefaultTimeout, userAccountSID, | |
| 443 | 458 | userAccount.Password.ToNotNull(), userAccount.FirstName.ToNotNull(), userAccount.LastName.ToNotNull(), database.CurrentNodeID(), |
| 444 | 459 | userAccount.Phone.ToNotNull(), userAccount.Email.ToNotNull(), database.Bool(userAccount.LockedOut), database.Bool(userAccount.UseADAuthentication), |
| 445 | 460 | changePasswordOn, CommonFunctions.CurrentUser, database.UtcNow(), CommonFunctions.CurrentUser, database.UtcNow()); |
| @@ -448,12 +463,17 @@ namespace GSF.TimeSeries.UI.DataModels · 2 unmodified lines | ||
| 448 | 463 | } |
| 449 | 464 | else |
| 450 | 465 | { |
| 466 | + existing = Convert.ToInt32(database.Connection.ExecuteScalar(database.ParameterizedQueryString("SELECT COUNT(*) FROM UserAccount WHERE Name = {0} AND ID <> {1}", "name", "id"), DefaultTimeout, userAccountSID, userAccount.ID)); | |
| 467 | + | |
| 468 | + if (existing > 0) | |
| 469 | + throw new InvalidOperationException(ErrorMessage); | |
| 470 | + | |
| 451 | 471 | query = database.ParameterizedQueryString("UPDATE UserAccount SET Name = {0}, " + pColumn + " = {1}, FirstName = {2}, LastName = {3}, " + |
| 452 | 472 | "DefaultNodeID = {4}, Phone = {5}, Email = {6}, LockedOut = {7}, UseADAuthentication = {8}, ChangePasswordOn = {9}, UpdatedBy = {10}, " + |
| 453 | 473 | "UpdatedOn = {11} WHERE ID = {12}", "name", "password", "firstName", "lastName", "defaultNodeID", "phone", "email", "lockedOut", |
| 454 | 474 | "useADAuthentication", "changePasswordOn", "updatedBy", "updatedOn", "id"); |
| 455 | 475 | |
| 456 | − database.Connection.ExecuteNonQuery(query, DefaultTimeout, UserInfo.AccountNameToSID(userAccount.Name), | |
| 476 | + database.Connection.ExecuteNonQuery(query, DefaultTimeout, userAccountSID, | |
| 457 | 477 | userAccount.Password.ToNotNull(), userAccount.FirstName.ToNotNull(), userAccount.LastName.ToNotNull(), database.Guid(userAccount.DefaultNodeID), |
| 458 | 478 | userAccount.Phone.ToNotNull(), userAccount.Email.ToNotNull(), database.Bool(userAccount.LockedOut), database.Bool(userAccount.UseADAuthentication), |
| 459 | 479 | changePasswordOn, CommonFunctions.CurrentUser, database.UtcNow(), database.Guid(userAccount.ID)); |