Who am I? How to use Microsoft Office Permission/UserPermission

前端 未结 3 365
孤城傲影
孤城傲影 2021-01-02 16:41

Microsoft Office documents, im my case: PowerPoint presentations, can have restricted permissions. How can I find out, programmatically, which permissions my code has on a g

相关标签:
3条回答
  • 2021-01-02 17:13

    I have opened a ticket with Microsoft on this (SRQ091221600157). After a lengthy discussion with Microsoft Support, the ticket is still pending but I think it is already safe to say that there is no explicit way to obtain the information I need.

    Microsoft explicitly states that there is no API in PowerPoint to obtain either the identity that was used to open a presentation, or the currently active permissions. A feature request to add that API has been filed.

    If you are in a closed environment with your own Rights Management Server, the following approaches would probably work (quoting Microsoft Support, I did not test this myself):

    1) Using the COM object ADSystemInfo object.

    Dim objADSystemInfo As Object
    Dim objUser As Object
    objADSystemInfo = CreateObject("ADSystemInfo")
    objUser = GetObject("LDAP://" + objADSystemInfo.UserName)
    objUser.Get("mail")  'This will return the AD email id 
    
    'We can use this to include in the permission related code that you had sent
    If (uperm.UserId = objUser.Get("mail")) Then
        'You can get the permission uperm.Permission for this userid (current logged in)
        MsgBox(uperm.UserId & "logged in user") 
    Else
        MsgBox(uperm.UserId & "other user")
    End If
    

    2) Using the .NET approach

    Dim oDS = New System.DirectoryServices.DirectorySearcher
    Dim strUserName As String = Environment.UserName
    Dim strFilter As String = "(&(objectCategory=User)(samAccountName=" & strUserName & "))"
    oDS.Filter = strFilter
    Dim oSr As System.DirectoryServices.SearchResult = oDS.FindOne()
    Dim oUser As System.DirectoryServices.DirectoryEntry
    oUser = oSr.GetDirectoryEntry()
    MessageBox.Show(oUser.InvokeGet("mail"))
    

    Here is the article that explains about these approaches –
    http://www.microsoft.com/technet/scriptcenter/resources/pstips/dec07/pstip1207.mspx

    However, these approaches do not work for identities that use online IRM services (Microsoft Passport). Also, even with your own Rights Management Server, it may be possible to change your identity in PowerPoint at runtime, in which case the above approaches probably would not yield the desired results (I did not investigate this any further).

    I the end, I had to come up with a workaround that tests the permissions I need by trying to run some representative API call and then checking if the call failed.

    Thank you for your contributions,
    Volker

    0 讨论(0)
  • 2021-01-02 17:24

    Try one of the functions GetUserName(), GetUserNameW() or GetUserNameA() and declare it thusly:

    Private Declare Function GetUserName Lib "advapi32.dll" Alias _
        "GetUserName" (ByVal lpBuffer As String, nSize As Long) As Long
    

    Also see MSDN about GetUserName.

    You need to dim a string with length 255 and pass 254 as parameter nSize. This string is passed ByVal back to the caller. Perhaps you need to left() the string before you can use it to compare it with uperm.UserId.

    0 讨论(0)
  • 2021-01-02 17:24

    Today I received an additional answer from Microsoft (still regarding SRQ091221600157) which actually seems to solve the problem, at least in my particular instance. This approach still smells like work-around and there is no documentation that would confirm that it actually works, but it seems plausible enough and withstands some ad-hoc tests. And, it feels much less patchy than any other work-around I came up with. It goes like this:

    Only users with msoPermissionFullControl can see permissions of other users (undocumented assumption). Thus, if a user does not have msoPermissionFullControl, the Permission collection contains exactly one item and this item reflects the current user's permissions. If the permission collection contains multiple items, this means that the current user must have msoPermissionFullControl. Also, the current user must be visible in the Permission collection, but there is still no way to find out which of the identities in the Permission collection represents the current user.

    0 讨论(0)
提交回复
热议问题