Ejemplo n.º 1
0
        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;
            }
        }