예제 #1
0
        public static AuthorizationResult Authorize(object target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            if (DesignerProperties.IsInDesignTool)
            {
                return(AuthorizationResult.Allowed);
            }

            AuthorizationSource source =
                Authorization.SourceFactory.CreateSource(AuthorizationRuleManager.GetAuthorizationAttributes(target));

            if (source == null)
            {
                throw new InvalidOperationException("Factory cannot return a null source.");
            }

            DependencyObject targetDo = target as DependencyObject;

            if (targetDo != null)
            {
                if (Authorization.GetBehaviorManager(targetDo) == null)
                {
                    Authorization.SetBehaviorManager(targetDo, new AuthorizationBehaviorManager(targetDo));
                }
                Authorization.GetBehaviorManager(targetDo).SetBehaviors(AuthorizationRuleManager.GetAuthorizationBehaviors(target), source);
            }

            return(source.Result);
        }
        public override void AddBehavior(object target, AuthorizationSource source)
        {
            DependencyObject targetDo = target as DependencyObject;

            if (targetDo != null)
            {
                BindingOperations.SetBinding(
                    targetDo,
                    PropertyBindingBehavior.GetDependencyProperty(target, this.PropertyName),
                    new Binding("Result")
                {
                    Source = source, Converter = new AuthorizationConverter()
                });
            }
        }
        public AuthorizationCommand(AuthorizationSource source, ICommand command)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            this._source  = source;
            this._command = command;

            this._source.PropertyChanged    += this.SourcePropertyChanged;
            this._command.CanExecuteChanged += this.CommandCanExecuteChanged;
        }
        public void SetBehaviors(IEnumerable <AuthorizationBehavior> behaviors, AuthorizationSource source)
        {
            if (this._behaviors != behaviors)
            {
                if (this._behaviors != null)
                {
                    foreach (AuthorizationBehavior behavior in this._behaviors)
                    {
                        behavior.RemoveBehavior(this._target);
                    }
                }

                this._behaviors = behaviors;

                if (this._behaviors != null)
                {
                    foreach (AuthorizationBehavior behavior in this._behaviors)
                    {
                        behavior.AddBehavior(this._target, source);
                    }
                }
            }
        }