using System; using System.Collections.Generic; using System.Configuration; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Collections.ObjectModel; using brecal.model; using brecal.mysql; using System.Security.Cryptography; using System.Security.Policy; using System.Windows.Markup; namespace RoleEditor { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { #region private fields private readonly ObservableCollection _participants = new ObservableCollection(); private readonly ObservableCollection _roles = new ObservableCollection(); private readonly ObservableCollection _securables = new ObservableCollection(); private readonly ObservableCollection _users = new ObservableCollection(); private readonly ObservableCollection _assignedRoles = new ObservableCollection(); private readonly ObservableCollection _assignedSecurables = new ObservableCollection(); private DBManager _dbManager; #endregion #region Construction / Loading public MainWindow() { InitializeComponent(); _dbManager = new(); } private async void Window_Loaded(object sender, RoutedEventArgs e) { // try database connection try { // load all participants foreach(Participant p in await Participant.LoadAll(_dbManager)) _participants.Add(p); this.listBoxParticipant.ItemsSource = _participants; // load all roles foreach(Role r in await Role.LoadAll(_dbManager)) _roles.Add(r); this.listBoxRoles.ItemsSource = _roles; // set other item sources this.listBoxUser.ItemsSource = _users; this.listBoxSecurables.ItemsSource = _securables; this.listBoxRoleSecurables.ItemsSource = _assignedSecurables; this.listBoxUserRoles.ItemsSource = _assignedRoles; } catch (Exception ex) { MessageBox.Show($"Database connection couldn't be established: {ex.Message}"); this.Close(); } } #endregion #region button callbacks private void buttonParticipantSave_Click(object sender, RoutedEventArgs e) { } private void buttonUserSave_Click(object sender, RoutedEventArgs e) { User? u = this.listBoxUser.SelectedItem as User; if(u != null) { u.Firstname = this.textBoxUserFirstName.Text.Trim(); u.Lastname = this.textBoxUserLastName.Text.Trim(); u.Username = this.textBoxUserUserName.Text.Trim(); if(this.textBoxUserPassword.Text.Trim().Length > 0 ) { var data = Encoding.UTF8.GetBytes(this.textBoxUserPassword.Text.Trim()); using SHA512 sha = SHA512.Create(); byte[] hashedInputBytes = sha.ComputeHash(data); var hashedInputStringBuilder = new StringBuilder(128); foreach (var b in hashedInputBytes) hashedInputStringBuilder.Append(b.ToString("X2")); u.PasswordHash = hashedInputStringBuilder.ToString(); } u.APIKey = this.textBoxUserAPIKey.Text.Trim(); u.Save(_dbManager); this.listBoxUser.ItemsSource = null; this.listBoxUser.ItemsSource = _users; } } private void buttonAddRole_Click(object sender, RoutedEventArgs e) { } private void buttonRemoveRole_Click(object sender, RoutedEventArgs e) { } private void buttonAddSecurable_Click(object sender, RoutedEventArgs e) { } private void buttonRemoveSecurable_Click(object sender, RoutedEventArgs e) { } private void buttonSaveSecurable_Click(object sender, RoutedEventArgs e) { } private void buttonSaveRole_Click(object sender, RoutedEventArgs e) { } #endregion #region listbox selection callbacks private async void listBoxParticipant_SelectionChanged(object sender, SelectionChangedEventArgs e) { Participant? p = this.listBoxParticipant.SelectedItem as Participant; this.textBoxParticipantName.Text = (p != null) ? p.Name : string.Empty; this.textBoxParticipantStreet.Text = (p != null) ? p.Street : string.Empty; this.textBoxParticipantPostalCode.Text = (p != null) ? p.PostalCode : string.Empty; this.textBoxParticipantCity.Text = (p != null) ? p.City : string.Empty; // this.checkboxParticipantActive.Checked = (p != null) ? p. this.textBoxParticipantCreated.Text = (p != null) ? p.Created.ToString() : string.Empty; this.textBoxParticipantModified.Text = (p != null) ? p.Modified.ToString() : string.Empty; // -> load users for this participant selection this._users.Clear(); foreach (User u in await User.LoadForParticipant(p, _dbManager)) _users.Add(u); } private void listBoxRoles_SelectionChanged(object sender, SelectionChangedEventArgs e) { } private void listBoxUser_SelectionChanged(object sender, SelectionChangedEventArgs e) { User? u = this.listBoxUser.SelectedItem as User; this.textBoxUserFirstName.Text = (u != null) ? u.Firstname : string.Empty; this.textBoxUserLastName.Text = (u != null) ? u.Lastname : string.Empty; this.textBoxUserUserName.Text = (u != null) ? u.Username : string.Empty; this.textBoxUserAPIKey.Text = (u != null) ? u.APIKey : string.Empty; this.textBoxUserCreated.Text = (u != null) ? u.Created.ToString() : string.Empty; this.textBoxUserModified.Text = (u != null) ? u.Modified.ToString() : string.Empty; this.textBoxUserPassword.Text = string.Empty; } private void listBoxSecurables_SelectionChanged(object sender, SelectionChangedEventArgs e) { } #endregion #region menuitem callbacks private void menuItemDeleteParticipant_Click(object sender, RoutedEventArgs e) { } private void menuItemNewParticipant_Click(object sender, RoutedEventArgs e) { } private void menuItemNewUser_Click(object sender, RoutedEventArgs e) { Participant? p = this.listBoxParticipant.SelectedItem as Participant; if(p != null) { User u = new(); u.Participant_Id = p.Id; _users.Add(u); this.listBoxUser.SelectedItem = u; } } private void menuItemDeleteUser_Click(object sender, RoutedEventArgs e) { } private void menuItemNewRole_Click(object sender, RoutedEventArgs e) { } private void menuItemDeleteRole_Click(object sender, RoutedEventArgs e) { } private void menuItemNewSecurable_Click(object sender, RoutedEventArgs e) { } private void menuItemDeleteSecurable_Click(object sender, RoutedEventArgs e) { } #endregion } }