コード例 #1
0
ファイル: BlockService.cs プロジェクト: webluddite/Rock-ChMS
        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);
                }
            }
        }
コード例 #2
0
ファイル: BlockService.cs プロジェクト: webluddite/Rock-ChMS
        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);
                }
            }
        }