Exemplo n.º 1
0
        public ComputerGroupsViewModel()
        {
            OpenEditMemberOf = ReactiveCommand.CreateFromObservable(() => _dialogRequests.Handle(new DialogInfo(new EditMemberOfDialog(), _computer.Principal.SamAccountName)));

            SaveDirectGroups = ReactiveCommand.CreateFromObservable(() =>
            {
                var saveFileDialog = new SaveFileDialog {
                    Filter = ExcelService.ExcelFileFilter, FileName = _computer.CN
                };
                return(saveFileDialog.ShowDialog() ?? false ? ExcelService.SaveGroupsToExcelFile(_directGroups, saveFileDialog.FileName) : Observable.Return(Unit.Default));
            });

            FindDirectGroup = ReactiveCommand.CreateFromTask(() => NavigationService.ShowWindow <GroupWindow>(_selectedDirectGroup));

            this.WhenActivated(disposables =>
            {
                Observable.Merge(
                    this.WhenAnyValue(vm => vm.Computer).WhereNotNull(),
                    OpenEditMemberOf.Select(_ => Computer))
                .Do(_ => _directGroups.Clear())
                .SelectMany(computerObject => GetDirectGroups(computerObject, TaskPoolScheduler.Default))
                .Select(directoryEntry => directoryEntry.Properties.Get <string>("cn"))
                .ObserveOnDispatcher()
                .Subscribe(cn => _directGroups.Add(cn))
                .DisposeWith(disposables);

                Observable.Merge <(string Title, string Message)>(
                    OpenEditMemberOf.ThrownExceptions.Select(ex => (("Could not open dialog", ex.Message))),
                    SaveDirectGroups.ThrownExceptions.Select(ex => (("Could not save groups", ex.Message))),
                    FindDirectGroup.ThrownExceptions.Select(ex => (("Could not open group", ex.Message))))
                .SelectMany(dialogContent => _messages.Handle(new MessageInfo(MessageType.Error, dialogContent.Message, dialogContent.Title)))
                .Subscribe()
                .DisposeWith(disposables);
            });
        }
