public frmUploadMessageBox(string text, string title, List<IDeviceObject> files = null, User uploadUser = null, IDevice goproDevice = null) { SetStyle(ControlStyles.OptimizedDoubleBuffer, true); InitializeComponent(); allfiles = files; _goproDevice = goproDevice; this.Text = title; this.lblMessage.Text = text; this.Icon = Intrensic.Properties.Resources.IntrensicDark; ucUserVideos.IsUpload = true; this.BackgroundImage = Context.GetImageForCustomer(); CheckForVideos(files, uploadUser); //ChangeSize(pnlMain.Width -5, pnlMain.Height); }
private void btnSave_Click(object sender, EventArgs e) { User usr = new User(); if (!ValidateUserInput()) return; using (CodeITDbContext ctx = new CodeITDbContext(Context.UserId)) { using (var transaction = ctx.Database.BeginTransaction()) { if (editUserId != 0) usr = ctx.Users.Where(x => x.Id == editUserId).FirstOrDefault(); if (editUserId == 0) { usr.CreatedBy = Context.UserId; } usr.ModifiedBy = Context.UserId; usr.IdNumber = txtID.Text.Trim(); usr.FirstName = txtFirstName.Text.Trim(); usr.MiddleName = txtMiddleName.Text.Trim(); usr.LastName = txtLastName.Text.Trim(); usr.UserName = txtUserName.Text.Trim(); usr.Password = txtPassword.Text.Trim(); usr.RoleId = getSelectedRole(); usr.CustomerId = Context.CustomerId; usr.DeviceId = txtDeviceId.Tag == null ? string.Empty : txtDeviceId.Tag.ToString(); if (editUserId <= 0) ctx.Users.Add(usr); ctx.SaveChanges(); if (editUserId == 0) { int newUserId = ctx.Users.Where(c=>c.UserName == usr.UserName && c.Password == usr.Password && c.CustomerId == usr.CustomerId).FirstOrDefault().Id; UserLicense usrLic = new UserLicense(); usrLic.LicenseId = ctx.UserLicenses.Where(c => c.UserId == Context.UserId).FirstOrDefault().LicenseId; usrLic.UserId = newUserId; ctx.UserLicenses.Add(usrLic); ctx.SaveChanges(); } if (hasNewImage) { UserPicture userPicture = new UserPicture(); bool isImageInDB = false; if (editUserId != 0) if (ctx.UserPictures.Where(x => x.UserId == editUserId).Count() > 0) { isImageInDB = true; userPicture = ctx.UserPictures.Where(x => x.UserId == editUserId).FirstOrDefault(); } MemoryStream ms = new MemoryStream(); System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Jpeg; userPicture.Picture = ms.ToArray(); userPicture.UserId = usr.Id; if (!isImageInDB) ctx.UserPictures.Add(userPicture); ctx.SaveChanges(); transaction.Commit(); if (isInitailUserFromLogin && usr.RoleId == (int)Role.Administrator) { System.Windows.MessageBox.Show("You have created your first user, please log in with this credentials now." + Environment.NewLine + "UserName: "******" Password: "******"Info", MessageBoxButton.OK, MessageBoxImage.Information); this.Close(); } } else transaction.Commit(); } } if (editUserId <= 0) System.Windows.MessageBox.Show("User created successfully", "Info", MessageBoxButton.OK, MessageBoxImage.Information); else { System.Windows.MessageBox.Show("User updated successfully", "Info", MessageBoxButton.OK, MessageBoxImage.Information); editUserId = 0; } btnClear.PerformClick(); btnClose.PerformClick(); btnSearch.PerformClick(); }
private void UpdateLicenseToDb(string licenseKey) { try { using (CodeITDL.CodeITDbContext ctx = new CodeITDL.CodeITDbContext()) { // Insert customer Guid customerId = new Guid(txtClientId.Text); Customer customer = new Customer(); customer.Id = customerId; customer.Name = txtAdminName.Text; if (ctx.Customers.FirstOrDefault(c => c.Id == customer.Id && c.Name == customer.Name) == null) { ctx.Customers.Add(customer); ctx.SaveChanges(); } else { MessageBox.Show("Failed to create license!", "License failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Insert user User currentUser = new User(); currentUser.FirstName = txtAdminName.Text; currentUser.UserName = txtAdminUsername.Text; currentUser.Password = txtAdminPassword.Text; currentUser.CreatedOn = DateTime.Now; currentUser.ModifiedOn = DateTime.Now; currentUser.CreatedBy = 1; currentUser.ModifiedBy = 1; Int32 newUserId = -1; if (ctx.Users.FirstOrDefault(u => u.UserName == currentUser.UserName && u.Password == currentUser.Password) == null) { ctx.Users.Add(currentUser); ctx.SaveChanges(); } else { MessageBox.Show("User already exist in the database, license creation failed!", "License failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Insert License if (ctx.Users.FirstOrDefault(u => u.UserName == currentUser.UserName && u.Password == currentUser.Password) != null) { newUserId = ctx.Users.FirstOrDefault(u => u.UserName == currentUser.UserName && u.Password == currentUser.Password).Id; } if (newUserId != -1) { CodeITDL.License license = new CodeITDL.License(); license.CustomerId = customerId; license.LicenseBytes = Encoding.UTF8.GetBytes(licenseKey); ctx.SaveChanges(); } Int32 licenseId = -1; if (ctx.Licenses.FirstOrDefault(l => l.CustomerId == customerId) != null) { licenseId = ctx.Licenses.FirstOrDefault(l => l.CustomerId == customerId).Id; } // Inser UserLicense if (licenseId != -1) { UserLicense userLicense = new UserLicense(); userLicense.LicenseId = licenseId; userLicense.UserId = newUserId; ctx.SaveChanges(); } else { MessageBox.Show("Failed to create license!", "License failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } ctx.SaveChanges(); } } catch (Exception ex) { MessageBox.Show("Failed to create license!", "License failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void PerformLogin() { DateTime started = DateTime.Now; btnLogin.Enabled = false; System.Windows.Forms.Application.DoEvents(); var loadingCancalationTokenSource = new CancellationTokenSource(); var loadingCancalationToken = loadingCancalationTokenSource.Token; var loadingTask = new Task(() => { var loadingForm = new frmLoading(); loadingForm.TopMost = true; loadingForm.Show(); while (!loadingCancalationToken.IsCancellationRequested) { Thread.Sleep(50); System.Windows.Forms.Application.DoEvents(); } loadingForm.Close(); }, loadingCancalationTokenSource.Token); loadingTask.Start(); string userName = txtUserName.Text.Trim(); string password = txtPassword.Text; bool hasUsers = false; CodeITDL.CodeITDbContext ctx = null; try { ctx = new CodeITDL.CodeITDbContext(0); if (ctx != null) { CodeITDL.License lic = null; string clientLicense = string.Empty; try { User user = ctx.Users.Where(d => d.UserName == userName && d.Password == password).FirstOrDefault(); lic = ctx.Licenses.Where(s => s.Id == (ctx.UserLicenses.Where(c => c.UserId == (user.Id)).FirstOrDefault()).LicenseId).FirstOrDefault(); } catch(Exception) { } if (lic != null) clientLicense = Encoding.UTF8.GetString(lic.LicenseBytes); if (!CodeITLicence.Licence.ValidateLicenceFromDB(clientLicense)) { System.Windows.Forms.MessageBox.Show("You don't have license. Please contact administrator.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { hasUsers = ctx.Users.Where(x => x.RoleId == (int)Role.Administrator && x.CustomerId == Context.CustomerId).Count() > 0; if (!hasUsers && userName.ToLower().Equals("administrator") && password.Equals("intrensic")) { loadingCancalationTokenSource.Cancel(true); System.Windows.MessageBox.Show("You are logged in as administrator, please add user with administrator role first", "Info", MessageBoxButton.OK, MessageBoxImage.Information); Administration.frmUsers frmUsers = new Administration.frmUsers(); frmUsers.ControlBox = true; frmUsers.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable; frmUsers.Icon = Intrensic.Properties.Resources.Intrensic; frmUsers.Name = "Initial User Creation Screen"; frmUsers.isInitailUserFromLogin = true; frmUsers.ShowDialog(); } else if (!hasUsers) { loadingCancalationTokenSource.Cancel(true); System.Windows.MessageBox.Show("There are no defined users and initial login information is not correct, please try again", "Info", MessageBoxButton.OK, MessageBoxImage.Information); txtUserName.Clear(); txtPassword.Clear(); txtUserName.Focus(); LoginAudit.WriteLoginAudit(CodeITConstants.LOGIN_INCORECT_CREDENTIALS_NO_USERS); } else if (hasUsers) { User usr = new User(); usr = ctx.Users.FirstOrDefault(x => x.UserName.ToLower().Equals(userName.ToLower()) && x.Password.ToLower().Equals(password.ToLower()) && x.CustomerId == Context.CustomerId); if (usr == null) usr = new User(); if (usr.Id <= 0) { loadingCancalationTokenSource.Cancel(true); LoginAudit.WriteLoginAudit(CodeITConstants.LOGIN_INCORECT_CREDENTIALS_HAS_USERS); System.Windows.MessageBox.Show("Username and/or password are not correct. Please try again", "Info", MessageBoxButton.OK, MessageBoxImage.Information); } else { foreach (Form frm in System.Windows.Forms.Application.OpenForms) { if (frm.Name == "frmProgressStatus") { //check if user trying to login is user with current upload progress if (!((frmProgressStatus)frm).getOwnerOfUploadProcess.UserName.ToLower().Equals(userName.ToLower())) { loadingCancalationTokenSource.Cancel(true); LoginAudit.WriteLoginAudit(CodeITConstants.LOGIN_WHILE_UPLOAD_IN_PROGRESS_BY_DIFFERENT_USER, usr.Id); System.Windows.MessageBox.Show("There is an active upload process initiated by user: "******"Please wait for the upload process to complete before you are able to login", "Info", MessageBoxButton.OK, MessageBoxImage.Information); return; } else { ((frmProgressStatus)frm).DisableCancelButtonsOnLogout(false); } } } Context.UserId = usr.Id; } if (Context.UserId > 0) { Context.UserId = usr.Id; LoginAudit.WriteLoginAudit(CodeITConstants.LOGIN_SUCCESSFULL); ContextMenuItems(true); txtUserName.Clear(); txtPassword.Clear(); frmUserMainScreen frmMain = new frmUserMainScreen(); frmMain.InitialGoToUpload = loginStartedFromGoProDevice; frmMain.InitialUploadPath = uploadPath; frmMain.Show(); Context.mainForm = frmMain; this.uploadPath = string.Empty; this.loginStartedFromGoProDevice = false; this.Hide(); //Thread.Sleep(5000); loadingCancalationTokenSource.Cancel(true); Context.CheckForGoProDevice(); } } } } } catch (Exception ex) { // } finally { if (ctx != null) { ctx.Dispose(); } loadingCancalationTokenSource.Cancel(true); btnLogin.Enabled = true; } }
private void CheckForVideos(List<IDeviceObject> _files, User _uploadUser) { string lastFilename = ""; string newfilename = ""; try { if (_files != null && _files.Count > 0) { GoProVideos = new List<FileFromDB>(); for (int i = 0; i < _files.Count; i++) { var item = _files[i]; VideoInfo info = VideoInfo.GetVideoInfo(item.Id); FileFromDB file = new FileFromDB(); FileInfo fileInfo = new FileInfo(item.Id); file.OriginalFileDate = ((Device)_goproDevice).getFileCreationDate(item.Id); file.OriginalFileName = item.Name; file.OriginalFileLocation = item.Name; file.NewFileName = GetFilenameFormat(file.OriginalFileDate, file.OriginalFileName); fileNames.Add(file.NewFileName); file.CreatedOn = DateTime.Now; file.isFromCard = true; file.UserName = item.Name; if (info != null) { file.Duration = (int)info.Duration; file.Resolution = info.Resolution; } GoProVideos.Add(file); } ucUserVideos.lvItemsContainer.ContextMenu.Visibility = System.Windows.Visibility.Hidden; ucUserVideos.lvItemsContainer.ContextMenu.IsOpen = false; ucUserVideos.IsUpload = true; ucUserVideos.lvItemsContainer.ItemsSource = GoProVideos; if (ucUserVideos.itemContainer != null) { ucUserVideos.itemContainer.Width = pnlMain.Width - 21 > 0 ? pnlMain.Width - 21 : pnlMain.Width; } ucUserVideos.ContextMenuClick -= uv_ContextMenuClick; ucUserVideos.ContextMenuClick += uv_ContextMenuClick; } } catch (Exception) { } }