Exemplo n.º 1
0
        public async Task<ActionResult> Import()
        {

            ArrayList newUsers = new ArrayList();
            //newUsers.Add("hello");
            // Filter Regex
            string regex = "^[a-z,A-Z,0-9]*@uwec.edu$";
            Regex r = new Regex(regex);
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.GetFullPath(Server.MapPath("~"));
                    path = Path.Combine(path , fileName); //H:\tfkt5\Mélodie\excel\
                    file.SaveAs(path);
                    foreach (var worksheet in Workbook.Worksheets(path))
                    {
                        foreach (var row in worksheet.Rows)
                        {
                            foreach (var cell in row.Cells)
                            {
                                if (cell != null)
                                {
                                    Match m = r.Match(cell.Text);
                                    if (m.Success)
                                    {
                                        newUsers.Add(cell.Text);
                                    }
                                }

                            }
                        }
                    }

                }
            }

            for (int x = 0; x < newUsers.Count; x++)
            {
                Users user = new Users();

                user.email = (String)newUsers[x];
                user.role_id = "Student";
                // generate a random password for the user
                AccountController ac = new AccountController();
                string password = GenerateRandomPassword(8);
                RegisterViewModel model = new RegisterViewModel();
                model.UserName = user.email.Substring(0, user.email.IndexOf('@'));
                model.Email = user.email;
                model.role_id = user.role_id;
                // notify the user that an account has been created in their name
                await ac.Register(model);
            }

            //stuff to make the computer stop yelling at me

            return RedirectToAction("Index");

        }
Exemplo n.º 2
0
        public async Task<ActionResult> Create([Bind(Include = "ID,username,email,role_id")] Users users, RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {

                db.Users.Add(users);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(users);
        }