Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dbe/20362 merge to 10.5 #42

Merged
merged 9 commits into from
Aug 15, 2024
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.5.0</Version>
<Version>10.5.1</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>User Provider</Title>
<Description>User Provider</Description>
Expand Down
120 changes: 68 additions & 52 deletions src/UserDestinationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@
private List<int> MappingsWithUpdateUsersByCustomerNumberMode = new List<int>();
private TableCollection _schemaTables = null;

/// <summary>
/// Return rows affected
/// </summary>
internal int RowsAffected
{
get; private set;
}

public UserDestinationWriter(Job job, SqlConnection connection,
bool removeMissingUsers, bool generateUserPasswords, bool encryptUserPasswords, bool removeMissingGroups, bool UseEmailForUsername,
string userKeyField, string mailSubject, string senderEmail, string emailTemplate, bool allowEmail, string destinationGroup, bool deleteOnlyFromGroupsThatAreImportedTo, ILogger logger,
Expand Down Expand Up @@ -467,7 +459,7 @@
foreach (ColumnMapping columnMapping in columnMappings.Where(cm => cm.Active))
{
object rowValue = null;
bool hasValueInRow = row.TryGetValue(columnMapping.SourceColumn?.Name, out rowValue);
bool hasValueInRow = columnMapping.SourceColumn is not null && row.TryGetValue(columnMapping.SourceColumn?.Name, out rowValue);
if (columnMapping.HasScriptWithValue || hasValueInRow)
{
object evaluatedValue = columnMapping.ConvertInputValueToOutputValue(rowValue);
Expand All @@ -486,7 +478,8 @@
}
else
{
throw new Exception(BaseDestinationWriter.GetRowValueNotFoundMessage(row, columnMapping.SourceColumn.Table.Name, columnMapping.SourceColumn.Name));
throw new Exception(GetRowValueNotFoundMessage(row, columnMapping.SourceColumn?.Table?.Name ?? columnMapping.DestinationColumn?.Table?.Name,
columnMapping.SourceColumn?.Name ?? columnMapping.DestinationColumn?.Name));
}
}

Expand Down Expand Up @@ -530,7 +523,7 @@
ColumnMapping cm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail");
if (cm != null)
{
dataRow["AccessUserUserName"] = row[cm.SourceColumn.Name];
dataRow["AccessUserUserName"] = GetValue(cm, row);
}
}
}
Expand All @@ -539,9 +532,13 @@
if (_useEmailForUsername)
{
ColumnMapping cm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail");
if (cm != null && !string.IsNullOrEmpty(Converter.ToString(row[cm.SourceColumn.Name])))
if (cm != null)
{
dataRow["AccessUserUserName"] = row[cm.SourceColumn.Name];
var accessUserUserName = GetValue(cm, row);
if (!string.IsNullOrEmpty(accessUserUserName))
{
dataRow["AccessUserUserName"] = accessUserUserName;
}
}
}
}
Expand All @@ -555,10 +552,10 @@
//the user is being imported for the first time, there is a mapping, and the input is either NULL or ""
var pcm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserPassword");
bool isPasswordPresentInSource = pcm != null &&
!string.IsNullOrEmpty(row[pcm.SourceColumn.Name] as string);
!string.IsNullOrEmpty(GetValue(pcm, row));
if (isPasswordPresentInSource)
{
password = row[pcm.SourceColumn.Name] as string;
password = GetValue(pcm, row);
if (_encryptUserPasswords)
{
string encryptedPassword = encryptedPassword = Crypto.EncryptPassword(password, _userPasswordHashAlgorithm);
Expand Down Expand Up @@ -587,18 +584,20 @@
string name = null;
var cm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserName");
if (cm != null)
name = row[cm.SourceColumn.Name] as string;
name = GetValue(cm, row);
string userName = null;
cm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserUserName");
if (cm != null)
userName = row[cm.SourceColumn.Name] as string;
if (columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail") != null && Converter.ToBoolean(dataRow["AccessUserActive"]) == true)
userName = GetValue(cm, row);
var emailMapping = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail");
if (emailMapping != null && Converter.ToBoolean(dataRow["AccessUserActive"]) == true)
{
string email = row[columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail").SourceColumn.Name] as string;
string email = GetValue(emailMapping, row);
UserPassword userPasswordData = new UserPassword(userName, name, password, email);
if (columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserCountry") != null)
var countryMapping = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserCountry");
if (countryMapping != null)
{
userPasswordData.Country = row[columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserCountry").SourceColumn.Name] as string;
userPasswordData.Country = GetValue(countryMapping, row);
}
UsersPasswordsToSend.Add(userPasswordData);
}
Expand Down Expand Up @@ -629,25 +628,21 @@
var accessUserUserNameMapping = columnMappings.FirstOrDefault(m => m.Active && m.DestinationColumn.Name == "AccessUserUserName");
if (accessUserNameMapping == null && accessUserUserNameMapping != null)
{
groupName = ((string)row[accessUserUserNameMapping.SourceColumn.Name]).Trim();
groupName = GetValue(accessUserUserNameMapping, row).Trim();
dataRow["AccessUserUserName"] = groupName;
dataRow["AccessUserName"] = groupName;//AccessUserName field should be the same as AccessUserUserName
}
else if (accessUserUserNameMapping == null && accessUserNameMapping != null)
{
groupName = ((string)row[accessUserNameMapping.SourceColumn.Name]).Trim();
groupName = GetValue(accessUserNameMapping, row).Trim();
dataRow["AccessUserName"] = groupName;
dataRow["AccessUserUserName"] = groupName;//AccessUserUserName field should be the same as AccessUserName
}
else
{
groupName = accessUserNameMapping.IsKey ? row[accessUserNameMapping.SourceColumn.Name].ToString().Trim() :
row[accessUserUserNameMapping.SourceColumn.Name].ToString().Trim();
}

if (columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserActive") == null)
{
dataRow["AccessUserActive"] = true;
}
}
if (columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserType") == null)
{
//Handle GroupType: If it is not existing group - import with AccessType = 2
Expand Down Expand Up @@ -697,17 +692,11 @@
}
break;
case "SystemFieldValue":
if (columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, "SystemFieldValueSystemName", true) == 0) != null)
var fieldValueSystemName = GetValue(columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, "SystemFieldValueSystemName", true) == 0), row);
if (!string.IsNullOrEmpty(fieldValueSystemName) && !SystemFields.Any(sf => string.Compare(sf.SystemName, fieldValueSystemName, true) == 0))
{
if (row[columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, "SystemFieldValueSystemName", true) == 0).SourceColumn.Name] != DBNull.Value)
{
string fieldValueSystemName = Converter.ToString(row[columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, "SystemFieldValueSystemName", true) == 0).SourceColumn.Name]);
if (!string.IsNullOrEmpty(fieldValueSystemName) && !SystemFields.Any(sf => string.Compare(sf.SystemName, fieldValueSystemName, true) == 0))
{
_logger.Log(string.Format("Can't find the '{0}' in the user system fields. Skipped row: '{1}'.", fieldValueSystemName, BaseProvider.GetFailedSourceRowMessage(row)));
return;
}
}
_logger.Log(string.Format("Can't find the '{0}' in the user system fields. Skipped row: '{1}'.", fieldValueSystemName, BaseProvider.GetFailedSourceRowMessage(row)));
return;
}
dataRow["SystemFieldValueTableName"] = "AccessUser";
break;
Expand Down Expand Up @@ -898,8 +887,7 @@
}

