Skip to content

Commit 8c261e2

Browse files
committed
Rework groups
No longer rely on `info_adh` field content No longer reword groups names
1 parent aa03129 commit 8c261e2

3 files changed

Lines changed: 29 additions & 39 deletions

File tree

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Before updating to version 3.0.0, please take care of the following:
1616
- the existing `options` entry in configuration file has been renamed to `authorize`. Please update your configuration file accordingly.
1717
- the `scopes` entry in configuration file has been added; some data you were previously using may be missing.
1818
- previous versions were using non Galette data (like `username`). If you were using this data and still want to rely on them; add a `legacy_data: true` in you applications entries.
19+
- Real Galette groups have been added to `member:groups:` scope
20+
- Member status is no longer the first groups entry
21+
- Groups hack from `info_adh` field has been removed
22+
- Groups names reformatting has been removed
1923

2024
# Configuration
2125

@@ -101,16 +105,6 @@ To declare multiple scopes, separate them with a space like `member member:phone
101105
* `member:due_date`:
102106
* due date
103107

104-
# Usage
105-
106-
## Nextcloud - how add groups for a specific member
107-
Edit a member : In `info_adh` field you can add a line with `#GROUPS:group1;group2#`
108-
109-
Example :
110-
```
111-
#GROUPS:accouting;home#
112-
```
113-
114108
# More information about OAuth2 Server
115109
* https://oauth2.thephpleague.com/
116110
* https://github.com/thephpleague/oauth2-server/

lib/GaletteOAuth2/Authorization/UserHelper.php

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ public static function logout(Container $container): void
102102
/**
103103
* Get user data
104104
*
105-
* @param Container $container Container instance
106-
* @param int $id User ID
107-
* @param string $acl Requested authorization
108-
* @param array|string $scopes Scopes
109-
* @param bool $legacy Legacy mode for data
105+
* @param Container $container Container instance
106+
* @param int $id User ID
107+
* @param string $acl Requested authorization
108+
* @param string[]|string $scopes Scopes
109+
* @param bool $legacy Legacy mode for data
110110
*
111-
* @return array
111+
* @return array<string, mixed>
112112
* @throws UserAuthorizationException
113113
* @throws \DI\DependencyException
114114
* @throws \DI\NotFoundException
@@ -267,9 +267,7 @@ public static function getUserData(Container $container, int $id, string $acl, a
267267

268268
//member:groups
269269
if (in_array('member:groups', $scopes)) {
270-
//nextcloud : set fields Groups claim (optional) = groups
271-
//FIXME: I don't know how nextcloud manages groups, but there are not groups...
272-
$oauth_data['groups'] = self::getUserGroups($member);
270+
$oauth_data['groups'] = self::getUserGroups($member, $legacy);
273271
}
274272

275273
//member:due_date
@@ -284,10 +282,11 @@ public static function getUserData(Container $container, int $id, string $acl, a
284282
* Comma separated groups names
285283
*
286284
* @param Adherent $member Member
285+
* @param bool $legacy Legacy mode for data
287286
*
288287
* @return array
289288
*/
290-
protected static function getUserGroups(Adherent $member): array
289+
protected static function getUserGroups(Adherent $member, bool $legacy = false): array
291290
{
292291
$groups = array_map(
293292
function ($group) {
@@ -314,24 +313,21 @@ function ($group) {
314313
$groups[] = 'uptodate';
315314
}
316315

317-
//FIXME: add groups from groups table? Or another way? info_adh does not seems a good way for everyone
318-
//FIXME: For example, data is replaced on duplication, thus oauth groups configuration would be lost
319-
//FIXME: maybe should we just rely on real Galette groups.
320-
//Add externals groups (free text in info_adh)
321-
//Example #GROUPS:compta;accueil#
322-
if (preg_match('/#GROUPS:([^#]*([^#]*))#/mui', $member->others_infos_admin, $matches, PREG_OFFSET_CAPTURE)) {
323-
$g = $matches[1][0];
324-
Debug::log("Groups added {$g}");
325-
$groups = array_merge($groups, explode(';', $g));
326-
}
316+
if ($legacy === true) {
317+
//Add externals groups (free text in info_adh)
318+
//Example #GROUPS:compta;accueil#
319+
if (preg_match('/#GROUPS:([^#]*([^#]*))#/mui', $member->others_infos_admin, $matches, PREG_OFFSET_CAPTURE)) {
320+
$g = $matches[1][0];
321+
$groups = array_merge($groups, explode(';', $g));
322+
}
327323

328-
//TODO: maybe a bit excessive for a global usage?
329-
//Reformat group with strtolower, remove whites & slashs
330-
foreach ($groups as &$group) {
331-
$group = trim($group);
332-
$group = str_replace([' ', '/', '(', ')'], ['_', '', '', ''], $group);
333-
$group = str_replace('__', '_', $group);
334-
$group = self::stripAccents($group);
324+
//Reformat group with strtolower, remove whites & slashs
325+
foreach ($groups as &$group) {
326+
$group = trim($group);
327+
$group = str_replace([' ', '/', '(', ')'], ['_', '', '', ''], $group);
328+
$group = str_replace('__', '_', $group);
329+
$group = self::stripAccents($group);
330+
}
335331
}
336332

337333
return $groups;

tests/GaletteOAuth2/Authorization/tests/units/UserHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public function testGetUserData(): void
191191
$this->assertSame(
192192
$expected_base + [
193193
'groups' => [
194-
'non-member',
194+
'Non-member',
195195
'admin'
196196
]
197197
],
@@ -208,7 +208,7 @@ public function testGetUserData(): void
208208
$this->assertSame(
209209
$expected_base + [
210210
'groups' => [
211-
'non-member',
211+
'Non-member',
212212
'admin'
213213
]
214214
],

0 commit comments

Comments
 (0)