Skip to content

Commit 9b7be7f

Browse files
authored
Ability to add users to groups when they are authenticated with google auth
1 parent 0881966 commit 9b7be7f

4 files changed

Lines changed: 56 additions & 4 deletions

File tree

Auth/GoogleAuthProvider.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ public function authenticate()
5959
{
6060
$profile = $this->getProfile();
6161

62-
if (! empty($profile)) {
62+
if (!empty($profile)) {
6363
$this->userInfo = new GoogleUserProvider($profile, $this->isAccountCreationAllowed($profile));
64+
6465
return true;
6566
}
6667

@@ -179,7 +180,7 @@ public function getGoogleClientSecret()
179180
*
180181
* @access public
181182
* @return string
182-
*/
183+
*/
183184
public function getGoogleEmailDomains()
184185
{
185186
if (defined('GOOGLE_EMAIL_DOMAINS') && GOOGLE_EMAIL_DOMAINS) {
@@ -189,6 +190,21 @@ public function getGoogleEmailDomains()
189190
return $this->configModel->get('google_email_domains');
190191
}
191192

193+
/**
194+
* Get Google allowed email domains
195+
*
196+
* @access public
197+
* @return string
198+
*/
199+
public function getGoogleSignupGroups()
200+
{
201+
if (defined('GOOGLE_SIGNUP_GROUPS') && GOOGLE_SIGNUP_GROUPS) {
202+
return GOOGLE_SIGNUP_GROUPS;
203+
}
204+
205+
return $this->configModel->get('google_signup_groups');
206+
}
207+
192208
/**
193209
* Return true if the account creation is allowed according to the settings
194210
*
@@ -201,7 +217,7 @@ public function isAccountCreationAllowed(array $profile)
201217
if ($this->configModel->get('google_account_creation', 0) == 1) {
202218
$domains = $this->getGoogleEmailDomains();
203219

204-
if (! empty($domains)) {
220+
if (!empty($domains)) {
205221
return $this->validateDomainRestriction($profile, $domains);
206222
}
207223

Plugin.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function onStartup()
3838
public function onLoginSuccess(AuthSuccessEvent $event)
3939
{
4040
if ($event->getAuthType() === 'Google') {
41+
/** @var GoogleAuthProvider */
4142
$provider = $this->authenticationManager->getProvider($event->getAuthType());
4243
$avatar_url = $provider->getUser()->getAvatarUrl();
4344
$user_id = $this->userSession->getId();
@@ -51,6 +52,18 @@ public function onLoginSuccess(AuthSuccessEvent $event)
5152

5253
$this->userMetadataModel->save($user_id, $options);
5354
}
55+
56+
$signup_groups = $provider->getGoogleSignupGroups();
57+
$user_groups = array_column($this->groupMemberModel->getGroups($user_id), 'id', 'id');
58+
if (!empty($signup_groups) && !is_null($signup_groups)) {
59+
$signup_groups = array_map('trim', explode(',', $signup_groups));
60+
$groups = $this->groupModel->getQuery()->in('name', $signup_groups)->findAll();
61+
foreach ($groups as $signup_group) {
62+
if (!isset($user_groups[$signup_group['id']])) {
63+
$this->groupMemberModel->addUser($signup_group['id'], $user_id);
64+
}
65+
}
66+
}
5467
}
5568
}
5669

Template/config/integration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
<?= $this->form->text('google_email_domains', $values) ?>
1717
<p class="form-help"><?= t('Use a comma to enter multiple domains: domain1.tld, domain2.tld') ?></p>
1818

19+
<?= $this->form->label(t('Add new users to groups'), 'google_signup_groups') ?>
20+
<?= $this->form->text('google_signup_groups', $values) ?>
21+
<p class="form-help"><?= t('Use a comma to enter multiple group names: developers,designers') ?></p>
22+
23+
1924
<p class="form-help"><a href="https://github.com/kanboard/plugin-google-auth#documentation"><?= t('Help on Google authentication') ?></a></p>
2025

2126
<div class="form-actions">

User/GoogleUserProvider.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ class GoogleUserProvider extends OAuthUserProvider
1717
*/
1818
protected $allowUserCreation;
1919

20+
/**
21+
* @var array
22+
*/
23+
protected $externalGroupIds;
24+
2025
/**
2126
* Constructor
2227
*
2328
* @access public
2429
* @param array $user
2530
* @param bool $allowUserCreation
31+
* @param array $externalGroupIds
2632
*/
27-
public function __construct(array $user, $allowUserCreation = false)
33+
public function __construct(array $user, $allowUserCreation = false, $externalGroupIds = [])
2834
{
2935
$this->user = $user;
3036
$this->allowUserCreation = $allowUserCreation;
37+
$this->externalGroupIds = $externalGroupIds;
3138
}
3239

3340
/**
@@ -96,4 +103,15 @@ public function getExtraAttributes()
96103

97104
return array();
98105
}
106+
107+
/**
108+
* Get external group ids
109+
*
110+
* @access public
111+
* @return array
112+
*/
113+
public function getExternalGroupIds()
114+
{
115+
return $this->externalGroupIds;
116+
}
99117
}

0 commit comments

Comments
 (0)