/// <summary> /// Returns Role object from cache. If role does not already exist in cache, it /// will be read and added to cache /// </summary> /// <param name="id">The id.</param> /// <returns></returns> public static Role Read(int id) { string cacheKey = Role.CacheKey(id); ObjectCache cache = Rock.Web.Cache.RockMemoryCache.Default; Role role = cache[cacheKey] as Role; if (role != null) { return(role); } else { using (var rockContext = new RockContext()) { Rock.Model.GroupService groupService = new Rock.Model.GroupService(rockContext); Rock.Model.Group groupModel = groupService.Get(id); if (groupModel != null && groupModel.IsSecurityRole == true) { role = new Role(); role.Id = groupModel.Id; role.Name = groupModel.Name; role.Users = new List <string>(); role.IsSecurityTypeGroup = groupModel.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()); foreach (Rock.Model.GroupMember member in groupModel.Members) { role.Users.Add(member.Person.Guid.ToString()); } cache.Set(cacheKey, role, new CacheItemPolicy()); return(role); } } } return(null); }
/// <summary> /// Loads the by identifier. /// </summary> /// <param name="id">The identifier.</param> /// <returns></returns> public static Role LoadById(int id) { using (var rockContext = new RockContext()) { var securityGroupType = GroupTypeCache.Read(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid(), rockContext); int securityGroupTypeId = securityGroupType != null ? securityGroupType.Id : 0; Rock.Model.GroupService groupService = new Rock.Model.GroupService(rockContext); Rock.Model.GroupMemberService groupMemberService = new Rock.Model.GroupMemberService(rockContext); Rock.Model.Group groupModel = groupService.Get(id); if (groupModel != null && (groupModel.IsSecurityRole == true || groupModel.GroupTypeId == securityGroupTypeId)) { var role = new Role(); role.Id = groupModel.Id; role.Name = groupModel.Name; role.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); } } return(null); }
/// <summary> /// Returns Role object from cache. If role does not already exist in cache, it /// will be read and added to cache /// </summary> /// <param name="id">The id.</param> /// <returns></returns> public static Role Read(int id) { string cacheKey = Role.CacheKey(id); ObjectCache cache = MemoryCache.Default; Role role = cache[cacheKey] as Role; if (role != null) { return(role); } else { Rock.Model.GroupService groupService = new Rock.Model.GroupService(); Rock.Model.Group groupModel = groupService.Get(id); if (groupModel != null && groupModel.IsSecurityRole == true) { role = new Role(); role.Id = groupModel.Id; role.Name = groupModel.Name; role.Users = new List <string>(); foreach (Rock.Model.GroupMember member in groupModel.Members) { role.Users.Add(member.Person.Guid.ToString()); } cache.Set(cacheKey, role, new CacheItemPolicy()); return(role); } else { return(null); } } }