From 28245366df8d07ba21eca1e5efaa33b3a3b1656f Mon Sep 17 00:00:00 2001
From: Thomas Hackl <hackl@data-quest.de>
Date: Thu, 5 Aug 2021 14:22:19 +0200
Subject: [PATCH] permission handling finally working as planned

---
 models/MatrixRoom.php | 45 ++++++++++++++++++++++++++-----------------
 plugin.manifest       |  2 +-
 vendor/libpatrix      |  2 +-
 3 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/models/MatrixRoom.php b/models/MatrixRoom.php
index 833e7eb..ac8db99 100644
--- a/models/MatrixRoom.php
+++ b/models/MatrixRoom.php
@@ -25,21 +25,23 @@ class MatrixRoom extends SimpleORMap
     const POWER_LEVEL_AUTOR = 0;
     const POWER_LEVEL_TUTOR = 50;
     const POWER_LEVEL_DOZENT = 50;
-    const EVENT_POWER_LEVELS = [
+    const ROOM_POWER_LEVELS = [
         'events' => [
             'ban' => self::POWER_LEVEL_TUTOR,
             'invite' => self::POWER_LEVEL_TUTOR,
             'kick' => self::POWER_LEVEL_TUTOR,
             'm.room.name' => self::POWER_LEVEL_DOZENT,
             'm.room.power_levels' => self::POWER_LEVEL_DOZENT,
-            'notifications' => [
-                'room' => self::POWER_LEVEL_TUTOR
-            ],
             'redact' => self::POWER_LEVEL_TUTOR
         ],
-        'events_default' => self::POWER_LEVEL_AUTOR,
-        'state_default' => self::POWER_LEVEL_TUTOR,
-        'users_default' => self::POWER_LEVEL_AUTOR
+        'notifications' => [
+            'room' => self::POWER_LEVEL_TUTOR
+        ],
+        'defaults' => [
+            'events_default' => self::POWER_LEVEL_AUTOR,
+            'state_default' => self::POWER_LEVEL_TUTOR,
+            'users_default' => self::POWER_LEVEL_AUTOR
+        ]
     ];
 
     protected static function configure($config = [])
@@ -67,7 +69,7 @@ class MatrixRoom extends SimpleORMap
             // Invite all members with existing Matrix account to newly created room.
             $invites = [];
             // Permission levels in the room to create.
-            /*$permissions = [
+            $permissions = [
                 $sysaccount->getUserId() => 100
             ];
             $accounts = [];
@@ -88,18 +90,21 @@ class MatrixRoom extends SimpleORMap
                             $power_level = MatrixRoom::POWER_LEVEL_AUTOR;
                     }
 
-                    // Collect accounts for auto-joining room.
-                    $accounts[$mapped->matrix_account_id] = $mapped->getLinkedAccount();
                     // Collect permissions for setting power levels.
                     $permissions[$mapped->matrix_account_id] = $power_level;
                 }
             }
 
-            $power_levels = self::EVENT_POWER_LEVELS;
-            $power_levels['users'] = $permissions;*/
+            $permission_set = new Patrix\RoomPermissionSet(
+                self::ROOM_POWER_LEVELS['defaults'],
+                $permissions,
+                self::ROOM_POWER_LEVELS['events'],
+                self::ROOM_POWER_LEVELS['notifications'],
+                self::POWER_LEVEL_AUTOR
+            );
 
             $newRoom = MatrixClient::get()->createRoom($sysaccount, Context::getHeaderLine(), false, '',
-                [], self::EVENT_POWER_LEVELS);
+                [],$permission_set);
 
             $mapping = new self();
             $mapping->range_id = $context->getId();
@@ -141,12 +146,16 @@ class MatrixRoom extends SimpleORMap
                 $perm = self::POWER_LEVEL_AUTOR;
             }
 
-        }
-        $permission = [
-            $account->getUserName() => $perm
-        ];
+            // Get existing room permissions so that they are not completely overwritten when adding the current user.
+            $permissions = MatrixClient::get()
+                ->getRoomPermissionLevels($sysaccount, $this->getLinkedRoom())
+                ->getUserPermissions();
 
-        //MatrixClient::get()->setRoomUserPermissionLevels($sysaccount, $this->getLinkedRoom(), $permission);
+            $permissions[$account->getUserName()] = $perm;
+
+            // Set correct permission for current user.
+            MatrixClient::get()->setRoomUserPermissionLevels($sysaccount, $this->getLinkedRoom(), $permissions);
+        }
     }
 
 }
\ No newline at end of file
diff --git a/plugin.manifest b/plugin.manifest
index 759a5b3..9e7879d 100644
--- a/plugin.manifest
+++ b/plugin.manifest
@@ -1,7 +1,7 @@
 pluginname=Matrix-Chat
 pluginclassname=MatrixPlugin
 origin=data-quest
-version=0.85
+version=0.9
 screenshot=assets/images/matrix_logo.png
 description=Matrix chat for Stud.IP courses
 studipMinVersion=4.5
diff --git a/vendor/libpatrix b/vendor/libpatrix
index a63da52..c05790e 160000
--- a/vendor/libpatrix
+++ b/vendor/libpatrix
@@ -1 +1 @@
-Subproject commit a63da521cef79651fbdc95b90298b5b67882957d
+Subproject commit c05790e055ca90fe7d5867ee6dbd26fd695814ad
-- 
GitLab