-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add FirstLoginListener to accept shares upon first ldap user login #30512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0284cdb
Add FirstLoginListener to accept shares upon first ldap user login
come-nc ddb4bb7
Use a standard array for the stateful cache
come-nc ea2afb2
Move event listener registration to register()
come-nc 1a6f829
Add logging to be able to debug FirstLoginListener
come-nc 1035e07
Update autoload for user_ldap
come-nc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add FirstLoginListener to accept shares upon first ldap user login
Signed-off-by: Côme Chilliet <[email protected]>
- Loading branch information
commit 0284cdbdae189414b6f352233bbaf332524b2ddf
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,140 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright (c) 2022, Côme Chilliet <[email protected]> | ||
| * | ||
| * @author Côme Chilliet <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as | ||
| * published by the Free Software Foundation, either version 3 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
| namespace OCA\User_LDAP; | ||
|
|
||
| use OCP\EventDispatcher\Event; | ||
| use OCP\EventDispatcher\IEventDispatcher; | ||
| use OCP\EventDispatcher\IEventListener; | ||
| use OCP\Group\Events\UserAddedEvent; | ||
| use OCP\IDBConnection; | ||
| use OCP\IGroupManager; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
| use OCP\User\Events\PostLoginEvent; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class FirstLoginListener implements IEventListener { | ||
| protected $somekindofstatefulhandler; | ||
come-nc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** @var Group_Proxy */ | ||
| private $groupBackend; | ||
| /** @var IEventDispatcher */ | ||
| private $dispatcher; | ||
| /** @var IGroupManager */ | ||
| private $groupManager; | ||
| /** @var IUserManager */ | ||
| private $userManager; | ||
| /** @var LoggerInterface */ | ||
| private $logger; | ||
| /** @var IDBConnection */ | ||
| private $dbc; | ||
|
|
||
| public function __construct( | ||
| Group_Proxy $groupBackend, | ||
| IEventDispatcher $dispatcher, | ||
| IGroupManager $groupManager, | ||
| IUserManager $userManager, | ||
| LoggerInterface $logger, | ||
| IDBConnection $dbc | ||
| ) { | ||
| $this->groupBackend = $groupBackend; | ||
| $this->dispatcher = $dispatcher; | ||
| $this->groupManager = $groupManager; | ||
| $this->userManager = $userManager; | ||
| $this->logger = $logger; | ||
| $this->dbc = $dbc; | ||
| } | ||
|
|
||
| public function handle(Event $event): void { | ||
| if ($event instanceof PostLoginEvent) { | ||
| $this->onPostLogin($event->getUser()->getUID()); | ||
| } | ||
| } | ||
|
|
||
| public function onAssignedId(string $username): void { | ||
| $this->somekindofstatefulhandler[$username]['id'] = 1; | ||
| $this->triggerUpdateGroups($username); | ||
| } | ||
|
|
||
| public function onPostLogin(string $username): void { | ||
| $this->somekindofstatefulhandler[$username]['login'] = 1; | ||
| $this->triggerUpdateGroups($username); | ||
| } | ||
|
|
||
| private function triggerUpdateGroups(string $username): void { | ||
| if (array_sum($this->somekindofstatefulhandler[$username] ?? []) >= 2) { | ||
| $this->updateGroups($username); | ||
| } | ||
| } | ||
|
|
||
| private function updateGroups(string $username): void { | ||
| $groups = $this->groupBackend->getUserGroups($username); | ||
|
|
||
| $qb = $this->dbc->getQueryBuilder(); | ||
| $qb->select(['owncloudusers']) | ||
| ->from('ldap_group_members') | ||
| ->where($qb->expr()->eq('owncloudname', $qb->createParameter('groupId'))); | ||
|
|
||
| $qbUpdate = $this->dbc->getQueryBuilder(); | ||
| $qbUpdate->update('ldap_group_members') | ||
| ->set('owncloudusers', $qb->createParameter('members')) | ||
| ->where($qb->expr()->eq('owncloudname', $qb->createParameter('groupId'))); | ||
|
|
||
| foreach ($groups as $group) { | ||
| $qb->setParameters([ | ||
| 'groupId' => $group | ||
| ]); | ||
|
|
||
| $qResult = $qb->execute(); | ||
| $data = $qResult->fetchOne(); | ||
| $qResult->closeCursor(); | ||
|
|
||
| $knownUsers = unserialize($data['owncloudusers']); | ||
| $hasChanged = false; | ||
|
|
||
| $groupObject = $this->groupManager->get($group); | ||
| if (!in_array($username, $knownUsers)) { | ||
| $userObject = $this->userManager->get($username); | ||
| if ($userObject instanceof IUser) { | ||
| $this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject)); | ||
| $this->logger->info( | ||
| __CLASS__ . ' – {user} added to {group}', | ||
| [ | ||
| 'app' => 'user_ldap', | ||
| 'user' => $username, | ||
| 'group' => $group | ||
| ] | ||
| ); | ||
| $qbUpdate->setParameters([ | ||
| 'members' => serialize(array_merge($knownUsers, [$username])), | ||
| 'groupId' => $group | ||
| ]); | ||
| $qbUpdate->execute(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.