Skip to content

Commit

Permalink
Add user to group of company after created
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Oct 30, 2023
1 parent 056060f commit 62a27ac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@

use OCA\Analytics\Datasource\DatasourceEvent;
use OCA\MyCompany\Listener\AnalyticsDatasourceListener;
use OCA\MyCompany\Listener\UserCreatedEventListener;
use OCA\MyCompany\Middleware\InjectionMiddleware;
use OCA\MyCompany\Provider\PublicShareTemplateProvider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\User\Events\UserCreatedEvent;

/**
* @codeCoverageIgnore
Expand All @@ -52,5 +54,6 @@ public function register(IRegistrationContext $context): void {
$context->registerMiddleWare(InjectionMiddleware::class, true);
$context->registerPublicShareTemplateProvider(PublicShareTemplateProvider::class);
$context->registerEventListener(DatasourceEvent::class, AnalyticsDatasourceListener::class);
$context->registerEventListener(UserCreatedEvent::class, UserCreatedEventListener::class);
}
}
56 changes: 56 additions & 0 deletions lib/Listener/UserCreatedEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023, Vitor Mattos <[email protected]>
*
* @author Vitor Mattos <[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\MyCompany\Listener;

use OCA\MyCompany\Service\CompanyService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IGroupManager;
use OCP\User\Events\UserCreatedEvent;
use Psr\Log\LoggerInterface;

class UserCreatedEventListener implements IEventListener {
public function __construct(
private IGroupManager $groupManager,
private CompanyService $companyService,
private LoggerInterface $logger,
) {
}

public function handle(Event $event): void {
if (!($event instanceof UserCreatedEvent)) {
// Unrelated
return;
}
$user = $event->getUser();
$group = $this->groupManager->get($this->companyService->getCompanyCode());
if ($group === null) {
return;
}
$group->addUser($user);
}
}

0 comments on commit 62a27ac

Please sign in to comment.