public async Task <IViewComponentResult> InvokeAsync()
        {
            bool isAuthenticated = false;
            bool isAdmin         = false;
            var  role            = "Passenger";

            if (User.Identity.IsAuthenticated)
            {
                isAuthenticated = true;

                if (UserClaimsPrincipal.HasClaim("AccessLevel", "Admin"))
                {
                    isAdmin = true;
                    role    = "Admin";
                }
            }
            var thisUser = new NavBarObject
            {
                IsAuthenticated = isAuthenticated,
                IsAdmin         = isAdmin,
                Name            = UserClaimsPrincipal.Identity.Name,
                Role            = role
            };

            return(View(thisUser));
        }
Exemple #2
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            bool isAuthenticated = false;
            bool isAdmin         = false;
            var  role            = "Passenger";

            if (User.Identity.IsAuthenticated)
            {
                isAuthenticated = true;

                if (UserClaimsPrincipal.HasClaim("AccessLevel", "Admin"))
                {
                    isAdmin = true;
                    role    = "Admin";
                }
            }

            var contactInfo = new FooterObject
            {
                Email           = _config.GetSection("ContactUs").GetValue <string>("Email"),
                ContactNumber   = _config.GetSection("ContactUs").GetValue <string>("ContactNumber"),
                IsAuthenticated = isAuthenticated,
                IsAdmin         = isAdmin,
                Name            = UserClaimsPrincipal.Identity.Name,
                Role            = role
            };

            return(View(contactInfo));
        }
Exemple #3
0
        /// <summary>
        /// Check if user can have access to the view component with his permissions.
        /// </summary>
        /// <returns><c>true</c>, if view component rights was valided, <c>false</c> otherwise.</returns>
        public bool ValidViewComponentRights()
        {
            string[] claims = null;
            string[] roles  = null;

            IEnumerable <ViewComponentAuthorizeAttribute> attributes = this.GetType().GetCustomAttributes <ViewComponentAuthorizeAttribute>();

            if (!attributes.Any())
            {
                return(true);
            }

            foreach (ViewComponentAuthorizeAttribute attribute in attributes)
            {
                // Get claims
                claims = attribute.GetClaims();

                if (claims != null)
                {
                    foreach (string claim in claims)
                    {
                        if (UserClaimsPrincipal.HasClaim(c => c.Value == claim))
                        {
                            return(true);
                        }
                    }
                }

                // Get roles
                if (roles != null)
                {
                    foreach (string role in roles)
                    {
                        if (User.IsInRole(role))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemple #4
0
        /// <summary>
        /// Has the required claim.
        /// </summary>
        /// <returns><c>true</c>, if required claim was hased, <c>false</c> otherwise.</returns>
        public bool HasRequiredClaim()
        {
            int requiredPermissionId = 0;

            if (_requiredClaims == null)
            {
                return(false);
            }

            foreach (PermissionInfo permission in _requiredClaims)
            {
                if (permission.Name == ModuleConfiguration.GrantedAccessPermission ||
                    (UserClaimsPrincipal.HasClaim(v => v.Type == ModuleConfiguration.ModulePermissionType &&
                                                  int.TryParse(v.Value, out requiredPermissionId) &&
                                                  requiredPermissionId == permission.PermissionId)))
                {
                    return(true);
                }
            }

            return(false);
        }