Example #1
0
        public void use_no_custom_auth_failure_handler()
        {
            var node = new AuthorizationNode();
            var def  = node.As <IContainerModel>().ToInstance().As <IConfiguredInstance>();

            def.FindDependencyDefinitionFor <IAuthorizationFailureHandler>().ShouldBeNull();
        }
Example #2
0
        public void use_no_custom_auth_failure_handler()
        {
            var node = new AuthorizationNode();
            var def  = node.As <IContainerModel>().ToObjectDef();

            def.DependencyFor <IAuthorizationFailureHandler>().ShouldBeNull();
        }
        public void OnSelectCommand(object sender, EventArgs e)
        {
            frmSelectCommand form = WorkItem.Items.Get <frmSelectCommand>(SelectCommandForm);

            if (form == null)
            {
                form = WorkItem.Items.AddNew <frmSelectCommand>(SelectCommandForm);
            }

            AuthorizationNode authNode = CurrentAuthNode.Tag as AuthorizationNode;

            form.AuthNode = authNode;
            form.RefreshList();

            // 为授权节点添加操作
            if (form.ShowDialog() == DialogResult.OK)
            {
                List <AuthorizationCommand> lcs = form.GetSelection();
                foreach (AuthorizationCommand ac in lcs)
                {
                    authNode.AddCommand(ac);
                    View.ListAuthorizationCommands(authNode); // 刷新操作列表
                    AuthorizationNodeService.Save(authNode);
                    UpdateRoleAuthorization(authNode);
                    CurrentAuthNode.Tag = authNode;
                }
            }
        }
        public void add_type_for_check()
        {
            var node = new AuthorizationNode();
            node.Add(typeof (FakeAuthCheck));

            node.Policies.Single().ShouldBeOfType<AuthorizationCheckPolicy<FakeAuthCheck>>();
        }
        /// <summary>
        /// 删除权限节点
        /// </summary>
        /// <param name="node">权限节点</param>
        private void DeleteAuthNode(TreeListNode node)
        {
            foreach (TreeListNode child in node.Nodes)
            {
                DeleteAuthNode(child);
            }

            AuthorizationNode authNode = node.Tag as AuthorizationNode;

            if (authNode != null)
            {
                try {
                    AuthorizationNodeService.Delete(authNode);

                    // 更新角色的授权信息
                    IList <AuthorizationStore> stores = AuthorizationStoreService.GetAll();
                    if (stores != null)
                    {
                        foreach (AuthorizationStore store in stores)
                        {
                            store.Remove(authNode);
                            AuthorizationStoreService.SaveAuthorization(store);
                        }
                    }
                }
                catch { }
            }
        }
        public BehaviorChain()
        {
            Authorization = new AuthorizationNode();
            UrlCategory = new UrlCategory();

            _output = new Lazy<OutputNode>(() =>
            {
                var outputType = ResourceType();
                if (outputType == null || outputType == typeof (void))
                    throw new InvalidOperationException(
                        "Cannot use the OutputNode if the BehaviorChain does not have at least one Action with output");

                return new OutputNode(outputType);
            });

            _input = new Lazy<InputNode>(() =>
            {
                var inputType = InputType();
                if (inputType == null)
                    throw new InvalidOperationException(
                        "Cannot use the InputNode if the BehaviorChain does not have at least one behavior that requires an input type");

                return new InputNode(inputType);
            });
        }
 /// <summary>
 /// 删除授权节点
 /// </summary>
 /// <param name="node">授权节点</param>
 public void Delete(AuthorizationNode node)
 {
     IList<AuthorizationNode> lns = db.Query<AuthorizationNode>((AuthorizationNode an) => an.AuthorizationUri == node.AuthorizationUri);
     foreach (AuthorizationNode an in lns) {
         db.Delete(an); // 删除所有授权节点
     }
 }
