public static async Task <string> deleteAdminUser(adminUser user) { if (!String.IsNullOrEmpty(adminAuth.Username)) { try { string result = ""; var parameters = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("adminUsername", adminAuth.Username), new KeyValuePair <string, string>("adminPassword", adminAuth.Password), new KeyValuePair <string, string>("username", user.Username) }); HttpClient client = new HttpClient(); var response = await client.PostAsync("https://pockettender.000webhostapp.com/web/process_deleteUser.php", parameters); if (response.IsSuccessStatusCode) { result = response.Content.ReadAsStringAsync().Result; } return(result); } catch (Exception ex) { Console.WriteLine("Delete user error: " + ex); return(null); } } else { return("Admin not logged in"); } }
async void onEditBtnTapped(object sender, EventArgs eventArgs) { var userSelected = ((TappedEventArgs)eventArgs).Parameter; adminUser user = (adminUser)userSelected; await Navigation.PushAsync(new editUserPage(user)); }
async void onDeleteBtnTapped(object sender, EventArgs eventArgs) { var userSelected = ((TappedEventArgs)eventArgs).Parameter; adminUser user = (adminUser)userSelected; List <adminUser> tempAdminList = (List <adminUser>)listView.ItemsSource; if (user.Username != adminAuth.Username) { var answer = await DisplayAlert("Remove User", "Are you sure you want to remove user '" + user.Username + "'?", "YES", "NO"); if (answer) { //remove user from listview foreach (var admin in tempAdminList.ToList()) { if (admin == user) { int index = tempAdminList.IndexOf(admin); tempAdminList.Remove(admin); } } //Refresh listview listView.ItemsSource = tempAdminList.ToList(); //Display error message when there are no user if (tempAdminList.Count <= 0) { errorMsg.Text = "No user found."; errorMsg.IsVisible = true; upBtn.IsVisible = false; } //Remove user from database string httpTask = await Task.Run <string>(() => HttpRequestHandler.deleteAdminUser(user)); var httpResult = httpTask.ToString(); Console.WriteLine(httpResult); int count = 0; while (count < 3 && httpResult != "Success") { Console.WriteLine("Looping for failure delete"); httpTask = await Task.Run <string>(() => HttpRequestHandler.deleteAdminUser(user)); httpResult = httpTask.ToString(); count++; } } } else { DisplayAlert("Cannot delete user", "Cannot delete your own account!", "OK"); } }
async Task getCurrentUserDetails() { adminUser user = new adminUser(); activityIndicator.IsVisible = true; activityIndicator.IsRunning = true; //Send HTTP request to get current user details string httpTask = await Task.Run <string>(() => HttpRequestHandler.getCurrentUserDetails()); string httpResult = httpTask.ToString(); activityIndicator.IsVisible = false; activityIndicator.IsRunning = false; if (httpResult != null) { if (httpResult == "No user found") { errorLbl.IsVisible = true; } else if (httpResult == "Admin not logged in") { errorLbl.Text = httpResult; errorLbl.IsVisible = true; } else { List <adminUser> adminUsers = JsonConvert.DeserializeObject <List <adminUser> >(httpResult); user = adminUsers[0]; nameInput.Text = user.administratorName; emailInput.Text = user.administratorEmail; if (user.Role == "Administrator") { rolePicker.SelectedIndex = 0; } else { rolePicker.SelectedIndex = 1; } usernameInput.Text = user.Username; } rolePicker.IsEnabled = false; } else { errorLbl.IsVisible = true; } }
public editUserPage(adminUser user) { InitializeComponent(); isCurrentUser = false; nameInput.Text = user.administratorName; emailInput.Text = user.administratorEmail; if (user.Role == "Administrator") { rolePicker.SelectedIndex = 0; } else { rolePicker.SelectedIndex = 1; } usernameInput.Text = user.Username; if (user.Username == adminAuth.Username) { rolePicker.IsEnabled = false; } }