public void CreateMany(string userNamePrefix, int usernameSuffix, int teamId, string password, int port, string userGroupName, string userNames, bool disablepwchange, bool pwneverexpires)
        {
            GroupPrincipal group = GroupPrincipal.FindByIdentity(context, userGroupName);

                string[] studentNames = userNames.Replace(Environment.NewLine, "").Split(',').Select(x => x.Trim()).ToArray();
                string usernamePrefix = userNamePrefix.Replace(" ", "");
                string username = usernamePrefix + usernameSuffix;
                string description = "Bruger oprettet med UserHelper";
                string physicalPath = "C:\\inetpub\\wwwroot\\" + username + "\\";
                try
                {
                    for (int i = 0; i < studentNames.Length; i++)
                    {
                        UserPrincipal user = new UserPrincipal(context);
                        UserManagement management = new UserManagement(user, group);
                        //Create Windows User
                        management.CreateLocalWindowsAccount(username, password, username, description, disablepwchange, pwneverexpires, user);
                        management.AddUserToGroup(group, user);
                        //Create IIS Website
                        iis.CreateWebsite(username, "DefaultAppPool", "*:" + port + ":", physicalPath);

                        //Create FTP Virtual Directory
                        //txtStatusMessages.Text += iis.CreateFTPVDir("localhost", username, physicalPath, username);
                        iis.CreateVirtualDirectory("_FTP", username, physicalPath);

                        //create databases
                        sql.CreateSQLLoginUserAndDatabase(username, username, password);

                        Credentials cred = new Credentials();
                        cred.DatabaseUserName = username;
                        cred.DatabasePassword = password;
                        cred.FTPUserName = username;
                        cred.FTPPassword = password;
                        cred.WebsitePort = port;
                        cred.WindowsUserGroupName = group.Name;

                        Student student = new Student();
                        student.Name = studentNames[i];
                        student.Team = db.Teams.Find(teamId);
                        student.Credentials = cred;
                        db.Students.Add(student);

                        //Change username and port for next iteration
                        usernameSuffix++;
                        username = usernamePrefix + usernameSuffix;
                        physicalPath = "C:\\inetpub\\wwwroot\\" + username + "\\";
                        port++;

                    }

                    db.SaveChanges();

                    BatchState.State = UserProcessState.INITIAL;
                    //done
                }
                catch (Exception)
                {
                    throw;
                }
        }
Exemple #2
0
 public void InsertStudent(Student student)
 {
     using (StudentsModel db = new StudentsModel())
     {
         db.Students.Add(student);
         db.SaveChanges();
     }
 }
 public ActionResult Create(Student student)
 {
     ViewBag.StartPort = (db.Credentials.Max(x => x.WebsitePort) + 1).ToString();
     student.Credentials.FTPUserName = student.Credentials.DatabaseUserName;
     student.Credentials.FTPPassword = student.Credentials.DatabasePassword;
     interaction.CreateSingleUser(student.Credentials.DatabaseUserName, student.Credentials.DatabasePassword, student.Credentials.WebsitePort, student.Name, student.TeamId, student.Credentials.WindowsUserGroupName, true, true);
     return RedirectToAction("Index");
 }
        public ActionResult Create()
        {
            Student student = new Student();
            Credentials cred = new Credentials();
            student.Credentials = cred;
            ViewBag.StartPort = (db.Credentials.Max(x => x.WebsitePort)+1).ToString();
            SelectList list = new SelectList(db.Teams.ToList(), "TeamId", "Name");
            ViewBag.Teams = list;

            PrincipalSearchResult<Principal> groups = interaction.GetUserGroups();
            ViewBag.UserGroup = new SelectList(groups.ToList(),"Name","Name");

            return View(student);
        }
