Exemplo n.º 1
0
        public async Task <PartialViewResult> EditUserProfile()
        {
            var identity = User.Identity as ClaimsIdentity; // Get current user

            var response = new HttpResponseMessage();

            var UserEditProfileViewModel = new UserEditProfileViewModel();

            if (identity.HasClaim("UserType", "Agent")) // Current user is Agent
            {
                response = await this.GetHttpClient().GetAsync(String.Format("Agent?userName={0}", this.User.Identity.Name));

                if (response.IsSuccessStatusCode)
                {
                    UserEditProfileViewModel = Mapper.Map <Agent, UserEditProfileViewModel>(await response.Content.ReadAsAsync <Agent>());


                    if (UserEditProfileViewModel == null)
                    {
                        return(PartialView("_EditUserProfile"));
                    }
                }
            }
            else if (identity.HasClaim("UserType", "Client")) // Current user is Client
            {
                response = await this.GetHttpClient().GetAsync(String.Format("Client?userName={0}", this.User.Identity.Name));

                if (response.IsSuccessStatusCode)
                {
                    UserEditProfileViewModel = Mapper.Map <Client, UserEditProfileViewModel>(await response.Content.ReadAsAsync <Client>());


                    if (UserEditProfileViewModel == null)
                    {
                        return(PartialView("_EditUserProfile"));
                    }
                }
            }
            else if (identity.HasClaim("UserType", "Admin")) // Current user is Admin
            {
                response = await this.GetHttpClient().GetAsync(String.Format("Admin?userName={0}", this.User.Identity.Name));

                if (response.IsSuccessStatusCode)
                {
                    UserEditProfileViewModel = Mapper.Map <Admin, UserEditProfileViewModel>(await response.Content.ReadAsAsync <Admin>());


                    if (UserEditProfileViewModel == null)
                    {
                        return(PartialView("_EditUserProfile"));
                    }
                }
            }

            return(PartialView("_EditUserProfile", UserEditProfileViewModel));
        }
        public ActionResult EditProfile()
        {
            var user = this.Data.Users.Find(this.User.Identity.GetUserId());

            var viewModel = new UserEditProfileViewModel()
            {
                Nickname = user.Nickname
            };

            return View(viewModel);
        }
Exemplo n.º 3
0
        public ActionResult EditProfile()
        {
            var user = this.Data.Users.Find(this.User.Identity.GetUserId());

            var viewModel = new UserEditProfileViewModel()
            {
                Nickname = user.Nickname
            };

            return(View(viewModel));
        }
