コード例 #1
0
        public static UserProfile Update(this UserProfile userProfile, UserProfileViewModel userProfileViewModel)
        {
            userProfile.FirstName = userProfileViewModel.FirstName;
              userProfile.LastName = userProfileViewModel.LastName;
              userProfile.Email = userProfileViewModel.Email;
              userProfile.PhoneNumber = userProfileViewModel.PhoneNumber;
              userProfile.Activated = userProfileViewModel.Activated;

              if (!userProfileViewModel.CompanyId.IsEmpty())
              {
            var guid = Guid.Parse(userProfileViewModel.CompanyId);
            if (!userProfile.CompanyId.Equals(guid))
            {
              userProfile.CompanyId = guid;
            }
              }
              else
              {
            userProfile.CompanyId = null;
              }

              return userProfile;
        }
コード例 #2
0
    public async Task<IHttpActionResult> UpdateUser(UserProfileViewModel userProfileViewModel)
    {
      if (ModelState.IsValid)
      {
        UserProfile user = await UserManager.FindByIdAsync(userProfileViewModel.Id);

        if (user == null)
        {
          return BadRequest();
        }

        if (user.Roles.Any())
        {
          IdentityUserRole role = user.Roles.FirstOrDefault();

          if ((!userProfileViewModel.RoleId.IsEmpty() && role != null && !role.RoleId.Equals(userProfileViewModel.RoleId)))
          {
            IdentityResult removeFromRoleResult = UserManager.RemoveFromRole(user.Id,
              RoleManager.FindById(role.RoleId).
                Name);

            IHttpActionResult removeFromRoleErrorResult = GetErrorResult(removeFromRoleResult);

            if (removeFromRoleErrorResult != null)
            {
              return removeFromRoleErrorResult;
            }
            IdentityResult addToRoleResult = await UserManager.AddToRoleAsync(user.Id,
              RoleManager.FindById(userProfileViewModel.RoleId).
                Name);

            IHttpActionResult addToRoleErrorResult = GetErrorResult(addToRoleResult);

            if (addToRoleErrorResult != null)
            {
              return addToRoleErrorResult;
            }
          }
          else if (userProfileViewModel.RoleId.IsEmpty())
          {
            IdentityResult removeFromRoleResult = UserManager.RemoveFromRole(user.Id,
              RoleManager.FindById(role.RoleId).
                Name);

            IHttpActionResult removeFromRoleErrorResult = GetErrorResult(removeFromRoleResult);

            if (removeFromRoleErrorResult != null)
            {
              return removeFromRoleErrorResult;
            }
          }
        }
        else if (!userProfileViewModel.RoleId.IsEmpty())
        {
          IdentityResult addToRoleResult = await UserManager.AddToRoleAsync(user.Id,
            RoleManager.FindById(userProfileViewModel.RoleId).
              Name);

          IHttpActionResult addToRoleErrorResult = GetErrorResult(addToRoleResult);

          if (addToRoleErrorResult != null)
          {
            return addToRoleErrorResult;
          }
        }

        if (!user.Activated && userProfileViewModel.Activated)
        {
          string authority = HttpContext.Current.Request.Url.Authority;

          var url = string.Format("http://{0}/account/login",
            authority);

          var notification = new AccountNotificationModel{
                                                           DisplayName = user.UserName,
                                                           Url = url
                                                         };

          string body = ViewRenderer.RenderView("~/Views/Mailer/accountActivated.cshtml",
            notification);

          await UserManager.SendEmailAsync(user.Id,
            "EnergieNetz: Ihr Konto wurde freigeschaltet",
            body);
        }

        user.Update(userProfileViewModel);

        IdentityResult updateUserResult = await UserManager.UpdateAsync(user);

        if (updateUserResult.Succeeded)
        {
          //var logger = UserManager.AuditLogger;
          //var log = logger.LastLog;
          //log.Username = UserManager.FindById(User.Identity.GetUserId()).UserName;
          //log.ToXml(XmlWriter.Create("../../temp/auditLog.xml"));

          return Ok();
        }

        IHttpActionResult updateUserErrorResult = GetErrorResult(updateUserResult);
        return updateUserErrorResult;
      }
      string errors = ModelState.Values.SelectMany(modelState => modelState.Errors).
        Aggregate(string.Empty,
          (current, error) => current + (error.ErrorMessage + Environment.NewLine));
      throw new Exception(errors);
    }