Ejemplo n.º 1
0
        private void button9_Click_1(object sender, EventArgs e)
        {
            System.Web.Configuration.WebConfigurationManager.AppSettings["DBInitType"] = "InitDbNotChanged";
            AjourDbContext context = new AjourDbContext(connectionString);

            foreach (KeyValuePair <string, string> empNewOldEID in XlsLoader.employeeIDs)
            {
                Employee empToChangeEID = context.Employees.Where(emp => emp.EID == empNewOldEID.Key).FirstOrDefault();
                empToChangeEID.EID = empNewOldEID.Value;
                context.SaveChanges();
                if (empNewOldEID.Key != empNewOldEID.Value)
                {
                    if (context.UserProfiles.Where(
                            u => u.UserName == empNewOldEID.Key).FirstOrDefault() != null
                        )
                    {
                        context.UserProfiles.Where(u => u.UserName == empNewOldEID.Key).FirstOrDefault().UserName = empNewOldEID.Value;
                    }
                }
            }
            context.SaveChanges();
            MessageBox.Show("All EIDs are replaced");
        }
Ejemplo n.º 2
0
        private void button7_Click_1(object sender, EventArgs e)
        {
            label6.Text          = "Updating configuration";
            progressBar1.Visible = true;
            progressBar1.Value   = 0;
            this.Refresh();
            ConfigurationManager.RefreshSection("connectionStrings");
            Thread.Sleep(1000);
            AjourDbContext context = new AjourDbContext(connectionString);

            label6.Text        = "Connecting to database";
            progressBar1.Value = 10;
            this.Refresh();
            context.Database.Initialize(true);
            WebSecurity.InitializeDatabaseConnection("AjourBTConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            if (!System.Web.Security.Roles.RoleExists("EMP"))
            {
                System.Web.Security.Roles.CreateRole("EMP");
            }

            if (!System.Web.Security.Roles.RoleExists("PU"))
            {
                System.Web.Security.Roles.CreateRole("PU");
            }

            if (!System.Web.Security.Roles.RoleExists("BTM"))
            {
                System.Web.Security.Roles.CreateRole("BTM");
            }

            if (!System.Web.Security.Roles.RoleExists("ADM"))
            {
                System.Web.Security.Roles.CreateRole("ADM");
            }

            if (!System.Web.Security.Roles.RoleExists("ACC"))
            {
                System.Web.Security.Roles.CreateRole("ACC");
            }

            if (!System.Web.Security.Roles.RoleExists("DIR"))
            {
                System.Web.Security.Roles.CreateRole("DIR");
            }

            if (!System.Web.Security.Roles.RoleExists("VU"))
            {
                System.Web.Security.Roles.CreateRole("VU");
            }

            Dictionary <string, string> employeesPasswords = new Dictionary <string, string>();

            label6.Text        = "Loading departments";
            progressBar1.Value = 20;
            this.Refresh();
            foreach (var dep in XlsLoader.departments)
            {
                repository.SaveDepartment(dep);
            }
            label6.Text        = "Loading positions";
            progressBar1.Value = 30;
            this.Refresh();
            foreach (var pos in XlsLoader.positions)
            {
                repository.SavePosition(pos);
            }
            label6.Text        = "Loading employees";
            progressBar1.Value = 40;
            this.Refresh();
            foreach (var emp in XlsLoader.employees)
            {
                emp.DepartmentID = repository.Departments.Where(d => d.DepartmentName == emp.Department.DepartmentName).Select(d => d.DepartmentID).FirstOrDefault();
                emp.Department   = null;
                emp.PositionID   = repository.Positions.Where(p => p.TitleUk == emp.Position.TitleUk).Select(p => p.PositionID).FirstOrDefault();
                emp.Position     = null;
                repository.SaveEmployee(emp);
                if (!WebSecurity.UserExists(emp.EID))
                {
                    //if (generate)
                    //{
                    //    currentEmployeePassword = Membership.GeneratePassword(8, 1);
                    //    employeesPasswords.Add(emp.EID, currentEmployeePassword);
                    //    WebSecurity.CreateUserAndAccount(emp.EID, currentEmployeePassword);
                    //}
                    //else
                    WebSecurity.CreateUserAndAccount(emp.EID, WebConfigurationManager.AppSettings["DefaultPassword"].ToString());
                }
                foreach (var role in XlsLoader.rolesDict[emp.EID])
                {
                    if (!System.Web.Security.Roles.IsUserInRole(emp.EID.ToString(), role))
                    {
                        System.Web.Security.Roles.AddUserToRole(emp.EID, role);
                    }
                }
            }
            label6.Text        = "Loading locations";
            progressBar1.Value = 50;
            this.Refresh();
            foreach (var loc in XlsLoader.locations)
            {
                repository.SaveLocation(loc);
            }
            label6.Text        = "Loading visas";
            progressBar1.Value = 60;
            this.Refresh();
            foreach (var visa in XlsLoader.visas)
            {
                int EmpID = repository.Employees.Where(emp => emp.EID == visa.VisaOf.EID).Select(emp => emp.EmployeeID).FirstOrDefault();
                visa.EmployeeID = EmpID;
                visa.VisaOf     = null;
                repository.SaveVisa(visa, EmpID);
            }
            label6.Text        = "Loading permits";
            progressBar1.Value = 70;
            this.Refresh();
            foreach (var permit in XlsLoader.permits)
            {
                int EmpID = repository.Employees.Where(emp => emp.EID == permit.PermitOf.EID).Select(emp => emp.EmployeeID).FirstOrDefault();
                permit.EmployeeID = EmpID;
                permit.PermitOf   = null;
                repository.SavePermit(permit, EmpID);
            }
            label6.Text        = "Loading businessTrips";
            progressBar1.Value = 80;
            this.Refresh();
            foreach (var bTrip in XlsLoader.businessTrips.Where(b => repository.Employees.Where(
                                                                    emp => (emp.EID == b.BTof.EID))
                                                                .FirstOrDefault() != null)
                     )
            {
                bTrip.EmployeeID        = repository.Employees.Where(emp => emp.EID == bTrip.BTof.EID).Select(emp => emp.EmployeeID).FirstOrDefault();
                bTrip.BTof              = null;
                bTrip.LocationID        = repository.Locations.Where(l => l.Title == bTrip.Location.Title).Select(l => l.LocationID).FirstOrDefault();
                bTrip.Location          = null;
                bTrip.LastCRUDTimestamp = DateTime.Now.ToLocalTimeAzure();
                repository.SaveBusinessTrip(bTrip);
            }
            string[] roles = new string[] { "" };
            //bool x = Membership.Provider.EnablePasswordRetrieval;
            AjourDbContext dbContext = new AjourDbContext(connectionString);

            foreach (KeyValuePair <string, string> empNewOldEID in XlsLoader.employeeIDs)
            {
                Employee empToChangeEID = repository.Employees.Where(emp => emp.EID == empNewOldEID.Key).FirstOrDefault();
                empToChangeEID.EID = empNewOldEID.Value;
                repository.SaveEmployee(empToChangeEID);
                if (empNewOldEID.Key != empNewOldEID.Value)
                {
                    if (dbContext.UserProfiles.Where(u => u.UserName == empNewOldEID.Key).FirstOrDefault() != null)
                    {
                        dbContext.UserProfiles.Where(u => u.UserName == empNewOldEID.Key).FirstOrDefault().UserName = empNewOldEID.Value;
                    }
                    //if (Membership.GetUser(empNewOldEID.Key) != null)
                    //{
                    //    password = Membership.Provider.GetUser(empNewOldEID.Key, false).GetPassword();
                    //    if (Roles.GetRolesForUser(empNewOldEID.Key).Count() > 0)
                    //    {
                    //        roles = Roles.GetRolesForUser(empNewOldEID.Key);
                    //        Roles.RemoveUserFromRoles(empNewOldEID.Key, Roles.GetRolesForUser(empNewOldEID.Key));
                    //    }
                    //    ((SimpleMembershipProvider)Membership.Provider).DeleteAccount(empNewOldEID.Key); // deletes record from webpages_Membership table
                    //    ((SimpleMembershipProvider)Membership.Provider).DeleteUser(empNewOldEID.Key, true); // deletes record from UserProfile table
                    //    Membership.CreateUser(empNewOldEID.Value, password);
                    //    Roles.AddUserToRoles(empNewOldEID.Value, roles);

                    //}
                }
            }
            dbContext.SaveChanges();
            XlsLoader.businessTrips.Clear();
            XlsLoader.departments.Clear();
            XlsLoader.employeeIDs.Clear();
            XlsLoader.employees.Clear();
            XlsLoader.locations.Clear();
            XlsLoader.rolesDict.Clear();
            XlsLoader.permits.Clear();
            XlsLoader.visas.Clear();
            XlsLoader.positions.Clear();

            //string messagesEIDPasswords = GenerateMessagesMemoryStream(employeesPasswords);
            //if (messagesEIDPasswords.Length != 0)
            //{
            //    return File(System.Text.Encoding.Unicode.GetBytes(messagesEIDPasswords), "text/plain", "Employees.txt");
            //}
            label6.Text        = "Generating text file";
            progressBar1.Value = 90;
            this.Refresh();
            StringBuilder sb = new StringBuilder();

            foreach (Employee emp in context.Employees)
            {
                sb.Append("We notify You, that your login and password for AjourBT system were created");
                sb.Append(System.Environment.NewLine);
                sb.Append(String.Format("Login: {0}, password: {1}", emp.EID, WebConfigurationManager.AppSettings["DefaultPassword"].ToString()));
                sb.Append(System.Environment.NewLine);
                sb.Append(System.Environment.NewLine);
            }
            label6.Text        = "All tasks are completed";
            progressBar1.Value = 100;
            this.Refresh();
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("Employees.txt"))
            {
                file.WriteLine(sb);
            }
            MessageBox.Show("Save completedEmployees logins and initial passwords were saved to Employees.txt");

            uploadStatus = UploadStatus.NoFilesUploaded;
            //context.Database.Connection.Close();
            //context.Dispose();
            //dbContext.Database.Connection.Close();
            //dbContext.Dispose();
            //SqlConnection.ClearAllPools();
            button7.Enabled = false;
        }