Exemplo n.º 2
0
        public GroupGroupsViewModel()
        {
            _allMemberOfGroupsView = new ListCollectionView(_allMemberOfGroups)
            {
                Filter           = TextFilter,
                SortDescriptions = { new SortDescription() }
            };

            OpenEditMemberOf = ReactiveCommand.CreateFromObservable(() => _dialogRequests.Handle(new DialogInfo(new EditMemberOfDialog(), _group.CN)));

            SaveDirectGroups = ReactiveCommand.CreateFromObservable(() =>
            {
                var saveFileDialog = new SaveFileDialog {
                    Filter = ExcelService.ExcelFileFilter, FileName = _group.CN
                };
                return(saveFileDialog.ShowDialog() ?? false ? ExcelService.SaveGroupsToExcelFile(_directMemberOfGroups, saveFileDialog.FileName) : Observable.Return(Unit.Default));
            });

            FindDirectMemberOfGroup = ReactiveCommand.CreateFromTask(() => NavigationService.ShowWindow <GroupWindow>(_selectedDirectMemberOfGroup));

            GetAllGroups = ReactiveCommand.CreateFromObservable(
                () =>
            {
                _allMemberOfGroups.Clear();
                return(_group.Principal.GetAllGroups(TaskPoolScheduler.Default)
                       .Select(directoryEntry => directoryEntry.Properties.Get <string>("name"))
                       .TakeUntil(this.WhenAnyValue(vm => vm.IsShowingMemberOf).Where(false)));
            },
                this.WhenAnyValue(vm => vm.IsShowingMemberOf));

            FindAllMemberOfGroup = ReactiveCommand.CreateFromTask(() => NavigationService.ShowWindow <GroupWindow>(_selectedAllMemberOfGroup));

            SaveAllGroups = ReactiveCommand.CreateFromObservable(() =>
            {
                var saveFileDialog = new SaveFileDialog {
                    Filter = ExcelService.ExcelFileFilter, FileName = _group.CN
                };
                return(saveFileDialog.ShowDialog() ?? false ? ExcelService.SaveGroupsToExcelFile(_allMemberOfGroups, saveFileDialog.FileName) : Observable.Return(Unit.Default));
            });

            OpenEditMembers = ReactiveCommand.CreateFromObservable(() => _dialogRequests.Handle(new DialogInfo(new EditMembersDialog(), _group.CN)));

            SaveMembers = ReactiveCommand.CreateFromObservable(() =>
            {
                var saveFileDialog = new SaveFileDialog {
                    Filter = ExcelService.ExcelFileFilter, FileName = _group.CN
                };
                return(saveFileDialog.ShowDialog() ?? false ? ExcelService.SaveUsersToExcelFile(_members, saveFileDialog.FileName) : Observable.Return(Unit.Default));
            });

            FindMember = ReactiveCommand.CreateFromTask(() =>
            {
                var principalType = _adFacade.DeterminePrincipalType(_selectedMember);
                if (principalType == PrincipalType.Group)
                {
                    return(NavigationService.ShowWindow <GroupWindow>(_selectedMember));
                }
                else if (principalType == PrincipalType.Computer)
                {
                    return(NavigationService.ShowWindow <ComputerWindow>(_selectedMember));
                }
                else
                {
                    return(NavigationService.ShowWindow <UserWindow>(_selectedMember));
                }
            });

            this.WhenActivated(disposables =>
            {
                this
                .WhenAnyValue(vm => vm.FilterString, y => y.UseFuzzy)
                .Subscribe(_ => _allMemberOfGroupsView?.Refresh())
                .DisposeWith(disposables);

                GetAllGroups
                .ObserveOnDispatcher()
                .Subscribe(groupName => _allMemberOfGroups.Add(groupName))
                .DisposeWith(disposables);

                GetAllGroups
                .ThrownExceptions
                .SelectMany(ex => _messages.Handle(new MessageInfo(MessageType.Error, ex.Message, "Could not get groups")))
                .Subscribe()
                .DisposeWith(disposables);

                this
                .WhenAnyValue(vm => vm.IsShowingMemberOf, vm => vm.IsShowingMembers, (isShowingMemberOf, isShowingMembers) => isShowingMemberOf || isShowingMembers)
                .Where(true)
                .Subscribe(_ => IsShowingDirectMemberOf = false)
                .DisposeWith(disposables);

                this
                .WhenAnyValue(vm => vm.IsShowingDirectMemberOf, vm => vm.IsShowingMembers, (isShowingDirectMemberOf, isShowingMembers) => isShowingDirectMemberOf || isShowingMembers)
                .Where(true)
                .Subscribe(_ => IsShowingMemberOf = false)
                .DisposeWith(disposables);

                this
                .WhenAnyValue(vm => vm.IsShowingDirectMemberOf, vm => vm.IsShowingMemberOf, (isShowingDirectMemberOf, isShowingMemberOf) => isShowingDirectMemberOf || isShowingMemberOf)
                .Where(true)
                .Subscribe(_ => IsShowingMembers = false)
                .DisposeWith(disposables);

                Observable.Merge(
                    this.WhenAnyValue(vm => vm.Group).WhereNotNull(),
                    OpenEditMemberOf.Select(_ => _group))
                .Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler)
                .Do(_ => _directMemberOfGroups.Clear())
                .SelectMany(group => GetDirectGroups(group.CN, TaskPoolScheduler.Default))
                .ObserveOnDispatcher()
                .Subscribe(cn => _directMemberOfGroups.Add(cn))
                .DisposeWith(disposables);

                Observable.Merge(
                    this.WhenAnyValue(vm => vm.Group).WhereNotNull(),
                    OpenEditMembers.Select(_ => _group))
                .Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler)
                .Do(_ => _members.Clear())
                .SelectMany(group => GetMembers(group.CN, TaskPoolScheduler.Default))
                .ObserveOnDispatcher()
                .Subscribe(cn => _members.Add(cn))
                .DisposeWith(disposables);

                Observable.Merge <(string Title, string Message)>(
                    OpenEditMemberOf.ThrownExceptions.Select(ex => (("Could not open dialog", ex.Message))),
                    SaveDirectGroups.ThrownExceptions.Select(ex => (("Could not save groups", ex.Message))),
                    FindDirectMemberOfGroup.ThrownExceptions.Select(ex => (("Could not open group", ex.Message))),
                    FindAllMemberOfGroup.ThrownExceptions.Select(ex => (("Could not find all groups", ex.Message))),
                    SaveAllGroups.ThrownExceptions.Select(ex => (("Could not save groups", ex.Message))),
                    OpenEditMembers.ThrownExceptions.Select(ex => (("Could not open dialog", ex.Message))),
                    SaveMembers.ThrownExceptions.Select(ex => (("Could not save members", ex.Message))),
                    FindMember.ThrownExceptions.Select(ex => (("Could not open AD object", ex.Message))))
                .SelectMany(dialogContent => _messages.Handle(new MessageInfo(MessageType.Error, dialogContent.Message, dialogContent.Title)))
                .Subscribe()
                .DisposeWith(disposables);
            });
        }
