Ejemplo n.º 1
0
        public static void UserToView(Controller controller)
        {
            User user = AuthenticationHandler.CurrentUser(controller.HttpContext.Session);

            controller.ViewBag.User     = user;
            controller.ViewBag.SignedIn = user.RoleID != 0;
        }
Ejemplo n.º 2
0
        public static void UserToView(Controller controller, ref bool SignedIn, ref User CurrentUser)
        {
            CurrentUser             = AuthenticationHandler.CurrentUser(controller.HttpContext.Session);
            controller.ViewBag.User = CurrentUser;

            SignedIn = CurrentUser.RoleID != 0;
            controller.ViewBag.SignedIn = SignedIn;
        }
        public void ReturnsAnnonymousUserIfNull()
        {
            Mock <ISession> session = new Mock <ISession>();

            session.Setup(s => s.GetObject <User>("USER")).Returns(delegate() { return(null); });

            User user = AuthenticationHandler.CurrentUser(session.Object);

            Assert.Equal(new User {
                Username = "", Password = "", RoleID = 0
            }, user);
        }
Ejemplo n.º 4
0
 public IActionResult Add()
 {
     if (!AuthorizationHandler.IsSignedIn(HttpContext.Session))
     {
         return(RedirectToAction("Index", "Home"));
     }
     Utilities.UserToView(this);
     ViewBag.Action = "Add";
     return(View("Edit", new Recipe()
     {
         Username = AuthenticationHandler.CurrentUser(HttpContext.Session).Username
     }));
 }
Ejemplo n.º 5
0
        public IActionResult Edit(int id)
        {
            if (!AuthorizationHandler.IsSignedIn(HttpContext.Session))
            {
                return(RedirectToAction("Index", "Home"));
            }
            var recipe = context.Recipes.Find(id);

            if (AuthenticationHandler.CurrentUser(HttpContext.Session).Username != recipe.Username)
            {
                return(RedirectToAction("Index", "Home"));
            }

            Utilities.UserToView(this);
            ViewBag.Action = "Edit";
            return(View(recipe));
        }
Ejemplo n.º 6
0
 public IActionResult Edit(Recipe recipe)
 {
     Utilities.UserToView(this);
     if (ModelState.IsValid)
     {
         if (!AuthorizationHandler.IsSignedIn(HttpContext.Session))
         {
             return(RedirectToAction("Index", "Home"));
         }
         if (AuthenticationHandler.CurrentUser(HttpContext.Session).Username != recipe.Username)
         {
             return(RedirectToAction("Index", "Home"));
         }
         int ID = recipe.RecipeId;
         if (recipe.RecipeId == 0)
         {
             context.Recipes.Add(recipe);
             context.SaveChanges();
             ID = context.Recipes.OrderByDescending(r => r.RecipeId).FirstOrDefault().RecipeId;
         }
         else
         {
             context.Recipes.Update(recipe);
             context.SaveChanges();
         }
         //check if file has been uploaded
         if (recipe.File != null)
         {
             string FilePath = Directory.GetCurrentDirectory() + "/wwwroot/RecipeImages";
             string name     = $"/{ID}{Path.GetExtension(recipe.File.FileName)}";
             //create and dispose of file stream from memory. Must be set to a file name and not a folder
             using (Stream str = new FileStream(FilePath + name, FileMode.Create))
             {
                 //copy file from ram into storage
                 recipe.File.CopyTo(str);
             }
         }
         return(RedirectToAction("Index", "Home"));
     }
     else
     {
         ViewBag.Action = (recipe.RecipeId == 0) ? "Add" : "Edit";
         return(View(recipe));
     }
 }