Esempio n. 1
0
        public bool Insert(
            ContactUsDetailsDto contactUsDetailsDto,
            int? addedByUserId,
            ref IList<string> errorMessages)
        {
            if (!contactUsDetailsDto.IsValid)
            {
                errorMessages = contactUsDetailsDto.ErrorMessages.ToList();
                return false;
            }

            var contactUsLog = new ContactUsLog
            {
                Name = contactUsDetailsDto.Name,
                EmailAddress = contactUsDetailsDto.EmailAddress,
                Message = contactUsDetailsDto.Message,
                AddedByUserId = addedByUserId,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _contactUsLogRepository.Insert(contactUsLog);

            if (addedByUserId.HasValue)
            {
                var userLog = new UserLog
                {
                    UserId = addedByUserId.Value,
                    UserLogTypeId = (int)UserLogType.Types.SubmittedContactUsMessage,
                    AddedDateTime = DateTime.Now.ToGmtDateTime()
                };

                _userLogRepository.Insert(userLog);
            }

            IEmail email = new Email();
            foreach (var contactUsEmailAddress in email.ContactUsEmailAddresses)
            {
                email = new Email
                {
                    ToEmailAddress = contactUsEmailAddress,
                    ToRecipientName = (!string.IsNullOrWhiteSpace(contactUsDetailsDto.Name) ? contactUsDetailsDto.Name : "Unknown"),
                    Subject = "Contact Us - Fallen Nova",
                    EmailBody = contactUsDetailsDto.Message
                };

                if (email.Dispatch())
                {
                    continue;
                }

                errorMessages.Add(string.Format("The contact us message wasn't sent successfully. Please contact the website administrator."));
                return false;
            }

            UnitOfWork.Commit();

            return true;
        }
Esempio n. 2
0
        public void LoggedOut(
            int userId)
        {
            // Prepare the user log.
            var userLog = new UserLog
            {
                UserId = userId,
                UserLogTypeId = (int)UserLogType.Types.LoggedOut,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _userLogRepository.Insert(userLog);
            UnitOfWork.Commit();
        }
Esempio n. 3
0
        public void AuthenticatedSuccessfully(
            int userId)
        {
            var user = _userRepository.GetById(userId);

            user.LastSuccessfulAuthenticationDateTime = DateTime.Now.ToGmtDateTime();
            user.ModifiedDateTime = DateTime.Now.ToGmtDateTime();

            var userLog = new UserLog
            {
                UserId = userId,
                UserLogTypeId = (int)UserLogType.Types.AuthenticatedSuccessfullyAutomaticLogin,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _userRepository.Update(user);
            _userLogRepository.Insert(userLog);
            UnitOfWork.Commit();
        }
Esempio n. 4
0
        public void LoginSuccessful(
            int userId)
        {
            var user = _userRepository.GetById(userId);

            user.UnsuccessfulLoginAttempts = 0;
            user.LastSuccessfulLoginDateTime = DateTime.Now.ToGmtDateTime();
            user.ModifiedDateTime = DateTime.Now.ToGmtDateTime();

            var userLog = new UserLog
            {
                UserId = userId,
                UserLogTypeId = (int)UserLogType.Types.LoggedInSuccessfullyManualLogin,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _userRepository.Update(user);
            _userLogRepository.Insert(userLog);
            UnitOfWork.Commit();
        }
Esempio n. 5
0
        public bool UpdateSkillTree(
            SkillTreeDetailsDto skillTreeDetailsDto,
            int addedByUserId,
            ref IList<string> errorMessages)
        {
            var skillTree = new XDocument();

            #region Validation

            if (!skillTreeDetailsDto.IsValid)
            {
                errorMessages = skillTreeDetailsDto.ErrorMessages.ToList();
            }

            if (_roleRepository.GetByUserId(addedByUserId).All(r => r.RoleId != (int)Role.Roles.Administrator))
            {
                errorMessages.Add("Wait a sec, you're attempting to update the skill tree and you're not an administrator. What gives?");
            }

            // Parse the Xml to ensure it meets some basic requirements, i.e. enough skills and skill groups.
            if (errorMessages.Count == 0)
            {
                // Xml validation is handled in the models so don't need to be checked here.
                skillTree = XDocument.Parse(skillTreeDetailsDto.Xml);

                // The "groupName" attribute is unique to skill group elements.
                var skillGroupCount =
                    (
                        from row in skillTree.Descendants(ConstRow)
                        where row.Attribute(ConstGroupName) != null
                        select row.Attribute(ConstGroupName).Value
                    ).Distinct().Count();

                // The "typeName" attribute is unique to skill elements.
                var skillCount =
                    (
                        from row in skillTree.Descendants(ConstRow)
                        where row.Attribute(ConstTypeName) != null
                        select row.Attribute(ConstTypeName).Value
                    ).Distinct().Count();

                if ((skillGroupCount < ConstMinimumTotalSkillGroups) ||
                    (skillCount < ConstMinimumTotalSkills))
                {
                    errorMessages.Add("This skill tree doesn't look valid. Are you sure it's correct? It should be around 400KB in size and contain a lot of XML.");
                }
            }

            if (errorMessages.Count > 0)
            {
                return false;
            }

            #endregion

            #region Process Skill Tree

            // Take care of the skill groups first.
            var skillGroups =
                (
                    skillTree.Descendants(ConstRow)
                        .Where(row => row.Attribute(ConstGroupName) != null)
                        .Where(row => !row.Attribute(ConstGroupName).Value.ToLower().Contains(ConstFakeSkill))
                        .Select(row => new
                        {
                            Id = (int)row.Attribute(ConstGroupId),
                            Name = (string)row.Attribute(ConstGroupName)
                        })
                ).Distinct();

            foreach (var skillGroup in skillGroups)
            {
                var eveOnlineSkillGroup = _eveOnlineSkillGroupRepository.GetById(skillGroup.Id);

                if (eveOnlineSkillGroup == null)
                {
                    _eveOnlineSkillGroupRepository.Insert(
                        new EveOnlineSkillGroup
                        {
                            EveOnlineSkillGroupId = skillGroup.Id,
                            Name = skillGroup.Name,
                        });
                }
                else
                {
                    eveOnlineSkillGroup.Name = skillGroup.Name;

                    _eveOnlineSkillGroupRepository.Update(eveOnlineSkillGroup);
                }
            }

            // Address skills next.
            var attributes = _eveOnlineAttributeRepository.GetAll().ToDictionary(a => a.Name.ToLower(), a => a.EveOnlineAttributeId);

            var skills =
                skillTree.Descendants(ConstRow)
                    .Where(row => row.Attribute(ConstTypeName) != null)
                    .Where(row => !row.Attribute(ConstTypeName).Value.ToLower().Contains(ConstFakeSkill))
                    .Select(row => new
                    {
                        Id = (int)row.Attribute(ConstTypeId),
                        GroupId = (int)row.Attribute(ConstGroupId),
                        Name = (string)row.Attribute(ConstTypeName),
                        Description = (string)row.Element(ConstDescription),
                        Rank = (int)row.Element(ConstRank),
                        PrimaryEveOnlineAttributeId = attributes[(string)row.Element(ConstRequiredAttributes).Element(ConstPrimaryEveOnlineAttributeName)],
                        SecondaryEveOnlineAttributeId = attributes[(string)row.Element(ConstRequiredAttributes).Element(ConstSecondaryEveOnlineAttributeName)],
                        RequiredSkills = row.Element(ConstRowSet).Elements(ConstRow)
                            .Where(rs => rs.Attribute(ConstTypeId) != null)
                                   .Select(rs => new
                                   {
                                       Id = (int)rs.Attribute(ConstTypeId),
                                       Level = (int)rs.Attribute(ConstSkillLevel)
                                   })
                    });

            // NOTE: This is the most efficient approach otherwise it's a lot of lookups, updates, inserts, deletes etc.
            // Better to just delete everything given the foreign key relationship type and insert everything from scratch.
            _eveOnlineRequiredSkillRepository.DeleteAll();

            foreach (var skill in skills)
            {
                var eveOnlineSkill = _eveOnlineSkillRepository.GetById(skill.Id);

                if (eveOnlineSkill == null)
                {
                    _eveOnlineSkillRepository.Insert(
                        new EveOnlineSkill
                        {
                            EveOnlineSkillId = skill.Id,
                            EveOnlineSkillGroupId = skill.GroupId,
                            Name = skill.Name,
                            Description = skill.Description,
                            Rank = skill.Rank,
                            PrimaryEveOnlineAttributeId = skill.PrimaryEveOnlineAttributeId,
                            SecondaryEveOnlineAttributeId = skill.SecondaryEveOnlineAttributeId
                        });
                }
                else
                {
                    eveOnlineSkill.Name = skill.Name;
                    eveOnlineSkill.EveOnlineSkillGroupId = skill.GroupId;
                    eveOnlineSkill.Description = skill.Description;
                    eveOnlineSkill.Rank = skill.Rank;
                    eveOnlineSkill.PrimaryEveOnlineAttributeId = skill.PrimaryEveOnlineAttributeId;
                    eveOnlineSkill.SecondaryEveOnlineAttributeId = skill.SecondaryEveOnlineAttributeId;

                    _eveOnlineSkillRepository.Update(eveOnlineSkill);
                }

                foreach (var requiredSkill in skill.RequiredSkills)
                {
                    _eveOnlineRequiredSkillRepository.Insert(
                        new EveOnlineRequiredSkill
                        {
                            EveOnlineSkillId = skill.Id,
                            RequiredEveOnlineSkillId = requiredSkill.Id,
                            RequiredSkillLevel = requiredSkill.Level
                        });
                }
            }

            #endregion

            // Prepare the Eve Online skill tree entity for storage. The system holds onto it for historical purposes.
            var eveOnlineSkillTree = new EveOnlineSkillTree
            {
                Xml = skillTreeDetailsDto.Xml,
                AddedByUserId = addedByUserId,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            var userLog = new UserLog
            {
                UserId = addedByUserId,
                UserLogTypeId = (int)UserLogType.Types.UpdatedEveOnlineSkillTree,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _eveOnlineSkillTreeRepository.Insert(eveOnlineSkillTree);
            _userLogRepository.Insert(userLog);
            UnitOfWork.Commit();

            return true;
        }
Esempio n. 6
0
        public bool UpdateStatus(
            UserStatusDetailsDto userStatusDetailsDto,
            int modifiedByUserId,
            ref IList<string> errorMessages)
        {
            var user = _userRepository.GetById(userStatusDetailsDto.UserId);

            #region Validation

            if (_roleRepository.GetByUserId(modifiedByUserId).All(r => r.RoleId != (int)Role.Roles.Administrator))
            {
                errorMessages.Add("Wo wo wo, you're attempting to update another user's status details. You're not allowed to unless you're an administrator.");
            }

            if (errorMessages.Count > 0)
            {
                return false;
            }

            #endregion

            user.UserStatusId = userStatusDetailsDto.UserStatusId;
            user.ModifiedByUserId = modifiedByUserId;
            user.ModifiedDateTime = DateTime.Now.ToGmtDateTime();

            _userRepository.Update(user);

            var userLog = new UserLog
            {
                UserId = modifiedByUserId,
                UserLogTypeId = (int)UserLogType.Types.EditedUserStatus,
                ActionAgainstUserId = userStatusDetailsDto.UserId,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _userLogRepository.Insert(userLog);
            UnitOfWork.Commit();

            return true;
        }
Esempio n. 7
0
        public bool UpdatePassword(
            UserPasswordDto userPasswordDto,
            int modifiedByUserId,
            ref IList<string> errorMessages)
        {
            var user = _userRepository.GetById(userPasswordDto.UserId);

            #region Validation

            if (!userPasswordDto.IsValid)
            {
                errorMessages = userPasswordDto.ErrorMessages.ToList();
            }

            if (!VerifyPassword(userPasswordDto.CurrentPassword, user.HashedPassword))
            {
                errorMessages.Add("The current password supplied doesn't match what's in the database.");
            }

            if (userPasswordDto.UserId != modifiedByUserId)
            {
                errorMessages.Add("Wo wo wo, you're attempting to update another user's password. You can only edit your own password.");
            }

            if (errorMessages.Count > 0)
            {
                return false;
            }

            #endregion

            user.HashedPassword = HashPassword(userPasswordDto.NewPassword);
            user.ModifiedByUserId = modifiedByUserId;
            user.ModifiedDateTime = DateTime.Now.ToGmtDateTime();

            _userRepository.Update(user);

            var userLog = new UserLog
            {
                UserId = modifiedByUserId,
                UserLogTypeId = (int)UserLogType.Types.EditedUserPassword,
                ActionAgainstUserId = userPasswordDto.UserId,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _userLogRepository.Insert(userLog);
            UnitOfWork.Commit();

            return true;
        }
Esempio n. 8
0
        public bool UpdateContactDetails(
            UserContactDetailsDto userContactDetailsDto,
            int modifiedByUserId,
            ref IList<string> errorMessages)
        {
            var user = _userRepository.GetById(userContactDetailsDto.UserId);

            #region Validation

            if (!userContactDetailsDto.IsValid)
            {
                errorMessages = userContactDetailsDto.ErrorMessages.ToList();
            }

            if (user.UserStatusId != (int)UserStatus.Statuses.Active)
            {
                errorMessages.Add("You can't edit a user whose account is disabled or deleted.");
            }

            if ((userContactDetailsDto.UserId != modifiedByUserId) &&
                (_roleRepository.GetByUserId(modifiedByUserId).All(r => r.RoleId != (int)Role.Roles.Administrator)))
            {
                errorMessages.Add("Wait, you're attempting to update another user's contact details. You can only edit your details, unless you're an administrator.");
            }

            if (errorMessages.Count > 0)
            {
                return false;
            }

            #endregion

            user.FirstName = userContactDetailsDto.FirstName;
            user.Surname = userContactDetailsDto.Surname;
            user.EmailAddress = userContactDetailsDto.EmailAddress;
            user.ModifiedByUserId = modifiedByUserId;
            user.ModifiedDateTime = DateTime.Now.ToGmtDateTime();

            _userRepository.Update(user);

            var newUserRole = new UserRole
            {
                UserId = user.UserId,
                RoleId = userContactDetailsDto.RoleId
            };

            _userRoleRepository.DeleteByUserId(user.UserId);
            _userRoleRepository.Insert(newUserRole);

            var userLog = new UserLog
            {
                UserId = modifiedByUserId,
                UserLogTypeId = (int)UserLogType.Types.EditedUserContactDetails,
                ActionAgainstUserId = userContactDetailsDto.UserId,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _userLogRepository.Insert(userLog);
            UnitOfWork.Commit();

            return true;
        }
Esempio n. 9
0
        public bool Insert(
            UserContactDetailsDto userContactDetailsDto,
            int addedByUserId,
            ref IList<string> errorMessages)
        {
            #region Validation

            if (!userContactDetailsDto.IsValid)
            {
                errorMessages = userContactDetailsDto.ErrorMessages.ToList();
                return false;
            }

            if (_roleRepository.GetByUserId(addedByUserId).All(r => r.RoleId != (int)Role.Roles.Administrator))
            {
                errorMessages.Add("Stop! You can't add a new user unless you're an administrator.");
            }

            if (errorMessages.Count > 0)
            {
                return false;
            }

            #endregion

            // Prepare the user entity.
            var addedDateTime = DateTime.Now.ToGmtDateTime();
            var user = new User
            {
                UserStatusId = (int)UserStatus.Statuses.Active,
                FirstName = userContactDetailsDto.FirstName,
                Surname = userContactDetailsDto.Surname,
                EmailAddress = userContactDetailsDto.EmailAddress,
                HashedPassword = null,
                UnsuccessfulLoginAttempts = 0,
                AddedByUserId = addedByUserId,
                AddedDateTime = addedDateTime,
                ModifiedByUserId = addedByUserId,
                ModifiedDateTime = addedDateTime
            };

            _userRepository.Insert(user);

            var userRole = new UserRole
            {
                UserId = user.UserId,
                RoleId = userContactDetailsDto.RoleId
            };

            _userRoleRepository.Insert(userRole);

            var userLog = new UserLog
            {
                UserId = addedByUserId,
                UserLogTypeId = (int)UserLogType.Types.AddedUser,
                ActionAgainstUserId = userContactDetailsDto.UserId,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _userLogRepository.Insert(userLog);

            // Dispatch email.
            IEmail email = new Email
            {
                ToEmailAddress = userContactDetailsDto.EmailAddress,
                ToRecipientName = string.Format("{0} {1}", userContactDetailsDto.FirstName, userContactDetailsDto.Surname),
                Subject = "Verify Email Address - Fallen Nova",
                EmailBody = "Email body goes here"
            };

            if (!email.Dispatch())
            {
                errorMessages.Add(string.Format("The user's account was created however the verification email wasn't sent succesfully. Contact the web admin."));
                return false;
            }

            userContactDetailsDto.UserId = user.UserId;

            UnitOfWork.Commit();

            return true;
        }
Esempio n. 10
0
        public void LoginUnsuccessful(
            string emailAddress)
        {
            var user = _userRepository.GetByEmailAddress(emailAddress).Single();

            user.UnsuccessfulLoginAttempts++;
            user.ModifiedDateTime = DateTime.Now.ToGmtDateTime();

            var userLog = new UserLog
            {
                UserId = user.UserId,
                UserLogTypeId = (int)UserLogType.Types.LoggedInUnsuccessfullyManualLogin,
                AddedDateTime = DateTime.Now.ToGmtDateTime()
            };

            _userRepository.Update(user);
            _userLogRepository.Insert(userLog);
            UnitOfWork.Commit();
        }