Inheritance: Blog.Common.Contracts.User
Example #1
0
        private async Task<UserViewModel> GetUserViewModel(int id)
        {
            var user = _usersResource.Get(id);

            if (user.Error != null)
            {
                return new UserViewModel { Error = user.Error };
            }

            var role = string.IsNullOrEmpty(user.IdentityId) ? string.Empty : await GetRoles(user.IdentityId);
            var userViewModel = new UserViewModel
            {
                Id = id,
                UserName = user.UserName,
                EmailAddress = user.EmailAddress,
                FirstName = user.FirstName,
                LastName = user.LastName,
                IdentityId = user.IdentityId,
                BirthDate = user.BirthDate,
                Address = user.Address,
                Education = user.Education,
                Picture = user.Picture,
                Background = user.Background,
                Hobbies = user.Hobbies,
                Role = role
            };

            return userViewModel;
        }
Example #2
0
        // GET: Users
        public async Task<ActionResult> Index()
        {
            try
            {
                var users = _usersResource.GetUsers(10, 0);
                var userViewModels = new List<UserViewModel>();

                foreach (var u in users)
                {
                    var role = string.IsNullOrEmpty(u.IdentityId) ? string.Empty : await GetRoles(u.IdentityId);

                    var userViewModel = new UserViewModel
                    {
                        Id = u.Id,
                        UserName = u.UserName,
                        FirstName = u.FirstName,
                        LastName = u.LastName,
                        IdentityId = u.IdentityId,
                        Role = role
                    };

                    userViewModels.Add(userViewModel);
                }

                return View(userViewModels);
            }
            catch (Exception ex)
            {
                _errorSignaler.SignalFromCurrentContext(ex);
                ViewBag.ErrorMessage = "Failed to get users list. Try refreshing the page.";
                return View();
            }
        }
Example #3
0
        public async Task<ActionResult> Edit(UserViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.ErrorMessage = "Something went wrong. Please try again.";
                    return View(model);
                }

                var user = await UserManager.FindByNameAsync(model.UserName);
                if (user == null)
                {
                    ViewBag.IdentityMissing = true;
                    throw new Exception(string.Format("User {0} has no identity.", model.UserName));
                }

                user.Email = model.EmailAddress;

                var result = await UserManager.UpdateAsync(user);

                if (result.Succeeded)
                {
                    var roles = await UserManager.GetRolesAsync(user.Id);
                    if (roles.Count > 0)
                    {
                        var roleRemoveResult = await UserManager.RemoveFromRolesAsync(user.Id, roles.ToArray());
                        if (!roleRemoveResult.Succeeded) throw new Exception("Failed to update this user's role.");
                    }

                    var addToRoleResult = await UserManager.AddToRoleAsync(user.Id, model.SelectedRole);
                    if (!addToRoleResult.Succeeded)
                    {
                        AddErrors(addToRoleResult);
                        return View(model);
                    }

                    var userProfile = new User
                                      {
                                          Id = model.Id,
                                          UserName = model.UserName,
                                          FirstName = model.FirstName,
                                          LastName = model.LastName,
                                          BirthDate = model.BirthDate,
                                          IdentityId = user.Id,
                                          EmailAddress = model.EmailAddress,
                                          Picture = model.Picture,
                                          Background = model.Background
                                      };

                    var blogUser = _usersResource.Update(userProfile);
                    if (blogUser.Error == null) return RedirectToAction("Index", "Users");

                    ViewBag.UserCreationError = blogUser.Error.Message;
                    return View(model);
                }

                AddErrors(result);
                return View(model);
            }
            catch (Exception ex)
            {
                _errorSignaler.SignalFromCurrentContext(ex);
                ViewBag.ErrorMessage = ex.Message;
                return View(model);
            }
        }