sqlCommand.CommandText = sqlClean.ToString();
var rowsAffected = sqlCommand.ExecuteNonQuery();
RowsAffected += rowsAffected;
var rowsAffected = sqlCommand.ExecuteNonQuery();
if (rowsAffected > 0)
_logger.Log($"The number of deleted rows: {rowsAffected} for the destination {mapping.DestinationTable.Name} table mapping");
}
Expand Down Expand Up @@ -1048,11 +1036,12 @@
groupMapping.AddMapping(randomColumn,
_schemaTables.Find(t => t.Name == "AccessUser").Columns.Find(c => c.Name == "AccessUserActive"), false);
}
if (groupColumnMappings.Find(cm => cm.DestinationColumn.Name == "AccessUserType") == null)
var userTypeMapping = groupColumnMappings.Find(cm => cm.Active && cm.DestinationColumn.Name == "AccessUserType");
if (userTypeMapping is null)
{
groupMapping.AddMapping(randomColumn,
_schemaTables.Find(t => t.Name == "AccessUser").Columns.Find(c => c.Name == "AccessUserType"), false);
}
_schemaTables.Find(t => t.Name == "AccessUser").Columns.Find(c => c.Name == "AccessUserType"), groupColumnMappings.IsKeyColumnExists());
}
}
}
}
Expand Down Expand Up @@ -1174,8 +1163,7 @@
}

