コード例 #1
0
ファイル: Users.cs プロジェクト: petredimov/Intrensic
        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();
        }
コード例 #2
0
ファイル: LicenceForm.cs プロジェクト: petredimov/Intrensic
        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);
            }
        }