-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Group and User Sync to IDP over Nextcloud
- Loading branch information
Showing
9 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\Group; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class NextcloudService | ||
{ | ||
public static function createGroup($groupId) | ||
{ | ||
Http::nextcloud()->post("/ocs/v1.php/cloud/groups", [ | ||
"groupid" => $groupId, | ||
])->throw(); | ||
return $groupId; | ||
} | ||
|
||
public static function setDisplayName($groupId, $displayName) | ||
{ | ||
Http::nextcloud()->put("https://cloud.eurofurence.org/ocs/v2.php/cloud/groups/{$groupId}", [ | ||
'key' => 'displayname', | ||
'value' => $displayName, | ||
])->throw(); | ||
} | ||
|
||
public static function checkUserExists($userId): bool | ||
{ | ||
$res = Http::nextcloud()->get("ocs/v2.php/cloud/users/".$userId)->throwIfServerError(); | ||
if ($res->notFound()) { | ||
return false; | ||
} | ||
if ($res->ok()) { | ||
return true; | ||
} | ||
$res->throw(); | ||
} | ||
|
||
public static function addUserToGroup(Group $group, User $user) | ||
{ | ||
// Check user | ||
if (!self::checkUserExists($user->hashid)) { | ||
self::createUser($user); // Create user also adds groups so we don't need to add them here | ||
} else { | ||
Http::nextcloud()->post("ocs/v2.php/cloud/users/{$user->hashid}/groups", [ | ||
"groupid" => $group->hashid, | ||
])->throw(); | ||
} | ||
} | ||
|
||
public static function removeUserFromGroup(Group $group, User $user) | ||
{ | ||
if (!self::checkUserExists($user->hashid)) { | ||
return; | ||
} | ||
Http::nextcloud()->delete("ocs/v2.php/cloud/users/{$user->hashid}/groups?groupid={$group->hashid}")->throw(); | ||
} | ||
|
||
public static function setManageAcl(Group $group, User $user, bool $allow): void | ||
{ | ||
Http::nextcloud()->post("apps/groupfolders/folders/{$group->nextcloud_folder_id}/manageACL", [ | ||
'mappingId' => $user->hashid, | ||
'mappingType' => 'user', | ||
'manageAcl' => $allow ? '1' : '0', | ||
])->throwIfServerError(); | ||
} | ||
|
||
public static function createUser(User $user) | ||
{ | ||
Http::nextcloud()->post("ocs/v2.php/cloud/users", [ | ||
'displayName' => $user->name, | ||
'email' => $user->email, | ||
'groups' => $user->groups()->whereNotNull('nextcloud_folder_name')->get()->pluck('hashid')->toArray(), | ||
'language' => 'en', | ||
'password' => '', | ||
'quota' => 'default', | ||
'subadmin' => [], | ||
'userid' => $user->hashid, | ||
])->throw(); | ||
} | ||
|
||
public static function createFolder(string $folderName, string $groupId): int | ||
{ | ||
$response = Http::nextcloud()->post("apps/groupfolders/folders", [ | ||
"mountpoint" => $folderName, | ||
])->throw(); | ||
$xml = simplexml_load_string($response->body()); | ||
|
||
// enable acl for group (we have that enabled for all groups) | ||
Http::nextcloud()->post("apps/groupfolders/folders/{$xml->data->id}/acl", [ | ||
"acl" => 1, | ||
])->throw(); | ||
// add group to folder apps/groupfolders/folders/$folderId/groups/$groupId | ||
Http::nextcloud()->post("apps/groupfolders/folders/{$xml->data->id}/groups", [ | ||
"group" => $groupId, | ||
])->throw(); | ||
return (int) $xml->data->id; | ||
} | ||
|
||
public static function renameFolder(int $folderId, string $folderName): void | ||
{ | ||
Http::nextcloud()->post("apps/groupfolders/folders/{$folderId}/mountpoint", [ | ||
"mountpoint" => $folderName, | ||
])->throw(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
database/migrations/2024_05_26_214441_add_nextcloud_folder_id_to_groups_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::table('groups', function (Blueprint $table) { | ||
$table->unsignedInteger('nextcloud_folder_id')->after('nextcloud_folder_name')->nullable(); | ||
// nextcloud group id | ||
$table->string('nextcloud_group_id')->after('nextcloud_folder_id')->nullable(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('groups', function (Blueprint $table) { | ||
$table->dropColumn('nextcloud_folder_id'); | ||
$table->dropColumn('nextcloud_group_id'); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters