Laravel Microsoft Graph

repository·master·Indexed 41 days ago

https://github.com/dcblogdev/laravel-microsoft-graph

A Laravel package providing a wrapper for the Microsoft Graph API. It supports user-delegated authentication via MsGraph and tenant-wide administrator authentication via MsGraphAdmin. Features include a dedicated middleware for automatic redirection, token storage, and simplified methods for interacting with contacts, emails, and folders.

Tokens
26.4K
Snippets
122
Records
132
Agent score
50%

What's inside Laravel Microsoft Graph

  1. How to access user emails in MsGraphAdmin

    master

    To interact with a user's emails using MsGraphAdmin, you must first initialize the email context by calling MsGraphAdmin::emails() and then specifying the target user with ->userid($userId). All subsequent email operations (listing, sending, reading, etc.) must be chained after these two calls.

    MsGraphAdmin::emails()->userid($userId);
  2. How to work with Contacts in MsGraphAdmin

    master

    To interact with a user's contacts using MsGraphAdmin, you must first initialize the contact context using MsGraphAdmin::contacts() and then specify the target user using ->userid($userId). All subsequent contact operations (listing, creating, viewing, updating, or deleting) must be chained after these two calls.

    MsGraphAdmin::contacts()->userid($userId);
  3. Understand the two MsGraph authentication modes

    master

    The package provides two distinct ways to interact with the Microsoft Graph API depending on your application's requirements:

    1. MsGraph: Used for user-centric flows where you log in as an individual user (Delegated permissions).
    2. MsGraphAdmin: Used for tenant-level or administrator flows, which is ideal for running background tasks, scheduled jobs, or accessing data across the entire tenant (Application permissions).
  4. Choose between MsGraph and MsGraphAdmin

    master

    The package provides two distinct modes of operation depending on your authentication requirements:

    • MsGraph: Used when you need to log in as an individual user (delegated permissions).
    • MsGraphAdmin: Used when you need to log in as a tenant administrator (application permissions). This mode is particularly useful for running background tasks or automated processes that do not require a user's interactive presence.
  5. How to access files in OneDrive, SharePoint, or Groups

    master

    To interact with files, you must first call MsGraph::files(). By default, all file operations target the authenticated user's OneDrive using the 'me' prefix.

    To target a different storage location, pass a $type argument to the files() call (or methods that accept it) using the appropriate prefix. For example, to access files within a specific Microsoft Group, use the prefix groups/$groupId.

    // Access default OneDrive
    MsGraph::files();
    
    // Access files from a specific group
    MsGraph::files()->getFiles(null, "groups/$groupId");
  6. Register the Microsoft Graph login event

    master

    In your app/Providers/EventServiceProvider.php, you must register the NewMicrosoft365SignInEvent to trigger your custom listener.

    // In EventServiceProvider.php
    
    use App\\Listeners\\NewMicrosoft365SignInListener;
    use Dcblogdev\\MsGraph\\Events\\NewMicrosoft365SignInEvent;
    
    public function boot()
    {
        Event::listen(
            NewMicrosoft365SignInEvent::class,
            [NewMicrosoft365SignInListener::class, 'handle']
        );
    }
  7. Publish configuration, migrations, and listeners

    master

    Use the following Artisan commands to publish the necessary package assets to your application:

    • Config: Publishes config/msgraph.php.
    • Migrations: Publishes the database migrations required for token storage.
    • Listeners: Publishes a listener for handling Microsoft 365 sign-ins (useful if using Graph as a login system).

    After publishing migrations, run php artisan migrate to create the required tables.

    # Publish config
    php artisan vendor:publish --provider="Dcblogdev\MsGraph\MsGraphServiceProvider" --tag="config"
    
    # Publish migrations
    php artisan vendor:publish --provider="Dcblogdev\MsGraph\MsGraphServiceProvider" --tag="migrations"
    
    # Publish listeners (optional for login systems)
    php artisan vendor:publish --provider="Dcblogdev\MsGraph\MsGraphServiceProvider" --tag="Listeners"
    
    # Run migrations
    php artisan migrate
  8. Configure Sharepoint/OneDrive file storage

    master

    To use Sharepoint or OneDrive as a filesystem disk, add a new configuration entry to config/filesystems.php using the msgraph driver. You must provide a driveId which identifies the specific SharePoint drive or OneDrive instance to interact with.

    To find your driveId:

    1. Use the Microsoft Graph Explorer.
    2. Authenticate and call the endpoint: https://graph.microsoft.com/v1.0/drives.
    3. Locate the id property in the JSON response (e.g., "id": "b!zMM792FbNkq...").
    4. Add this ID to your .env file using the MSGRAPH_DRIVEID key.
    // config/filesystems.php
    'msgraph' => [
        'driver' => 'msgraph',
        'driveId' => env('MSGRAPH_DRIVEID', ''),
    ],
    # .env
    MSGRAPH_DRIVEID='your-drive-id-here'