public void Create(UserRoleModel viewModel, EducationSecurityPrincipal user)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException("viewModel");
            }
            if (viewModel.PostedRoles == null)
            {
                throw new ValidationException(new ValidationResult("You must select at least 1 role", new string[] { "PostedRoles" }), null, null);
            }
            var item = UserRepository.Items.
                       Include("UserRoles.Role").
                       Include("UserRoles.Schools").
                       Include("UserRoles.Providers").
                       SingleOrDefault(u => u.Id == viewModel.UserId);

            if (item == null)
            {
                throw new EntityNotFoundException("User does not exist.");
            }
            item.Active   = true;
            item.Comments = viewModel.Comments;
            AddRoles(item, viewModel.PostedRoles, viewModel, user);
            UserRepository.Update(item);
            UserAccessChangeEventRepository.Add(Auditor.CreateAccessChangeEvent(item, user.Identity.User));
            RepositoryContainer.Save();
        }
        public void UpdateActiveStatus(int id, bool activeStatus, EducationSecurityPrincipal user)
        {
            var item = UserRepository.Items.
                       Include("UserRoles.Role").
                       Include("UserRoles.Schools").
                       Include("UserRoles.Providers").
                       SingleOrDefault(u => u.Id == id);

            if (item == null)
            {
                throw new EntityNotFoundException("User does not exist");
            }
            item.Active = activeStatus;
            UserRepository.Update(item);
            UserAccessChangeEventRepository.Add(Auditor.CreateAccessChangeEvent(item, user.Identity.User));
            RepositoryContainer.Save();
        }
        public void UpdateActiveStatus(IEnumerable <int> ids, bool activeStatus, EducationSecurityPrincipal user)
        {
            var items = UserRepository.Items.
                        Include("UserRoles.Role").
                        Include("UserRoles.Schools").
                        Include("UserRoles.Providers").
                        Where(u => ids.Contains(u.Id)).ToList();

            if (items.Count == 0)
            {
                throw new EntityNotFoundException("Users do not exist");
            }
            foreach (var item in items)
            {
                item.Active = activeStatus;
                UserRepository.Update(item);
                UserAccessChangeEventRepository.Add(Auditor.CreateAccessChangeEvent(item, user.Identity.User));
            }
            RepositoryContainer.Save();
        }
        public void Edit(UserRoleModel viewModel, EducationSecurityPrincipal user)
        {
            if (viewModel.PostedRoles != null && viewModel.PostedRoles.Count() > 1)
            {
                throw new ValidationException(new ValidationResult("You can't have more than 1 role", new string[] { "PostedRoles" }), null, null);
            }
            var item = UserRepository.Items.
                       Include("UserRoles.Role").
                       Include("UserRoles.Schools").
                       Include("UserRoles.Providers").
                       SingleOrDefault(u => u.Id == viewModel.UserId);

            if (item == null)
            {
                throw new EntityNotFoundException("User does not exist.");
            }
            item.Comments = viewModel.Comments;
            if (viewModel.PostedRoles == null || !viewModel.PostedRoles.Any())
            {
                //in case someone decides to manually remove all roles associated with the user
                RemoveRoles(item, Enumerable.Empty <int>().ToList());
            }
            else
            {
                //remove any roles the user does not want anymore
                RemoveRoles(item, viewModel.PostedRoles);

                //check to see if any of the roles and schools associated need to be edited
                EditRoles(item, viewModel.PostedRoles, viewModel, user);

                //add roles that the user has selected. if it's a site coordinator we need to link the role to the proper schools
                AddRoles(item, viewModel.PostedRoles, viewModel, user);
            }
            UserRepository.Update(item);
            UserAccessChangeEventRepository.Add(Auditor.CreateAccessChangeEvent(item, user.Identity.User));
            RepositoryContainer.Save();
        }