Exemple #5
0
        public static MemoryStream CreatePDF(Student[] students)
        {
            try {
            MemoryStream ms = new MemoryStream();
            Document doc = new Document();
            doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
            PdfWriter writer = PdfWriter.GetInstance(doc, ms);

            PdfPTable table = new PdfPTable(7);
            table.TotalWidth = 800f;
            table.DefaultCell.FixedHeight = 110f;
            table.LockedWidth = true;
            float[] widths = { 2f, 2f,2f,2f,2f,2f,2f};
            table.SetWidths(widths);
            table.HorizontalAlignment = 0;

            //PdfPCell cell = new PdfPCell();
            //cell.Colspan = 3;
            //cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            Paragraph para = new Paragraph("Holdliste for: " + students[0].Team.Name);

            foreach (var item in students)
            {

                PdfPCell cell2 = new PdfPCell(new Phrase("Navn"));
                cell2.BackgroundColor = new BaseColor(198, 198, 198);
                PdfPCell cell3 = new PdfPCell(new Phrase("Hold"));
                cell3.BackgroundColor = new BaseColor(198, 198, 198);
                PdfPCell cell4 = new PdfPCell(new Phrase("DB Bruger"));
                cell4.BackgroundColor = new BaseColor(198, 198, 198);
                PdfPCell cell5 = new PdfPCell(new Phrase("DB PW"));
                cell5.BackgroundColor = new BaseColor(198, 198, 198);
                PdfPCell cell6 = new PdfPCell(new Phrase("FTP Bruger"));
                cell6.BackgroundColor = new BaseColor(198, 198, 198);
                PdfPCell cell7 = new PdfPCell(new Phrase("FTP PW"));
                cell7.BackgroundColor = new BaseColor(198, 198, 198);
                PdfPCell cell8 = new PdfPCell(new Phrase("Website Port"));
                cell8.BackgroundColor = new BaseColor(198, 198, 198);

                table.AddCell(cell2);
                table.AddCell(cell3);
                table.AddCell(cell4);
                table.AddCell(cell5);
                table.AddCell(cell6);
                table.AddCell(cell7);
                table.AddCell(cell8);
                table.AddCell(item.Name);
                table.AddCell(item.Team.Name);
                table.AddCell(item.Credentials.DatabaseUserName);
                table.AddCell(item.Credentials.DatabasePassword);
                table.AddCell(item.Credentials.FTPUserName);
                table.AddCell(item.Credentials.FTPPassword);
                table.AddCell(item.Credentials.WebsitePort + "");
            }

            doc.Open();
            doc.Add(para);
            doc.Add(table);
            doc.Close();

            return ms;
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void DeleteStudentAndCredentials(int studentId)
        {
            Student s = new Student { StudentId = studentId };
            try
            {
                s = _userInteraction.DeleteUserWebsiteFTPDatabase(studentId);
                db.SaveChanges();

            }
            catch (Exception)
            {
                _userInteraction.RollbackOnError(BatchState.State, s.Credentials.FTPUserName);
                throw;
            }
        }
        public void CreateSingleUser(string userName, string password, int port, string name, int teamId, string userGroupName, bool disablepwchange, bool pwneverexpires)
        {
            UserPrincipal user = new UserPrincipal(context);
                GroupPrincipal group = GroupPrincipal.FindByIdentity(context, userGroupName);
                Repository rep = new Repository();
                UserManagement management = new UserManagement(user, group);
                string username = userName.Replace(" ", "");
                string description = "Bruger oprettet med UserHelper";
                string physicalPath = "C:\\inetpub\\wwwroot\\" + username + "\\";
                try
                {
                    //Create Windows User
                    management.CreateLocalWindowsAccount(username, password, username, description, disablepwchange, pwneverexpires, user);
                    management.AddUserToGroup(group, user);
                    //Create IIS Website
                    iis.CreateWebsite(username, "DefaultAppPool", "*:" + port + ":", physicalPath);

                    //Create FTP Virtual Directory
                    //txtStatusMessages.Text += iis.CreateFTPVDir("localhost", username, physicalPath, username);
                    iis.CreateVirtualDirectory("_FTP", username, physicalPath);

                    //Create database for user
                    sql.CreateSQLLoginUserAndDatabase(username, username, password);

                    Credentials cred = new Credentials();
                    cred.DatabaseUserName = username;
                    cred.DatabasePassword = password;
                    cred.FTPUserName = username;
                    cred.FTPPassword = password;
                    cred.WebsitePort = port;
                    cred.WindowsUserGroupName = group.Name;
                    Student student = new Student();
                    student.Name = name;
                    sql.InsertUserWithCredentialsOnTeam(student,cred,teamId);

                    BatchState.State = UserProcessState.INITIAL;
                    //done

                }
                catch (Exception)
                {
                    RollbackOnError(BatchState.State, username);
                    throw;
                }
        }
Exemple #8
0
 public void InsertUserWithCredentialsOnTeam(Student student, Credentials cred, int teamId)
 {
     try
     {
         using (StudentsModel ctx = new StudentsModel())
         {
             student.Credentials = cred;
             student.TeamId = teamId;
             ctx.Students.Add(student);
             ctx.SaveChanges();
             BatchState.State = UserProcessState.SQL_INSERT_USER_DATA_OK;
         }
     }
     catch (Exception)
     {
         BatchState.State = UserProcessState.SQL_INSERT_USER_DATA_ERROR;
         throw;
     }
 }
 public ActionResult Edit(Student student)
 {
     Student s = db.Students.Find(student.TeamId);
     if (TryUpdateModel<Student>(student))
     {
         try
         {
             db.Entry(student).State = System.Data.Entity.EntityState.Modified;
             db.SaveChanges();
         }
         catch (Exception ex)
         {
             ModelState.AddModelError("", "Error updating model " + ex.Message);
         }
     }
     return RedirectToAction("Index");
 }