Skip to content

Commit e2ba185

Browse files
committed
update
1 parent 7917655 commit e2ba185

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
Unreleased
5+
--------------------
6+
- Fix: Don't add to group a user that is not active
7+
48
1.2.3 (February 20, 2023)
59
--------------------
610
- Fix: For some users, the corresponding user ID on Keycloak was not saved in `user_auth` table, which prevents group sync from working.

jobs/GroupsFullSync.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use humhub\modules\authKeycloak\models\GroupKeycloak;
1616
use humhub\modules\queue\ActiveJob;
1717
use humhub\modules\user\models\Auth;
18+
use humhub\modules\user\models\User;
1819
use Throwable;
1920
use Yii;
2021
use yii\base\InvalidConfigException;
@@ -215,9 +216,11 @@ protected function addHumhubGroupsToKeycloak()
215216
protected function initUsersKeycloakIdToHumhubId()
216217
{
217218
$auths = Auth::find()
218-
->orderBy(['id' => SORT_ASC]) // Get the latest if it has multiple
219-
->where(['source' => Keycloak::DEFAULT_NAME])
220-
->indexBy('user_id') // Remove duplicated
219+
->joinWith('user')
220+
->andWhere(['user.status' => User::STATUS_ENABLED])
221+
->andWhere(['user_auth.source' => Keycloak::DEFAULT_NAME])
222+
->orderBy(['user_auth.id' => SORT_ASC]) // Get the latest if it has multiple
223+
->indexBy('user_id') // Removes duplicated
221224
->all();
222225
$this->usersKeycloakIdToHumhubId = ArrayHelper::map($auths, 'source_id', 'user_id');
223226
}
@@ -280,10 +283,10 @@ protected function addKeycloakUsersToHumhubGroups()
280283
{
281284
foreach ($this->keycloakGroupsMembers as $keycloakGroupId => $keycloakUserIds) {
282285
foreach ($keycloakUserIds as $keycloakUserId) {
283-
$humhubUserId = $this->usersKeycloakIdToHumhubId[$keycloakUserId];
286+
$humhubUserId = $this->getHumhubUserId($keycloakUserId);
284287
$humhubGroup = $this->humhubGroupsByKeycloakId[$keycloakGroupId];
285288
$humhubGroupMembers = $this->humhubGroupsMembers[$humhubGroup->id];
286-
if (!in_array($humhubUserId, $humhubGroupMembers)) {
289+
if ($humhubUserId !== null && !in_array($humhubUserId, $humhubGroupMembers)) {
287290
$humhubGroup->addUser($humhubUserId);
288291
$this->humhubGroupsMembers[$humhubGroup->id][] = $humhubUserId;
289292
}

jobs/GroupsUserSync.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use humhub\modules\authKeycloak\models\ConfigureForm;
1414
use humhub\modules\authKeycloak\models\GroupKeycloak;
1515
use humhub\modules\queue\ActiveJob;
16+
use humhub\modules\user\models\User;
1617
use Throwable;
1718
use yii\base\InvalidConfigException;
1819
use yii\queue\RetryableJobInterface;
@@ -49,7 +50,8 @@ class GroupsUserSync extends ActiveJob implements RetryableJobInterface
4950
*/
5051
public function run()
5152
{
52-
if (!$this->userId) {
53+
$user = User::find()->active()->andWhere(['id' => $this->userId])->one();
54+
if ($user === null) {
5355
return;
5456
}
5557

@@ -71,12 +73,12 @@ public function run()
7173
->all();
7274

7375
// Add Humhub user to Humhub groups
74-
foreach ($this->keycloakApi->getUserGroups($this->userId) as $keycloakGroupId) {
76+
foreach ($this->keycloakApi->getUserGroups($user->id) as $keycloakGroupId) {
7577
if (!array_key_exists($keycloakGroupId, $this->humhubGroupsByKeycloakId)) {
7678
continue;
7779
}
7880
$humhubGroup = $this->humhubGroupsByKeycloakId[$keycloakGroupId];
79-
$humhubGroup->addUser($this->userId);
81+
$humhubGroup->addUser($user);
8082
}
8183
}
8284

0 commit comments

Comments
 (0)