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); } } }
/// <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 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> /// Adds Attribute model to cache, and returns cached object /// </summary> /// <param name="attributeModel">The attributeModel to cache</param> /// <returns></returns> public static Attribute Read(Rock.Core.Attribute attributeModel) { Attribute attribute = Attribute.CopyModel(attributeModel); string cacheKey = Attribute.CacheKey(attributeModel.Id); ObjectCache cache = MemoryCache.Default; cache.Set(cacheKey, attribute, new CacheItemPolicy()); return(attribute); }
/// <summary> /// Copies the properties of a <see cref="Rock.Core.Attribute"/> object to a <see cref="Attribute"/> object/> /// </summary> /// <param name="attributeModel">The attribute model.</param> /// <returns></returns> public static Attribute CopyModel(Rock.Core.Attribute attributeModel) { Attribute attribute = new Attribute(); attribute.Id = attributeModel.Id; attribute.Key = attributeModel.Key; attribute.Name = attributeModel.Name; attribute.Category = attributeModel.Category; attribute.Description = attributeModel.Description; attribute.GridColumn = attributeModel.GridColumn; attribute.FieldTypeId = attributeModel.FieldTypeId; attribute.DefaultValue = attributeModel.DefaultValue; attribute.Required = attributeModel.Required; attribute.MultiValue = attributeModel.MultiValue; attribute.QualifierValues = new Dictionary <string, KeyValuePair <string, string> >(); foreach (Rock.Core.AttributeQualifier qualifier in attributeModel.AttributeQualifiers) { attribute.QualifierValues.Add(qualifier.Key, new KeyValuePair <string, string>(qualifier.Name, qualifier.Value)); } return(attribute); }
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); } } }
public void CreateAttribute( 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 = new Rock.Core.Attribute(); AttributeService.Add( existingAttribute, currentUser.PersonId ); 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 ); } }