private void LoadSecurityPermissions(object argument)
        {
            var dialog = new OpenFileDialog();

            dialog.Title  = "Please select the security permissions file (*.xml) to load.";
            dialog.Filter = "XML Files (*.xml)|*.xml";
            var result = dialog.ShowDialog(Application.Current.MainWindow);

            if (result == true)
            {
                try
                {
                    var persistedPermissions = PermissionChangePersistenceData.Load(dialog.FileName);
                    this.SecurityGroupChange.ResetPermissionChanges();
                    foreach (var persistedPermission in persistedPermissions)
                    {
                        var group = this.SecurityGroupChange.PermissionGroupChanges.FirstOrDefault(g => g.PermissionGroup.Scope == persistedPermission.Scope);
                        if (group != null)
                        {
                            var permission = group.PermissionChanges.FirstOrDefault(p => p.Permission.Scope == persistedPermission.Scope && string.Equals(p.Permission.PermissionConstant, persistedPermission.Name, StringComparison.OrdinalIgnoreCase));
                            if (permission != null)
                            {
                                permission.Action = persistedPermission.Action;
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    this.Logger.Log(string.Format(CultureInfo.CurrentCulture, "An error occurred while loading the security permissions from \"{0}\"", dialog.FileName), exc);
                    MessageBox.Show("An error occurred while loading the security permissions. See the log file for details", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
        private void SaveSecurityPermissions(object argument)
        {
            var dialog = new SaveFileDialog();

            dialog.Title  = "Please select the security permissions file (*.xml) to save.";
            dialog.Filter = "XML Files (*.xml)|*.xml";
            var result = dialog.ShowDialog(Application.Current.MainWindow);

            if (result == true)
            {
                try
                {
                    PermissionChangePersistenceData.Save(dialog.FileName, this.SecurityGroupChange.PermissionGroupChanges.SelectMany(g => g.PermissionChanges).Select(p => new PermissionChangePersistenceData(p)).ToArray());
                }
                catch (Exception exc)
                {
                    this.Logger.Log(string.Format(CultureInfo.CurrentCulture, "An error occurred while saving the security permissions to \"{0}\"", dialog.FileName), exc);
                    MessageBox.Show("An error occurred while saving the security permissions. See the log file for details", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
        public static void ExportPermissions(ILogger logger, ApplicationTask task, TfsTeamProjectCollection tfs, TfsMajorVersion tfsVersion, IList <SecurityGroupPermissionExportRequest> exportRequests)
        {
            if (exportRequests.Any())
            {
                var step = 0;
                var securityNamespaces = tfs.GetService <ISecurityService>().GetSecurityNamespaces();
                var ims = tfs.GetService <IIdentityManagementService>();
                foreach (var exportRequest in exportRequests)
                {
                    task.SetProgress(step++, string.Format(CultureInfo.CurrentCulture, "Exporting \"{0}\" permissions from Team Project \"{1}\"", exportRequest.SecurityGroup.Name, exportRequest.SecurityGroup.TeamProject.Name));
                    try
                    {
                        var identity = ims.ReadIdentity(IdentitySearchFactor.Identifier, exportRequest.SecurityGroup.Sid, MembershipQuery.None, ReadIdentityOptions.None);
                        if (identity == null)
                        {
                            var message = "The security group \"{0}\" could not be retrieved.".FormatCurrent(exportRequest.SecurityGroup.FullName);
                            logger.Log(message, TraceEventType.Warning);
                            task.SetWarning(message);
                        }
                        else
                        {
                            var permissions = new List <PermissionChangePersistenceData>();
                            foreach (var securityNamespace in securityNamespaces)
                            {
                                foreach (var factory in permissionGroupFactories)
                                {
                                    if (factory.AppliesTo(securityNamespace.Description.NamespaceId))
                                    {
                                        var tokens = factory.GetObjectTokens(tfs, tfsVersion, exportRequest.SecurityGroup.TeamProject.Name, exportRequest.SecurityGroup.TeamProject.Uri.ToString());
                                        if (tokens != null)
                                        {
                                            var permissionGroup = factory.GetPermissionGroup(securityNamespace);
                                            foreach (var token in tokens)
                                            {
                                                var acl = securityNamespace.QueryAccessControlList(token, new[] { identity.Descriptor }, false);
                                                foreach (var ace in acl.AccessControlEntries)
                                                {
                                                    foreach (var permission in permissionGroup.Permissions)
                                                    {
                                                        var action = PermissionChangeAction.Inherit;
                                                        if ((permission.PermissionBit & ace.Allow) == permission.PermissionBit)
                                                        {
                                                            action = PermissionChangeAction.Allow;
                                                        }
                                                        else if ((permission.PermissionBit & ace.Deny) == permission.PermissionBit)
                                                        {
                                                            action = PermissionChangeAction.Deny;
                                                        }
                                                        permissions.Add(new PermissionChangePersistenceData(permission.Scope, permission.PermissionConstant, action));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            Directory.CreateDirectory(Path.GetDirectoryName(exportRequest.FileName));
                            PermissionChangePersistenceData.Save(exportRequest.FileName, permissions);
                        }
                    }
                    catch (Exception exc)
                    {
                        var message = string.Format(CultureInfo.CurrentCulture, "An error occurred while exporting \"{0}\" permissions from Team Project \"{1}\"", exportRequest.SecurityGroup.Name, exportRequest.SecurityGroup.TeamProject.Name);
                        logger.Log(message, exc);
                        task.SetError(message, exc);
                    }
                    if (task.IsCanceled)
                    {
                        task.Status = "Canceled";
                        break;
                    }
                }
            }
        }