Esempio n. 1
0
        public override string[] GetRolesForUser(string userName)
        {
            if (String.IsNullOrEmpty(userName))
            {
                throw new ProviderException("User name cannot be empty or null.");
            }

            string[] roles;

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XPView xpvRoles = new XPView(uow, typeof(XpoRole),
                                                 new CriteriaOperatorCollection()
                    {
                        XpoRole.Fields.RoleName
                    },
                                                 new GroupOperator(
                                                     GroupOperatorType.And,
                                                     new BinaryOperator(XpoRole.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                     new ContainsOperator(XpoRole.Fields.Users, new BinaryOperator(XpoUser.Fields.UserName, userName, BinaryOperatorType.Equal))));
                    roles = Helper.GetRoleNameStrings(xpvRoles);
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "GetRolesForUser");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }

            return(roles);
        }
Esempio n. 2
0
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole = true)
        {
            if (!RoleExists(roleName))
            {
                throw new ProviderException("Role does not exist.");
            }

            if (throwOnPopulatedRole && GetUsersInRole(roleName).Length > 0)
            {
                throw new ProviderException("Cannot delete a populated role.");
            }

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XpoRole role = uow.FindObject <XpoRole>(new GroupOperator(
                                                                GroupOperatorType.And,
                                                                new BinaryOperator(XpoRole.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                new BinaryOperator(XpoRole.Fields.RoleName, roleName, BinaryOperatorType.Equal)));
                    uow.Delete(role);
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "DeleteRole");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }

            return(!RoleExists(roleName));
        }
        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection mclUsers = new MembershipUserCollection();

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XPCollection <XpoUser> xpcUsers = new XPCollection <XpoUser>(uow,
                                                                                 new BinaryOperator(XpoUser.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                                 new SortProperty(XpoUser.Fields.UserName, DevExpress.Xpo.DB.SortingDirection.Ascending));

                    totalRecords = xpcUsers.Count;
                    int startIndex = pageSize * pageIndex;
                    int endIndex   = startIndex + pageSize;
                    endIndex = totalRecords > endIndex ? endIndex : totalRecords;
                    for (int i = startIndex; i < endIndex; i++)
                    {
                        MembershipUser mUser = GetUserFromXpoUser(xpcUsers[i]);
                        mclUsers.Add(mUser);
                    }
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "GetAllUsers");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }

            return(mclUsers);
        }
        public override bool ChangePasswordQuestionAndAnswer(string userName, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            if (!ValidateUser(userName, password))
            {
                return(false);
            }

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XpoUser user = uow.FindObject <XpoUser>(new GroupOperator(
                                                                GroupOperatorType.And,
                                                                new BinaryOperator(XpoUser.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                new BinaryOperator(XpoUser.Fields.UserName, userName, BinaryOperatorType.Equal)));
                    if (user == null)
                    {
                        return(false);
                    }
                    user.PasswordQuestion = newPasswordQuestion;
                    user.PasswordAnswer   = EncodePassword(newPasswordAnswer);
                    uow.CommitChanges();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "ChangePasswordQuestionAndAnswer");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }
        }
        public override void UpdateUser(MembershipUser mUser)
        {
            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XpoUser xUser = uow.FindObject <XpoUser>(new GroupOperator(
                                                                 GroupOperatorType.And,
                                                                 new BinaryOperator(XpoUser.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                 new BinaryOperator(XpoUser.Fields.UserName, mUser.UserName, BinaryOperatorType.Equal)));

                    if (xUser == null)
                    {
                        throw new ProviderException("The specified user is not found.");
                    }

                    xUser.Email         = mUser.Email;
                    xUser.Comment       = mUser.Comment;
                    xUser.IsApproved    = mUser.IsApproved;
                    xUser.LastLoginDate = mUser.LastLoginDate;
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "UpdateUser");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }
        }
