Exemple #1
0
        public void TestOnChangeUserAccountStatus()
        {
            bool   success  = true;
            string userName = "******" + Guid.NewGuid();

            var mockAuditLog = new Mock <IAuditLog>(MockBehavior.Strict);

            mockAuditLog.Setup(al => al.OnChangeUserAccountStatus(success, userName, UserAccountStatusEnum_Enumeration.Active.ToString(), UserAccountStatusEnum_Enumeration.Disabled.ToString()));

            var eventTarget = new AuditLogUserAccountEventTarget(mockAuditLog.Object);

            var user = new UserAccount {
                Name = userName
            };

            user.AccountStatus_Enum = UserAccountStatusEnum_Enumeration.Active;
            user.Save();

            user.AccountStatus_Enum = UserAccountStatusEnum_Enumeration.Disabled;

            IDictionary <string, object> state = new Dictionary <string, object>();

            eventTarget.GatherAuditLogEntityDetailsForSave(user, state);
            eventTarget.WriteSaveAuditLogEntries(success, user.Id, state);

            mockAuditLog.VerifyAll();
        }
Exemple #2
0
        public void TestOnChangeUserAccountPassword()
        {
            bool   success  = true;
            string userName = "******" + Guid.NewGuid();

            var mockAuditLog = new Mock <IAuditLog>(MockBehavior.Strict);

            mockAuditLog.Setup(al => al.OnChangeUserAccountPassword(success, userName));

            var eventTarget = new AuditLogUserAccountEventTarget(mockAuditLog.Object);

            var user = new UserAccount {
                Name = userName
            };

            user.Password = "******";
            user.Save();

            user.Password = "******";

            IDictionary <string, object> state = new Dictionary <string, object>();

            eventTarget.GatherAuditLogEntityDetailsForSave(user, state);
            eventTarget.WriteSaveAuditLogEntries(success, user.Id, state);

            mockAuditLog.VerifyAll();
        }
Exemple #3
0
        public void TestOnChangeUserAccountExpiry()
        {
            bool     success           = true;
            string   userName          = "******" + Guid.NewGuid();
            DateTime oldExpirationDate = DateTime.UtcNow;
            DateTime newExpirationDate = DateTime.UtcNow.AddDays(1);

            var mockAuditLog = new Mock <IAuditLog>(MockBehavior.Strict);

            mockAuditLog.Setup(al => al.OnChangeUserAccountExpiry(success, userName, oldExpirationDate, newExpirationDate));

            var eventTarget = new AuditLogUserAccountEventTarget(mockAuditLog.Object);

            var user = new UserAccount {
                Name = userName
            };

            user.AccountExpiration = oldExpirationDate;
            user.Save();

            user.AccountExpiration = newExpirationDate;

            IDictionary <string, object> state = new Dictionary <string, object>();

            eventTarget.GatherAuditLogEntityDetailsForSave(user, state);
            eventTarget.WriteSaveAuditLogEntries(success, user.Id, state);

            mockAuditLog.VerifyAll();
        }
Exemple #4
0
        public void TestOnChangeUserRoleMembers()
        {
            bool   success   = true;
            string userName  = "******" + Guid.NewGuid();
            string roleName1 = "Role1" + Guid.NewGuid();
            string roleName2 = "Role2" + Guid.NewGuid();

            ISet <string> addedMembers = new SortedSet <string> {
                userName
            };

            var mockAuditLog = new Mock <IAuditLog>(MockBehavior.Strict);

            mockAuditLog.Setup(al => al.OnChangeUserRoleMembers(success, roleName1, It.Is <ISet <string> >(m => m.Count <= 0), It.Is <ISet <string> >(m => m.SetEquals(addedMembers))));

            mockAuditLog.Setup(al => al.OnChangeUserRoleMembers(success, roleName2, It.Is <ISet <string> >(m => m.SetEquals(addedMembers)), It.Is <ISet <string> >(m => m.Count <= 0)));

            var eventTarget = new AuditLogUserAccountEventTarget(mockAuditLog.Object);

            var role1 = new Role {
                Name = roleName1
            };
            var role2 = new Role {
                Name = roleName2
            };

            var user = new UserAccount {
                Name = userName
            };

            user.UserHasRole.Add(role1);
            user.Save();

            user.UserHasRole.Remove(role1);
            user.UserHasRole.Add(role2);

            IDictionary <string, object> state = new Dictionary <string, object>();

            eventTarget.GatherAuditLogEntityDetailsForSave(user, state);
            eventTarget.WriteSaveAuditLogEntries(success, user.Id, state);

            mockAuditLog.VerifyAll();
        }
        /// <summary>
        ///     Called before saving the enumeration of entities.
        /// </summary>
        /// <param name="entities">The entities.</param>
        /// <param name="state">The state.</param>
        /// <returns>
        ///     True to cancel the save operation; false otherwise.
        /// </returns>
        public bool OnBeforeSave(IEnumerable <IEntity> entities, IDictionary <string, object> state)
        {
            long passwordFieldId = Entity.GetId("core:password");

            IList <IEntity> enumerable = entities as IList <IEntity> ?? entities.ToList( );

            foreach (IEntity entity in enumerable)
            {
                var userAccount = entity.As <UserAccount>( );
                if (userAccount == null)
                {
                    continue;
                }

                EntityFieldCache.Instance.Get(0);

                _auditLogEventTarget.GatherAuditLogEntityDetailsForSave(userAccount, state);

                var writableCacheKey = new EntityFieldModificationCache.EntityFieldModificationCacheKey((( IEntityInternal )entity.Entity).ModificationToken);

                IEntityFieldValues cachedFieldValues;

                if (EntityFieldModificationCache.Instance.TryGetValue(writableCacheKey, out cachedFieldValues))
                {
                    object newPassword;

                    if (cachedFieldValues.TryGetValue(passwordFieldId, out newPassword))
                    {
                        string password = newPassword as string;

                        var userAccountInternal = userAccount as IEntityInternal;
                        var savedUserAccount    = Entity.Get <UserAccount>(userAccount.Id);

                        if (!userAccountInternal.IsTemporaryId &&
                            password == savedUserAccount.Password)
                        {
                            // Password is unchanged
                            continue;
                        }

                        // Validate the password against the password policy
                        PasswordPolicyHelper.ValidatePassword(PasswordPolicyHelper.GetDefaultPasswordPolicy(), password);

                        // Hash the password before saving
                        userAccount.Password = CryptoHelper.CreateEncodedSaltedHash(password);
                        // The password field was modified, so set the last password change date.
                        userAccount.PasswordLastChanged = DateTime.UtcNow;
                    }
                }

                if (HasUserAccountStatusChanged(userAccount))
                {
                    if (userAccount.AccountStatus_Enum == UserAccountStatusEnum_Enumeration.Active &&
                        userAccount.BadLogonCount > 0)
                    {
                        // Reset the bad logon account when the account is made active again.
                        userAccount.BadLogonCount = 0;
                    }
                }
            }

            return(false);
        }