Esempio n. 1
0
        protected virtual void DeserializeRole(IRoleData role)
        {
            bool addedRole = false;

            // Add role if needed
            var name = role.RoleName;

            if (!System.Web.Security.Roles.RoleExists(name))
            {
                _loaderLogger.AddedNewRole(role);
                addedRole = true;
                System.Web.Security.Roles.CreateRole(name);
            }

            Role targetRole           = Role.FromName(name);
            var  currentSourceParents = new SitecoreRoleData(targetRole).MemberOfRoles;
            var  currentTargetParents = role.MemberOfRoles;

            var addedRoleMembership   = new List <string>();
            var removedRoleMembership = new List <string>();
            var deferredUpdateLog     = new DeferredLogWriter <IRoleLoaderLogger>();

            // Loop over the serialized parent roles and set db roles if needed
            foreach (var serializedMemberRoleName in currentTargetParents)
            {
                var memberRole = Role.FromName(serializedMemberRoleName);

                // add nonexistant parent role if needed. NOTE: parent role need not be one we have serialized or included.
                if (!Role.Exists(serializedMemberRoleName))
                {
                    deferredUpdateLog.AddEntry(log => log.AddedNewRoleMembership(new SitecoreRoleData(memberRole)));
                    System.Web.Security.Roles.CreateRole(serializedMemberRoleName);
                }

                // Add membership if not already in the parent role
                if (!RolesInRolesManager.IsRoleInRole(targetRole, memberRole, false))
                {
                    addedRoleMembership.Add(memberRole.Name);
                    RolesInRolesManager.AddRoleToRole(targetRole, memberRole);
                }
            }

            // Loop over parent roles that exist in the database but not in serialized and remove them
            var membershipToRemove = currentSourceParents.Where(parent => !currentTargetParents.Contains(parent, StringComparer.OrdinalIgnoreCase));

            foreach (var roleToRemove in membershipToRemove)
            {
                removedRoleMembership.Add(roleToRemove);
                RolesInRolesManager.RemoveRoleFromRole(targetRole, Role.FromName(roleToRemove));
            }

            if (!addedRole && (addedRoleMembership.Count > 0 || removedRoleMembership.Count > 0))
            {
                _loaderLogger.RoleMembershipChanged(role, addedRoleMembership.ToArray(), removedRoleMembership.ToArray());
            }

            deferredUpdateLog.ExecuteDeferredActions(_loaderLogger);
        }
        public virtual void RoleDeleted(string roleName)
        {
            var role = new SitecoreRoleData(Role.FromName(roleName));

            if (_predicate == null || !_predicate.Includes(role).IsIncluded)
            {
                return;
            }

            _dataStore.Remove(role);
        }