Example #8
0
        /// <summary>
        /// 加载所有的权限节点信息
        /// </summary>
        private void LoadAuthorizationNodes(AuthorizationStore store)
        {
            using (WaitCursor cursor = new WaitCursor(true))
            {
                tlAuth.BeginUpdate();
                tlAuth.Nodes.Clear();
                try
                {
                    AuthorizationNode authNode = new AuthorizationNode()
                    {
                        Id   = "Shell",
                        Name = "系统权限"
                    };
                    authNode.AuthorizationUri = GlobalConstants.Uri_Separator + "Shell";
                    TreeListNode tlNode = tlAuth.AppendNode(new object[] { authNode.Name, authNode.Id }, -1, authNode);
                    tlNode.Tag = authNode;
                    ((AuthorizationNode)tlNode.Tag).AuthorizationUri = GetAuthrizationNodePath(tlNode);
                    tlNode.ImageIndex       = 0;
                    tlNode.SelectImageIndex = 1;

                    // 加载角色的相关权限节点
                    foreach (AuthorizationNode child in store.Nodes)
                    {
                        LoadAuthorizationNode(child, tlAuth.Nodes[0]);
                    }
                    tlAuth.Nodes[0].ExpandAll(); // 展开所有子节点
                }
                finally
                {
                    tlAuth.EndUpdate();
                }
            }
        }
Example #9
0
        public void LoadAuthorizationsNodes()
        {
            using (WaitCursor cursor = new WaitCursor(true)) {
                tlAuth.BeginUpdate();
                tlAuth.FocusedNodeChanged -= new FocusedNodeChangedEventHandler(tlAuth_FocusedNodeChanged);
                tlAuth.Nodes.Clear();
                try
                {
                    IList <AuthorizationNode> nodes = Presenter.AuthorizationNodeService.GetAll();

                    AuthorizationNode authNode = new AuthorizationNode()
                    {
                        Id   = "Shell",
                        Name = "系统权限"
                    };
                    authNode.AuthorizationUri = GlobalConstants.Uri_Separator + "Shell";
                    TreeListNode tlNode = tlAuth.AppendNode(new object[] { authNode.Name, authNode.Id }, -1, authNode);
                    tlNode.Tag = authNode;
                    ((AuthorizationNode)tlNode.Tag).AuthorizationUri = GetAuthrizationNodePath(tlNode);
                    tlNode.ImageIndex       = 0;
                    tlNode.SelectImageIndex = 1;

                    foreach (AuthorizationNode child in nodes)
                    {
                        LoadAuthorizationNode(child, tlAuth.Nodes[0]);
                    }
                    tlAuth.Nodes[0].ExpandAll(); // 展开所有子节点
                }
                finally {
                    tlAuth.EndUpdate();
                    tlAuth.FocusedNodeChanged += new FocusedNodeChangedEventHandler(tlAuth_FocusedNodeChanged);
                }
            }
        }
        public void add_type_for_a_policy()
        {
            var node = new AuthorizationNode();
            node.Add(typeof (AlwaysAllowPolicy));

            node.Policies.Single().ShouldBeOfType<AlwaysAllowPolicy>();
        }
        public void OnNewAuthNode(object sender, EventArgs e)
        {
            frmAuthNode form = GetAuthNodeForm();

            form.EditMode(false);
            form.AuthId   = "";
            form.AuthName = "";
            if (form.ShowDialog() == DialogResult.OK)
            {
                TreeListNode node = View.TLAuth.AppendNode(new object[] { form.AuthName, form.AuthId }, CurrentAuthNode);
                node.Tag = new AuthorizationNode()
                {
                    Id   = form.AuthId,
                    Name = form.AuthName
                };

                AuthorizationNode authNode = node.Tag as AuthorizationNode;
                if (authNode != null)
                {
                    authNode.AuthorizationUri = View.GetAuthrizationNodePath(node);
                    AuthorizationNodeService.Save(authNode);
                    UpdateRoleAuthorization(authNode);
                }

                node.ImageIndex       = 0;
                node.SelectImageIndex = 1;
                CurrentAuthNode.ExpandAll();
            }
        }
