/// <summary> /// Returns Global Attributes from cache. If they are not already in cache, they /// will be read and added to cache /// </summary> /// <returns></returns> public static GlobalAttributes Read() { string cacheKey = GlobalAttributes.CacheKey(); ObjectCache cache = MemoryCache.Default; GlobalAttributes globalAttributes = cache[cacheKey] as GlobalAttributes; if (globalAttributes != null) { return(globalAttributes); } else { globalAttributes = new GlobalAttributes(); globalAttributes.AttributeValues = new Dictionary <string, KeyValuePair <string, string> >(); var attributeService = new Rock.Core.AttributeService(); var attributeValueService = new Rock.Core.AttributeValueService(); foreach (Rock.Core.Attribute attribute in attributeService.Queryable(). Where(a => a.Entity == "" && (a.EntityQualifierColumn ?? string.Empty) == "" && (a.EntityQualifierValue ?? string.Empty) == "")) { // TODO: Need to add support for multiple values var attributeValue = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, null).FirstOrDefault(); globalAttributes.AttributeValues.Add(attribute.Key, new KeyValuePair <string, string>(attribute.Name, (attributeValue != null && !string.IsNullOrEmpty(attributeValue.Value)) ? attributeValue.Value : attribute.DefaultValue)); } cache.Set(cacheKey, globalAttributes, new CacheItemPolicy()); return(globalAttributes); } }
/// <summary> /// Returns Attribute object from cache. If attribute does not already exist in cache, it /// will be read and added to cache /// </summary> /// <param name="id">The id of the Attribute to read</param> /// <returns></returns> public static Attribute Read(int id) { string cacheKey = Attribute.CacheKey(id); ObjectCache cache = MemoryCache.Default; Attribute attribute = cache[cacheKey] as Attribute; if (attribute != null) { return(attribute); } else { Rock.Core.AttributeService attributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute attributeModel = attributeService.Get(id); if (attributeModel != null) { attribute = Attribute.CopyModel(attributeModel); cache.Set(cacheKey, attribute, new CacheItemPolicy()); return(attribute); } else { return(null); } } }
public Rock.Core.DTO.Attribute ApiGet(string id, string apiKey) { using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope()) { Rock.CMS.UserService userService = new Rock.CMS.UserService(); Rock.CMS.User user = userService.Queryable().Where(u => u.ApiKey == apiKey).FirstOrDefault(); if (user != null) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute Attribute = AttributeService.Get(int.Parse(id)); if (Attribute.Authorized("View", user)) { return(Attribute.DataTransferObject); } else { throw new WebFaultException <string>("Not Authorized to View this Attribute", System.Net.HttpStatusCode.Forbidden); } } else { throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden); } } }
public void UpdateAttribute(string id, Rock.Core.DTO.Attribute Attribute) { var currentUser = Rock.CMS.UserService.GetCurrentUser(); if (currentUser == null) { throw new WebFaultException <string>("Must be logged in", System.Net.HttpStatusCode.Forbidden); } using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope()) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute existingAttribute = AttributeService.Get(int.Parse(id)); if (existingAttribute.Authorized("Edit", currentUser)) { uow.objectContext.Entry(existingAttribute).CurrentValues.SetValues(Attribute); if (existingAttribute.IsValid) { AttributeService.Save(existingAttribute, currentUser.PersonId); } else { throw new WebFaultException <string>(existingAttribute.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest); } } else { throw new WebFaultException <string>("Not Authorized to Edit this Attribute", System.Net.HttpStatusCode.Forbidden); } } }
public void DeleteAttribute(string id) { var currentUser = Rock.CMS.UserService.GetCurrentUser(); if (currentUser == null) { throw new WebFaultException <string>("Must be logged in", System.Net.HttpStatusCode.Forbidden); } using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope()) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute Attribute = AttributeService.Get(int.Parse(id)); if (Attribute.Authorized("Edit", currentUser)) { AttributeService.Delete(Attribute, currentUser.PersonId); AttributeService.Save(Attribute, currentUser.PersonId); } else { throw new WebFaultException <string>("Not Authorized to Edit this Attribute", System.Net.HttpStatusCode.Forbidden); } } }
public void ApiCreateAttribute(string apiKey, Rock.Core.DTO.Attribute Attribute) { using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope()) { Rock.CMS.UserService userService = new Rock.CMS.UserService(); Rock.CMS.User user = userService.Queryable().Where(u => u.ApiKey == apiKey).FirstOrDefault(); if (user != null) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute existingAttribute = new Rock.Core.Attribute(); AttributeService.Add(existingAttribute, user.PersonId); uow.objectContext.Entry(existingAttribute).CurrentValues.SetValues(Attribute); if (existingAttribute.IsValid) { AttributeService.Save(existingAttribute, user.PersonId); } else { throw new WebFaultException <string>(existingAttribute.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest); } } else { throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden); } } }
/// <summary> /// Returns Global Attributes from cache. If they are not already in cache, they /// will be read and added to cache /// </summary> /// <returns></returns> public static GlobalAttributes Read() { string cacheKey = GlobalAttributes.CacheKey(); ObjectCache cache = MemoryCache.Default; GlobalAttributes globalAttributes = cache[cacheKey] as GlobalAttributes; if ( globalAttributes != null ) return globalAttributes; else { globalAttributes = new GlobalAttributes(); globalAttributes.AttributeValues = new Dictionary<string, KeyValuePair<string, string>>(); var attributeService = new Rock.Core.AttributeService(); var attributeValueService = new Rock.Core.AttributeValueService(); foreach ( Rock.Core.Attribute attribute in attributeService.Queryable(). Where( a => a.Entity == "" && ( a.EntityQualifierColumn ?? string.Empty ) == "" && ( a.EntityQualifierValue ?? string.Empty ) == "" ) ) { // TODO: Need to add support for multiple values var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, null ).FirstOrDefault(); globalAttributes.AttributeValues.Add( attribute.Key, new KeyValuePair<string, string>( attribute.Name, (attributeValue != null && !string.IsNullOrEmpty(attributeValue.Value)) ? attributeValue.Value : attribute.DefaultValue ) ); } cache.Set( cacheKey, globalAttributes, new CacheItemPolicy() ); return globalAttributes; } }
/// <summary> /// Loads the <see cref="P:IHasAttributes.Attributes"/> and <see cref="P:IHasAttributes.AttributeValues"/> of any <see cref="IHasAttributes"/> object /// </summary> /// <param name="item">The item.</param> public static void LoadAttributes(Rock.Attribute.IHasAttributes item) { var attributes = new SortedDictionary <string, List <Web.Cache.Attribute> >(); var attributeValues = new Dictionary <string, KeyValuePair <string, List <Rock.Core.DTO.AttributeValue> > >(); Dictionary <string, PropertyInfo> properties = new Dictionary <string, PropertyInfo>(); Type entityType = item.GetType(); if (entityType.Namespace == "System.Data.Entity.DynamicProxies") { entityType = entityType.BaseType; } foreach (PropertyInfo propertyInfo in entityType.GetProperties()) { properties.Add(propertyInfo.Name.ToLower(), propertyInfo); } Rock.Core.AttributeService attributeService = new Rock.Core.AttributeService(); foreach (Rock.Core.Attribute attribute in attributeService.GetByEntity(entityType.FullName)) { if (string.IsNullOrEmpty(attribute.EntityQualifierColumn) || (properties.ContainsKey(attribute.EntityQualifierColumn.ToLower()) && (string.IsNullOrEmpty(attribute.EntityQualifierValue) || properties[attribute.EntityQualifierColumn.ToLower()].GetValue(item, null).ToString() == attribute.EntityQualifierValue))) { Rock.Web.Cache.Attribute cachedAttribute = Rock.Web.Cache.Attribute.Read(attribute); if (!attributes.ContainsKey(cachedAttribute.Category)) { attributes.Add(cachedAttribute.Category, new List <Web.Cache.Attribute>()); } attributes[cachedAttribute.Category].Add(cachedAttribute); attributeValues.Add(attribute.Key, new KeyValuePair <string, List <Rock.Core.DTO.AttributeValue> >(attribute.Name, attribute.GetValues(item.Id))); } } item.Attributes = attributes; item.AttributeValues = attributeValues; }
public void ApiDeleteAttribute( string id, string apiKey ) { using ( Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope() ) { Rock.CMS.UserService userService = new Rock.CMS.UserService(); Rock.CMS.User user = userService.Queryable().Where( u => u.ApiKey == apiKey ).FirstOrDefault(); if (user != null) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute Attribute = AttributeService.Get( int.Parse( id ) ); if ( Attribute.Authorized( "Edit", user ) ) { AttributeService.Delete( Attribute, user.PersonId ); AttributeService.Save( Attribute, user.PersonId ); } else throw new WebFaultException<string>( "Not Authorized to Edit this Attribute", System.Net.HttpStatusCode.Forbidden ); } else throw new WebFaultException<string>( "Invalid API Key", System.Net.HttpStatusCode.Forbidden ); } }
public void ApiCreateAttribute( string apiKey, Rock.Core.DTO.Attribute Attribute ) { using ( Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope() ) { Rock.CMS.UserService userService = new Rock.CMS.UserService(); Rock.CMS.User user = userService.Queryable().Where( u => u.ApiKey == apiKey ).FirstOrDefault(); if (user != null) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute existingAttribute = new Rock.Core.Attribute(); AttributeService.Add( existingAttribute, user.PersonId ); uow.objectContext.Entry(existingAttribute).CurrentValues.SetValues(Attribute); if (existingAttribute.IsValid) AttributeService.Save( existingAttribute, user.PersonId ); else throw new WebFaultException<string>( existingAttribute.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest ); } else throw new WebFaultException<string>( "Invalid API Key", System.Net.HttpStatusCode.Forbidden ); } }
public Rock.Core.DTO.Attribute Get(string id) { var currentUser = Rock.CMS.UserService.GetCurrentUser(); if (currentUser == null) { throw new WebFaultException <string>("Must be logged in", System.Net.HttpStatusCode.Forbidden); } using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope()) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute Attribute = AttributeService.Get(int.Parse(id)); if (Attribute.Authorized("View", currentUser)) { return(Attribute.DataTransferObject); } else { throw new WebFaultException <string>("Not Authorized to View this Attribute", System.Net.HttpStatusCode.Forbidden); } } }
/// <summary> /// Loads the <see cref="P:IHasAttributes.Attributes"/> and <see cref="P:IHasAttributes.AttributeValues"/> of any <see cref="IHasAttributes"/> object /// </summary> /// <param name="item">The item.</param> public static void LoadAttributes( Rock.Attribute.IHasAttributes item ) { var attributes = new SortedDictionary<string, List<Web.Cache.Attribute>>(); var attributeValues = new Dictionary<string, KeyValuePair<string, List<Rock.Core.DTO.AttributeValue>>>(); Dictionary<string, PropertyInfo> properties = new Dictionary<string, PropertyInfo>(); Type entityType = item.GetType(); if ( entityType.Namespace == "System.Data.Entity.DynamicProxies" ) entityType = entityType.BaseType; foreach ( PropertyInfo propertyInfo in entityType.GetProperties() ) properties.Add( propertyInfo.Name.ToLower(), propertyInfo ); Rock.Core.AttributeService attributeService = new Rock.Core.AttributeService(); foreach ( Rock.Core.Attribute attribute in attributeService.GetByEntity( entityType.FullName ) ) { if ( string.IsNullOrEmpty( attribute.EntityQualifierColumn ) || ( properties.ContainsKey( attribute.EntityQualifierColumn.ToLower() ) && ( string.IsNullOrEmpty( attribute.EntityQualifierValue ) || properties[attribute.EntityQualifierColumn.ToLower()].GetValue( item, null ).ToString() == attribute.EntityQualifierValue ) ) ) { Rock.Web.Cache.Attribute cachedAttribute = Rock.Web.Cache.Attribute.Read(attribute); if ( !attributes.ContainsKey( cachedAttribute.Category ) ) attributes.Add( cachedAttribute.Category, new List<Web.Cache.Attribute>() ); attributes[cachedAttribute.Category].Add( cachedAttribute ); attributeValues.Add( attribute.Key, new KeyValuePair<string, List<Rock.Core.DTO.AttributeValue>>( attribute.Name, attribute.GetValues( item.Id ) ) ); } } item.Attributes = attributes; item.AttributeValues = attributeValues; }
public void UpdateAttribute( string id, Rock.Core.DTO.Attribute Attribute ) { var currentUser = Rock.CMS.UserService.GetCurrentUser(); if ( currentUser == null ) throw new WebFaultException<string>("Must be logged in", System.Net.HttpStatusCode.Forbidden ); using ( Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope() ) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute existingAttribute = AttributeService.Get( int.Parse( id ) ); if ( existingAttribute.Authorized( "Edit", currentUser ) ) { uow.objectContext.Entry(existingAttribute).CurrentValues.SetValues(Attribute); if (existingAttribute.IsValid) AttributeService.Save( existingAttribute, currentUser.PersonId ); else throw new WebFaultException<string>( existingAttribute.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest ); } else throw new WebFaultException<string>( "Not Authorized to Edit this Attribute", System.Net.HttpStatusCode.Forbidden ); } }
public Rock.Core.DTO.Attribute Get( string id ) { var currentUser = Rock.CMS.UserService.GetCurrentUser(); if ( currentUser == null ) throw new WebFaultException<string>("Must be logged in", System.Net.HttpStatusCode.Forbidden ); using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope()) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute Attribute = AttributeService.Get( int.Parse( id ) ); if ( Attribute.Authorized( "View", currentUser ) ) return Attribute.DataTransferObject; else throw new WebFaultException<string>( "Not Authorized to View this Attribute", System.Net.HttpStatusCode.Forbidden ); } }
public void DeleteAttribute( string id ) { var currentUser = Rock.CMS.UserService.GetCurrentUser(); if ( currentUser == null ) throw new WebFaultException<string>("Must be logged in", System.Net.HttpStatusCode.Forbidden ); using ( Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope() ) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.AttributeService AttributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute Attribute = AttributeService.Get( int.Parse( id ) ); if ( Attribute.Authorized( "Edit", currentUser ) ) { AttributeService.Delete( Attribute, currentUser.PersonId ); AttributeService.Save( Attribute, currentUser.PersonId ); } else throw new WebFaultException<string>( "Not Authorized to Edit this Attribute", System.Net.HttpStatusCode.Forbidden ); } }
/// <summary> /// Returns Attribute object from cache. If attribute does not already exist in cache, it /// will be read and added to cache /// </summary> /// <param name="id">The id of the Attribute to read</param> /// <returns></returns> public static Attribute Read( int id ) { string cacheKey = Attribute.CacheKey( id ); ObjectCache cache = MemoryCache.Default; Attribute attribute = cache[cacheKey] as Attribute; if ( attribute != null ) return attribute; else { Rock.Core.AttributeService attributeService = new Rock.Core.AttributeService(); Rock.Core.Attribute attributeModel = attributeService.Get( id ); if ( attributeModel != null ) { attribute = Attribute.CopyModel( attributeModel ); cache.Set( cacheKey, attribute, new CacheItemPolicy() ); return attribute; } else return null; } }