views:

179

answers:

1

Hey guys, I am writing an application that retrieves the Group Names and access permissions for each NT group populated from the foreach loop. In addition, I have included a DataGridView control where each cell has a checkbox column, the application is going to check each cell accordingly e.g Read, Write, Modify, etc for each group. I can not for the life of me, figure out how to check these boxes accordingly. The code snippet below demonstrates what I am trying to do with a standard DataGridView control textbox column, but I would like to make these checkboxes rather than textboxes. Any feedback would be greatly appreciated. In the code snippet be below Property is the path that is passed in from another method.

 private void CheckDirPermissions(ResultProperty Property)
    {
        if (Property.Type == typeof(string) && !Property.IsArray)
        {
            try
            {
                FileSecurity folderSecurity = File.GetAccessControl(Property.String);
                foreach (FileSystemAccessRule fileSystemAccessRule in folderSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
                {



                    string IdentityReference = fileSystemAccessRule.IdentityReference.ToString();
                    string AccessControlType = fileSystemAccessRule.AccessControlType.ToString();
                    string filesystemrights = fileSystemAccessRule.FileSystemRights.ToString();
                    string IsInherited = fileSystemAccessRule.IsInherited.ToString();




                    DataGridDirPermissions.Rows.Add(IdentityReference,
                                                    filesystemrights,                                       
                                                    AccessControlType,
                                                    IsInherited);

                }
            }
            catch (Exception)
            {
                MessageBox.Show("Path does not exist.", "Path Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else return;
    }
+1  A: 

You have to define the Read, Write, Modify, etc. columns of your DataGridView of type DataGridViewCheckBoxColumn. Then, when you read the permissions for each group, you can get the corresponding boolean values for each permission and create the row with these values:

foreach (FileSystemAccessRule fileSystemAccessRule in folderSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
{
    string IdentityReference = fileSystemAccessRule.IdentityReference.ToString();

    AccessControlType accessControlType = fileSystemAccessRule.AccessControlType;
    FileSystemRights filesystemrights = fileSystemAccessRule.FileSystemRights;
    bool isInherited = fileSystemAccessRule.IsInherited;

    // .. Get specific permissions ...

    bool allowControlType = accessControlType == AccessControlType.Allow;
    bool canRead = (filesystemrights & FileSystemRights.Read) == FileSystemRights.Read;
    bool canWrite = (filesystemrights & FileSystemRights.Write) == FileSystemRights.Write;
    bool canExecute = (filesystemrights & FileSystemRights.ExecuteFile) == FileSystemRights.ExecuteFile;

    // ... Any more specific permissions ...

    dataGridView1.Rows.Add(IdentityReference, allowControlType, canRead, canWrite, canExecute, ... );
}

That way your DataGridView will have the Group Name in the first cell (as a TextBox) and a CheckBox for each specific permission, like:

Everyone                    (check)      (check)     (no check)   (no check)
BUILTIN\Administrators      (check)      (check)     (check)      (check)
BUILTIN\Users               (check)      (check)     (check)      (no check)

and so on...

alexphi
This works perfectly. Thanks a lot Alexphi
Sanch01R