/// <summary>
        /// Executes when the user selects another user to edit the profile
        /// </summary>
        /// <param name="selected">The selected user</param>
        private void OnAdminPanelEditProfileSelectUser(UserEntity selected) => Task.Run(() =>
        {
            // Displays the loading
            this.DisplayLoading(Lang.main_admin_edituser_select_loading);

            try
            {
                // Gets the user
                var user = Database.Instance.GetUser(selected);

                // Checks if no user got found
                if (user == null)
                {
                    // Displays the error
                    this.DisplayInfo(
                        Lang.main_admin_edituser_select_error_not_found_title,
                        Lang.main_admin_edituser_select_error_not_found_text,
                        () => this.DisplayAdminPanel(),
                        Lang.main_popup_close
                        );
                    return;
                }

                this.Dispatcher.Invoke(() =>
                {
                    // Shows the admin panel
                    this.DisplayAdminPanel();

                    // Inserts the user
                    this.FormEditProfile.UserInput = user;

                    // Enables the edit form
                    this.FormEditProfile.IsEnabled = this.ButtonEditProfileSave.IsEnabled = true;

                    // Sets the selected user
                    this.AdminPanelStoredUser = user;
                });
            }
            catch (MySqlException)
            {
                // Displays the error
                this.DisplayInfo(
                    Lang.main_database_error_connect_title,
                    Lang.main_database_error_connect_text,
                    () => this.DisplayAdminPanel(),
                    Lang.main_popup_close
                    );
            }
            catch {
                // Displays the error
                this.DisplayFatalError();
            }
        });
        /// <summary>
        /// Tries to grab the user and display it inside the admin-edit user panel
        /// </summary>
        /// <param name="rfid">The rfid that shall be searched for</param>
        private void AdminPanelSelectUserByRFID(string rfid)
        {
            // Tries to get a user by it's rfid
            Task.Run(() =>
            {
                try
                {
                    // Gets the user
                    var user = Database.Instance.GetUser(rfid);

                    // Checks if a user got found
                    if (user == null)
                    {
                        // Shows the error
                        this.DisplayInfo(
                            Lang.main_rfid_error_loading_title,
                            Lang.main_rfid_error_loading_text,
                            () => this.DisplayAdminPanel(),
                            Lang.main_popup_close
                            );
                        return;
                    }

                    // Updates the user
                    this.Dispatcher.Invoke(() =>
                    {
                        // Shows the admin panel
                        this.DisplayAdminPanel();
                        // Inserts the user
                        this.AdminPanelStoredUser      = user;
                        this.FormEditProfile.UserInput = user;
                        // Enables the edit form
                        this.FormEditProfile.IsEnabled = this.ButtonEditProfileSave.IsEnabled = true;
                    });
                }
                catch (MySqlException)
                {
                    // Displays the error
                    this.DisplayInfo(
                        Lang.main_database_error_connect_title,
                        Lang.main_database_error_connect_user_text,
                        this.CloseOverlay,
                        Lang.main_popup_close
                        );
                }
                catch
                {
                    // Displays the error
                    this.DisplayFatalError();
                }
            });
        }
        /// <summary>
        /// Executes when the user has successfully input the password and clicked the open admin panel button.
        /// </summary>
        private void OnAdminPanelLoginClick(object sender, RoutedEventArgs e)
        {
            this.CloseOverlay();

            // Shows the adminpanel overlay
            this.Overlay.Visibility =
                this.OverlayAdminPanel.Visibility = Visibility.Visible;

            // Clears any previously input profile data
            this.AdminPanelStoredUser = null;
            this.FormEditProfile.ResetForm();
            this.FormEditProfile.IsEnabled = false;
        }
 /// <summary>
 /// Displays the admin panel
 /// </summary>
 private void DisplayAdminPanel(bool resetStoredUser = true) => this.Dispatcher.Invoke(() =>
 {
     this.CloseOverlay();
     this.OverlayAdminPanel.Visibility = this.Overlay.Visibility = Visibility.Visible;
     // Checks if the stored user should be reset
     if (resetStoredUser)
     {
         // Resets the form
         this.FormEditProfile.ResetForm();
         this.AdminPanelStoredUser = null;
         // Disables the form and button
         this.FormEditProfile.IsEnabled = this.ButtonEditProfileSave.IsEnabled = false;
     }
 });
