Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, ContactEditViewModel editModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(editModel));
            }
            var contact = await _context.Contact.SingleOrDefaultAsync(m => m.ContactId == id);

            if (contact == null)
            {
                return(NotFound());
            }
            var isAuthorized = await _authorizationService.AuthorizeAsync(User, contact, ContactOperations.Update);

            if (!isAuthorized)
            {
                return(new ChallengeResult());
            }

            contact = ViewModel_to_model(contact, editModel);
            if (contact.Status == ContactStatus.Approved)
            {
                var canApprove = await _authorizationService.AuthorizeAsync(User, contact, ContactOperations.Approve);

                if (!canApprove)
                {
                    contact.Status = ContactStatus.Submitted;
                }
            }

            _context.Update(contact);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }