Step 3.2. Controller

Step 3.2. Controller

Skip and go to the next step

On this step, we will create a new controller and define 3 main actions in it.

Now, for these 3 actions we will implement basic definition but we will 'extend' it later, while going through the tutorial:

  • Index() - checks whether a user is authorized and if the check result is positive, redirects to the appropriate view (manager or employee).
  • Manager() - redirects to the view intended for managers.
  • Employee() - redirects to the view intended for employees.

In all the actions defined on this and further steps (except Index()), we will do 'rights checking' to protect the app from unauthorized access in case a user skips the login page and loads our server-side login script URL directly in the browser. To not repeat one and the same code several times, on this step we'll also specify a function (RoleIs) that will implement such checking functionality and later, when we will need to check rights, we'll just call this function.

Also, for correct handling the application we should do some corrections in the files generated by default by Visual Studio:

  • Global.aspx
  • AccountController.cs

These files refer to the default Home.cs controller within themselves. We should change this default value to the name of our new controller.

Your actions:

  1. Create a Controller file and name it SystemController.cs.
  2. Delete the existing code from the opened file and put into it the code below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Security;
     
    using DHTMLX.Common;
    using DHTMLX.Scheduler;
    using DHTMLX.Scheduler.Authentication;
    using DHTMLX.Scheduler.Controls;
    using DHTMLX.Scheduler.Data;
     
    using TaskManagementSystem.Models;
    namespace TaskManagementSystem.Controllers
    {
      public class SystemController : Controller {
            public ActionResult Index()
            {
     
                if (!this.Request.IsAuthenticated)
                {
                    return RedirectToAction("LogOn", "Account");// the 'LogOn' action is defined in the 'AccountController' generated automatically.
                }
                else
                {
                    if (User.IsInRole("Manager"))
                    {
                        return RedirectToAction("Manager", "System");
                    }
                    else
                    {
                        return RedirectToAction("Employee", "System");
                    }
                }
            }
            public ActionResult Manager() {
                return View();
            }
     
     
            public ActionResult Employee() {
                return View();
            }
     
            protected bool RoleIs(string role) {
                if (!Request.IsAuthenticated || !(User.IsInRole(role)))
                {
                    return false;
                }
                return true;
            }
        }
    }
  3. Move to Solution Explorer→Controllers and open the AccountController.cs file.
  4. In the opened file check all over the code and rewrite the following string:

    RedirectToAction("Index", "Home");
    //to the string
    RedirectToAction("Index", "System");
  5. Move to Solution Explorer→Global.asax and open the Global.asax.cs file.
  6. In the opened file rewrite the default map route as follows:

    routes.MapRoute(
                    "Default", // the route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "System", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );




comments powered by Disqus