Exemplo n.º 3
0
        public UserGroupsViewModel()
        {
            _allGroupsCollectionView = new ListCollectionView(_allGroups);
            _allGroupsCollectionView.SortDescriptions.Add(new SortDescription());
            _allGroupsCollectionView.Filter = TextFilter;

            OpenEditMemberOf = ReactiveCommand.CreateFromObservable(() => _dialogRequests.Handle(new DialogInfo(new EditMemberOfDialog(), _user.Principal.SamAccountName)));

            SaveDirectGroups = ReactiveCommand.CreateFromObservable(() =>
            {
                var saveFileDialog = new SaveFileDialog {
                    Filter = ExcelService.ExcelFileFilter, FileName = _user.CN
                };
                return(saveFileDialog.ShowDialog() ?? false ? ExcelService.SaveGroupsToExcelFile(_directGroups, saveFileDialog.FileName) : Observable.Return(Unit.Default));
            });

            SaveAllGroups = ReactiveCommand.CreateFromObservable(() =>
            {
                var saveFileDialog = new SaveFileDialog {
                    Filter = ExcelService.ExcelFileFilter, FileName = _user.CN
                };
                return(saveFileDialog.ShowDialog() ?? false ? ExcelService.SaveGroupsToExcelFile(_allGroups, saveFileDialog.FileName) : Observable.Return(Unit.Default));
            });

            GetAllGroups = ReactiveCommand.CreateFromObservable(
                () =>
            {
                AllGroups.Clear();
                return(User.Principal.GetAllGroups(TaskPoolScheduler.Default)
                       .TakeUntil(this.WhenAnyValue(vm => vm.IsShowingAllGroups).Where(false)));
            },
                this.WhenAnyValue(vm => vm.IsShowingAllGroups));
            GetAllGroups
            .Select(group => group.Properties.Get <string>("cn"))
            .ObserveOnDispatcher()
            .Subscribe(groupName => AllGroups.Add(groupName));

            _isLoadingGroups = GetAllGroups
                               .IsExecuting
                               .ToProperty(this, vm => vm.IsLoadingGroups);

            FindDirectGroup = ReactiveCommand.CreateFromTask(() => NavigationService.ShowWindow <GroupWindow>(_selectedDirectGroup));

            FindAllGroup = ReactiveCommand.CreateFromTask(() => NavigationService.ShowWindow <GroupWindow>(_selectedAllGroup));

            this.WhenActivated(disposables =>
            {
                this
                .WhenAnyValue(vm => vm.GroupFilter, vm => vm.UseFuzzy)
                .Subscribe(_ => AllGroupsCollectionView?.Refresh())
                .DisposeWith(disposables);

                GetAllGroups
                .ThrownExceptions
                .SelectMany(ex => _messages.Handle(new MessageInfo(MessageType.Error, ex.Message, "Couldn't get groups")))
                .Subscribe()
                .DisposeWith(disposables);

                Observable.Merge(
                    this.WhenAnyValue(vm => vm.User).WhereNotNull(),
                    OpenEditMemberOf.Select(_ => User))
                .Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler)
                .Do(_ => _directGroups.Clear())
                .SelectMany(user => GetDirectGroups(user.Principal.SamAccountName, TaskPoolScheduler.Default))
                .Select(group => group.Properties.Get <string>("cn"))
                .ObserveOnDispatcher()
                .Subscribe(groupCn => _directGroups.Add(groupCn))
                .DisposeWith(disposables);

                this
                .WhenAnyValue(vm => vm.IsShowingDirectGroups)
                .Where(true)
                .Subscribe(_ => IsShowingAllGroups = false)
                .DisposeWith(disposables);

                this
                .WhenAnyValue(vm => vm.IsShowingAllGroups)
                .Where(true)
                .Subscribe(_ => IsShowingDirectGroups = false)
                .DisposeWith(disposables);

                Observable.Merge <(string Title, string Message)>(
                    OpenEditMemberOf.ThrownExceptions.Select(ex => ("Could not open dialog", ex.Message)),
                    SaveAllGroups.ThrownExceptions.Select(ex => ("Could not save groups", ex.Message)),
                    SaveDirectGroups.ThrownExceptions.Select(ex => ("Could not save groups", ex.Message)),
                    FindDirectGroup.ThrownExceptions.Select(ex => ("Could not open group", ex.Message)),
                    FindAllGroup.ThrownExceptions.Select(ex => ("Could not open group", ex.Message)))
                .SelectMany(dialogContent => _messages.Handle(new MessageInfo(MessageType.Error, dialogContent.Message, dialogContent.Title)))
                .Subscribe()
                .DisposeWith(disposables);
            });
        }