PHP code example of joeystowe / ms-graph-api

1. Go to this page and download the library: Download joeystowe/ms-graph-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joeystowe / ms-graph-api example snippets


Route::get('/', function () {
    return view('welcome');
})->middleware('ms-auth');

Route::middleware('ms-auth')->group(function () {
    Route::get('/admin/dashboard', 'AdminController@dashboard');
});

// services.php
...
'azure' => [
	'client_id' => env('AZURE_CLIENT_ID'),
	'client_secret' => env('AZURE_CLIENT_SECRET'),
	'tenant' => env('AZURE_TENANT_ID'),
	'redirect' => env('AZURE_REDIRECT_URI'),
],
...

session()->put('ms:user', (object)$user);
session()->put('ms:username', $user['bannerUsername']);
session()->put('ms:email', $user['email']);
session()->put('ms:principalName', $user['principalName']);
session()->put('ms:id', $user['id']);
session()->put('ms:session-token', $user['token']);

// Returns an object with the following properties set
Joeystowe\MsGraphApi\LoggedInUser::user();
{
  "id" => "1111-2222-33333-44444" //ms user id
  "name" => "John Doe" //Full Name
  "email" => "[email protected]"
  "principalName" => "[email protected]"
  "bannerUsername" => "jdoe"
  "token" => "1111-2222-3333-4444" //ms session token
}

//Fetch users properties as an array
Joeystowe\MsGraphApi\LoggedInUser::userArray();

//Fetch users properties as a pre-filled User model
Joeystowe\MsGraphApi\LoggedInUser::userModel();

//Fetch a single user attribute (throws exception is property is not found)
Joeystowe\MsGraphApi\LoggedInUser::userAttribute('principalName')
//returns "[email protected]"

$user = Joeystowe\MsGraphApi\LoggedInUser::user();
//resolve instance of current user API
$graphApi = app(Joeystowe\MsGraphApi\MsGraphCurrentUserApi::class, ['token' => $user->token]);

//Get all user's groups, returns array of groups
$graphApi->groups()

//Check if a user is in a specific group, returns boolean
$graphApi->inGroup(groupId: $groupIdToCheck)
bash
php artisan vendor:publish --tag=assets --ansi --force