private void btnSearch_Click(object sender, EventArgs e)
        {
            using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
            {
                var usernameSearch = (from u in dbConnection.User
                                      where u.Username.Equals(txtUserID.Text.ToString())
                                      select u).FirstOrDefault();


                if (usernameSearch != null)
                {
                    UserId = usernameSearch.Id;

                    lblUsernametxt.Text  = usernameSearch.Username;
                    lblFirstnametxt.Text = usernameSearch.Firstname;
                    lblLastnametxt.Text  = usernameSearch.Lastname;
                    lblEmailtxt.Text     = usernameSearch.Email;

                    lblNoSuchUser.Visible = false;
                    panel1.Visible        = true;
                }

                else
                {
                    lblNoSuchUser.Visible = true;
                }
            }
        }
Esempio n. 2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            //Save things to DB and give a success message!
            var hashedPassword = Hash.sha256(txtPassword.Text);

            try
            {
                using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
                {
                    var newUser = new User();
                    newUser.Username  = txtUsername.Text.ToString();
                    newUser.Password  = hashedPassword;
                    newUser.RoleId    = cmbRole.SelectedIndex;
                    newUser.Firstname = txtFirstname.Text.ToString();
                    newUser.Lastname  = txtLastname.Text.ToString();
                    newUser.Email     = txtEmail.Text.ToString();

                    dbConnection.User.Add(newUser);
                    dbConnection.SaveChanges();
                }
            }
            catch (EntityException ex)
            {
                MessageBox.Show("Could not populate the dropdown list." + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("General error when populating the dropdown list." + ex.Message + ex.InnerException);
            }

            finally
            {
                Close();
            }
        }
Esempio n. 3
0
        //Populates the main window with data from the database.
        private void getNumbers()
        {
            try
            {
                using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
                {
                    // Get number of users here
                    int countUsers = (from u in dbConnection.User
                                      select u).Count();
                    lblNumberOfUserstxt.Text = countUsers.ToString();

                    //Get numbers of admins
                    int countAdmins = (from u in dbConnection.User
                                       where u.RoleId == 1
                                       select u).Count();
                    lblNumberOfAdminstxt.Text = countAdmins.ToString();

                    //Get number of tests
                    int countTests = (from u in dbConnection.Test
                                      select u).Count();
                    lblNumberOfTeststxt.Text = countTests.ToString();
                }
            }
            catch (EntityException ex)
            {
                MessageBox.Show("Could not load data from database." + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error when loading from Database." + ex.Message + ex.InnerException);
            }
        }
Esempio n. 4
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //Disables the login button to prevent several requests.
            btnLogin.Enabled = false;
            var hashedPassword = Hash.sha256(txtPassword.Text);

            try
            {
                using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
                {
                    //Selecting the user with the given credentials.
                    var user = (from u in dbConnection.User
                                where u.Username == txtUsername.Text.ToString() &&
                                u.RoleId.Equals(1) &&
                                u.Password == hashedPassword
                                select u).FirstOrDefault();

                    //If the user is an Admin (Role no. 1) and the right credentials are given.
                    //Saves the users Id for usage further more.
                    if (user != null)
                    {
                        DialogResult        = DialogResult.OK;
                        LoggedInUser.UserId = user.Id;
                    }

                    //If the user is a regular user (Role no. 0) and the right credentials are given.
                    if (user != null && user.RoleId.Equals(0))
                    {
                        lblNotAdmin.Visible         = true;
                        lblWrongCredentials.Visible = false;
                        clearAllFields();
                    }

                    //If there's not an user with the given credentials.
                    else
                    {
                        lblNotAdmin.Visible         = false;
                        lblWrongCredentials.Visible = true;
                        clearAllFields();
                    }
                }
            }

            catch (EntityException ex)
            {
                MessageBox.Show("Could not load userinfo from database." + ex.Message);
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error when loading from database." + ex.Message + ex.InnerException);
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
            {
                var selectedUser = (from u in dbConnection.User
                                    where u.Id == UserId
                                    select u).FirstOrDefault();

                if (selectedUser != null)
                {
                    dbConnection.User.Remove(selectedUser);
                    dbConnection.SaveChanges();
                }
            }

            panel1.Visible = false;
        }
Esempio n. 6
0
        public void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
                {
                    var usernameSearch = (from u in dbConnection.User
                                          where u.Username.Equals(txtSearchUsername.Text.ToString())
                                          select u).FirstOrDefault();

                    if (usernameSearch != null)
                    {
                        UserId               = usernameSearch.Id;
                        txtUsername.Text     = usernameSearch.Username;
                        txtFirstname.Text    = usernameSearch.Firstname;
                        txtLastname.Text     = usernameSearch.Lastname;
                        txtEmail.Text        = usernameSearch.Email;
                        cbRole.SelectedIndex = usernameSearch.RoleId;

                        lblNoSuchUsername.Visible = false;
                        panel1.Visible            = true;
                    }

                    if (usernameSearch == null)
                    {
                        lblNoSuchUsername.Visible = true;
                        txtSearchUsername.Text    = "";
                    }
                }
            }

            catch (EntityException ex)
            {
                MessageBox.Show("Could not search the database for the requested username." + ex.Message);
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error when searching database." + ex.Message + ex.InnerException);
            }
        }
Esempio n. 7
0
        public void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                using (GlossaryAdminModel dbConnection = new GlossaryAdminModel())
                {
                    var selectedUser = (from u in dbConnection.User
                                        where u.Id == UserId
                                        select u).FirstOrDefault();

                    if (selectedUser != null)
                    {
                        selectedUser.Username  = txtUsername.Text;
                        selectedUser.Firstname = txtFirstname.Text;
                        selectedUser.Lastname  = txtLastname.Text;
                        selectedUser.Email     = txtEmail.Text;
                        selectedUser.RoleId    = cbRole.SelectedIndex;

                        dbConnection.SaveChanges();
                    }
                }
            }
            catch (EntityException ex)
            {
                MessageBox.Show("Could not save the user to database." + ex.Message);
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error when saving to Database." + ex.Message + ex.InnerException);
            }

            finally
            {
                showFirstPage();
            }
        }