Exemplo n.º 4
0
        public async Task <PartialViewResult> EditUserProfile(UserEditProfileViewModel model)
        {
            var response = new HttpResponseMessage();

            if (!String.IsNullOrEmpty(model.Email))
            {
                if (model.CurrentEmail != model.Email)
                {
                    // If email is taken, add model error with following error message "The Email is unavalilable."
                    if (this.GetUserManager.FindByEmail(model.Email) != null)
                    {
                        ModelState.AddModelError("Email", "The Email is unavalilable.");
                    }
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    // Attempt to update an email and a user name
                    var user = await this.GetUserManager.FindByNameAsync(model.UserName);

                    user.Email = model.Email;

                    var result = await this.GetUserManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        // If user name and email successfully updated, attempt to update FullName user claim
                        result = await this.GetUserManager.RemoveClaimAsync(user.Id, new Claim("FullName", model.CurrentFullName));

                        if (!result.Succeeded)
                        {
                            var message = String.Format("FullName claim could not be removed from user {0}.", model.UserName);

                            if (!Utilities.IsEmpty(result.Errors))
                            {
                                foreach (var error in result.Errors)
                                {
                                    message += " " + error;
                                }
                            }

                            throw new ClaimsAssignmentException(message);
                        }

                        result = await this.GetUserManager.AddClaimAsync(user.Id, new Claim("FullName", model.FirstName + " " + model.LastName));

                        if (!result.Succeeded)
                        {
                            var message = "FullName claim could not be assigned to user " + user.UserName + ".";

                            if (!Utilities.IsEmpty(result.Errors))
                            {
                                foreach (var error in result.Errors)
                                {
                                    message += " " + error;
                                }
                            }

                            throw new ClaimsAssignmentException(message);
                        }

                        if (model.UserType == UserType.Agent)
                        {
                            // If FullName user claim successfully updated, attempt to update employee profile
                            var agent = Mapper.Map <UserEditProfileViewModel, Agent>(model);

                            response = await this.GetHttpClient().PutAsJsonAsync("Agent", agent);

                            if (response.IsSuccessStatusCode)
                            {
                                agent = await response.Content.ReadAsAsync <Agent>();

                                if (agent != null)
                                {
                                    var editUserConfirmationViewModel = Mapper.Map <Agent, EditUserConfirmationViewModel>(agent);

                                    return(PartialView("_EditUserConfirmation", editUserConfirmationViewModel));
                                }
                                else
                                {
                                    throw new EmployeeUpdateException("Null is returned after updating an employee. Status Code: " + response.StatusCode);
                                }
                            }
                            else
                            {
                                throw new EmployeeUpdateException("Employee profile could not be updated. Status Code: " + response.StatusCode);
                            }
                        }
                        else if (model.UserType == UserType.Client)
                        {
                            // If FullName user claim successfully updated, attempt to update employee profile
                            var client = Mapper.Map <UserEditProfileViewModel, Client>(model);

                            response = await this.GetHttpClient().PutAsJsonAsync("Client", client);

                            if (response.IsSuccessStatusCode)
                            {
                                client = await response.Content.ReadAsAsync <Client>();

                                if (client != null)
                                {
                                    var editUserConfirmationViewModel = Mapper.Map <Client, EditUserConfirmationViewModel>(client);

                                    return(PartialView("_EditUserConfirmation", editUserConfirmationViewModel));
                                }
                                else
                                {
                                    throw new EmployeeUpdateException("Null is returned after updating an employee. Status Code: " + response.StatusCode);
                                }
                            }
                            else
                            {
                                throw new EmployeeUpdateException("Employee profile could not be updated. Status Code: " + response.StatusCode);
                            }
                        }
                        else if (model.UserType == UserType.Admin)
                        {
                            // If FullName user claim successfully updated, attempt to update employee profile
                            var admin = Mapper.Map <UserEditProfileViewModel, Admin>(model);

                            response = await this.GetHttpClient().PutAsJsonAsync("Admin", admin);

                            if (response.IsSuccessStatusCode)
                            {
                                admin = await response.Content.ReadAsAsync <Admin>();

                                if (admin != null)
                                {
                                    var editUserConfirmationViewModel = Mapper.Map <Admin, EditUserConfirmationViewModel>(admin);

                                    return(PartialView("_EditUserConfirmation", editUserConfirmationViewModel));
                                }
                                else
                                {
                                    throw new EmployeeUpdateException("Null is returned after updating an employee. Status Code: " + response.StatusCode);
                                }
                            }
                            else
                            {
                                throw new EmployeeUpdateException("Employee profile could not be updated. Status Code: " + response.StatusCode);
                            }
                        }
                    }
                    else
                    {
                        var message = "User could not be updated.";

                        if (!Utilities.IsEmpty(result.Errors))
                        {
                            foreach (var error in result.Errors)
                            {
                                message += " " + error;
                            }
                        }

                        throw new UserUpdateException(message);
                    }
                }
                catch (UserUpdateException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    var stringBuilder = new StringBuilder();

                    stringBuilder.Append("<div class='text-center'><h4><strong>User could NOT be updated.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been caught while attempting to update a user profile. Please review an exception log for more details about the exception.</div></div>");

                    var userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_Error", userConfirmationViewModel));
                }
                catch (ClaimsAssignmentException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    var stringBuilder = new StringBuilder();

                    stringBuilder.Append("<div class='text-center'><h4><strong>Claim could NOT be assigned to the user.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been caught while attempting to assign a user claim. Please review an exception log for more details about the exception.</div></div>");

                    var userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_Error", userConfirmationViewModel));
                }
                catch (EmployeeUpdateException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    var stringBuilder = new StringBuilder();

                    stringBuilder.Append("<div class='text-center'><h4><strong>Employee could NOT be updated.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been caught while attempting to update an employee profile. Please review an exception log for more details about the exception.</div></div>");

                    var userConfirmationViewModel = new UserConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_Error", userConfirmationViewModel));
                }
            }

            return(PartialView("_EditUserProfile", model));
        }
Exemplo n.º 5
0
 public UserEditProfile()
 {
     InitializeComponent();
     editProfileViewModel = App.Locator.UserEditProfile;
     BindingContext       = editProfileViewModel;
 }