Example #12
0
        /// <summary>
        /// 获取当前授权节点的路径
        /// </summary>
        /// <param name="node">Tree list node</param>
        /// <returns>返回从根节点到当前节点的路径值</returns>
        public string GetAuthrizationNodePath(TreeListNode node)
        {
            string authPath = "";

            if (node.Tag != null)
            {
                AuthorizationNode authNode = node.Tag as AuthorizationNode; // 获取节点的授权信息
                if (authNode == null)
                {
                    return(authPath);
                }
                authPath = authNode.Id;
                TreeListNode curr = node.ParentNode;

                // 递归获取每一层节点的路径信息
                while (curr != null)
                {
                    if (curr.Tag != null)
                    {
                        authNode = curr.Tag as AuthorizationNode;
                        if (authNode == null)
                        {
                            return(authPath);
                        }
                        authPath = authNode.Id + authPath;
                    }
                    curr = curr.ParentNode;
                }
                return(authPath);
            }
            return(authPath);
        }
Example #13
0
        /// <summary>
        /// Lists the authorization commands.
        /// </summary>
        /// <param name="authNode">The auth node.</param>
        public void ListAuthorizationCommands(AuthorizationNode authNode)
        {
            using (WaitCursor cursor = new WaitCursor(true))
            {
                Dictionary <string, TreeListNode> categories = new Dictionary <string, TreeListNode>(); // 分组
                tlCommands.BeginUpdate();
                tlCommands.ClearNodes();
                try
                {
                    if (authNode != null)
                    {
                        foreach (AuthorizationCommand cmd in authNode.Commands)
                        {
                            if (!categories.ContainsKey(cmd.Category))
                            {
                                categories.Add(cmd.Category, GetCategoryNode(cmd.Category));
                            }

                            TreeListNode parent = categories[cmd.Category];
                            TreeListNode child  = tlCommands.AppendNode(new object[] { cmd.Name, cmd.CommandUri, cmd.Image }, parent, cmd);
                            child.ImageIndex       = 3;
                            child.SelectImageIndex = 3;
                        }
                    }
                }
                finally
                {
                    tlCommands.ExpandAll();
                    tlCommands.EndUpdate();
                }
            }
        }
Example #14
0
        public void with_rules()
        {
            var node = new AuthorizationNode();

            node.AddRole("Role A");

            node.HasRules().ShouldBeTrue();
        }
Example #15
0
        public void add_type_for_check()
        {
            var node = new AuthorizationNode();

            node.Add(typeof(FakeAuthCheck));

            node.Policies.Single().ShouldBeOfType <AuthorizationCheckPolicy <FakeAuthCheck> >();
        }
Example #16
0
        public void add_type_for_a_policy()
        {
            var node = new AuthorizationNode();

            node.Add(typeof(AlwaysAllowPolicy));

            node.Policies.Single().ShouldBeOfType <AlwaysAllowPolicy>();
        }
        public void adding_a_role()
        {
            var node = new AuthorizationNode();
            node.AddRole("RoleA");

            var authorizationBehavior = toBehavior(node);
            authorizationBehavior.Policies.Count().ShouldBe(1);
            authorizationBehavior.Policies.First().ShouldBeOfType<AllowRole>().Role.ShouldBe("RoleA");
        }
Example #18
0
        public void adding_a_rule()
        {
            var node = new AuthorizationNode();

            node.AddRule(typeof(UrlModelShouldStartWithJ));

            toBehavior(node).Policies.Single().ShouldBeOfType <AuthorizationPolicy <UrlModel> >()
            .InnerRule.ShouldBeOfType <UrlModelShouldStartWithJ>();
        }
Example #19
0
        public void adding_a_type_that_is_not_a_rule()
        {
            var node = new AuthorizationNode();

            Exception <ArgumentOutOfRangeException> .ShouldBeThrownBy(() =>
            {
                node.AddRule(GetType());
            });
        }
        public void adding_a_policy()
        {
            var node = new AuthorizationNode();
            var policy = MockRepository.GenerateMock<IAuthorizationPolicy>();

            node.AddPolicy(policy);

            var authorizationBehavior = toBehavior(node);
            authorizationBehavior.Policies.Single().ShouldBeTheSameAs(policy);
        }
