// GET: Boat
        public async Task <IActionResult> Index()
        {
            var result = await _boatService.GetAll();

            // User is fully authorized to all content if he is a manager or admin
            var isFullyAuthorized =
                User.IsInRole(RoleName.Administrator) ||
                User.IsInRole(RoleName.Manager);

            // If he is an admin or manager indeed
            if (isFullyAuthorized)
            {
                // Return a view with all the resources displayed
                return(View(result));
            }

            // If user is only a boat owner instead
            if (User.IsInRole(RoleName.BoatOwner))
            {
                // Get the logged in user's related boat owner object
                var loggedPerson = await _userService.GetUserAsync(User);

                var boatOwner = _userService.GetBoatOwnerFromPerson(loggedPerson);

                // Filter results so that he only sees his boats rather than all of them
                result = result.Where(boat => boat.BoatOwnerId == boatOwner.BoatOwnerId);

                // Return a view that only displays that boat owner's boats
                return(View(result));
            }

            // Forbid access to the page if user is none of the roles of a boat owner, manager or admin
            return(Forbid());
        }
示例#2
0
        public async Task <IActionResult> Index()
        {
            // User is fully authorized to all content if he is a manager or admin
            var isFullyAuthorized =
                User.IsInRole(RoleName.Administrator) ||
                User.IsInRole(RoleName.Manager);

            // If he is an admin or manager
            if (isFullyAuthorized)
            {
                // Get all the boats in the system as choices of booking
                var boats = await _boatService.GetAll();

                ViewBag.Boat = new SelectList(boats, "BoatId", "Name");
                // Needed for user prompt when deciding to change important values in the booking
                ViewBag.SessionBooking = HttpContext.Session.Get <Booking>("Booking");
                var booking = await _bookingService.CreateEmptyBooking();

                return(View(booking));
            }

            // If user is only a boat owner instead
            if (User.IsInRole(RoleName.BoatOwner))
            {
                // Get the logged in user's related boat owner object
                var loggedPerson = await _userService.GetUserAsync(User);

                var boatOwner = _userService.GetBoatOwnerFromPerson(loggedPerson);

                // Filter results so that he only gets his boats rather than all of them
                var boats = (await _boatService.GetAll()).Where(boat => boat.BoatOwnerId == boatOwner.BoatOwnerId);

                ViewBag.Boat = new SelectList(boats, "BoatId", "Name");
                // Needed for user prompt when deciding to change important values in the booking
                ViewBag.SessionBooking = HttpContext.Session.Get <Booking>("Booking");
                var booking = await _bookingService.CreateEmptyBooking();

                return(View(booking));
            }

            return(Forbid());
        }