/// <summary>
        /// Called by UI to assign permissions
        /// </summary>
        /// <param name="partOfValue"></param>
        /// <param name="claimType"></param>
        /// <returns></returns>
        public override IEnumerable <SecurityEntity> Search(string partOfValue, string claimType)
        {
            switch (claimType)
            {
            case ClaimTypes.Email:
                return(_extendedUserProvider.FindUsersByEmail(partOfValue, 1, 100, out var total)
                       .Select(x => new SecurityEntity(x.Username, SecurityEntityType.User)));

            case ClaimTypes.Name:
                return(_synchedProvider.Search(partOfValue, claimType));

            case ClaimTypes.Role:
            default:
                var hashSet       = new HashSet <SecurityEntity>(_synchedProvider.Search(partOfValue, claimType), new SecurityNameComparer());
                var extendedRoles = _extendedRoleProvider.GetAllRoles();

                if (!string.IsNullOrWhiteSpace(partOfValue))
                {
                    extendedRoles = extendedRoles.Where(r => r.Name.Contains(partOfValue));
                }

                foreach (var manual in extendedRoles)
                {
                    hashSet.Add(new SecurityEntity(manual.Name, SecurityEntityType.Role));
                }

                return(hashSet.OrderBy(x => x.Name).ToList());
            }
        }
Exemplo n.º 2
0
        public ActionResult Index(SecurityPage currentPage)
        {
            currentPage.SecuritySystem = new SecurityPage.System();
            currentPage.SecurityUser   = new SecurityPage.User();
            // get current user security information
            var principal = PrincipalInfo.CurrentPrincipal;

            if (principal is RolePrincipal) // ASP.NET Membership
            {
                currentPage.SecuritySystem.Provider = "ASP.NET Membership";
            }
            else if (principal is ClaimsPrincipal) // ASP.NET Identity
            {
                currentPage.SecuritySystem.Provider = "ASP.NET Identity";
            }
            currentPage.SecurityUser.Claims =
                (principal.Identity as ClaimsIdentity).Claims.ToArray();
            currentPage.SecurityUser.Roles = currentPage.SecurityUser
                                             .Claims.Where(c => c.Type ==
                                                           "http://schemas.microsoft.com/ws/2008/06/identity/claims/role")
                                             .Select(c => c.Value).ToArray();
            currentPage.SecurityUser.Name        = principal.Identity.Name;
            currentPage.SecurityUser.IsAnonymous =
                !principal.Identity.IsAuthenticated;
            currentPage.SecurityUser.HasAccessToPlugins =
                PrincipalInfo.Current.HasPathAccess("Views/Plugins");
            currentPage.SecurityUser.IsAdministrator =
                principal.IsInRole("CmsAdmins");
            currentPage.SecurityUser.IsEditor = principal.IsInRole("CmsEditors");
            // or use following that check access to paths /admins /edit
            currentPage.SecurityUser.IsAdministrator = PrincipalInfo.HasAdminAccess;
            currentPage.SecurityUser.IsEditor        = PrincipalInfo.HasEditAccess;
            // get system security information
            currentPage.SecuritySystem.StoredRoles =
                roles.GetAllRoles().Select(r => r.Name).ToArray();
            ProviderSettingsCollection virtualRoles =
                EPiServerFrameworkSection.Instance.VirtualRoles.Providers;
            var list = new List <string>();

            foreach (var setting in virtualRoles.Cast <ProviderSettings>())
            {
                string item = setting.Name;
                if (setting.ElementInformation.Properties
                    .Cast <PropertyInformation>().Any(pi => pi.Name == "roles"))
                {
                    item += " <-- " + setting.ElementInformation
                            .Properties["roles"].DefaultValue;
                }
                list.Add(item);
            }
            currentPage.SecuritySystem.VirtualRoles = list.ToArray();
            // create view model
            var viewmodel = PageViewModel.Create(currentPage);

            return(View(viewmodel));
        }
Exemplo n.º 3
0
        public ActionResult Index()
        {
            bool created = false;
            var  user    = _userProvider.GetUser("*****@*****.**");

            if (user == null)
            {
                var userCreated = _userProvider.CreateUser("*****@*****.**", "Episerver123!", "*****@*****.**", "Do you like cats?", "Who doesn't like cats?", isApproved: true, out UIUserCreateStatus status, out IEnumerable <string> errors);

                if (status != UIUserCreateStatus.Success)
                {
                    return(Content($"Admin user ([email protected] / Episerver123!) failed to create [Error: {string.Join(",", errors)}]"));
                }

                created = true;
                user    = userCreated;
            }

            // Ensure Role Exists
            var adminRole = _roleProvider.GetAllRoles();

            if (!adminRole.Any(x => x.Name == AdminRole))
            {
                _roleProvider.CreateRole(AdminRole);
            }

            // Ensure seed user in admin role
            var roles = _roleProvider.GetRolesForUser(user.Username);

            if (!roles.Contains(AdminRole))
            {
                _roleProvider.AddUserToRoles(user.Username, new[] { AdminRole });
            }

            return(Content($"Admin user ([email protected] / Episerver123!) exists already and is in {AdminRole} role"));
        }