Example #21
0
        public void use_custom_auth_failure_handler_by_type()
        {
            var node = new AuthorizationNode();

            node.FailureHandler <FakeAuthHandler>();

            var def = node.As <IContainerModel>().ToInstance().As <IConfiguredInstance>();

            def.FindDependencyDefinitionFor <IAuthorizationFailureHandler>()
            .ReturnedType.ShouldBe(typeof(FakeAuthHandler));
        }
Example #22
0
        private AuthorizationBehavior toBehavior(AuthorizationNode node)
        {
            AuthorizationBehavior behavior = null;

            using (var runtime = FubuRuntime.Basic())
            {
                behavior = (AuthorizationBehavior)runtime.Get <IContainer>().GetInstance <IActionBehavior>(node.As <IContainerModel>().ToInstance());
            }

            return(behavior);
        }
Example #23
0
        public void use_custom_auth_failure_handler_by_type()
        {
            var node = new AuthorizationNode();

            node.FailureHandler <FakeAuthHandler>();

            var def = node.As <IContainerModel>().ToObjectDef();

            def.DependencyFor <IAuthorizationFailureHandler>().ShouldBeOfType <ConfiguredDependency>()
            .Definition.Type.ShouldEqual(typeof(FakeAuthHandler));
        }
Example #24
0
        public void adding_a_policy()
        {
            var node   = new AuthorizationNode();
            var policy = MockRepository.GenerateMock <IAuthorizationPolicy>();

            node.AddPolicy(policy);

            var authorizationBehavior = toBehavior(node);

            authorizationBehavior.Policies.Single().ShouldBeTheSameAs(policy);
        }
Example #25
0
        public void adding_a_role()
        {
            var node = new AuthorizationNode();

            node.AddRole("RoleA");

            var authorizationBehavior = toBehavior(node);

            authorizationBehavior.Policies.Count.ShouldEqual(1);
            authorizationBehavior.Policies.First().ShouldBeOfType <AllowRole>().Role.ShouldEqual("RoleA");
        }
Example #26
0
        public void use_custom_failure_handler_by_value()
        {
            var node = new AuthorizationNode();

            var handler = new FakeAuthHandler();

            node.FailureHandler(handler);

            var def = node.As <IContainerModel>().ToInstance().As <IConfiguredInstance>();

            def.FindDependencyValueFor <IAuthorizationFailureHandler>().ShouldBeTheSameAs(handler);
        }
Example #27
0
        public void use_custom_failure_handler_by_value()
        {
            var node = new AuthorizationNode();

            var handler = new FakeAuthHandler();

            node.FailureHandler(handler);

            var def = node.As <IContainerModel>().ToObjectDef();

            def.DependencyFor <IAuthorizationFailureHandler>().ShouldBeOfType <ConfiguredDependency>()
            .Definition.Value.ShouldBeTheSameAs(handler);
        }
        /// <summary>
        /// 刷新数据列表视图
        /// </summary>
        public override void RefreshDataSource()
        {
            base.RefreshDataSource();

            AuthorizationNode authNode = CurrentAuthNode.Tag as AuthorizationNode;

            if (authNode != null)
            {
                using (WaitCursor cursor = new WaitCursor(true)) {
                    View.ListAuthorizationCommands(authNode);
                }
            }
        }
Example #29
0
        public void SetUp()
        {
            chain = new BehaviorChain();

            var node = new AuthorizationNode();

            node.AddRole("RoleA");
            node.AddRole("RoleB");
            node.AddRole("RoleC");

            chain.AddToEnd(node);

            endpointObjectDef = node.As <IAuthorizationRegistration>().ToEndpointAuthorizorObjectDef();
        }
        public void adding_multiple_roles()
        {
            var node = new AuthorizationNode();
            node.AddRole("RoleA");
            node.AddRole("RoleB");
            node.AddRole("RoleC");

            var authorizationBehavior = toBehavior(node);
            authorizationBehavior.Policies.Count().ShouldBe(3);

            authorizationBehavior.Policies.ToArray()[0].ShouldBeOfType<AllowRole>().Role.ShouldBe("RoleA");
            authorizationBehavior.Policies.ToArray()[1].ShouldBeOfType<AllowRole>().Role.ShouldBe("RoleB");
            authorizationBehavior.Policies.ToArray()[2].ShouldBeOfType<AllowRole>().Role.ShouldBe("RoleC");
        }
