Exemplo n.º 1
0
        public async Task <IHttpActionResult> Post(int id, AccountViewModel accountView)
        {
            //UserRoleRole userRoleRole = new UserRoleRole();
            if (id == 0)
            {
                /*
                 * t_User user=new t_User();
                 * user.LockoutEnabled = accountView.LockoutEnabled;
                 * user.Position = accountView.Position;
                 * user.SetPasswordEnabled = accountView.SetPasswordEnabled;
                 * user.FIO = accountView.FIO;
                 * user.UserName = accountView.UserName;
                 * _db.t_User.Add(user);
                 * _db.SaveChanges();
                 * _db.Entry(user).GetDatabaseValues();
                 * //int userId= _userManager.GetIdByName(_userName);
                 * await _userManager.AddUserToRoleAsync(user.UserID, accountView.UserRole);
                 * //userRoleRole.AddUserToRole(user.UserID, accountView.UserRole);*/
                //_userManager.UpdateAsync(user);
                if (await _userManager.CreateUserAsync(accountView) == IdentityResult.Success)
                {
                    return(Ok());
                }
                return(BadRequest());
            }
            else
            {
                //t_User user = _db.t_User.Find(id);
                var user = await _userManager.FindByIdAsync(id);

                if (user != null)
                {
                    user.LockoutEnabled     = accountView.LockoutEnabled;
                    user.Position           = accountView.Position;
                    user.SetPasswordEnabled = accountView.SetPasswordEnabled;
                    user.FIO      = accountView.FIO;
                    user.UserName = accountView.UserName;
                    //_db.Entry(user).State=EntityState.Modified;
                    //_db.SaveChanges();
                    await _userManager.UpdateAsync(user);

                    //int userId = _userManager.GetIdByName(_userName);
                    await _userManager.RemoveUserFromRoleAsync(id);

                    //userRoleRole.RemoveUserFromRole(id, accountView.UserRole);

                    await _userManager.AddUserToRoleAsync(id, accountView.UserRole);

                    //userRoleRole.AddUserToRole(id, accountView.UserRole);
                    //_userManager.UpdateAsync(user);
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
        }