Esempio n. 6
0
        public override bool RoleExists(string roleName)
        {
            if (String.IsNullOrEmpty(roleName))
            {
                throw new ProviderException("Role name cannot be empty or null.");
            }

            int numRole;

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    numRole = (int)uow.Evaluate <XpoRole>(
                        CriteriaOperator.Parse("Count()"), new GroupOperator(
                            GroupOperatorType.And,
                            new BinaryOperator(XpoRole.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                            new BinaryOperator(XpoRole.Fields.RoleName, roleName, BinaryOperatorType.Equal)));
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "RoleExists");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }

            return(numRole > 0 ? true : false);
        }
        public override bool UnlockUser(string userName)
        {
            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XpoUser user = uow.FindObject <XpoUser>(new GroupOperator(
                                                                GroupOperatorType.And,
                                                                new BinaryOperator(XpoUser.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                new BinaryOperator(XpoUser.Fields.UserName, userName, BinaryOperatorType.Equal)));

                    if (user == null)
                    {
                        return(false);
                    }

                    user.IsLockedOut = false;
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "UnlockUser");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }

            return(true);
        }
Esempio n. 8
0
        public override void AddUsersToRoles(string[] userNames, string[] roleNames)
        {
            foreach (string roleName in roleNames)
            {
                if (String.IsNullOrEmpty(roleName))
                {
                    throw new ProviderException("Role name cannot be empty or null.");
                }
                if (roleName.Contains(","))
                {
                    throw new ArgumentException("Role name cannot contain commas.");
                }
                if (!RoleExists(roleName))
                {
                    throw new ProviderException("Role name not found.");
                }
            }

            foreach (string userName in userNames)
            {
                if (String.IsNullOrEmpty(userName))
                {
                    throw new ProviderException("User name cannot be empty or null.");
                }
                if (userName.Contains(","))
                {
                    throw new ArgumentException("User names cannot contain commas.");
                }

                //foreach (string rolename in roleNames)
                //{
                //    if (IsUserInRole(userName, rolename))
                //        throw new ProviderException("User is already in role.");
                //}
            }

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XPCollection <XpoUser> xpcUsers = new XPCollection <XpoUser>(uow, new GroupOperator(
                                                                                     GroupOperatorType.And,
                                                                                     new BinaryOperator(XpoUser.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                                     new InOperator(XpoUser.Fields.UserName.PropertyName, userNames)));
                    XPCollection <XpoRole> xpcRoles = new XPCollection <XpoRole>(uow, new GroupOperator(
                                                                                     GroupOperatorType.And,
                                                                                     new BinaryOperator(XpoRole.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                                     new InOperator(XpoRole.Fields.RoleName.PropertyName, roleNames)));
                    foreach (XpoUser user in xpcUsers)
                    {
                        user.Roles.AddRange(xpcRoles);
                    }
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "AddUsersToRoles");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }
        }
