Пример #1
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;
        }
Пример #2
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;
        }