public override void DeleteNews(int id)
 {
     var context = new MyLifeEntities();
     var obj = new tblNews {Id = id};
     context.DeleteObject(obj);
     context.SaveChanges();
 }
 public override void UpdateLink(Link link)
 {
     var context = new MyLifeEntities();
     var obj = new tblLinks {Id = link.Id};
     context.AttachTo("tblLinks", obj);
     BizObject<Link, int>.CopyToObject(link, obj);
     context.SaveChanges();
 }
 public override void DeleteLink(int id)
 {
     var context = new MyLifeEntities();
     var obj = new tblLinks {Id = id};
     context.AttachTo("tblLinks", obj);
     context.DeleteObject(obj);
     context.SaveChanges();
 }
 public override void UpdateNews(Web.News.News news)
 {
     var context = new MyLifeEntities();
     var obj = new tblNews {Id = news.Id};
     context.AttachTo("tblNews", obj);
     news.CopyToObject(obj);
     context.SaveChanges();
 }
 public override int InsertLink(Link link)
 {
     var context = new MyLifeEntities();
     var obj = new tblLinks();
     BizObject<Link, int>.CopyToObject(link, obj);
     context.AddTotblLinks(obj);
     context.SaveChanges();
     return obj.Id;
 }
 public override int InsertNews(Web.News.News news)
 {
     var context = new MyLifeEntities();
     var obj = new tblNews();
     news.CopyToObject(obj);
     context.AddTotblNews(obj);
     context.SaveChanges();
     return obj.Id;
 }
        public override void CreateRole(string roleName)
        {
            SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");
            var context = new MyLifeEntities();
            var obj = new tblRoles {Name = roleName};
            context.AddTotblRoles(obj);

            try
            {
                context.SaveChanges();
            }
            catch (DbException)
            {
                throw new ProviderException(string.Format("The role '{0}' already exists.", roleName));
            }
        }
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     var context = new MyLifeEntities();
     var obj = context.tblRoles.Where(item => item.Name == roleName).FirstOrDefault();
     if (obj != null)
     {
         if (throwOnPopulatedRole && obj.Users.Count() > 0)
         {
             throw new ProviderException("This role cannot be deleted because there are users present in it.");
         }
         context.DeleteObject(obj);
         context.SaveChanges();
         return true;
     }
     return false;
 }
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            var context = new MyLifeEntities();
            var users =
                context.tblUsers.Include("Roles").Where(Utils.BuildContainsExpression<tblUsers, string>(item => item.UserName, usernames))
                    .ToList();
            var roles =
                context.tblRoles.Where(Utils.BuildContainsExpression<tblRoles, string>(item => item.Name, roleNames)).
                    ToList();

            foreach (var user in users)
            {
                user.Roles.Load();
                foreach (var role in roles)
                {
                    if (!user.Roles.Contains(role))
                    {
                        user.Roles.Add(role);
                    }
                }
            }
            context.SaveChanges();
        }
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            SecUtility.CheckParameter(ref username, true, true, true, 255, "username");
            SecUtility.CheckParameter(ref oldPassword, true, true, false, 128, "oldPassword");
            SecUtility.CheckParameter(ref newPassword, true, true, false, 128, "newPassword");

            var context = new MyLifeEntities();
            var user = context.tblUsers.Where(item => item.UserName == username).FirstOrDefault();
            if (user == null)
            {
                return false;
            }

            var encodedOldPassword =
                EncodePassword(oldPassword.ToLowerInvariant(), user.PasswordFormat, user.PasswordSalt);

            if (!user.Password.Equals(encodedOldPassword))
            {
                return false;
            }

            if (newPassword.Length < MinRequiredPasswordLength)
            {
                throw new ArgumentException(
                    string.Format("The length of parameter '{0}' needs to be greater or equal to '{1}'.", "newPassword",
                                  MinRequiredPasswordLength));
            }

            var count = 0;
            for (var i = 0; i < newPassword.Length; i++)
            {
                if (!char.IsLetterOrDigit(newPassword, i))
                {
                    count++;
                }
            }

            if (count < MinRequiredNonAlphanumericCharacters)
            {
                throw new ArgumentException(
                    string.Format("Non alpha numeric characters in '{0}' needs to be greater than or equal to '{1}'.",
                                  "newPassword",
                                  MinRequiredNonAlphanumericCharacters.ToString(CultureInfo.InvariantCulture)));
            }

            if (PasswordStrengthRegularExpression.Length > 0)
            {
                if (!Regex.IsMatch(newPassword, PasswordStrengthRegularExpression))
                {
                    throw new ArgumentException(
                        string.Format(
                            "The parameter '{0}' does not match the regular expression specified in config file.",
                            "newPassword"));
                }
            }

            var encodedNewPassword =
                EncodePassword(newPassword.ToLowerInvariant(), user.PasswordFormat, user.PasswordSalt);
            if (encodedNewPassword.Length > 128)
            {
                throw new ArgumentException("The password is too long: it must not exceed 128 chars after encrypting.");
            }

            var e = new ValidatePasswordEventArgs(username, newPassword, false);
            OnValidatingPassword(e);

            if (e.Cancel)
            {
                if (e.FailureInformation != null)
                {
                    throw e.FailureInformation;
                }
                throw new ArgumentException("The custom password validation failed.");
            }

            user.Password = encodedNewPassword;
            user.LastPasswordChangedDate = DateTime.UtcNow;

            try
            {
                context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public override bool ValidateUser(string username, string password)
        {
            if (!SecUtility.ValidateParameter(ref username, true, true, true, 255))
                return false;

            if (!SecUtility.ValidateParameter(ref password, true, true, false, 128))
                return false;

            var context = new MyLifeEntities();
            var user = context.tblUsers.Where(item => item.UserName == username).FirstOrDefault();
            if (user == null)
            {
                return false;
            }

            if (!user.IsApproved)
                return false;

            if (user.IsLockedOut)
                return false;

            var encodedPassword = EncodePassword(password.ToLowerInvariant(), user.PasswordFormat, user.PasswordSalt);
            var isPasswordCorrect = user.Password.Equals(encodedPassword);
            var dt = DateTime.UtcNow;

            if (isPasswordCorrect)
                user.LastLoginDate = dt;

            context.SaveChanges();

            return isPasswordCorrect;
        }
 public override void UpdateUser(MembershipUser user)
 {
     var temp = user.Email;
     SecUtility.CheckParameter(ref temp, true, true, false, 255, "Email");
     user.Email = temp;
     var context = new MyLifeEntities();
     var id = (int) user.ProviderUserKey;
     var obj = context.tblUsers.Where(item => item.Id == id).FirstOrDefault();
     if (requiresUniqueEmail)
     {
         obj = context.tblUsers.Where(item => item.Email == user.Email).FirstOrDefault();
         if (obj != null && !obj.UserName.Equals(user.UserName, StringComparison.InvariantCultureIgnoreCase))
         {
             throw new ArgumentException(Messages.DuplicateEmail);
         }
     }
     obj.Email = user.Email.ToLowerInvariant();
     obj.IsApproved = user.IsApproved;
     obj.LastLoginDate = user.LastLoginDate.ToUniversalTime();
     context.SaveChanges();
 }
        public override bool UnlockUser(string username)
        {
            SecUtility.CheckParameter(ref username, true, true, true, 255, "username");
            var context = new MyLifeEntities();
            var user = context.tblUsers.Where(item => item.UserName == username).FirstOrDefault();
            user.IsLockedOut = false;

            try
            {
                context.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public override string ResetPassword(string username, string answer)
        {
            if (!EnablePasswordReset)
            {
                throw new NotSupportedException(
                    "This provider is not configured to allow password resets. To enable password reset, set enablePasswordReset to \"true\" in the configuration file.");
            }

            SecUtility.CheckParameter(ref username, true, true, true, 255, "username");
            var context = new MyLifeEntities();
            var user = context.tblUsers.Where(item => item.UserName == username).FirstOrDefault();
            if (user.IsLockedOut || !user.IsApproved)
                return null;

            var newPassword = GeneratePassword().ToLowerInvariant();
            var newEncodedPassword =
                EncodePassword(newPassword, user.PasswordFormat, user.PasswordSalt);
            var e = new ValidatePasswordEventArgs(username, newPassword, false);
            OnValidatingPassword(e);
            if (e.Cancel)
            {
                if (e.FailureInformation != null)
                {
                    throw e.FailureInformation;
                }
                throw new ProviderException("The custom password validation failed.");
            }

            user.Password = newEncodedPassword;
            user.LastPasswordChangedDate = DateTime.UtcNow;

            try
            {
                context.SaveChanges();
                return newPassword;
            }
            catch
            {
                return null;
            }
        }
 public override bool DeleteUser(string username, bool deleteAllRelatedData)
 {
     var context = new MyLifeEntities();
     var user = context.tblUsers.Where(item => item.UserName == username).FirstOrDefault();
     if (user != null)
     {
         context.DeleteObject(user);
         context.SaveChanges();
         return true;
     }
     return false;
 }
        public override MembershipUser CreateUser(string username, string password, string email,
            string passwordQuestion, string passwordAnswer, bool isApproved,
            object providerUserKey, out MembershipCreateStatus status)
        {
            if (!SecUtility.ValidateParameter(ref password, true, true, false, 128))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            var salt = GenerateSalt();
            var encodedPassword = EncodePassword(password.ToLowerInvariant(), (int) PasswordFormat, salt);
            if (encodedPassword.Length > 128)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            string encodedPasswordAnswer;

            if (!string.IsNullOrEmpty(passwordAnswer))
            {
                passwordAnswer = passwordAnswer.Trim();
                if (passwordAnswer.Length > 128)
                {
                    status = MembershipCreateStatus.InvalidAnswer;
                    return null;
                }
                encodedPasswordAnswer = EncodePassword(passwordAnswer.ToLowerInvariant(), (int) PasswordFormat, salt);
            }
            else
            {
                encodedPasswordAnswer = passwordAnswer;
            }

            if (!SecUtility.ValidateParameter(ref encodedPasswordAnswer, RequiresQuestionAndAnswer, true, false, 128))
            {
                status = MembershipCreateStatus.InvalidAnswer;
                return null;
            }

            if (!SecUtility.ValidateParameter(ref username, true, true, true, 255))
            {
                status = MembershipCreateStatus.InvalidUserName;
                return null;
            }

            if (!SecUtility.ValidateParameter(ref email, RequiresUniqueEmail, RequiresUniqueEmail, false, 255))
            {
                status = MembershipCreateStatus.InvalidEmail;
                return null;
            }

            if (!SecUtility.ValidateParameter(ref passwordQuestion, RequiresQuestionAndAnswer, true, false, 255))
            {
                status = MembershipCreateStatus.InvalidQuestion;
                return null;
            }

            if (password.Length < MinRequiredPasswordLength)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            var count = 0;

            for (var i = 0; i < password.Length; i++)
            {
                if (!char.IsLetterOrDigit(password, i))
                {
                    count++;
                }
            }

            if (count < MinRequiredNonAlphanumericCharacters)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            if (PasswordStrengthRegularExpression.Length > 0)
            {
                if (!Regex.IsMatch(password, PasswordStrengthRegularExpression))
                {
                    status = MembershipCreateStatus.InvalidPassword;
                    return null;
                }
            }

            var e = new ValidatePasswordEventArgs(username, password, true);
            OnValidatingPassword(e);

            if (e.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            var context = new MyLifeEntities();
            var user = context.tblUsers.Where(item => item.UserName == username).FirstOrDefault();
            if (user != null)
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return null;
            }

            if (RequiresUniqueEmail)
            {
                user = context.tblUsers.Where(item => item.Email == email).FirstOrDefault();
                if (user != null)
                {
                    status = MembershipCreateStatus.DuplicateEmail;
                    return null;
                }
            }

            user = new tblUsers
                       {
                           UserName = username.ToLowerInvariant(),
                           Password = encodedPassword,
                           PasswordFormat = ((int) PasswordFormat),
                           PasswordSalt = salt,
                           Email = email.ToLowerInvariant(),
                           IsApproved = isApproved,
                           IsLockedOut = false,
                           CreatedDate = DateTime.UtcNow,
                           LastLoginDate = Constants.DateTime.MinSqlDate,
                           LastPasswordChangedDate = Constants.DateTime.MinSqlDate,
                           LastLockoutDate = Constants.DateTime.MinSqlDate
                       };

            try
            {
                context.AddTotblUsers(user);
                context.SaveChanges();
                status = MembershipCreateStatus.Success;
                return Convert(user);
            }
            catch
            {
                status = MembershipCreateStatus.UserRejected;
            }
            return null;
        }
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            var username = (string) context["UserName"];
            var userIsAuthenticated = (bool) context["IsAuthenticated"];
            if (string.IsNullOrEmpty(username) || collection.Count < 1)
                return;

            var db = new MyLifeEntities();
            var profile = db.tblProfiles.Where(item => item.UserName == username).FirstOrDefault();
            if (profile == null)
            {
                profile = new tblProfiles {LastUpdatedDate = DateTime.UtcNow};
                db.AddTotblProfiles(profile);
                profile.UserName = username.ToLowerInvariant();
            }

            var names = new StringBuilder();
            var values = new StringBuilder();
            foreach (SettingsPropertyValue property in collection)
            {
                var name = property.Name;
                var value = property.PropertyValue;
                var allowAnonymous = (bool) property.Property.Attributes["AllowAnonymous"];
                if (!userIsAuthenticated && !allowAnonymous) continue;
                if (value == null) continue;
                names.Append(name).Append(";#");
                values.Append(Base64Serializer.Serialize(value)).Append(";#");
            }
            profile.PropertyNames = names.ToString();
            profile.PropertyValues = values.ToString();
            profile.LastUpdatedDate = DateTime.UtcNow;
            db.SaveChanges();
        }