Example #1
0
        private void BtnChangeRole_Click(object sender, EventArgs e)
        {
            if (authenicatedUser.Role > User.Role)
            {
                int SelectedRoleValue = (int)User.Role;
                if (!int.TryParse(comboRole.SelectedValue.ToString(), out SelectedRoleValue))
                {
                    MessageBox.Show("Please Select Valid Role From ComboBox !", "Error In Changing Role", buttons: MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Domain.Entities.User oUserInNewContext =
                    db.Users.Find(User.Id);
                if (oUserInNewContext != null)
                {
                    oUserInNewContext.Role            = (Domain.Enums.Role)SelectedRoleValue;
                    db.Entry(oUserInNewContext).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    db.SaveChanges();

                    lblRole.Text            = oUserInNewContext.Role.ToString();
                    comboRole.SelectedIndex = (int)oUserInNewContext.Role;
                    MessageBox.Show("User Role Updated !", "Changing Role", buttons: MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
        }
Example #2
0
        private void BtnDelete_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("Are Sure You Want to Delete This User !?", "Deleting User", buttons: MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                Domain.Entities.User oUserInNewContext =
                    db.Users.Find(User.Id);
                if (oUserInNewContext != null)
                {
                    if (authenicatedUser.Role >= Domain.Enums.Role.Administrator)
                    {
                        var booksList =
                            db.Books
                            .Where(current => (!current.IsDeleted || authenicatedUser.Role >= Domain.Enums.Role.Administrator) && current.UploaderUserId == oUserInNewContext.Id)
                            .ToList();

                        foreach (var item in booksList)
                        {
                            db.Books.Remove(item);
                        }

                        db.Users.Remove(oUserInNewContext);
                        db.SaveChanges();
                    }

                    this.Close();
                }
            }
        }
Example #3
0
 private void Login_Load(object sender, EventArgs e)
 {
     Domain.Entities.User oAdminUser =
         db.Users.Where(current => current.Username.Contains("librarynumber"))
         .FirstOrDefault();
     if (oAdminUser != null)
     {
         Usernametxt.Text = oAdminUser.Username;
         Passwordtxt.Text = "123456789";
     }
 }
Example #4
0
        private void MainPage_Load(object sender, EventArgs e)
        {
            Domain.Entities.User oUser =
                db.Users.Find(authenicatedUser.Id);
            if (oUser == null)
            {
                MessageBox.Show("An Error occured in Your Authenication", "Error In Authenication", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Login oLoginForm = new Login();
                oLoginForm.Show();
                this.Close();
            }
            if (oUser.IsDeleted && oUser.Role < Domain.Enums.Role.Administrator)
            {
                MessageBox.Show("An Error occured in Your Authenication", "Error In Authenication", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Login oLoginForm = new Login();
                oLoginForm.Show();
                this.Close();
            }

            lblWelcome.Text =
                string.Format("Welcome {0} {1}", oUser.FirstName, oUser.LastName);
            btnLogout.Image =
                Infrastructure.ImageUtils.GetThumbnailByImagePath("c:\\BookLibraryData\\Default\\exit.png", 25, 25);

            if (authenicatedUser.Role > Domain.Enums.Role.User)
            {
                btnManageUsers.Visible = true;
            }
            if (authenicatedUser.Role >= Domain.Enums.Role.Administrator)
            {
                btnSettings.Visible = true;
            }
            if (authenicatedUser.ConnectedToWS)
            {
                btnWSBookList.Visible        = true;
                btnBookRentingSystem.Visible = true;
            }

            FillBookGridView(string.Empty);

            Thread thread = new Thread(SetNewRequestBadgeData);

            thread.Start();
        }
Example #5
0
        private void UsersGridView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string strSelectedUsername = UsersGridView.Rows[UsersGridView.CurrentRow.Index].Cells[2].Value.ToString();

            if (!string.IsNullOrEmpty(strSelectedUsername))
            {
                Domain.Entities.User oUser =
                    db.Users.Where(current => current.Username == strSelectedUsername)
                    .FirstOrDefault();
                if (oUser != null)
                {
                    if (oUser.Role <= authenicatedUser.Role)
                    {
                        db.Entry(oUser).Reload();

                        UserDetail UserDetailForm = new UserDetail();
                        UserDetailForm.setUser(oUser);
                        UserDetailForm.SetAuthenicatedUser(authenicatedUser);
                        UserDetailForm.Show();
                    }
                }
            }
        }
Example #6
0
        private void UsersList_Load(object sender, EventArgs e)
        {
            Domain.Entities.User oUser =
                db.Users.Find(authenicatedUser.Id);
            if (oUser == null)
            {
                MessageBox.Show("An Error occured in Your Authenication", "Error In Authenication", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Login oLoginForm = new Login();
                oLoginForm.Show();
                this.Close();
            }
            if (oUser.IsDeleted && oUser.Role < Domain.Enums.Role.Administrator)
            {
                MessageBox.Show("An Error occured in Your Authenication", "Error In Authenication", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Login oLoginForm = new Login();
                oLoginForm.Show();
                this.Close();
            }

            lblRole.Text =
                string.Format("You Are {0}", oUser.Role.ToString());

            FillUserGridView(string.Empty);
        }
Example #7
0
        private void Loading_Load(object sender, EventArgs e)
        {
            DatabaseContext databaseContextCreated = new DatabaseContext();

            try
            {
                bool _databaseInitilized = databaseContextCreated.Database.EnsureCreated();
                if (_databaseInitilized && (!databaseContextCreated.Users.Any() || !databaseContextCreated.ApplicationSettings.Any()))
                {
                    ApplicationSetting oApplicationSetting = new ApplicationSetting()
                    {
                        LibraryName          = "Library Number " + Utility.RandomDoubleCodeGeneratorByDigitsCount(2).ToString(),
                        LibraryRegisterdInWS = false,
                        LibraryAuthCode      = string.Empty,
                    };

                    User oUser = new User()
                    {
                        FirstName          = "Zahra",
                        LastName           = "Nabizzz",
                        Username           = oApplicationSetting.LibraryName.Trim().Replace("  ", " ").Replace(" ", "").ToLower(),
                        PhoneNumber        = "09372302283",
                        PhoneNumberIsoCode = "IR",
                        EmailAddress       = "*****@*****.**",
                        Role     = Domain.Enums.Role.Administrator,
                        Password = Hashing.GetSha256("123456789"),
                        IsPhoneNumberVerified  = true,
                        IsEmailAddressVerified = true,
                    };
                    databaseContextCreated.Users.Add(oUser);
                    databaseContextCreated.SaveChanges();

                    databaseContextCreated.ApplicationSettings.Add(oApplicationSetting);
                    databaseContextCreated.SaveChanges();
                }
                _databaseCreatedSuccessfuly = true;
            }
            catch (Exception ex)
            {
                DialogResult result = MessageBox.Show("DatabaseCreationProcess Error With Detail : \n \n" + ex.Message, "Database Error!", MessageBoxButtons.OK);
                if (result == DialogResult.OK)
                {
                    MessageBoxOkClick();
                }
            }

            LibraryManagmentConnectSDK.ConnectionSetting.SetupConnectionChannel("localhost:5000");
            if (!LibraryManagmentConnectSDK.ConnectionSetting.ConnectedToServer)
            {
                MessageBox.Show("Couldnt Connect To The Library Managment System !", "Connection Error!", MessageBoxButtons.OK);
            }
            if (!Utility.GetApplicationSetting().LibraryRegisterdInWS)
            {
                if (!LibraryManagmentConnectSDK.ConnectionSetting.ConnectedToServer)
                {
                    DialogResult result = MessageBox.Show("Couldnt Connect To The Library Managment System \n For The First Time You Should Connect To WS Server \n Try Agin!", "Connection Error!", MessageBoxButtons.OK);
                    if (result == DialogResult.OK)
                    {
                        MessageBoxOkClick();
                    }
                }
                else
                {
                    LibraryManagmentConnectSDK.ServerConnection     oServerConnection   = new LibraryManagmentConnectSDK.ServerConnection();
                    LibraryManagmentConnectSDK.LibraryRegisterReply oRegistreationReply =
                        oServerConnection.LibraryRegister(new LibraryManagmentConnectSDK.LibraryRegisterRequest {
                        Name = Utility.GetApplicationSetting().LibraryName
                    });
                    if (oRegistreationReply == null || oRegistreationReply.IsSuccessfull == false)
                    {
                        DialogResult result = MessageBox.Show("Couldnt Register This Library To The Library Managment System \n For The First Time You Should Register To WS Server \n Try Agin! \n With Error : "
                                                              + ((oRegistreationReply == null)? "Couldnt Get Response From Server" : ((Domain.Enums.ResponseErrorType)oRegistreationReply?.ErrorType).ToString()), "Registration Error!", MessageBoxButtons.OK);
                        if (result == DialogResult.OK)
                        {
                            MessageBoxOkClick();
                            return;
                        }
                    }

                    Domain.Entities.User oAdminUser =
                        databaseContextCreated.Users.Where(current => current.Username == Utility.GetApplicationSetting().LibraryName.Trim().Replace("  ", " ").Replace(" ", "").ToLower())
                        .FirstOrDefault();
                    if (oAdminUser == null)
                    {
                        DialogResult result = MessageBox.Show("Couldnt Connect To The Library Managment System \n For The First Time You Should Connect To WS Server \n Try Agin!", "Connection Error!", MessageBoxButtons.OK);
                        if (result == DialogResult.OK)
                        {
                            MessageBoxOkClick();
                            return;
                        }
                    }

                    oServerConnection.SetLibrary(oRegistreationReply.AuthCode);

                    LibraryManagmentConnectSDK.AddUserRequest oAddUserRequest =
                        new LibraryManagmentConnectSDK.AddUserRequest
                    {
                        FirstName          = oAdminUser.FirstName,
                        LastName           = oAdminUser.LastName,
                        Username           = oAdminUser.Username,
                        EmailAddress       = oAdminUser.EmailAddress,
                        PhoneNumber        = oAdminUser.PhoneNumber,
                        Password           = oAdminUser.Password,
                        PhoneNumberIsoCode = oAdminUser.PhoneNumberIsoCode,
                        Role = (int)oAdminUser.Role,
                    };

                    LibraryManagmentConnectSDK.AddUserReply oAddUserReply =
                        oServerConnection.AddUser(oAddUserRequest);
                    if (oAddUserReply == null || oAddUserReply.IsSuccessfull == false)
                    {
                        DialogResult result = MessageBox.Show("Couldnt Register This User To The Library Managment System \n For The Signup Process You Should Connect To WS Server, Try Agin! \n With Error : "
                                                              + oAddUserReply == null ? "Couldnt Get Response From Server" : ((Domain.Enums.ResponseErrorType)oAddUserReply.ErrorType).ToString(), "Registration Error!", MessageBoxButtons.OK);
                        if (result == DialogResult.OK)
                        {
                            MessageBoxOkClick();
                            return;
                        }
                    }

                    oAdminUser.ConnectedToWS = true;
                    oAdminUser.WSAuthCode    = oAddUserReply.UserAuthCode;

                    Domain.Entities.ApplicationSetting oApplicationSetting
                        = databaseContextCreated.ApplicationSettings.OrderByDescending(current => current.RegisterDate).FirstOrDefault();
                    oApplicationSetting.LibraryRegisterdInWS = true;
                    oApplicationSetting.LibraryAuthCode      = oRegistreationReply.AuthCode;

                    databaseContextCreated.Entry(oAdminUser).State          = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    databaseContextCreated.Entry(oApplicationSetting).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    databaseContextCreated.SaveChanges();
                    Utility.UpdateApplicationSetting();
                }
            }

            MainTimer          = new Timer();
            MainTimer.Interval = 2000;
            MainTimer.Enabled  = true;
            MainTimer.Tick    += new EventHandler(t_Tick);
        }
Example #8
0
        private void Button1_Click(object sender, EventArgs e)
        {
            ViewModels.LoginInput oLoginInput =
                new ViewModels.LoginInput()
            {
                Password = Passwordtxt.Text,
                Username = Usernametxt.Text,
            };

            FluentValidation.Results.ValidationResult validationResult = Utility.GeneralViewModelValidator
                                                                         <ViewModels.LoginInput, Validations.LoginInputValidator>(oLoginInput);
            if (!validationResult.IsValid)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show(error_message, "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Domain.Entities.User oUser =
                db.Users.Where(current => current.Username == oLoginInput.Username.Trim().ToLower())
                .FirstOrDefault();
            if (oUser == null)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("Your Login Information is Not Correct! ☹️ Try Again!", "Incorrect Login Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (oUser.IsDeleted && oUser.Role < Domain.Enums.Role.Administrator)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("Your Login Information is Not Correct! ☹️ Try Again!", "Incorrect Login Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string hashPassword = Hashing.GetSha256(oLoginInput.Password);

            if (oUser.Password != hashPassword)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("Your Login Information is Not Correct! ☹️ Try Again!", "Incorrect Login Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            AuthenicatedUser oAuthenicatedUser = new AuthenicatedUser(oUser);

            //Domain.Entities.User oUsertest =
            //	db.Users.Where(current => current.Id == oAuthenicatedUser.Id)
            //	.FirstOrDefault();
            //if (oUsertest == null)
            //{

            //}
            //if (oUser.IsDeleted && oUser.Role < Domain.Enums.Role.Administrator)
            //{

            //}

            MainPage oLibraryFirstPageForm = new MainPage();

            oLibraryFirstPageForm.SetAuthenicatedUser(oAuthenicatedUser);
            oLibraryFirstPageForm.Show();

            this.Close();
        }
Example #9
0
        private void AddClick_Click(object sender, EventArgs e)
        {
            int    PulishDate = 0;
            double Price      = 0;

            if (!int.TryParse(PublishedDatetxt.Text, out PulishDate))
            {
                MessageBox.Show("You Shuold Enter PublishDate In Number Format!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!double.TryParse(Pricetxt.Text, out Price))
            {
                MessageBox.Show("You Shuold Enter Price In Number Format!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ViewModels.BookInput oBookInput =
                new ViewModels.BookInput()
            {
                Name          = Nametxt.Text,
                Author        = Authoretxt.Text,
                Translator    = Translatortxt.Text,
                Publisher     = Publishertxt.Text,
                PublishedDate = PulishDate,
                Circulation   = Circulationtxt.Text,
                Price         = Price,
                ISBN          = ISBNtxt.Text,
            };
            FluentValidation.Results.ValidationResult validationResult = Utility.GeneralViewModelValidator
                                                                         <ViewModels.BookInput, Validations.BookInoutValidator>(oBookInput);
            if (!validationResult.IsValid)
            {
                string error_message = validationResult.ToString();
                MessageBox.Show(error_message, "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (db.Books.Where(current => current.Name == oBookInput.Name.Trim().ToLower()).Any())
            {
                string error_message = validationResult.ToString();
                MessageBox.Show("A book registerd with this 'Name' Before, Please choose a new one!", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Domain.Entities.User oUser =
                db.Users.Where(current => current.Id == authenicatedUser.Id)
                .FirstOrDefault();
            if (oUser == null)
            {
                MessageBox.Show("An Error accrued in Your Authenication", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (oUser.IsDeleted && oUser.Role < Domain.Enums.Role.Administrator)
            {
                MessageBox.Show("An Error accrued in Your Authenication", "Error In Input Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Book oBook = new Book()
            {
                Name           = oBookInput.Name,
                Author         = oBookInput.Author,
                Translator     = oBookInput.Translator,
                Publisher      = oBookInput.Publisher,
                PublishedDate  = oBookInput.PublishedDate,
                Circulation    = oBookInput.Circulation,
                Price          = oBookInput.Price,
                ISBN           = oBookInput.ISBN,
                UploaderUser   = oUser,
                UploaderUserId = oUser.Id,
            };


            string BookText = BookTextRichBox.Text.ToString();
            string TextPath = "c:\\BookLibraryData\\Book-" + oBook.Id.ToString();

            try
            {
                bool exists = Directory.Exists(TextPath);
                if (exists)
                {
                    Directory.Delete(TextPath, true);
                }
                Directory.CreateDirectory(TextPath);
                StreamWriter Save = new StreamWriter(TextPath + "\\" + "Text.txt", true);
                Save.WriteLine(BookText);
                Save.Close();
                if (!string.IsNullOrEmpty(ImagePath))
                {
                    Image oImage = Image.FromFile(ImagePath);
                    if (oImage == null)
                    {
                        MessageBox.Show("This Image is not Exists !", "Error In Image", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (oImage.Height < 100 || oImage.Width < 100)
                    {
                        MessageBox.Show("Image Size is very small !", "Error Image Size", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (oImage.Height > 2000 || oImage.Width > 2000)
                    {
                        MessageBox.Show("Image Size is very big !", "Error Image Size", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    oImage.Save(TextPath + "\\" + "Image.jpg");
                    oBook.HasImage = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An Unknown error has occurred : \n \n" + ex.Message, "File Error!", MessageBoxButtons.OK);
                return;
            }


            db.Books.Add(oBook);
            db.SaveChanges();

            var resault = MessageBox.Show("The Book Successfuly Added To Library!", "Book Added", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (resault == DialogResult.OK)
            {
                this.Close();
            }
        }
        private void BookDetail_Load(object sender, EventArgs e)
        {
            btnEdit.Enabled     = false;
            btnDelete.Enabled   = false;
            btnUplaodWS.Enabled = false;
            if (authenicatedUser.Role > Domain.Enums.Role.User || Book.UploaderUserId == authenicatedUser.Id)
            {
                btnEdit.Enabled   = true;
                btnDelete.Enabled = true;
            }
            if (authenicatedUser.Role >= Domain.Enums.Role.Administrator || Book.UploaderUserId == authenicatedUser.Id)
            {
                btnUplaodWS.Enabled = true;
            }

            if (Book.IsDeleted)
            {
                if (authenicatedUser.Role < Domain.Enums.Role.Administrator)
                {
                    this.Close();
                }
                else
                {
                    Domain.Entities.User oDeleterUser =
                        db.Users.Find(Book.DeleterUserId);
                    if (oDeleterUser != null)
                    {
                        lblDeletedData.Text = string.Format("This Book Was Deleted By {0} {1} With Username {2}, On {3}", oDeleterUser.FirstName, oDeleterUser.LastName, oDeleterUser.Username, Book.DeleteDate.Value.ToString());
                    }
                    else
                    {
                        lblDeletedData.Text = string.Format("This Book Was Deleted By UnAvailable User On {0}", Book.DeleteDate.Value.ToString());
                    }
                    lblDeletedData.Visible = true;
                    btnRecover.Visible     = true;
                    btnDelete.Text         = "Hard Delete";
                }
            }

            if (Book.IsEdited)
            {
                if (authenicatedUser.Role >= Domain.Enums.Role.Administrator)
                {
                    Domain.Entities.User oEditerUser =
                        db.Users.Find(Book.EditerUserId);
                    if (oEditerUser != null)
                    {
                        lblEdited.Text = string.Format("This Book Was Edited By {0} {1} With Username {2}, On {3}", oEditerUser.FirstName, oEditerUser.LastName, oEditerUser.Username, Book.LastEditDate.Value.ToString());
                    }
                    else
                    {
                        lblEdited.Text = string.Format("This Book Was Edited By UnAvailable User On {0}", Book.LastEditDate.Value.ToString());
                    }
                    lblEdited.Visible = true;
                }
            }

            if (Book.UploadedToWS)
            {
                btnUplaodWS.Text = "Remove Online";
            }

            lblName.Text        = Book.Name;
            lblAuthor.Text      = Book.Author;
            lblTranslator.Text  = Book.Translator;
            lblPublishing.Text  = Book.Publisher;
            lblPublishDate.Text = Book.PublishedDate.ToString();
            lblCir.Text         = Book.Circulation;
            LblPrice.Text       = Book.Price.ToString();
            lblISBN.Text        = Book.ISBN;
            checkOnline.Checked = Book.UploadedToWS;

            picBook.Image = Infrastructure.ImageUtils.GetThumbnailByImagePath("c:\\BookLibraryData\\Book-" + Book.Id.ToString() + "\\" + "Image.jpg", 245, 325);
        }
Example #11
0
 public void setUser(Domain.Entities.User user)
 {
     User = user;
 }