Example #31
0
        private void LoadAuthorizationNode(AuthorizationNode authNode, TreeListNode tlNode)
        {
            string[]     authPath = authNode.AuthorizationUri.Split(new string[] { GlobalConstants.Uri_Separator }, StringSplitOptions.None);
            TreeListNode current  = tlNode;

            if (authPath.Length < 1)
            {
                return;
            }

            for (int i = 2; i < authPath.Length; ++i)
            {
                bool found = false;
                foreach (TreeListNode node in current.Nodes)
                {
                    string id = node.GetDisplayText(colId);
                    if (id == authPath[i])
                    {
                        current = node;
                        found   = true;
                        break;
                    }
                }

                if (found)
                {
                    if (i == authPath.Length - 1 && current.Tag == null)
                    {
                        current.SetValue(colNode, authNode.Name);
                        current.Tag = authNode;
                    }
                }
                else
                {
                    TreeListNode child = tlAuth.AppendNode(new object[] { "tempnode", authPath[i] }, current); // 我们创建的可能是中间节点
                    child.ImageIndex       = 0;
                    child.SelectImageIndex = 1;

                    // 设置最终要创建的节点的相关属性
                    if (i == authPath.Length - 1)
                    {
                        child.SetValue(colNode, authNode.Name);
                        child.Tag = authNode;
                        LoadAuthorizationCommandNode(child, authNode);
                    }
                    current = child;
                }
            }
        }
        /// <summary>
        /// 更新角色的授权信息
        /// </summary>
        /// <param name="authNode">The auth node.</param>
        private void UpdateRoleAuthorization(AuthorizationNode authNode)
        {
            Guard.ArgumentNotNull(authNode, "Authorization node");

            IList <AuthorizationStore> stores = AuthorizationStoreService.GetAll();

            if (stores != null)
            {
                foreach (AuthorizationStore store in stores)
                {
                    store.Store(authNode);
                    AuthorizationStoreService.SaveAuthorization(store);
                }
            }
        }
        public void OnEditAuthNode(object sender, EventArgs e)
        {
            frmAuthNode       form     = GetAuthNodeForm();
            AuthorizationNode authNode = CurrentAuthNode.Tag as AuthorizationNode;

            form.EditMode(true);
            form.AuthId   = authNode.Id;
            form.AuthName = authNode.Name;
            if (form.ShowDialog() == DialogResult.OK)
            {
                authNode.Id   = form.AuthId;
                authNode.Name = form.AuthName;
                AuthorizationNodeService.Save(authNode);
                UpdateRoleAuthorization(authNode);
            }
        }
