Example #1
0
        public async Task <IActionResult> Index()
        {
            CampPlannerUser user = await _userManager.FindByNameAsync(User.GetUserName());

            var camps = Mapper.Map <IEnumerable <CampViewModel> >(_repository.GetAllCamps(user));

            return(View(camps));
        }
Example #2
0
 //TODO summary
 //TODO Correct user handling
 internal bool CanAccess(CampPlannerUser user)
 {
     if (Owner.Id == user.Id)
     {
         return(true);
     }
     return(false);
 }
Example #3
0
        public async Task <IActionResult> Manage(int id)
        {
            CampPlannerUser user = await _userManager.FindByNameAsync(User.GetUserName());

            var camp = _repository.GetCamp(id);

            //TODO correct authorisation
            if (camp.CanAccess(user))
            {
                return(View(Mapper.Map <CampViewModel>(camp)));
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }
Example #4
0
 public IEnumerable <Camp> GetAllCamps(CampPlannerUser user)
 {
     try
     {
         _logger.LogInformation("Getting camps for user {userName} from database");
         var camps = _context.Camps
                     //c.Owner.Id == user.Id (because states can change)
                     .Where(c => c.Owner.Id == user.Id)
                     .OrderBy(t => t.Name)
                     .ToList();
         //return camps.Where(c => c.Owner == user).ToList();
         return(camps.ToList());
     }
     catch (Exception ex)
     {
         _logger.LogError("Could not get camps from database", ex);
         return(null);
     }
 }
        public async Task EnsureSeedDataAsync()
        {
            if ((await _userManager.FindByEmailAsync("*****@*****.**")) == null)
            {
                //Add the user
                var newUSer = new CampPlannerUser()
                {
                    UserName = "******",
                    Email    = "*****@*****.**"
                };
                await _userManager.CreateAsync(newUSer, "Passw0rd!");
            }

            if (!_context.Camps.Any())
            {
                CampPlannerUser cu = await _userManager.FindByEmailAsync("*****@*****.**");

                Camp seedCamp = new Camp
                {
                    Name      = "SeedCamp",
                    StartDate = DateTime.Now.AddDays(6),
                    EndDate   = DateTime.Now.AddDays(20)
                };
                seedCamp.Owner = cu;
                _context.Camps.Add(seedCamp);

                Camp seedCamp2 = new Camp
                {
                    Name      = "SeedCamp2",
                    StartDate = DateTime.Now.AddDays(60),
                    EndDate   = DateTime.Now.AddDays(74)
                };
                seedCamp2.Owner = cu;
                _context.Camps.Add(seedCamp2);



                _context.SaveChanges();
            }
        }
Example #6
0
        // TODO [ValidateAntiForgeryToken]
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new CampPlannerUser {
                    UserName = model.Username, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //TODO mail confirmation
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation("User created a new account with password.");
                    return(RedirectToAction("Index", "Camp"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }