コード例 #1
0
        /// <summary>
        /// Loads the by identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public static RoleCache LoadById(int id)
        {
            using (var rockContext = new RockContext())
            {
                var securityGroupType   = GroupTypeCache.Get(SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid(), rockContext);
                var securityGroupTypeId = securityGroupType?.Id ?? 0;

                var groupService       = new Model.GroupService(rockContext);
                var groupMemberService = new Model.GroupMemberService(rockContext);
                var groupModel         = groupService.Get(id);

                if (groupModel == null || !groupModel.IsActive ||
                    (!groupModel.IsSecurityRole && groupModel.GroupTypeId != securityGroupTypeId))
                {
                    return(null);
                }

                var role = new RoleCache
                {
                    Id     = groupModel.Id,
                    Guid   = groupModel.Guid,
                    Name   = groupModel.Name,
                    People = new ConcurrentDictionary <Guid, bool>()
                };

                var groupMembersQry = groupMemberService.Queryable().Where(a => a.GroupId == groupModel.Id);

                // Add the members
                foreach (var personGuid in groupMembersQry
                         .Where(m => m.GroupMemberStatus == Model.GroupMemberStatus.Active)
                         .Select(m => m.Person.Guid)
                         .ToList()
                         .Distinct())
                {
                    role.People.TryAdd(personGuid, true);
                }

                role.IsSecurityTypeGroup = groupModel.GroupTypeId == securityGroupTypeId;

                return(role);
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns a list of all the possible Roles
        /// </summary>
        /// <returns></returns>
        public static List <RoleCache> AllRoles()
        {
            var roles = new List <RoleCache>();

            var securityGroupType   = GroupTypeCache.Get(SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid());
            var securityGroupTypeId = securityGroupType?.Id ?? 0;

            var groupService = new Model.GroupService(new RockContext());

            foreach (var id in groupService.Queryable()
                     .Where(g =>
                            g.IsActive &&
                            (g.GroupTypeId == securityGroupTypeId || g.IsSecurityRole))
                     .OrderBy(g => g.Name)
                     .Select(g => g.Id)
                     .ToList())
            {
                roles.Add(Get(id));
            }

            return(roles);
        }