Пример #1
0
 public async Task <IHttpActionResult> SaveUsers([DynamicBody] dynamic param)
 {
     try
     {
         IEnumerable <Ksmed.Models.User> users = param.users;
         return(Ok(await _applicationUsersService.SaveUsers(users)));
     }
     catch (Exception ex)
     {
         _traceManager.TraceError(ex, ex.Message);
         return(InternalServerError(ex));
     }
 }
Пример #2
0
        public async Task <ActionResult> UpdateUser(CRUDModel <UserViewModel> user)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var message = string.Join(" | ", ModelState.Values
                                              .SelectMany(v => v.Errors)
                                              .Select(e => e.ErrorMessage));
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, message));
                }

                if (user.Value.Roles == null || !user.Value.Roles.Any())
                {
                    throw new Exception(LocalizedStrings.GetString("AskSelectRole"));
                }

                var(Users, Roles, Languages, Teams) = await _applicationUsersService.GetUsersAndRolesAndLanguages();

                var userEdit = Users.FirstOrDefault(u => u.UserId == user.Value.UserId);
                userEdit.StartTracking();

                userEdit.DefaultLanguageCode = user.Value.DefaultLanguageCode;
                userEdit.Tenured             = user.Value.Tenured;
                userEdit.Username            = user.Value.Username;
                userEdit.Firstname           = user.Value.Firstname;
                userEdit.Name        = user.Value.Name;
                userEdit.Email       = user.Value.Email;
                userEdit.PhoneNumber = user.Value.PhoneNumber;
                if (user.Value.NewPassword != null)
                {
                    var hash = new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(
                        Encoding.Default.GetBytes(user.Value.NewPassword));
                    userEdit.Password = hash;
                }
                // Reset the roles
                userEdit.Roles.Clear();
                foreach (var role in user.Value.Roles[0].Split(','))
                {
                    if (string.IsNullOrWhiteSpace(role))
                    {
                        continue;
                    }

                    userEdit.Roles.Add(Roles.First(u => u.RoleCode == role));
                }

                IEnumerable <User> editUser = new[] { userEdit };
                await _applicationUsersService.SaveUsers(editUser);

                var users = await GetData();

                var updateUser = users.UsersViewModel.First(u => u.Username == userEdit.Username);
                return(Json(updateUser, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                var message = string.Join(" | ", ModelState.Values
                                          .SelectMany(v => v.Errors)
                                          .Select(e => e.ErrorMessage));
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, ex.Message));
            }
        }