예제 #1
0
        public ActionResult Create(viewModels.CreateCityViewModel Model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    Country country = repository
                        .Country
                        .FirstOrDefault(c => c.CountryID == Model.CountryID);
                    if (country == null)
                    {
                        ModelState.AddModelError("Country", "Страна не существует в базе");
                        return View(Model);
                    }

                    City city = repository
                        .City
                        .FirstOrDefault(c => c.Name == Model.City && c.Area == Model.Area && c.Region == Model.Region && c.CountryID == country.CountryID);
                    if (city != null)
                    {
                        ModelState.AddModelError("City", "Город (населенный пункт) с таким именем уже существует");
                        return View(Model);
                    }

                    city = new City()
                        {
                            Name = Model.City,
                            Area = Model.Area,
                            Region = Model.Region,
                            CountryID = country.CountryID
                        };

                    int userID = WebSecurity.CurrentUserId;
                    if (userID != 1)
                    {
                        UserProfile user = repository.Users.FirstOrDefault(u => u.UserId == userID);
                        //institution.UsersCanModify = new List<UserProfile>();
                        //institution.UsersCanModify.Add(user);
                    }
                    int cityID = repository.AddCity(city);

                    logger.Info("User " + WebSecurity.GetUserId(User.Identity.Name) + " \"" + User.Identity.Name +
                        "\" create city \"" + cityID + "\"");
                    TempData["SuccessMessage"] = "Город (населенный пункт) успешно создан!";
                    return RedirectToAction("Create");
                }
                catch (MembershipCreateUserException ex)
                {
                    logger.Warn("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) +
                        " \"" + User.Identity.Name + "\" city creating: ", ex);
                    TempData["ErrorMessage"] = "Произошла ошибка при создании города (населенного пункта)";
                }
            }

            return View(Model);
        }
예제 #2
0
        public ActionResult Update(viewModels.EditCityViewModel Model, int CityID = -1, string Update = null, string Delete = null, string Cancel = null)
        {
            if (CityID == -1)
            {
                logger.Warn("City with id = " + CityID + " not found");
                throw new HttpException(404, "City not found");
            }

            if (Delete != null)
                return DeleteCity(CityID);
            if (Cancel != null)
                return CancelCity();
            if (Update != null)
                return UpdateCity(Model, CityID);

            return CancelCity();
        }
예제 #3
0
        private ActionResult UpdateCity(viewModels.EditCityViewModel Model, int CityID = -1)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    Country country = repository
                        .Country
                        .FirstOrDefault(c => c.CountryID == Model.CountryID);
                    if (country == null)
                    {
                        ModelState.AddModelError("Country", "Страна не существует в базе");
                        return View(Model);
                    }

                    City city = repository
                        .City
                        .FirstOrDefault(c => c.Name == Model.City && c.Area == Model.Area && c.Region == Model.Region && c.CityID != CityID);
                    if (city != null)
                    {
                        ModelState.AddModelError("City", "Город (населенный пункт) с таким именем уже существует");
                        return View(Model);
                    }

                    city = repository
                        .City
                        .FirstOrDefault(c => c.CityID == Model.CityID);
                    if (city == null)
                    {
                        TempData["ErrorMessage"] = "Произошла ошибка при обновлении города (населенного пункта)";
                        logger.Warn("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) +
                            " \"" + User.Identity.Name + "\" city updating: city with id " + Model.CityID + " not exist");
                        return View(Model);
                    }

                    city.Name = Model.City;
                    city.Area = Model.Area;
                    city.Region = Model.Region;
                    city.CountryID = country.CountryID;

                    repository.AddCity(city);

                    logger.Info("User " + WebSecurity.GetUserId(User.Identity.Name) +
                        " \"" + User.Identity.Name + "\" update city \"" + Model.CityID + "\"");
                    TempData["SuccessMessage"] = "Город (населенный пункт) успешно обновлен!";
                    return RedirectToAction("Update", new { CityID = Model.CityID });
                }
                catch (MembershipCreateUserException ex)
                {
                    logger.Warn("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) +
                        " \"" + User.Identity.Name + "\" city updating: ", ex);
                    TempData["ErrorMessage"] = "Произошла ошибка при обновлении города (населенного пункта)";
                }
            }

            return RedirectToAction("Update", new { CityID = CityID });
        }
예제 #4
0
        public ActionResult Create(viewModels.CreateInstitutionViewModel Model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    Country country = repository
                        .Country
                        .FirstOrDefault(c => c.CountryID == Model.CountryID);
                    if (country == null)
                    {
                        ModelState.AddModelError("Country", "Страна не существует в базе");
                        return View(Model);
                    }

                    City city = repository
                        .City
                        .FirstOrDefault(c => c.CityID == Model.CityID && c.CountryID == Model.CountryID);
                    if (city == null)
                    {
                        ModelState.AddModelError("City", "Город не существует в базе");
                        return View(Model);
                    }

                    Institution institution = repository
                        .Institutions
                        .FirstOrDefault(i => i.Name == Model.Institution && i.CityID == city.CityID);
                    if (institution != null)
                    {
                        ModelState.AddModelError("Institution", "Образовательное учреждение с таким именем уже существует");
                        return View(Model);
                    }

                    institution = new Institution()
                        {
                            Name = Model.Institution,
                            CityID = city.CityID
                        };

                    int userID = WebSecurity.CurrentUserId;
                    if (userID != 1)
                    {
                        UserProfile user = repository.Users.FirstOrDefault(u => u.UserId == userID);
                        institution.UsersCanModify = new List<UserProfile>();
                        institution.UsersCanModify.Add(user);
                    }
                    int institutionID = repository.AddInstitution(institution);

                    logger.Info("User " + WebSecurity.GetUserId(User.Identity.Name) + " \"" + User.Identity.Name +
                        "\" create institution \"" + institutionID + "\"");
                    TempData["SuccessMessage"] = "Образовательное учреждение (организация) успешно создано(а)!";
                    return RedirectToAction("Create");
                }
                catch (MembershipCreateUserException ex)
                {
                    logger.Warn("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) +
                        " \"" + User.Identity.Name + "\" institution creating: ", ex);
                    TempData["ErrorMessage"] = "Произошла ошибка при создании образовательного учреждения (организации)";
                }
            }

            return View(Model);
        }
