Beispiel #1
0
        public ActionResult GenerateUsers()
        {
            logger.Debug("User " + WebSecurity.GetUserId(User.Identity.Name) +
                         " \"" + User.Identity.Name + "\" visited AccountsManagement/Membership/GenerateUsers");

            var model = new viewModels.GenerateUsersViewModel();

            return(View(model));
        }
Beispiel #2
0
        public ActionResult GenerateUsers(viewModels.GenerateUsersViewModel Model)
        {
            if (ModelState.IsValid)
            {
                if (Model.Titles != null && Model.Titles.ContentLength > 0)
                {
                    try
                    {
                        // Extract only the fielname
                        var fileName = Path.GetFileName(Model.Titles.FileName);

                        // Store the file inside ~/App_Data/uploads folder
                        var path = Path.Combine(LocalPath.RootDirectory, fileName);

                        Model.Titles.SaveAs(path);

                        List <viewModels.Account> accounts = new List <viewModels.Account>();

                        using (StreamReader sr = new StreamReaderExtended(path, true))
                        {
                            int         i = 1;
                            string[]    fullname;
                            string      firstName  = "";
                            string      secondName = "";
                            string      thirdName  = "";
                            string      userName   = "";
                            string      password   = "";
                            UserProfile user;
                            while (!sr.EndOfStream)
                            {
                                firstName  = "";
                                secondName = "";
                                thirdName  = "";

                                fullname = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                                if (fullname.Count() >= 1)
                                {
                                    secondName = fullname[0];
                                }
                                if (fullname.Count() >= 2)
                                {
                                    firstName = fullname[1];
                                }
                                if (fullname.Count() >= 3)
                                {
                                    thirdName = fullname[2];
                                }

                                do
                                {
                                    userName = Model.UserNameTemplate + i.ToString();
                                    i++;
                                    user = repository.Users.FirstOrDefault(u => u.UserName == userName);
                                }while (user != null);

                                password = password.RandomString(7);

                                accounts.Add(new viewModels.Account()
                                {
                                    UserName   = userName,
                                    SecondName = secondName,
                                    FirstName  = firstName,
                                    ThirdName  = thirdName,
                                    Password   = password
                                });
                            }
                        }

                        if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }

                        List <SelectListItem> tournaments = new List <SelectListItem>();
                        tournaments.Add(new SelectListItem()
                        {
                            Value = "0", Text = "Не регистрировать"
                        });
                        repository.Tournaments
                        .Where(t => t.EndDate > DateTime.Now)
                        .Each(t => tournaments.Add(new SelectListItem()
                        {
                            Value = t.TournamentID.ToString(), Text = t.Name
                        }));

                        return(View("ConfirmGenerateUsers",
                                    new GeneratedUsersListViewModel()
                        {
                            Accounts = accounts,
                            AccountTemplate = new viewModels.Account(),
                            TournamentList = tournaments,
                            Generated = false
                        }));
                    }
                    catch (Exception ex)
                    {
                        logger.Error("Error occurred on user " + WebSecurity.GetUserId(User.Identity.Name) +
                                     " \"" + User.Identity.Name + "\" attempt generating accounts: ", ex);

                        ModelState.AddModelError("", "Произошла ошибка при генерации аккаунтов");
                    }
                }
            }

            return(View(Model));
        }
        public ActionResult GenerateUsers()
        {
            logger.Debug("User " + WebSecurity.GetUserId(User.Identity.Name) +
                " \"" + User.Identity.Name + "\" visited AccountsManagement/Membership/GenerateUsers");

            var model = new viewModels.GenerateUsersViewModel();

            return View(model);
        }