modCurrentUser.bas

' ============================================================
' Copyright (c) 2025-2026 Peter Bryant (Corylus Business Systems)
'                    and  Andrew Richards (The IT Service Ltd)
' Part of VBAuth - https://vbauth.com
' Licensed under the MIT Licence - see LICENCE.txt
' This copyright notice must not be removed or altered.
' ============================================================

' ============================================================
' modCurrentUser - VBAuth example: Get current user details
' ============================================================
' Demonstrates the standard VBAuth call pattern:
'   1. Set intAppID to identify the API configuration record
'   2. Call SetEntraAPPIdValues to load credentials and build URL
'   3. Call AcquireBearerToken to obtain or reuse a valid token
'   4. Make the HTTP call using the global verb, URL and token
'   5. Parse the JSON response with JSONConverter
'
' Admin table record required: AppID = 1
'   Verb:       GET
'   URLPart1:   https://graph.microsoft.com/v1.0/me
'   ScopeClaims: User.Read
' ============================================================

' ------------------------------------------------------------
' DEPENDENCIES - required modules in any target database:
'   modVBAuthGlobalConstants  (globals, SetEntraAPPIdValues,
'                              AcquireBearerToken)
'   JSONConverter              (Tim Hall VBA-JSON parser)
' ------------------------------------------------------------
' PORTING NOTE: WhoAmI is a setup verification function.
' It is not intended for porting to production databases.

Option Compare Database
Option Explicit

Public Function WhoAmI(ByRef strResult As String) As Boolean
Dim http        As Object
Dim strResponse As String
Dim strWhoAmI   As String
Dim strPhones   As String
Dim varPhone    As Variant
Dim objJson     As Object
    
    'default fail
    WhoAmI = False
    
    On Error GoTo ErrorHandler
    
    intAppID = 1
   
    blnResult = SetEntraAPPIdValues(False, False)
    If blnResult = False Then
        'note the called function will feedback the error situation
        Exit Function
    End If

    blnResult = AcquireBearerToken(strClientID, strTenantID, strScopeRequested)
    If blnResult = False Then
        'note the called function will feedback the error situation via strMessage
        Debug.Print "AcquireBearerToken failed: " & strMessage
        Exit Function
    End If
    
    Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    http.Open strGraphAPI_Verb, strGraphAPI_URL, False
    http.setRequestHeader "Authorization", "Bearer " & strBearerToken
    http.setRequestHeader "Accept", "application/json"
    http.setTimeouts lngHTTPTimeout, lngHTTPTimeout, lngHTTPTimeout, lngHTTPTimeout
    http.Send
    
    If http.status = 200 Then
        strResponse = http.ResponseText  ' Preserve for Debug.Print before parsing
        Debug.Print strResponse
        Set objJson = JSONConverter.ParseJson(strResponse)

        For Each varPhone In objJson("businessPhones")
            strPhones = strPhones & varPhone & ", "
        Next varPhone
        If Len(strPhones) > 0 Then
            strPhones = Left(strPhones, Len(strPhones) - 2)
        End If
        strWhoAmI = "Hello " & objJson("givenName") & " " & objJson("surname") & vbCrLf
        strWhoAmI = strWhoAmI & vbTab & "AKA:  " & objJson("displayName") & vbCrLf
        strWhoAmI = strWhoAmI & vbTab & "Job Title: " & objJson("jobTitle") & vbCrLf
        strWhoAmI = strWhoAmI & vbTab & "Primary email: " & objJson("mail") & vbCrLf
        strWhoAmI = strWhoAmI & vbTab & "Business phones: " & strPhones & vbCrLf
        strWhoAmI = strWhoAmI & vbTab & "Mobile: " & objJson("mobilePhone") & vbCrLf
        strWhoAmI = strWhoAmI & vbTab & "Location: " & objJson("officeLocation") & vbCrLf
        strWhoAmI = strWhoAmI & vbTab & "Preferred language: " & objJson("preferredLanguage") & vbCrLf
        strWhoAmI = strWhoAmI & vbTab & "Principal email: " & objJson("userPrincipalName") & vbCrLf
        strWhoAmI = strWhoAmI & vbTab & "Account GUID: " & objJson("id")
        strResult = strWhoAmI
        WhoAmI = True
    Else
        MsgBox "HTTP error " & http.status & ": " & http.ResponseText, vbCritical
        strResult = ""
    End If
    Exit Function

ErrorHandler:
    If Err.Number = -2147012894 Then
        strResult = "The request to Microsoft Graph timed out after " & _
                    lngHTTPTimeout / 1000 & " seconds. " & _
                    "Please check your network connection and try again."
        MsgBox strResult, vbCritical, "Current User"
    Else
        MsgBox "Request failed: " & Err.Number & " - " & Err.Description, vbCritical, "Current User"
        strResult = ""
    End If
End Function
Code language: PHP (php)
Updated on April 3, 2026