Esempio n. 9
0
        public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
        {
            foreach (string rolename in roleNames)
            {
                if (String.IsNullOrEmpty(rolename))
                {
                    throw new ProviderException("Role name cannot be empty or null.");
                }
                if (!RoleExists(rolename))
                {
                    throw new ProviderException("Role name not found.");
                }
            }

            foreach (string username in userNames)
            {
                if (String.IsNullOrEmpty(username))
                {
                    throw new ProviderException("User name cannot be empty or null.");
                }

                foreach (string rolename in roleNames)
                {
                    if (!IsUserInRole(username, rolename))
                    {
                        throw new ProviderException("User is not in role.");
                    }
                }
            }

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XPCollection <XpoRole> xpcRoles = new XPCollection <XpoRole>(uow, new GroupOperator(
                                                                                     GroupOperatorType.And,
                                                                                     new BinaryOperator(XpoRole.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                                     new InOperator(XpoRole.Fields.RoleName.PropertyName, roleNames)));

                    XPCollection <XpoUser> xpcUsers;
                    foreach (XpoRole role in xpcRoles)
                    {
                        xpcUsers = new XPCollection <XpoUser>(uow, new GroupOperator(
                                                                  GroupOperatorType.And,
                                                                  new BinaryOperator(XpoUser.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                  new InOperator(XpoUser.Fields.UserName.PropertyName, userNames),
                                                                  new ContainsOperator(XpoUser.Fields.Roles, new BinaryOperator(XpoRole.Fields.RoleName, role.RoleName, BinaryOperatorType.Equal))));
                        for (int i = xpcUsers.Count - 1; i >= 0; i--)
                        {
                            role.Users.Remove(xpcUsers[i]);
                        }
                    }
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "RemoveUsersFromRoles");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }
        }
        public override string ResetPassword(string userName, string answer)
        {
            if (!EnablePasswordReset)
            {
                throw new NotSupportedException("Password Reset is not enabled.");
            }

            if ((answer == null) && (RequiresQuestionAndAnswer))
            {
                UpdateFailureCount(userName, FailureType.PasswordAnswer);
                throw new ProviderException("Password answer required for password Reset.");
            }

            int    minPasswordLenth = MinRequiredPasswordLength > 8 ? MinRequiredPasswordLength : 8;
            string newPassword      = Membership.GeneratePassword(minPasswordLenth,
                                                                  MinRequiredNonAlphanumericCharacters);

            ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(userName, newPassword, true);

            OnValidatingPassword(args);

            if (args.Cancel)
            {
                if (args.FailureInformation != null)
                {
                    throw args.FailureInformation;
                }
                else
                {
                    throw new MembershipPasswordException("Reset password canceled due to password answer validation failure.");
                }
            }

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XpoUser user = uow.FindObject <XpoUser>(new GroupOperator(
                                                                GroupOperatorType.And,
                                                                new BinaryOperator(XpoUser.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                new BinaryOperator(XpoUser.Fields.UserName, userName, BinaryOperatorType.Equal)));

                    if (user == null)
                    {
                        throw new MembershipPasswordException("The specified user is not found.");
                    }
                    if (user.IsLockedOut)
                    {
                        throw new MembershipPasswordException("The specified user is locked out.");
                    }

                    if (RequiresQuestionAndAnswer && (!CheckPassword(answer, user.PasswordAnswer)))
                    {
                        UpdateFailureCount(userName, FailureType.PasswordAnswer);

                        throw new MembershipPasswordException("Incorrect password answer.");
                    }

                    user.Password = EncodePassword(newPassword);
                    user.LastPasswordChangedDate = DateTime.Now;
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "ResetPassword");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }

            return(newPassword);
        }
        public override string GetPassword(string userName, string answer)
        {
            if (!EnablePasswordRetrieval)
            {
                throw new ProviderException("Password Retrieval Not Enabled.");
            }

            if (PasswordFormat == MembershipPasswordFormat.Hashed)
            {
                throw new ProviderException("Cannot retrieve Hashed passwords.");
            }

            string password;
            string passwordAnswer;

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XpoUser user = uow.FindObject <XpoUser>(new GroupOperator(
                                                                GroupOperatorType.And,
                                                                new BinaryOperator(XpoUser.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                new BinaryOperator(XpoUser.Fields.UserName, userName, BinaryOperatorType.Equal)));

                    if (user == null)
                    {
                        throw new MembershipPasswordException("The specified user is not found.");
                    }
                    if (user.IsLockedOut)
                    {
                        throw new MembershipPasswordException("The specified user is locked out.");
                    }
                    password       = user.Password;
                    passwordAnswer = user.PasswordAnswer;
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "GetPassword");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }

            if (RequiresQuestionAndAnswer && !CheckPassword(answer, passwordAnswer))
            {
                UpdateFailureCount(userName, FailureType.PasswordAnswer);

                throw new MembershipPasswordException("Incorrect password answer.");
            }

            if (PasswordFormat == MembershipPasswordFormat.Encrypted)
            {
                password = DecodePassword(password);
            }

            return(password);
        }
        public override MembershipUser CreateUser(string userName, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(userName, password, true);

            OnValidatingPassword(args);

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

            if (requiresQuestionAndAnswer && String.IsNullOrEmpty(passwordAnswer))
            {
                status = MembershipCreateStatus.InvalidAnswer;
                return(null);
            }

            if (RequiresUniqueEmail)
            {
                if (!IsEmail(email))
                {
                    status = MembershipCreateStatus.InvalidEmail;
                    return(null);
                }
                if (!String.IsNullOrEmpty(GetUserNameByEmail(email)))
                {
                    status = MembershipCreateStatus.DuplicateEmail;
                    return(null);
                }
            }

            MembershipUser mUser = GetUser(userName, false);

            if (mUser != null)
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return(null);
            }

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    DateTime creationDate = DateTime.Now;
                    DateTime minDate      = DateTime.MinValue;

                    XpoUser xUser = new XpoUser(uow)
                    {
                        ApplicationName                        = this.ApplicationName,
                        UserName                               = userName,
                        Password                               = EncodePassword(password),
                        Email                                  = String.IsNullOrEmpty(email) ? String.Empty : email,
                        PasswordQuestion                       = passwordQuestion,
                        PasswordAnswer                         = EncodePassword(passwordAnswer),
                        IsApproved                             = isApproved,
                        CreationDate                           = creationDate,
                        LastPasswordChangedDate                = creationDate,
                        LastActivityDate                       = creationDate,
                        IsLockedOut                            = false,
                        LastLockedOutDate                      = minDate,
                        LastLoginDate                          = creationDate,
                        FailedPasswordAnswerAttemptCount       = 0,
                        FailedPasswordAnswerAttemptWindowStart = minDate,
                        FailedPasswordAttemptCount             = 0,
                        FailedPasswordAttemptWindowStart       = minDate,
                    };
                    uow.CommitChanges();
                    status = MembershipCreateStatus.Success;
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "CreateUser");
                }

                status = MembershipCreateStatus.ProviderError;
            }

            return(GetUser(userName, false));
        }
        private void UpdateFailureCount(string userName, FailureType failureType)
        {
            DateTime windowStart;
            DateTime windowEnd;
            int      failureCount;

            try
            {
                using (XtraUnitOfWork uow = new XtraUnitOfWork(DataLayer))
                {
                    XpoUser user = uow.FindObject <XpoUser>(new GroupOperator(
                                                                GroupOperatorType.And,
                                                                new BinaryOperator(XpoUser.Fields.ApplicationName, ApplicationName, BinaryOperatorType.Equal),
                                                                new BinaryOperator(XpoUser.Fields.UserName, userName, BinaryOperatorType.Equal)));

                    switch (failureType)
                    {
                    case FailureType.Password:
                        failureCount = user.FailedPasswordAttemptCount;
                        windowStart  = user.FailedPasswordAttemptWindowStart;
                        windowEnd    = windowStart.AddMinutes(PasswordAttemptWindow);

                        user.FailedPasswordAttemptWindowStart = DateTime.Now;

                        if (DateTime.Now > windowEnd)
                        {
                            user.FailedPasswordAttemptCount = 1;
                        }
                        else
                        {
                            user.FailedPasswordAttemptCount++;
                        }

                        if (user.FailedPasswordAttemptCount >= MaxInvalidPasswordAttempts)
                        {
                            if (!user.IsLockedOut)
                            {
                                user.LastLockedOutDate = DateTime.Now;
                                user.IsLockedOut       = true;
                            }
                        }
                        break;

                    case FailureType.PasswordAnswer:
                        failureCount = user.FailedPasswordAnswerAttemptCount;
                        windowStart  = user.FailedPasswordAnswerAttemptWindowStart;
                        windowEnd    = windowStart.AddMinutes(PasswordAttemptWindow);

                        user.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;

                        if (DateTime.Now > windowEnd)
                        {
                            user.FailedPasswordAnswerAttemptCount = 1;
                        }
                        else
                        {
                            user.FailedPasswordAnswerAttemptCount++;
                        }

                        if (user.FailedPasswordAnswerAttemptCount >= MaxInvalidPasswordAttempts)
                        {
                            if (!user.IsLockedOut)
                            {
                                user.LastLockedOutDate = DateTime.Now;
                                user.IsLockedOut       = true;
                            }
                        }
                        break;
                    }
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(ex, "UpdateFailureCount");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw ex;
                }
            }
        }