Example #34
0
        public void adding_multiple_roles()
        {
            var node = new AuthorizationNode();

            node.AddRole("RoleA");
            node.AddRole("RoleB");
            node.AddRole("RoleC");

            var authorizationBehavior = toBehavior(node);

            authorizationBehavior.Policies.Count.ShouldEqual(3);

            authorizationBehavior.Policies[0].ShouldBeOfType <AllowRole>().Role.ShouldEqual("RoleA");
            authorizationBehavior.Policies[1].ShouldBeOfType <AllowRole>().Role.ShouldEqual("RoleB");
            authorizationBehavior.Policies[2].ShouldBeOfType <AllowRole>().Role.ShouldEqual("RoleC");
        }
        /// <summary>
        /// 删除选定数据资料
        /// </summary>
        public override void Delete()
        {
            if (XtraMessageBox.Show("你真的要删除选定的操作项吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                base.Delete();

                AuthorizationCommand command = View.TLCommands.Selection[0].Tag as AuthorizationCommand; // 获取准备删除的操作项

                if (CurrentAuthNode != null)
                {
                    AuthorizationNode authNode = CurrentAuthNode.Tag as AuthorizationNode;
                    authNode.RemoveCommand(command);
                    View.TLCommands.DeleteNode(View.TLCommands.Selection[0]);
                    CurrentAuthNode.Tag = authNode;
                    AuthorizationNodeService.Save(authNode); // 将变化保存回后端数据库

                    UpdateRoleAuthorization(authNode);
                }
            }
        }
Example #36
0
        /// <summary>
        /// 加载权限节点下的操作项
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="authNode">The auth node.</param>
        private void LoadAuthorizationCommandNode(TreeListNode parent, AuthorizationNode authNode)
        {
            Dictionary <string, TreeListNode> categories = new Dictionary <string, TreeListNode>(); // 分组

            foreach (AuthorizationCommand cmd in authNode.Commands)
            {
                if (!categories.ContainsKey(cmd.Category))
                {
                    categories[cmd.Category] = GetCategoryNode(cmd.Category, parent);
                }
                TreeListNode category = categories[cmd.Category];
                TreeListNode child    = tlAuth.AppendNode(new object[] { cmd.Name, cmd.CommandUri }, category, cmd);
                child.ImageIndex       = 3;
                child.SelectImageIndex = 3;

                // 设置当前操作的权限操作
                string authorizationUri = GetAuthorizationUri(child);
                child.Checked = authStore.CanExecute(SecurityUtility.HashObject(authorizationUri + cmd.CommandUri));
                SetCheckedParentNodes(child, child.CheckState);
            }
        }
        /// <summary>
        /// Lists the authorization commands.
        /// </summary>
        /// <param name="authNode">The auth node.</param>
        public void ListAuthorizationCommands(AuthorizationNode authNode)
        {
            using (WaitCursor cursor = new WaitCursor(true))
            {
                Dictionary<string, TreeListNode> categories = new Dictionary<string, TreeListNode>(); // 分组
                tlCommands.BeginUpdate();
                tlCommands.ClearNodes();
                try
                {
                    if (authNode != null)
                    {
                        foreach (AuthorizationCommand cmd in authNode.Commands)
                        {
                            if (!categories.ContainsKey(cmd.Category))
                                categories.Add(cmd.Category, GetCategoryNode(cmd.Category));

                            TreeListNode parent = categories[cmd.Category];
                            TreeListNode child = tlCommands.AppendNode(new object[] { cmd.Name, cmd.CommandUri, cmd.Image }, parent, cmd);
                            child.ImageIndex = 3;
                            child.SelectImageIndex = 3;
                        }
                    }
                }
                finally
                {
                    tlCommands.ExpandAll();
                    tlCommands.EndUpdate();
                }
            }
        }
        public void LoadAuthorizationsNodes()
        {
            using (WaitCursor cursor = new WaitCursor(true)) {
                tlAuth.BeginUpdate();
                tlAuth.FocusedNodeChanged -= new FocusedNodeChangedEventHandler(tlAuth_FocusedNodeChanged);
                tlAuth.Nodes.Clear();
                try
                {
                    IList<AuthorizationNode> nodes = Presenter.AuthorizationNodeService.GetAll();

                    AuthorizationNode authNode = new AuthorizationNode()
                    {
                        Id = "Shell",
                        Name = "系统权限"
                    };
                    authNode.AuthorizationUri = GlobalConstants.Uri_Separator + "Shell";
                    TreeListNode tlNode = tlAuth.AppendNode(new object[] { authNode.Name, authNode.Id }, -1, authNode);
                    tlNode.Tag = authNode;
                    ((AuthorizationNode)tlNode.Tag).AuthorizationUri = GetAuthrizationNodePath(tlNode);
                    tlNode.ImageIndex = 0;
                    tlNode.SelectImageIndex = 1;

                    foreach (AuthorizationNode child in nodes)
                    {
                        LoadAuthorizationNode(child, tlAuth.Nodes[0]);
                    }
                    tlAuth.Nodes[0].ExpandAll(); // 展开所有子节点
                }
                finally {
                    tlAuth.EndUpdate();
                    tlAuth.FocusedNodeChanged += new FocusedNodeChangedEventHandler(tlAuth_FocusedNodeChanged);
                }
            }

        }
        private void LoadAuthorizationNode(AuthorizationNode authNode, TreeListNode tlNode)
        {
            string[] authPath = authNode.AuthorizationUri.Split(new string[] { GlobalConstants.Uri_Separator }, StringSplitOptions.None);
            TreeListNode current = tlNode;

            if (authPath.Length < 1)
                return;

            for (int i = 2; i < authPath.Length; ++i)
            {
                bool found = false;
                foreach (TreeListNode node in current.Nodes)
                {
                    string id = node.GetDisplayText(colId);
                    if (id == authPath[i])
                    {
                        current = node;
                        found = true;
                        break;
                    }
                }

                if (found)
                {
                    if (i == authPath.Length - 1 && current.Tag == null)
                    {
                        current.SetValue(colNode, authNode.Name);
                        current.Tag = authNode;
                    }
                }
                else
                {
                    TreeListNode child = tlAuth.AppendNode(new object[] { "tempnode", authPath[i] }, current); // 我们创建的可能是中间节点
                    child.ImageIndex = 0;
                    child.SelectImageIndex = 1;

                    // 设置最终要创建的节点的相关属性
                    if (i == authPath.Length - 1)
                    {
                        child.SetValue(colNode, authNode.Name);
                        child.Tag = authNode;
                        LoadAuthorizationCommandNode(child, authNode);
                    }
                    current = child;
                }
            }
        }
        /// <summary>
        /// 加载权限节点下的操作项
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="authNode">The auth node.</param>
        private void LoadAuthorizationCommandNode(TreeListNode parent, AuthorizationNode authNode)
        {
            Dictionary<string, TreeListNode> categories = new Dictionary<string, TreeListNode>(); // 分组
            foreach (AuthorizationCommand cmd in authNode.Commands) {

                if (!categories.ContainsKey(cmd.Category))
                    categories[cmd.Category] = GetCategoryNode(cmd.Category, parent);
                TreeListNode category = categories[cmd.Category];
                TreeListNode child = tlAuth.AppendNode(new object[] { cmd.Name, cmd.CommandUri }, category, cmd);
                child.ImageIndex = 3;
                child.SelectImageIndex = 3;
                
                // 设置当前操作的权限操作
                string authorizationUri = GetAuthorizationUri(child);
                child.Checked = authStore.CanExecute(SecurityUtility.HashObject(authorizationUri + cmd.CommandUri));
                SetCheckedParentNodes(child, child.CheckState); 
            }
        }
        /// <summary>
        /// 加载所有的权限节点信息
        /// </summary>
        private void LoadAuthorizationNodes(AuthorizationStore store)
        {
            using (WaitCursor cursor = new WaitCursor(true))
            {
                tlAuth.BeginUpdate();
                tlAuth.Nodes.Clear();
                try
                {
                    AuthorizationNode authNode = new AuthorizationNode() {
                        Id = "Shell",
                        Name = "系统权限"
                    };
                    authNode.AuthorizationUri = GlobalConstants.Uri_Separator + "Shell";
                    TreeListNode tlNode = tlAuth.AppendNode(new object[] { authNode.Name, authNode.Id }, -1, authNode);
                    tlNode.Tag = authNode;
                    ((AuthorizationNode)tlNode.Tag).AuthorizationUri = GetAuthrizationNodePath(tlNode);
                    tlNode.ImageIndex = 0;
                    tlNode.SelectImageIndex = 1;

                    // 加载角色的相关权限节点
                    foreach (AuthorizationNode child in store.Nodes) {
                        LoadAuthorizationNode(child, tlAuth.Nodes[0]);
                    }
                    tlAuth.Nodes[0].ExpandAll(); // 展开所有子节点
                }
                finally
                {
                    tlAuth.EndUpdate();
                }
            }
        }
        private AuthorizationBehavior toBehavior(AuthorizationNode node)
        {
            AuthorizationBehavior behavior = null;
            using (var runtime = FubuRuntime.Basic())
            {
                behavior = (AuthorizationBehavior) runtime.Get<IContainer>().GetInstance<IActionBehavior>(node.As<IContainerModel>().ToInstance());
            }

            return behavior;
        }
        /// <summary>
        /// 存储授权节点
        /// </summary>
        /// <param name="node">授权节点</param>
        public void Store(AuthorizationNode node)
        {
            Guard.ArgumentNotNull(node, "node");

            Dictionary<string, AuthorizationAction> acs = new Dictionary<string, AuthorizationAction>();
            //// 原来的授权信息
            //foreach (AuthorizationCommand cmd in node.Commands) {
            //    string key = SecurityUtility.HashObject(node.AuthorizationUri + cmd.CommandUri);
            //    if (actions.ContainsKey(key))
            //        acs[key] = actions[key];
            //}

            //Remove(node.AuthorizationUri); // 删除原来的授权信息
            //authorizations[node.AuthorizationUri] = node;
            //foreach (AuthorizationCommand cmd in node.Commands) {
            //    string key = SecurityUtility.HashObject(node.AuthorizationUri + cmd.CommandUri);
            //    if (acs.ContainsKey(key))
            //        actions[key] = acs[key];
            //    else
            //        actions[key] = AuthorizationAction.Deny;
            //}

            // 首先删除原来的授权信息
            foreach (AuthorizationCommand cmd in node.Commands) {
                string key = SecurityUtility.HashObject(node.AuthorizationUri + cmd.CommandUri);
                AuthorizationActionInfo actionInfo = new AuthorizationActionInfo(key, cmd.Action);
                if (actions.Contains(actionInfo))
                    actions.Remove(actionInfo);
            }
            Remove(node.AuthorizationUri);
            authorizations.Add(node);
            foreach (AuthorizationCommand cmd in node.Commands) {
                string key = SecurityUtility.HashObject(node.AuthorizationUri + cmd.CommandUri);
                AuthorizationActionInfo actionInfo = new AuthorizationActionInfo(key, cmd.Action);
                actions.Add(actionInfo);
            }
        }
        public void with_rules()
        {
            var node = new AuthorizationNode();
            node.AddRole("Role A");

            node.HasRules().ShouldBeTrue();
        }
        public void use_custom_auth_failure_handler_by_type()
        {
            var node = new AuthorizationNode();
            node.FailureHandler<FakeAuthHandler>();

            var def = node.As<IContainerModel>().ToInstance().As<IConfiguredInstance>();

            def.FindDependencyDefinitionFor<IAuthorizationFailureHandler>()
                .ReturnedType.ShouldBe(typeof (FakeAuthHandler));
        }
        public void use_custom_failure_handler_by_value()
        {
            var node = new AuthorizationNode();

            var handler = new FakeAuthHandler();

            node.FailureHandler(handler);

            var def = node.As<IContainerModel>().ToInstance().As<IConfiguredInstance>();

            def.FindDependencyValueFor<IAuthorizationFailureHandler>().ShouldBeTheSameAs(handler);
        }
 /// <summary>
 /// 保存授权节点
 /// </summary>
 /// <param name="node">授权节点</param>
 public void Save(AuthorizationNode node)
 {
     Delete(node);
     db.Store(node);
 }
 /// <summary>
 /// 删除授权节点信息
 /// </summary>
 /// <param name="node">授权节点</param>
 public void Remove(AuthorizationNode node)
 {
     if (node != null && !String.IsNullOrEmpty(node.AuthorizationUri))
         Remove(node.AuthorizationUri);
 }
        public void use_no_custom_auth_failure_handler()
        {
            var node = new AuthorizationNode();
            var def = node.As<IContainerModel>().ToInstance().As<IConfiguredInstance>();

            def.FindDependencyDefinitionFor<IAuthorizationFailureHandler>().ShouldBeNull();
        }