Skip to content

Commit ae7246f

Browse files
committed
Team Permissions Model
1 parent 9c03bd5 commit ae7246f

File tree

4 files changed

+101
-26
lines changed

4 files changed

+101
-26
lines changed

README.md

+37-12
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
* [Validate incoming webhook payloads](#validate-incoming-webhook-payloads)
114114
* [License](#license)
115115

116-
<!-- Added by: zanbaldwin, at: Thu 15 Sep 11:30:29 CEST 2022 -->
116+
<!-- Added by: zanbaldwin, at: Fri 16 Sep 09:48:23 CEST 2022 -->
117117

118118
<!--te-->
119119

@@ -161,6 +161,33 @@ Returns an array of created jobs. One for every synchronization.
161161

162162
### Team
163163

164+
The permissions available for a team are:
165+
- `canEditTeamPackages`: members of the team can edit and remove packages, assign package permissions (only applies to packages assigned to team).
166+
- `canAddPackages`: members of the team can add packages to organization; add, edit and remove credentials and mirrored third-party repositories.
167+
- `canCreateSubrepositories`: members of the team can create subrepositories.
168+
- `canViewVendorCustomers`: members of the team can view customers, their Composer information, their packages, and their install statistics.
169+
- `canManageVendorCustomers`: members of the team can create and delete customers, add and remove packages, update their settings, view Composer information and install statistics.
170+
171+
```php
172+
use PrivatePackagist\ApiClient\TeamPermissions;
173+
174+
$permissions = new TeamPermissions;
175+
// Grant all permissions.
176+
$permissions->canEditTeamPackages = true;
177+
$permissions->canAddPackages = true;
178+
$permissions->canCreateSubrepositories = true;
179+
$permissions->canManageVendorCustomers = true;
180+
$permissions->canManageVendorCustomers = true;
181+
```
182+
183+
The permissions model can also be constructed via flags:
184+
185+
```php
186+
use PrivatePackagist\ApiClient\TeamPermissions;
187+
188+
$permissions = TeamPermissions::fromFlags(TeamPermissions::PERMISSION_CAN_CREATE_SUBREPOSITORIES | TeamPermissions::PERMISSION_CAN_ADD_PACKAGES);
189+
```
190+
164191
#### List an organization's teams
165192
```php
166193
$teams = $client->teams()->all();
@@ -169,21 +196,19 @@ Returns an array of teams.
169196

170197
#### Create a New Team
171198
```php
172-
$team = $client->teams()->create('New Team Name', true, false, false, true, false);
173-
```
174-
Creates a team and sets permissions applied to team members. Those permissions are (in order):
175-
176-
- `canEditTeamPackages`: members of the team can edit and remove packages, assign package permissions (only applies to packages assigned to team).
177-
- `canAddPackages`: members of the team can add packages to organization; add, edit and remove credentials and mirrored third-party repositories.
178-
- `canCreateSubrepositories`: members of the team can create subrepositories.
179-
- `canViewVendorCustomers`: members of the team can view customers, their Composer information, their packages, and their install statistics.
180-
- `canManageVendorCustomers`: members of the team can create and delete customers, add and remove packages, update their settings, view Composer information and install statistics.
199+
use PrivatePackagist\ApiClient\TeamPermissions;
181200

182-
Returns the newly-created team.
201+
$permissions = new TeamPermissions;
202+
$team = $client->teams()->create('New Team Name', $permissions);
203+
```
204+
Creates a team and sets permissions applied to team members. Returns the newly-created team.
183205

184206
#### Edit a Team
185207
```php
186-
$team = $client->teams()->edit($teamId, 'Altered Team Name', true, false, false, true, false);
208+
use PrivatePackagist\ApiClient\TeamPermissions;
209+
210+
$permissions = new TeamPermissions;
211+
$team = $client->teams()->edit($teamId, 'Altered Team Name', $permissions);
187212
```
188213
Edits a team's name and permissions to be applied to team members. Returns the updated team.
189214

src/Api/Teams.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,41 @@
99

1010
namespace PrivatePackagist\ApiClient\Api;
1111

12+
use PrivatePackagist\ApiClient\TeamPermissions;
13+
1214
class Teams extends AbstractApi
1315
{
1416
public function all()
1517
{
1618
return $this->get('/teams/');
1719
}
1820

19-
public function create($name, $canEditTeamPackages = false, $canAddPackages = false, $canCreateSubrepositories = false, $canViewVendorCustomers = false, $canManageVendorCustomers = false)
21+
public function create($name, TeamPermissions $permissions)
2022
{
2123
$parameters = [
2224
'name' => $name,
2325
'permissions' => [
24-
'canEditTeamPackages' => $canEditTeamPackages,
25-
'canAddPackages' => $canAddPackages,
26-
'canCreateSubrepositories' => $canCreateSubrepositories,
27-
'canViewVendorCustomers' => $canViewVendorCustomers,
28-
'canManageVendorCustomers' => $canManageVendorCustomers,
26+
'canEditTeamPackages' => (bool) $permissions->canEditTeamPackages,
27+
'canAddPackages' => (bool) $permissions->canAddPackages,
28+
'canCreateSubrepositories' => (bool) $permissions->canCreateSubrepositories,
29+
'canViewVendorCustomers' => (bool) $permissions->canViewVendorCustomers,
30+
'canManageVendorCustomers' => (bool) $permissions->canManageVendorCustomers,
2931
],
3032
];
3133

3234
return $this->post('/teams/', $parameters);
3335
}
3436

35-
public function edit($teamId, $name, $canEditTeamPackages, $canAddPackages, $canCreateSubrepositories, $canViewVendorCustomers, $canManageVendorCustomers)
37+
public function edit($teamId, $name, TeamPermissions $permissions)
3638
{
3739
$parameters = [
3840
'name' => $name,
3941
'permissions' => [
40-
'canEditTeamPackages' => $canEditTeamPackages,
41-
'canAddPackages' => $canAddPackages,
42-
'canCreateSubrepositories' => $canCreateSubrepositories,
43-
'canViewVendorCustomers' => $canViewVendorCustomers,
44-
'canManageVendorCustomers' => $canManageVendorCustomers,
42+
'canEditTeamPackages' => (bool) $permissions->canEditTeamPackages,
43+
'canAddPackages' => (bool) $permissions->canAddPackages,
44+
'canCreateSubrepositories' => (bool) $permissions->canCreateSubrepositories,
45+
'canViewVendorCustomers' => (bool) $permissions->canViewVendorCustomers,
46+
'canManageVendorCustomers' => (bool) $permissions->canManageVendorCustomers,
4547
],
4648
];
4749

src/TeamPermissions.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* (c) Packagist Conductors GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace PrivatePackagist\ApiClient;
11+
12+
class TeamPermissions
13+
{
14+
public const PERMISSION_CAN_EDIT_TEAM_PACKAGES = 1 << 0;
15+
public const PERMISSION_CAN_ADD_PACKAGES = 1 << 1;
16+
public const PERMISSION_CAN_CREATE_SUBREPOSITORIES = 1 << 2;
17+
public const PERMISSION_CAN_VIEW_VENDOR_CUSTOMERS = 1 << 3;
18+
public const PERMISSION_CAN_MANAGE_VENDOR_CUSTOMERS = 1 << 4;
19+
20+
/** @var bool */
21+
public $canEditTeamPackages = false;
22+
/** @var bool */
23+
public $canAddPackages = false;
24+
/** @var bool */
25+
public $canCreateSubrepositories = false;
26+
/** @var bool */
27+
public $canViewVendorCustomers = false;
28+
/** @var bool */
29+
public $canManageVendorCustomers = false;
30+
31+
public static function fromFlags(int $flags): self
32+
{
33+
$permissions = new self;
34+
$permissions->canEditTeamPackages = ($flags & self::PERMISSION_CAN_EDIT_TEAM_PACKAGES) > 0;
35+
$permissions->canAddPackages = ($flags & self::PERMISSION_CAN_ADD_PACKAGES) > 0;
36+
$permissions->canCreateSubrepositories = ($flags & self::PERMISSION_CAN_CREATE_SUBREPOSITORIES) > 0;
37+
$permissions->canViewVendorCustomers = ($flags & self::PERMISSION_CAN_VIEW_VENDOR_CUSTOMERS) > 0;
38+
$permissions->canManageVendorCustomers = ($flags & self::PERMISSION_CAN_MANAGE_VENDOR_CUSTOMERS) > 0;
39+
return $permissions;
40+
}
41+
}

tests/Api/TeamsTest.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PrivatePackagist\ApiClient\Api;
1111

1212
use PHPUnit\Framework\MockObject\MockObject;
13+
use PrivatePackagist\ApiClient\TeamPermissions;
1314

1415
class TeamsTest extends ApiTestCase
1516
{
@@ -125,7 +126,10 @@ public function testCreateTeam()
125126
]))
126127
->willReturn($expected);
127128

128-
$this->assertSame($expected, $api->create('New Team', true, false, false, true, false));
129+
$permissions = new TeamPermissions;
130+
$permissions->canEditTeamPackages = true;
131+
$permissions->canViewVendorCustomers = true;
132+
$this->assertSame($expected, $api->create('New Team', $permissions));
129133
}
130134

131135
public function testEditTeam()
@@ -160,7 +164,10 @@ public function testEditTeam()
160164
]))
161165
->willReturn($expected);
162166

163-
$this->assertSame($expected, $api->edit(123, 'New Team', true, false, false, true, false));
167+
$permissions = new TeamPermissions;
168+
$permissions->canEditTeamPackages = true;
169+
$permissions->canViewVendorCustomers = true;
170+
$this->assertSame($expected, $api->edit(123, 'New Team', $permissions));
164171
}
165172

166173
public function testDeleteTeam()

0 commit comments

Comments
 (0)