public void Register() { string[] defaultAddressTypes = new string[] { "Home", "Work" }; string[] defaultEmailTypes = new string[] { "Primary", "Secondary", "Work", "Spam" }; string[] defaultPhoneTypes = new string[] { "Mobile", "Home", "Work" }; foreach (string t in defaultAddressTypes) { AddressRelationType type = Db.SQL<AddressRelationType>("SELECT t FROM Simplified.Ring3.AddressRelationType t WHERE t.Name = ?", t).First; if (type != null) { continue; } Db.Transact(() => { type = new AddressRelationType(); type.Name = t; }); } foreach (string t in defaultEmailTypes) { EmailAddressRelationType type = Db.SQL<EmailAddressRelationType>("SELECT t FROM Simplified.Ring3.EmailAddressRelationType t WHERE t.Name = ?", t).First; if (type != null) { continue; } Db.Transact(() => { type = new EmailAddressRelationType(); type.Name = t; }); } foreach (string t in defaultPhoneTypes) { PhoneNumberRelationType type = Db.SQL<PhoneNumberRelationType>("SELECT t FROM Simplified.Ring3.PhoneNumberRelationType t WHERE t.Name = ?", t).First; if (type != null) { continue; } Db.Transact(() => { type = new PhoneNumberRelationType(); type.Name = t; }); } }
/// <summary> /// Registers new SystemUser and creates related Person, EmailAddress, EmailAddressRelation objects. /// NOTE: This method does no checks for username or email duplications, and does not commit changes! /// </summary> /// <param name="Username"></param> /// <param name="Email"></param> /// <param name="Password"></param> /// <returns>Newly created SystemUser instance</returns> public static SystemUser RegisterSystemUser(string Username, string Email, string Password) { string hash; string salt = Convert.ToBase64String(GenerateSalt(16)); Person person = new Person(); string relationTypeName = "Primary"; EmailAddressRelationType type = Db.SQL<EmailAddressRelationType>("SELECT t FROM Simplified.Ring3.EmailAddressRelationType t WHERE t.Name = ?", relationTypeName).First; GeneratePasswordHash(Username.ToLower(), Password, salt, out hash); if (type == null) { type = new EmailAddressRelationType() { Name = relationTypeName }; } EmailAddress email = new EmailAddress() { Name = Email }; EmailAddressRelation relation = new EmailAddressRelation() { ContactInfo = email, Somebody = person, ContactInfoRelationType = type }; SystemUser user = new SystemUser() { Username = Username, WhoIs = person, Password = hash, PasswordSalt = salt }; return user; }