Пример #5
0
        /// <summary>
        /// Edits a users profile with the given changes
        /// </summary>
        /// <param name="user">The user and his new profile settings</param>
        /// /// <returns>
        /// Retuns a status code that resembels the following
        /// 0 = Success
        /// 1 = The name has already been used
        /// 2 = The rfidCode has already been used
        /// </returns>
        public int EditUser(ExtendedUserEntity user)
        {
            this.EnsureOpenConnection();

            // Updates the user
            try {
                // Creates the query
                var query = new MySqlCommand(@"
                    UPDATE `user` SET
                      `email`=@email,
                      `telephone`=@telephone,
                      `rfidcode`=@rfidcode,
                      `autodeleteaccount`=@autodelete,
                      `firstname`=@firstname,
                      `lastname`=@lastname,
                      `plz`=@plz,
                      `location`=@location,
                      `street`=@street,
                      `housenumber`=@housenumber
                    WHERE
                      `user`.`id` = @userid;
                ", this.connection);
                // Inserts the values
                query.Parameters.AddWithValue("@firstname", user.Firstname);
                query.Parameters.AddWithValue("@lastname", user.Lastname);
                query.Parameters.AddWithValue("@plz", user.PLZ);
                query.Parameters.AddWithValue("@location", user.Location);
                query.Parameters.AddWithValue("@street", user.Street);
                query.Parameters.AddWithValue("@housenumber", user.StreetNumber);
                query.Parameters.AddWithValue("@email", user.Email);
                query.Parameters.AddWithValue("@telephone", user.TelephoneNumber);
                query.Parameters.AddWithValue("@rfidcode", user.RFID);
                query.Parameters.AddWithValue("@autodelete", user.AutoDeleteAccount);
                query.Parameters.AddWithValue("@userid", user.Id);
                query.Prepare();

                // Executes the update
                query.ExecuteNonQuery();

                return(0);
            }catch (MySqlException e)
            {
                // Checks if the exception is a duplicated entry
                if (e.Number == 1062)
                {
                    // The identifier to seperate the column name
                    string identifier = "key '";

                    // Gets the message and the index of the identifier
                    string msg           = e.Message;
                    int    identifierPos = msg.LastIndexOf(identifier);

                    // Gets the column name
                    string colName = msg.Substring(identifierPos + identifier.Length, msg.Length - identifier.Length - identifierPos - 1);

                    // Checks if the duplicated column was the name combination
                    if (colName.ToLower().Equals("uq_name"))
                    {
                        return(1);
                    }

                    // Checks if the duplicated column was the rfidcode
                    if (colName.ToLower().Equals("rfidcode"))
                    {
                        return(2);
                    }
                }

                // Parses on the exception
                throw e;
            }
        }
Пример #6
0
        /// <summary>
        /// Sends the user to the database. Doesnt uses a userentity because this has not all properties that a registered user needs.
        /// This is done for dataprotection
        /// </summary>
        /// <returns>
        /// Retuns a status code that resembels the following
        /// 0 = Success
        /// 1 = The name has already been used
        /// 2 = The rfidCode has already been used
        /// </returns>
        public int RegisterUser(ExtendedUserEntity user)
        {
            this.EnsureOpenConnection();

            // Registers the user
            try
            {
                // Creates the query
                var query = new MySqlCommand("INSERT INTO `user` (`id`, `firstname`, `lastname`, `plz`, `location`, `street`, `housenumber`,`email`,`telephone`,`rfidcode`,`autodeleteaccount`,`createdate`) VALUES (NULL, @firstname, @lastname, @plz, @location, @street, @housenumber,@email,@telephone,@rfidcode,@autodelete,@createdate);", this.connection);

                // Inserts the values
                query.Parameters.AddWithValue("@firstname", user.Firstname);
                query.Parameters.AddWithValue("@lastname", user.Lastname);
                query.Parameters.AddWithValue("@plz", user.PLZ);
                query.Parameters.AddWithValue("@location", user.Location);
                query.Parameters.AddWithValue("@street", user.Street);
                query.Parameters.AddWithValue("@housenumber", user.StreetNumber);
                query.Parameters.AddWithValue("@email", user.Email);
                query.Parameters.AddWithValue("@telephone", user.TelephoneNumber);
                query.Parameters.AddWithValue("@rfidcode", user.RFID);
                query.Parameters.AddWithValue("@autodelete", user.AutoDeleteAccount);
                query.Parameters.AddWithValue("@createdate", DateTime.Now);
                query.Prepare();

                // Gets the result
                query.ExecuteNonQuery();

                // Exits without any error
                return(0);
            }catch (MySqlException e)
            {
                // Checks if the exception is a duplicated entry
                if (e.Number == 1062)
                {
                    // The identifier to seperate the column name
                    string identifier = "key '";

                    // Gets the message and the index of the identifier
                    string msg           = e.Message;
                    int    identifierPos = msg.LastIndexOf(identifier);

                    // Gets the column name
                    string colName = msg.Substring(identifierPos + identifier.Length, msg.Length - identifier.Length - identifierPos - 1);

                    // Checks if the duplicated column was the name combination
                    if (colName.ToLower().Equals("uq_name"))
                    {
                        return(1);
                    }

                    // Checks if the duplicated column was the rfidcode
                    if (colName.ToLower().Equals("rfidcode"))
                    {
                        return(2);
                    }
                }

                // Parses on the exception
                throw e;
            }
        }