예제 #5
0
        private ActionResult UpdateInstitution(viewModels.EditInstitutionViewModel Model, int InstitutionID = -1)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    Country country = repository
                        .Country
                        .FirstOrDefault(c => c.CountryID == Model.CountryID);
                    if (country == null)
                    {
                        ModelState.AddModelError("Country", "Страна не существует в базе");
                        return View(Model);
                    }

                    City city = repository
                        .City
                        .FirstOrDefault(c => c.CityID == Model.CityID);
                    if (city == null)
                    {
                        ModelState.AddModelError("City", "Город не существует в базе");
                        return View(Model);
                    }

                    Institution institution = repository
                        .Institutions
                        .FirstOrDefault(i => i.Name == Model.Institution && i.CityID == city.CityID && i.InstitutionID != Model.InstitutionID);
                    if (institution != null)
                    {
                        ModelState.AddModelError("Institution", "Образовательное учреждение с таким именем уже существует");
                        return View(Model);
                    }

                    institution = repository
                        .Institutions
                        .FirstOrDefault(i => i.InstitutionID == Model.InstitutionID);
                    if (institution == null)
                    {
                        TempData["ErrorMessage"] = "Произошла ошибка при обновлении образовательного учреждения (организации)";
                        logger.Warn("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) +
                            " \"" + User.Identity.Name + "\" institution updating: institution with id " + Model.InstitutionID + " not exist");
                        return View(Model);
                    }

                    institution.Name = Model.Institution;
                    institution.CityID = city.CityID;

                    repository.AddInstitution(institution);

                    logger.Info("User " + WebSecurity.GetUserId(User.Identity.Name) +
                        " \"" + User.Identity.Name + "\" update institution \"" + Model.InstitutionID + "\"");
                    TempData["SuccessMessage"] = "Образовательное учреждение (организация) успешно обновлено(а)!";
                    return RedirectToAction("Update", new { InstitutionID = Model.InstitutionID });
                }
                catch (MembershipCreateUserException ex)
                {
                    logger.Warn("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) +
                        " \"" + User.Identity.Name + "\" institution updating: ", ex);
                    TempData["ErrorMessage"] = "Произошла ошибка при обновлении образовательного учреждения (организации)";
                }
            }

            return RedirectToAction("Update", new { InstitutionID = InstitutionID });
        }
예제 #6
0
        public ActionResult Update(viewModels.EditInstitutionViewModel Model, int InstitutionID = -1, string Update = null, string Delete = null, string Cancel = null)
        {
            if (InstitutionID == -1)
            {
                logger.Warn("Institution with id = " + InstitutionID + " not found");
                throw new HttpException(404, "Institution not found");
            }

            if (Delete != null)
                return DeleteInstitution(InstitutionID);
            if (Cancel != null)
                return CancelInstitution();
            if (Update != null)
                return UpdateInstitution(Model, InstitutionID);

            return CancelInstitution();
        }
예제 #7
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);
        }
예제 #8
0
        public ActionResult CreateUser(viewModels.RegisterViewModel Model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    string confirmationToken = null;
                    if (Model.Approve)
                    {
                        confirmationToken = WebSecurity.CreateUserAndAccount(
                            Model.UserName, Model.Password, new { Email = Model.Email, CreatedByUserID = WebSecurity.CurrentUserId }, true);

                        userMailer.RegisterConfirmation(Model.Email, Model.UserName, confirmationToken).Send();
                    }
                    else
                    {
                        WebSecurity.CreateUserAndAccount(Model.UserName, Model.Password, new { Email = Model.Email, CreatedByUserID = WebSecurity.CurrentUserId });
                    }

                    Roles.AddUserToRole(Model.UserName, "User");

                    logger.Info("User " + WebSecurity.GetUserId(User.Identity.Name) +
                        " \"" + User.Identity.Name + "\" created new account \"" + Model.UserName + "\"");

                    return RedirectToAction("GrantRolesToUsers", new { UserID = WebSecurity.GetUserId(Model.UserName) });
                }
                catch (MembershipCreateUserException ex)
                {
                    logger.Warn("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) +
                        " \"" + User.Identity.Name + "\" account creating: \"" + ErrorCodeToString(ex.StatusCode) + "\"");
                    ModelState.AddModelError("", ErrorCodeToString(ex.StatusCode));
                }
            }

            return View(Model);
        }