This commit is contained in:
2018-09-25 23:09:35 +00:00
parent f50ba01d97
commit eb0af26c6e
4 changed files with 91 additions and 31 deletions

View File

@@ -283,6 +283,62 @@ namespace AyaNova.Util
}
public static void GenSeedUserViaBiz(int count, AuthorizationRoles roles, UserType userType, bool active = true, string login = null, string password = null)
{
AyContext ct = ServiceProviderProvider.DBContext;
for (int x = 0; x < count; x++)
{
User u = new User();
u.Active = active;
u.OwnerId = 1;
var p = new Bogus.Person();
u.Name = p.FullName;
u.Salt = Hasher.GenerateSalt();
if (login != null)
{
u.Login = login;
u.Name += " - " + login;
}
else
u.Login = p.FirstName;
if (password != null)
u.Password = Hasher.hash(u.Salt, password);
else
u.Password = Hasher.hash(u.Salt, u.Login);
u.Roles = roles;
u.LocaleId = ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID;
u.UserType = userType;
//TODO: After have USER and HEADOFFICE and VENDOR, if usertype is subcontractor or client or headoffice it needs to set a corresponding user's parent org record id to go with it
//Children and relations
u.UserOptions = new UserOptions(1);
u.UserOptions.EmailAddress = p.Email.Replace("gmail.com","helloayanova.com").Replace("hotmail.com","helloayanova.com").Replace("yahoo.com","helloayanova.com");
ct.User.Add(u);
}
//save to get id values
ct.SaveChanges();
//Now that we have the ID values bulk add the event log entries
//To save a db call iterate the local collection in the context, but...
//can't modify the context in the foreach, even if it's another collection entirely, so need to save the id's in a temporary list
List<long> ItemsAdded = new List<long>();
foreach (User o in ct.User.Local)
{
ItemsAdded.Add(o.Id);
}
//Now we have all the id's can actually add them to the context
foreach (long l in ItemsAdded)
{
EventLogProcessor.AddEntry(new Event(1, l, AyaType.User, AyaEvent.Created), ct);
}
//Now save the Event Log entries
ct.SaveChanges();
}
//////////////////////////////////////////////////////
//Seed widget for testing
//