_sqlCommand.CommandText = sqlUpdateInsert;
var rowsAffected = _sqlCommand.ExecuteNonQuery();
RowsAffected += rowsAffected;
var rowsAffected = _sqlCommand.ExecuteNonQuery();
if (rowsAffected > 0)
_logger.Log($"The number of rows affected: {rowsAffected} in the {mapping.DestinationTable.Name} table");
}
Expand Down Expand Up @@ -1255,7 +1243,7 @@
_sqlCommand.CommandText = updateParentGroupIdSql.ToString();
try
{
RowsAffected += _sqlCommand.ExecuteNonQuery();
_sqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -1379,7 +1367,7 @@
// string.Format("@{0}@", string.Join("@@", userGroupsRelations[userColumnUserValuePair])), userColumnUserValuePair.Item1, userColumnUserValuePair.Item2);
try
{
RowsAffected += _sqlCommand.ExecuteNonQuery();
_sqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -1421,7 +1409,7 @@
// groupId, column, users.ToString().TrimStart(new char[] { ',' }));
try
{
RowsAffected += _sqlCommand.ExecuteNonQuery();
_sqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -1667,9 +1655,10 @@
{
DataRow ret = null;
var columnMappings = mapping.GetColumnMappings();
if (columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, searchColumn, true) == 0) != null)
var cm = columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, searchColumn, true) == 0);
if (cm != null)
{
string searchValue = Converter.ToString(row[columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, searchColumn, true) == 0).SourceColumn.Name]);
string searchValue = GetValue(cm, row);
ret = GetExistingUserBySearchColumn(searchColumn, searchValue);
}
return ret;
Expand Down Expand Up @@ -1857,7 +1846,7 @@
_sqlCommand.CommandText = updateUserIdSql.ToString();
try
{
RowsAffected += _sqlCommand.ExecuteNonQuery();
_sqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -1964,6 +1953,33 @@
}
}

private string GetValue(ColumnMapping? columnMapping, Dictionary<string, object> row)

Check warning on line 1956 in src/UserDestinationWriter.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
string? result = null;
if (columnMapping != null && (columnMapping.HasScriptWithValue || row.ContainsKey(columnMapping.SourceColumn.Name)))
{
switch (columnMapping.ScriptType)
{
case ScriptType.None:
result = Converter.ToString(row[columnMapping.SourceColumn.Name]);
break;
case ScriptType.Append:
result = Converter.ToString(row[columnMapping.SourceColumn.Name]) + columnMapping.ScriptValue;
break;
case ScriptType.Prepend:
result = columnMapping.ScriptValue + Converter.ToString(row[columnMapping.SourceColumn.Name]);
break;
case ScriptType.Constant:
result = columnMapping.GetScriptValue();
break;
case ScriptType.NewGuid:
result = columnMapping.GetScriptValue();
break;
}
}
return result;
}

//internal void CleanRelationsTables(SqlTransaction transaction)
//{
// _sqlCommand.Transaction = transaction;
Expand Down
8 changes: 4 additions & 4 deletions src/UserProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;
using Dynamicweb.Logging;
using Dynamicweb.Security.Permissions;
using Dynamicweb.Security.UserManagement;
using Dynamicweb.Security.UserManagement.Common.SystemFields;
using Microsoft.CodeAnalysis;
using System;
Expand Down Expand Up @@ -255,6 +253,7 @@
UserKeyField = "Auto";
DiscardDuplicates = false;
}

