Example #1
0
        protected void btnSave_Click( object sender, EventArgs e )
        {
            Rock.CMS.Block block;

            int blockId = 0;
            if ( !Int32.TryParse( hfBlockId.Value, out blockId ) )
                blockId = 0;

            if ( blockId == 0 )
            {
                block = new Rock.CMS.Block();
                _blockService.Add( block, CurrentPersonId );
            }
            else
                block = _blockService.Get( blockId );

            block.Name = tbName.Text;
            block.Path = tbPath.Text;
            block.Description = tbDescription.Text;

            _blockService.Save( block, CurrentPersonId );

            Rock.Web.Cache.Block.Flush( block.Id );

            BindGrid();

            pnlDetails.Visible = false;
            phList.Visible = true;
        }
Example #2
0
        public Rock.CMS.DTO.Block 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.CMS.BlockService BlockService = new Rock.CMS.BlockService();
                    Rock.CMS.Block        Block        = BlockService.Get(int.Parse(id));
                    if (Block.Authorized("View", user))
                    {
                        return(Block.DataTransferObject);
                    }
                    else
                    {
                        throw new WebFaultException <string>("Not Authorized to View this Block", System.Net.HttpStatusCode.Forbidden);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Example #3
0
        public void UpdateBlock(string id, Rock.CMS.DTO.Block Block)
        {
            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.CMS.BlockService BlockService  = new Rock.CMS.BlockService();
                Rock.CMS.Block        existingBlock = BlockService.Get(int.Parse(id));
                if (existingBlock.Authorized("Edit", currentUser))
                {
                    uow.objectContext.Entry(existingBlock).CurrentValues.SetValues(Block);

                    if (existingBlock.IsValid)
                    {
                        BlockService.Save(existingBlock, currentUser.PersonId);
                    }
                    else
                    {
                        throw new WebFaultException <string>(existingBlock.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Not Authorized to Edit this Block", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Example #4
0
        public void ApiDeleteBlock(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.CMS.BlockService BlockService = new Rock.CMS.BlockService();
                    Rock.CMS.Block        Block        = BlockService.Get(int.Parse(id));
                    if (Block.Authorized("Edit", user))
                    {
                        BlockService.Delete(Block, user.PersonId);
                        BlockService.Save(Block, user.PersonId);
                    }
                    else
                    {
                        throw new WebFaultException <string>("Not Authorized to Edit this Block", System.Net.HttpStatusCode.Forbidden);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Example #5
0
        public void DeleteBlock(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.CMS.BlockService BlockService = new Rock.CMS.BlockService();
                Rock.CMS.Block        Block        = BlockService.Get(int.Parse(id));
                if (Block.Authorized("Edit", currentUser))
                {
                    BlockService.Delete(Block, currentUser.PersonId);
                    BlockService.Save(Block, currentUser.PersonId);
                }
                else
                {
                    throw new WebFaultException <string>("Not Authorized to Edit this Block", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Example #6
0
        public void ApiCreateBlock(string apiKey, Rock.CMS.DTO.Block Block)
        {
            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.CMS.BlockService BlockService  = new Rock.CMS.BlockService();
                    Rock.CMS.Block        existingBlock = new Rock.CMS.Block();
                    BlockService.Add(existingBlock, user.PersonId);
                    uow.objectContext.Entry(existingBlock).CurrentValues.SetValues(Block);

                    if (existingBlock.IsValid)
                    {
                        BlockService.Save(existingBlock, user.PersonId);
                    }
                    else
                    {
                        throw new WebFaultException <string>(existingBlock.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Example #7
0
        protected void ShowEdit(int blockId)
        {
            Rock.CMS.Block block = _blockService.Get(blockId);

            if (block != null)
            {
                modalDetails.Title = "Edit Block";
                hfId.Value         = block.Id.ToString();

                tbName.Text        = block.Name;
                tbPath.Text        = block.Path;
                tbDescription.Text = block.Description;
            }
            else
            {
                modalDetails.Title = "Add Block";
                hfId.Value         = string.Empty;

                tbName.Text        = string.Empty;
                tbPath.Text        = string.Empty;
                tbDescription.Text = string.Empty;
            }

            modalDetails.Show();
        }
Example #8
0
        void modalDetails_SaveClick(object sender, EventArgs e)
        {
            Rock.CMS.Block block;

            int blockId = 0;

            if (hfId.Value != string.Empty && !Int32.TryParse(hfId.Value, out blockId))
            {
                blockId = 0;
            }

            if (blockId == 0)
            {
                block = new Rock.CMS.Block();
                _blockService.Add(block, CurrentPersonId);
            }
            else
            {
                Rock.Web.Cache.Block.Flush(blockId);
                block = _blockService.Get(blockId);
            }

            block.Name        = tbName.Text;
            block.Path        = tbPath.Text;
            block.Description = tbDescription.Text;

            _blockService.Save(block, CurrentPersonId);

            BindGrid();
        }
Example #9
0
        /// <summary>
        /// Returns Block object from cache.  If block does not already exist in cache, it
        /// will be read and added to cache
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Block Read(int id)
        {
            string cacheKey = Block.CacheKey(id);

            ObjectCache cache = MemoryCache.Default;
            Block       block = cache[cacheKey] as Block;

            if (block != null)
            {
                return(block);
            }
            else
            {
                Rock.CMS.BlockService blockService = new CMS.BlockService();
                Rock.CMS.Block        blockModel   = blockService.Get(id);
                if (blockModel != null)
                {
                    block             = new Block();
                    block.Id          = blockModel.Id;
                    block.Path        = blockModel.Path;
                    block.Name        = blockModel.Name;
                    block.Description = blockModel.Description;
                    block.InstancePropertiesVerified = false;

                    Rock.Attribute.Helper.LoadAttributes(blockModel);

                    block.AttributeValues = blockModel.AttributeValues;

                    if (blockModel.Attributes != null)
                    {
                        foreach (var category in blockModel.Attributes)
                        {
                            foreach (var attribute in category.Value)
                            {
                                block.AttributeIds.Add(attribute.Id);
                            }
                        }
                    }

                    // Block cache expiration monitors the actual block on the file system so that it is flushed from
                    // memory anytime the file contents change.  This is to force the cmsPage object to revalidate any
                    // BlockInstancePropery attributes that may have been added or modified
                    string        physicalPath = System.Web.HttpContext.Current.Request.MapPath(block.Path);
                    List <string> filePaths    = new List <string>();
                    filePaths.Add(physicalPath);
                    filePaths.Add(physicalPath + ".cs");

                    CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
                    cacheItemPolicy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
                    cache.Set(cacheKey, block, cacheItemPolicy);

                    return(block);
                }
                else
                {
                    return(null);
                }
            }
        }
Example #10
0
        protected void rGrid_Delete(object sender, RowEventArgs e)
        {
            Rock.CMS.Block block = _blockService.Get(( int )rGrid.DataKeys[e.RowIndex]["id"]);
            if (BlockInstance != null)
            {
                _blockService.Delete(block, CurrentPersonId);
                _blockService.Save(block, CurrentPersonId);

                Rock.Web.Cache.Block.Flush(block.Id);
            }

            BindGrid();
        }
Example #11
0
        /// <summary>
        /// Saves the attribute values.
        /// </summary>
        /// <param name="personId">The person id.</param>
        public void SaveAttributeValues(int?personId)
        {
            Rock.CMS.BlockService blockService = new CMS.BlockService();
            Rock.CMS.Block        blockModel   = blockService.Get(this.Id);

            if (blockModel != null)
            {
                Rock.Attribute.Helper.LoadAttributes(blockModel);
                foreach (var category in blockModel.Attributes)
                {
                    foreach (var attribute in category.Value)
                    {
                        Rock.Attribute.Helper.SaveAttributeValues(blockModel, attribute, this.AttributeValues[attribute.Key].Value, personId);
                    }
                }
            }
        }
Example #12
0
        public void ApiCreateBlock( string apiKey, Rock.CMS.DTO.Block Block )
        {
            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.CMS.BlockService BlockService = new Rock.CMS.BlockService();
                    Rock.CMS.Block existingBlock = new Rock.CMS.Block();
                    BlockService.Add( existingBlock, user.PersonId );
                    uow.objectContext.Entry(existingBlock).CurrentValues.SetValues(Block);

                    if (existingBlock.IsValid)
                        BlockService.Save( existingBlock, user.PersonId );
                    else
                        throw new WebFaultException<string>( existingBlock.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest );
                }
                else
                    throw new WebFaultException<string>( "Invalid API Key", System.Net.HttpStatusCode.Forbidden );
            }
        }
Example #13
0
        public Rock.CMS.DTO.Block 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.CMS.BlockService BlockService = new Rock.CMS.BlockService();
                Rock.CMS.Block        Block        = BlockService.Get(int.Parse(id));
                if (Block.Authorized("View", currentUser))
                {
                    return(Block.DataTransferObject);
                }
                else
                {
                    throw new WebFaultException <string>("Not Authorized to View this Block", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
Example #14
0
        void modalDetails_SaveClick( object sender, EventArgs e )
        {
            Rock.CMS.Block block;

            int blockId = 0;
            if ( hfId.Value != string.Empty && !Int32.TryParse( hfId.Value, out blockId ) )
                blockId = 0;

            if ( blockId == 0 )
            {
                block = new Rock.CMS.Block();
                _blockService.Add( block, CurrentPersonId );
            }
            else
            {
                Rock.Web.Cache.Block.Flush( blockId );
                block = _blockService.Get( blockId );
            }

            block.Name = tbName.Text;
            block.Path = tbPath.Text;
            block.Description = tbDescription.Text;

            _blockService.Save( block, CurrentPersonId );

            BindGrid();
        }
Example #15
0
        public void CreateBlock( Rock.CMS.DTO.Block Block )
        {
            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.CMS.BlockService BlockService = new Rock.CMS.BlockService();
                Rock.CMS.Block existingBlock = new Rock.CMS.Block();
                BlockService.Add( existingBlock, currentUser.PersonId );
                uow.objectContext.Entry(existingBlock).CurrentValues.SetValues(Block);

                if (existingBlock.IsValid)
                    BlockService.Save( existingBlock, currentUser.PersonId );
                else
                    throw new WebFaultException<string>( existingBlock.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest );
            }
        }