However, I am still finding this to be an issue. I tracked down what I think is happening. Here is what I found and how I fixed it.
1. If you create an empty database and set your Settings.txt file config strings and run the application, all the tables are created with no installation data sans for a few exceptions.
2. RegisterDefaultPermissionsStartupTask.InstallPermissions is called which in turn calls StandardPermissionProvider.GetDefaultPermissions(). It is the later that creates 4 DefaultPermissionRecords which includes a respective CustomerRoleSystemName.
3. Here then is where the culprit is:
//default customer role mappings
var defaultPermissions = permissionProvider.GetDefaultPermissions();
foreach (var defaultPermission in defaultPermissions)
{
var customerRole = _customerService.GetCustomerRoleBySystemName(defaultPermission.CustomerRoleSystemName);
if (customerRole == null)
{
//new role (save it)
customerRole = new CustomerRole()
{
Name = defaultPermission.CustomerRoleSystemName,
Active = true,
SystemName = defaultPermission.CustomerRoleSystemName
};
_customerService.InsertCustomerRole(customerRole);
}
4. Finally if you view the db before running the install scripts, you will see the 4 customer roles already in the db.
What I did to fix:
1. Obviously we don't need to re-create these roles in InstallationService.InstallData.
2. Because these entries are already in the db BEFORE InstallData(); is called I passed in ICustomerService into the constructor of InstallationService.
3. From there I pulled necessary CustomerRoleSystemNames.
4. Here is the modified code from InstallData();
var adminRole = _customerService.GetCustomerRoleBySystemName("Administrators");
var registeredRole = _customerService.GetCustomerRoleBySystemName("Registered");
var guestRole = _customerService.GetCustomerRoleBySystemName("Guests");
//admin user
var adminUser = new Customer
{
CustomerGuid = Guid.NewGuid(),
Email = defaultUserEmail,
Password = defaultUserPassword,
PasswordFormat = PasswordFormat.Clear,
PasswordSalt = "",
IsActive = true,
CreatedOnUtc = DateTime.UtcNow,
LastActivityDateUtc = DateTime.UtcNow,
};
adminUser.CustomerRoles.Add(adminRole);
adminUser.CustomerRoles.Add(registeredRole);
_customerRepository.Insert(adminUser);
//search engine (crawler) built-in user
var searchEngineUser = new Customer()
{
Email = "builtin@search_engine_record.com",
CustomerGuid = Guid.NewGuid(),
PasswordFormat = PasswordFormat.Clear,
IsActive = true,
IsSystemAccount = true,
SystemName = SystemCustomerNames.SearchEngine,
CreatedOnUtc = DateTime.UtcNow,
LastActivityDateUtc = DateTime.UtcNow,
};
searchEngineUser.CustomerRoles.Add(guestRole);
_customerRepository.Insert(searchEngineUser);
That's it. No more duplicates. I may be missing something but I don't understand how NOT doing something like this will fix the problem. Unless I have a version where InstallPermissions is NOT called at startup. The fact remains these roles get installed BEFORE installation data. I think this is the right way. We just don't need them included in InstallData();
Hope this was helpful and I welcome any comments.
t