Add Office365 User Permission

This commit is contained in:
OlinetMacbookAir 2025-06-25 19:30:57 +02:00
parent 3a087d35b2
commit f935ae9889
6 changed files with 252 additions and 0 deletions

BIN
AddOn/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,102 @@
<?php
class OutlookCalendar {
private $clientId;
private $clientSecret;
private $redirectUri;
private $accessToken;
private $refreshToken;
public function __construct($clientId, $clientSecret, $redirectUri, $accessToken, $refreshToken) {
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->redirectUri = $redirectUri;
$this->accessToken = $accessToken;
$this->refreshToken = $refreshToken;
}
// ------------------------------------------
// 🔁 Refresh Token verwenden
public function refreshAccessToken() {
$url = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
$data = [
'grant_type' => 'refresh_token',
'refresh_token' => $this->refreshToken,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectUri,
'scope' => 'https://graph.microsoft.com/.default'
];
$response = $this->sendRequest($url, $data, true);
if (isset($response['access_token'])) {
$this->accessToken = $response['access_token'];
$this->refreshToken = $response['refresh_token'] ?? $this->refreshToken;
}
return $response;
}
// ------------------------------------------
// 📅 Termin erstellen
public function createEvent($eventData) {
$url = "https://graph.microsoft.com/v1.0/me/events";
return $this->sendRequest($url, $eventData, false, 'POST');
}
// ✏️ Termin bearbeiten
public function updateEvent($eventId, $eventData) {
$url = "https://graph.microsoft.com/v1.0/me/events/{$eventId}";
return $this->sendRequest($url, $eventData, false, 'PATCH');
}
// ❌ Termin löschen
public function deleteEvent($eventId) {
$url = "https://graph.microsoft.com/v1.0/me/events/{$eventId}";
return $this->sendRequest($url, [], false, 'DELETE');
}
// ------------------------------------------
// 🔧 API Request senden (intern)
private function sendRequest($url, $data = [], $isForm = false, $method = 'POST') {
$ch = curl_init($url);
$headers = [
'Authorization: Bearer ' . $this->accessToken,
];
if ($isForm) {
$body = http_build_query($data);
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
} else {
$body = json_encode($data);
$headers[] = 'Content-Type: application/json';
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ($method !== 'GET' && !empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
return ['error' => $err];
}
return json_decode($response, true);
}
// 🔓 Getter für AccessToken
public function getAccessToken() {
return $this->accessToken;
}
public function getRefreshToken() {
return $this->refreshToken;
}
}

View File

@ -0,0 +1,14 @@
<?php
$config = require 'config.php';
$params = [
'client_id' => $config['client_id'],
'response_type' => 'code',
'redirect_uri' => $config['redirect_uri'],
'response_mode' => 'query',
'scope' => $config['scopes'],
];
$authUrl = $config['auth_url'] . '?' . http_build_query($params);
header("Location: $authUrl");
exit;

View File

@ -0,0 +1,56 @@
<?php
require 'OutlookCalendar.php';
$config = require 'config.php';
$email = $_GET['email'] ?? null;
if (!$email || !file_exists($config['token_storage'])) {
die("Bitte logge dich zuerst über auth.php ein.");
}
$tokens = json_decode(file_get_contents($config['token_storage']), true);
if (!isset($tokens[$email])) {
die("Kein Token für diese E-Mail gefunden.");
}
$data = $tokens[$email];
$calendar = new OutlookCalendar(
$config['client_id'],
$config['client_secret'],
$config['redirect_uri'],
$data['access_token'],
$data['refresh_token']
);
// Optional: Token auffrischen, wenn abgelaufen
if (time() > ($data['time_saved'] + $data['expires_in'] - 60)) {
$calendar->refreshAccessToken();
$tokens[$email]['access_token'] = $calendar->getAccessToken();
$tokens[$email]['refresh_token'] = $calendar->getRefreshToken();
$tokens[$email]['time_saved'] = time();
file_put_contents($config['token_storage'], json_encode($tokens, JSON_PRETTY_PRINT));
}
// ➤ Beispiel: Termin anlegen
$event = [
"subject" => "Test-Meeting via PHP",
"start" => [
"dateTime" => "2025-06-25T10:00:00",
"timeZone" => "Europe/Berlin"
],
"end" => [
"dateTime" => "2025-06-25T11:00:00",
"timeZone" => "Europe/Berlin"
],
"body" => [
"contentType" => "HTML",
"content" => "Meeting von OutlookCalendar PHP"
]
];
$response = $calendar->createEvent($event);
echo "<pre>";
print_r($response);
echo "</pre>";

View File

@ -0,0 +1,69 @@
<?php
$config = require 'config.php';
if (!isset($_GET['code'])) {
die("Authorization code fehlt.");
}
$code = $_GET['code'];
// ➤ Token anfordern
$data = [
'client_id' => $config['client_id'],
'scope' => $config['scopes'],
'code' => $code,
'redirect_uri' => $config['redirect_uri'],
'grant_type' => 'authorization_code',
'client_secret' => $config['client_secret'],
];
$ch = curl_init($config['token_url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded',
]);
$response = curl_exec($ch);
curl_close($ch);
$tokenData = json_decode($response, true);
if (!isset($tokenData['access_token'])) {
die("Fehler beim Token-Abruf: " . $response);
}
// ➤ E-Mail-Adresse vom User holen
$ch = curl_init($config['graph_url'] . '/me');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $tokenData['access_token'],
]);
$userResponse = curl_exec($ch);
curl_close($ch);
$userData = json_decode($userResponse, true);
$email = $userData['mail'] ?? $userData['userPrincipalName'] ?? null;
if (!$email) {
die("Fehler beim Abrufen der E-Mail.");
}
// ➤ Speichern in tokens.json
$tokens = [];
if (file_exists($config['token_storage'])) {
$tokens = json_decode(file_get_contents($config['token_storage']), true);
}
$tokens[$email] = [
'access_token' => $tokenData['access_token'],
'refresh_token' => $tokenData['refresh_token'],
'expires_in' => $tokenData['expires_in'],
'time_saved' => time()
];
file_put_contents($config['token_storage'], json_encode($tokens, JSON_PRETTY_PRINT));
echo "Erfolgreich verbunden mit: <b>$email</b><br>";
echo "<a href='calendar.php?email=" . urlencode($email) . "'>Zum Kalender-Tool</a>";

View File

@ -0,0 +1,11 @@
<?php
return [
'client_id' => '<Application ID (Client ID)>',
'client_secret' => '<Cleint Secret Geheimnise / Zertifikate Wert>',
'redirect_uri' => 'https://servicebericht.isgus.de/calsync/callback.php',
'scopes' => 'offline_access user.read calendars.readwrite',
'auth_url' => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
'token_url' => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
'graph_url' => 'https://graph.microsoft.com/v1.0',
'token_storage' => __DIR__ . '/tokens.json',
];