Esempio n. 1
0
        public ActionResult GrantModeratorRights(ToModerator toModerator)
        {
            var entry = new SectionModerator
            {
                UserId      = toModerator.UserId,
                SectionId   = toModerator.SectionId,
                DateGranted = DateTime.Now
            };
            User user = _repository.GetUserById(toModerator.UserId);

            user.RoleId = roleRepository.GetAllRoles().Single(x => x.Name == "Moderator").RoleId;
            _repository.UpdateUser(user);
            sectionModeratorsRepository.Add(entry);
            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public void UpdateUser(User user, bool updateNavigationProperties = false)
        {
            if (user == null)
            {
                throw new ArgumentNullException();
            }
            var userToUpdate = GetUserById(user.UserId);

            if (userToUpdate == null)
            {
                throw new ArgumentException
                          ($"User with Id = {user.UserId} does not exists");
            }

            userToUpdate.Image      = user.Image ?? userToUpdate.Image;
            userToUpdate.Reputation = user.Reputation ?? userToUpdate.Reputation;
            userToUpdate.Username   = user.Username ?? userToUpdate.Username;
            userToUpdate.E_mail     = user.E_mail ?? userToUpdate.E_mail;
            if (updateNavigationProperties)
            {
                userToUpdate.RoleId = user.RoleId;
                foreach (Topic topic in user.Topics)
                {
                    Topic topicOrm = topicRepository.GetById(topic.TopicId);
                    if (topicOrm == null)
                    {
                        topicRepository.Add(topic);
                    }
                    else
                    {
                        topicRepository.Update(topic);
                    }
                }
                foreach (Message message in user.Messages)
                {
                    Message messageOrm = messageRepository.GetById(message.MessageId);
                    if (messageOrm == null)
                    {
                        messageRepository.Add(message);
                    }
                    else
                    {
                        messageRepository.Update(message);
                    }
                }
                foreach (SectionModerator moderationInfoEntry in user.SectionModerators)
                {
                    SectionModerator moderationInfoEntryCurrent = sectionModeratorsRepository
                                                                  .GetByPrimaryKey(moderationInfoEntry.SectionId, moderationInfoEntry.UserId);
                    if (moderationInfoEntryCurrent == null)
                    {
                        sectionModeratorsRepository.Add(moderationInfoEntry);
                    }
                    else
                    {
                        sectionModeratorsRepository.Update(moderationInfoEntry);
                    }
                }
            }

            ctx.Entry(userToUpdate).State = EntityState.Modified;
            ctx.SaveChanges();
        }