public override Schema GetOriginalSourceSchema()
{
List<string> tablestToKeep = new() { "AccessUser", "AccessUserAddress", "AccessUserSecondaryRelation" };
Expand Down Expand Up @@ -477,7 +476,7 @@
case "RepositoriesIndexUpdate":
if (node.HasChildNodes)
{
RepositoriesIndexUpdate = node.FirstChild.Value;

Check warning on line 479 in src/UserProvider.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'UserProvider.RepositoriesIndexUpdate' is obsolete: 'Use Job.RepositoriesIndexSettings'
}
break;
case "SkipFailingRows":
Expand Down Expand Up @@ -540,7 +539,7 @@
xmlTextWriter.WriteElementString("DeleteOnlyFromGroupsThatAreImportedTo", DeleteOnlyFromGroupsThatAreImportedTo.ToString(CultureInfo.CurrentCulture));
xmlTextWriter.WriteElementString("DiscardDuplicates", DiscardDuplicates.ToString(CultureInfo.CurrentCulture));
xmlTextWriter.WriteElementString("ImportUsersBelongExactlyImportGroups", ImportUsersBelongExactlyImportGroups.ToString(CultureInfo.CurrentCulture));
xmlTextWriter.WriteElementString("RepositoriesIndexUpdate", RepositoriesIndexUpdate);

Check warning on line 542 in src/UserProvider.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'UserProvider.RepositoriesIndexUpdate' is obsolete: 'Use Job.RepositoriesIndexSettings'
xmlTextWriter.WriteElementString("SkipFailingRows", SkipFailingRows.ToString(CultureInfo.CurrentCulture));
GetSchema().SaveAsXml(xmlTextWriter);
}
Expand Down Expand Up @@ -568,7 +567,7 @@
DeleteOnlyFromGroupsThatAreImportedTo = newProvider.DeleteOnlyFromGroupsThatAreImportedTo;
DiscardDuplicates = newProvider.DiscardDuplicates;
ImportUsersBelongExactlyImportGroups = newProvider.ImportUsersBelongExactlyImportGroups;
RepositoriesIndexUpdate = newProvider.RepositoriesIndexUpdate;

Check warning on line 570 in src/UserProvider.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'UserProvider.RepositoriesIndexUpdate' is obsolete: 'Use Job.RepositoriesIndexSettings'

Check warning on line 570 in src/UserProvider.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'UserProvider.RepositoriesIndexUpdate' is obsolete: 'Use Job.RepositoriesIndexSettings'
SkipFailingRows = newProvider.SkipFailingRows;
SqlConnectionString = newProvider.SqlConnectionString;
ManualConnectionString = newProvider.ManualConnectionString;
Expand Down Expand Up @@ -719,7 +718,7 @@
Writer.DeleteExcessFromMainTable(sqlTransaction);
sqlTransaction.Commit();
Writer.SendUserPasswords();
MoveRepositoriesIndexToJob(job);
MoveRepositoriesIndexToJob(job);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -747,7 +746,8 @@
}

if (sqlTransaction != null)
sqlTransaction.Rollback();
sqlTransaction.Rollback();

return false;
}
finally
Expand Down Expand Up @@ -871,15 +871,15 @@

private void MoveRepositoriesIndexToJob(Job job)
{
if (!string.IsNullOrEmpty(RepositoriesIndexUpdate))

Check warning on line 874 in src/UserProvider.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'UserProvider.RepositoriesIndexUpdate' is obsolete: 'Use Job.RepositoriesIndexSettings'
{
char[] separator = [','];
// if the provider already have RepositoriesIndexUpdate set, then we move them to the job, and set the add-in to string.empty
if (job.RepositoriesIndexSettings?.RepositoriesIndexes?.Count == 0)
{
job.RepositoriesIndexSettings = new RepositoriesIndexSettings(new Collection<string>([.. RepositoriesIndexUpdate.Split(separator, StringSplitOptions.RemoveEmptyEntries)]));

Check warning on line 880 in src/UserProvider.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'UserProvider.RepositoriesIndexUpdate' is obsolete: 'Use Job.RepositoriesIndexSettings'
}
RepositoriesIndexUpdate = string.Empty;

Check warning on line 882 in src/UserProvider.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'UserProvider.RepositoriesIndexUpdate' is obsolete: 'Use Job.RepositoriesIndexSettings'
job.Save();
}
}
Expand Down
Loading