示例#1
0
        public void Invoke(UserModel userModel, Guid guid)
        {
            var project = _projectRepository.GetByGuid(guid).WithoutStatuses(ProjectStatus.Deleted);

            if (project == null)
            {
                throw new ArgumentNullException(
                          $"Cannot add user to project with Guid:{guid}, because this project has been deleted.");
            }

            var newUser = _repository.GetAll().FirstOrDefault(x => x.Login == userModel.Login);

            if (newUser == null)
            {
                var user = new User
                {
                    PasswordSalt       = userModel.PasswordSalt,
                    ActivationToken    = userModel.ActivationToken,
                    Login              = userModel.Login,
                    Password           = userModel.Password,
                    FirstName          = userModel.FirstName,
                    LastName           = userModel.LastName,
                    ProjectUsers       = new List <ProjectUser>(),
                    LastPasswordChange = DateTime.UtcNow
                };

                user.ProjectUsers.Add(new ProjectUser {
                    ProjectId = project.Id
                });

                setDefaultUserCustomDataBasedOnProject.SetDefaultUserCustomData(project, user);

                _repository.Add(user);

                _eventBus.Publish(new UserAdded(user.FirstName, user.LastName, user.Login));
                return;
            }

            newUser.PasswordSalt       = userModel.PasswordSalt;
            newUser.FirstName          = userModel.FirstName;
            newUser.LastName           = userModel.LastName;
            newUser.Password           = userModel.Password;
            newUser.ActivationToken    = userModel.ActivationToken;
            newUser.Status             = UserStatus.Registered;
            newUser.LastPasswordChange = DateTime.UtcNow;
            newUser.ProjectUsers       = new List <ProjectUser> {
                new ProjectUser {
                    ProjectId = project.Id
                }
            };

            setDefaultUserCustomDataBasedOnProject.SetDefaultUserCustomData(project, newUser);

            _repository.Edit(newUser);
            _eventBus.Publish(new UserMerged(newUser.Login, newUser.FirstName, newUser.LastName));
        }
示例#2
0
        public void Invoke(int projectId, int userId)
        {
            var project = projectRepository.GetById(projectId).WithoutStatuses(ProjectStatus.Deleted);

            if (project == null)
            {
                throw new ArgumentNullException(
                          $"Cannot assign users to project with id:{projectId}, because this project non exist.");
            }

            if (project.ProjectUsers == null)
            {
                project.ProjectUsers = new List <ProjectUser>();
            }

            var user = userRepository.GetById(userId).WithoutStatuses(UserStatus.Deleted);

            if (user == null)
            {
                throw new ArgumentNullException(
                          $"Cannot assign user to project with id:{projectId}, because user non exist.");
            }

            project.ProjectUsers.Add(new ProjectUser {
                UserId = user.Id
            });

            setDefaultUserCustomDataBasedOnProject.SetDefaultUserCustomData(project, user);

            userRepository.Edit(user);
            projectRepository.Edit(project);
            eventBus.Publish(new UserAssigned(project.Id, project.Name, userId));
        }
        public void Invoke(List <UserModel> userModels, int projectId,
                           ref int usersAdded, ref int userdDuplicated, ref List <Tuple <string, string> > userAddedEmails)
        {
            var project = projectRepository.GetById(projectId).WithoutStatuses(ProjectStatus.Deleted);

            if (project == null)
            {
                throw new ArgumentNullException($"Cannot add user to project with id: {projectId}.");
            }

            var attribute = new EmailAddressAttribute();

            foreach (var userModel in userModels)
            {
                if (!attribute.IsValid(userModel.Login))
                {
                    continue;
                }

                var newUser = repository.GetAll().FirstOrDefault(x => x.Login == userModel.Login);

                if (newUser == null)
                {
                    var user = new User
                    {
                        PasswordSalt       = string.Empty,
                        ActivationToken    = Guid.NewGuid().ToString().Replace("-", string.Empty),
                        Login              = userModel.Login,
                        Password           = string.Empty,
                        FirstName          = userModel.FirstName,
                        LastName           = userModel.LastName,
                        Status             = UserStatus.AccountCreated,
                        ProjectUsers       = new List <ProjectUser>(),
                        LastPasswordChange = default(DateTime)
                    };

                    userAddedEmails.Add(new Tuple <string, string>(user.Login, user.ActivationToken));
                    user.ProjectUsers.Add(new ProjectUser {
                        ProjectId = project.Id
                    });

                    setDefaultUserCustomDataBasedOnProject.SetDefaultUserCustomData(project, user);

                    usersAdded++;

                    repository.Add(user);
                }
                else
                {
                    if (newUser.Status != UserStatus.Deleted)
                    {
                        userdDuplicated++;
                        eventBus.Publish(new DuplicateUserImported(newUser.FirstName, newUser.LastName, newUser.Login,
                                                                   projectId));
                        continue;
                    }

                    newUser.PasswordSalt       = string.Empty;
                    newUser.FirstName          = userModel.FirstName;
                    newUser.LastName           = userModel.LastName;
                    newUser.Password           = string.Empty;
                    newUser.ActivationToken    = Guid.NewGuid().ToString().Replace("-", string.Empty);
                    newUser.Status             = UserStatus.AccountCreated;
                    newUser.LastPasswordChange = default(DateTime);
                    newUser.ProjectUsers       = new List <ProjectUser> {
                        new ProjectUser {
                            ProjectId = project.Id
                        }
                    };

                    setDefaultUserCustomDataBasedOnProject.SetDefaultUserCustomData(project, newUser);

                    userAddedEmails.Add(new Tuple <string, string>(newUser.Login, newUser.ActivationToken));
                    usersAdded++;
                    repository.Edit(newUser);
                }
            }
            eventBus.Publish(new UsersImported(usersAdded, userdDuplicated, userModels.Count, projectId));
        }