public MembershipUser CreateUser(string login, string password, 
            string email, string firstName, string lastName, DateTime birthDate)
        {
            MembershipUser membershipUser = GetUser(login, false);

            if (membershipUser != null)
            {
                return null;
            }

            var user = new UserEntity
            {
                Login = login,
                Password = Crypto.HashPassword(password),

                //http://msdn.microsoft.com/ru-ru/library/system.web.helpers.crypto(v=vs.111).aspx
                //CreationDate = DateTime.Now
            };

            var profile = new ProfileEntity
            {
                FirstName = firstName,
                LastName = lastName,
                Email = email,
                BirthDate = birthDate
            };

            user.RoleId = RoleService.GetRoleId("User");
            
            UserService.CreateUser(user, profile);
            membershipUser = GetUser(login, false);
            return membershipUser;
        }
        public UserEntity ToBllUser(UserViewModel user)
        {
            
            var profile = profileService.GetProfileByUserId(user.Id);
            if(profile==null)
            {
                profileService.CreateProfile(profile = new ProfileEntity()
                {
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    UserId = user.Id
                });
            }
            else
            {
                profile.FirstName = user.FirstName;
                profile.LastName = user.LastName;
                profileService.UpdateProfile(profile);            
            }

            return new UserEntity()
            {
                Id = user.Id,
                Email = user.Email,
                RoleId =(int)user.Role
            };
        }
 public void CreateUser(UserEntity user, ProfileEntity profile)
 {
     DalUser du = user.ToDalUser();
     du.Profile = profile.ToDalProfile();
     DalProfile dp = profile.ToDalProfile();
     dp.User = user.ToDalUser();
     uow.Users.Create(du);
     //uow.Profiles.Create(dp);
     uow.Commit();
 }
        public UserViewModel ToMvcUser(UserEntity user)
        {
            var profile = profileService.GetProfileByUserId(user.Id);
            if (profile == null) profile = new ProfileEntity();
            return new UserViewModel()
            {
                Id = user.Id,
                Email = user.Email,
                FirstName = profile.FirstName,
                LastName = profile.LastName,
                Role = (Role)user.RoleId

            };
        }
 public void DeleteProfile(ProfileEntity profile)
 {
     profileRepository.Delete(profile.ToDalProfile());
     uow.Commit();
 }
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            // получаем логин пользователя
            var username = (string)context["UserName"];

            if (string.IsNullOrEmpty(username) || collection.Count < 1)
                return;
            var user = userService.GetAllEntities().FirstOrDefault(u => u.Email.Equals(username));
            if (user == null) return;
            var profile = user.Profile;
            // получаем id пользователя из таблицы Users по логину
            if (profile != null)
            {
                    foreach (SettingsPropertyValue val in collection)
                    {
                        profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue);
                    }
                    profile.LastUpdate = DateTime.Now;
                }
                else
                {
                    // если нет, то создаем новый профиль и добавляем его
                    profile = new ProfileEntity();
                    foreach (SettingsPropertyValue val in collection)
                    {
                        profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue);
                    }
                    profile.LastUpdate = DateTime.Now;
                    profile.Id = user.Id;
                    profileService.Create(profile);
                }
        }
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            // получаем логин пользователя
            var username = (string)context["UserName"];

            if (string.IsNullOrEmpty(username) || collection.Count < 1)
                return;
            // получаем id пользователя из таблицы Users по логину
            var firstOrDefault = userService.GetUserEntityByEmail(username);
            if (firstOrDefault != null)
            {
                int userId = firstOrDefault.Id;
                // по этому id извлекаем профиль из таблицы профилей
                ProfileEntity profile = profileService.GetProfileByUserId(userId);
                // если такой профиль уже есть изменяем его
                if (profile != null)
                {
                    foreach (SettingsPropertyValue val in collection)
                    {
                        profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue);
                    }

                    profileService.UpdateProfile(profile);
                }
                else
                {
                    // если нет, то создаем новый профиль и добавляем его
                    profile = new ProfileEntity();
                    foreach (SettingsPropertyValue val in collection)
                    {
                        profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue);
                    }

                    profileService.CreateProfile(profile);

                }
            }

        }