2525
2626namespace OCA \VO_Federation \AppInfo ;
2727
28+ use OC \EventDispatcher \EventDispatcher ;
29+ use OCA \VO_Federation \Backend \GroupBackend ;
30+ use OCA \VO_Federation \Service \GroupsService ;
31+ use OCA \VO_Federation \Service \ProviderService ;
2832use OCP \AppFramework \App ;
2933use OCP \AppFramework \Bootstrap \IBootContext ;
3034use OCP \AppFramework \Bootstrap \IBootstrap ;
3135use OCP \AppFramework \Bootstrap \IRegistrationContext ;
36+ use OCP \AppFramework \Db \DoesNotExistException ;
37+ use OCP \IGroupManager ;
38+ use OCP \User \Events \UserLoggedInEvent ;
3239
3340/**
3441 * Class Application
@@ -51,5 +58,38 @@ public function register(IRegistrationContext $context): void {
5158 }
5259
5360 public function boot (IBootContext $ context ): void {
61+ $ context ->injectFn (function (
62+ IGroupManager $ groupManager ,
63+ GroupBackend $ groupBackend ,
64+ ) {
65+ $ groupManager ->addBackend ($ groupBackend );
66+ });
67+
68+ $ context ->injectFn (function (EventDispatcher $ dispatcher ,
69+ ProviderService $ providerService ,
70+ GroupsService $ groupsService ) {
71+ /*
72+ * @todo move the OCP events and then move the registration to `register`
73+ */
74+ $ dispatcher ->addListener (
75+ UserLoggedInEvent::class,
76+ function (\OCP \User \Events \UserLoggedInEvent $ event ) use ($ providerService , $ groupsService ) {
77+ $ providers = $ providerService ->getProvidersWithSettings ();
78+ foreach ($ providers as $ provider ) {
79+ $ providerId = $ provider ['id ' ];
80+ $ userId = $ event ->getUser ()->getUID ();
81+ try {
82+ $ session = $ providerService ->getProviderSession ($ userId , $ providerId );
83+ if ($ session !== null ) {
84+ $ groupsService ->syncUser ($ userId , $ providerId );
85+ }
86+ } catch (DoesNotExistException $ e ) {
87+ } catch (\Exception $ other ) {
88+ // TODO: Handle server availability
89+ }
90+ }
91+ }
92+ );
93+ });
5494 }
5595}
0 commit comments