Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
violinist_teams
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
violinist_teams
Commits
db2af846
Commit
db2af846
authored
1 year ago
by
Eirik Morland
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3354163
: Make it possible to get all admins of a team
parent
4c279d0f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!8
Issue #3354163: Make it possible to get all admins of a team
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/TeamNode.php
+66
-0
66 additions, 0 deletions
src/TeamNode.php
tests/src/Kernel/KernelTestBase.php
+22
-0
22 additions, 0 deletions
tests/src/Kernel/KernelTestBase.php
tests/src/Kernel/TeamNodeTest.php
+18
-0
18 additions, 0 deletions
tests/src/Kernel/TeamNodeTest.php
with
106 additions
and
0 deletions
src/TeamNode.php
+
66
−
0
View file @
db2af846
...
...
@@ -12,6 +12,7 @@ class TeamNode extends Node {
const
NODE_TYPE
=
'team'
;
const
MEMBERS_FIELD
=
'field_team_members'
;
const
ADMIN_FIELD
=
'field_team_admins'
;
/**
* Get the team plan.
...
...
@@ -31,6 +32,62 @@ class TeamNode extends Node {
}
}
/**
* Check if a user is an admin of the team.
*/
public
function
isAdmin
(
UserInterface
$user
)
{
if
(
!
$user
->
id
())
{
return
FALSE
;
}
return
in_array
(
$user
->
id
(),
$this
->
getAdministrators
());
}
/**
* Get all of the admins of a team.
*
* Will return an array of user ids.
*/
public
function
getAdministratorIds
()
:
array
{
if
(
!
$this
->
hasField
(
self
::
ADMIN_FIELD
)
||
$this
->
get
(
self
::
ADMIN_FIELD
)
->
isEmpty
())
{
return
[];
}
return
array_map
(
function
(
$item
)
{
return
$item
[
"target_id"
];
},
$this
->
get
(
self
::
ADMIN_FIELD
)
->
getValue
());
}
/**
* Get all of the admins of a team, the actual user objects.
*/
public
function
getAdministrators
()
:
array
{
if
(
!
$this
->
hasField
(
self
::
ADMIN_FIELD
)
||
$this
->
get
(
self
::
ADMIN_FIELD
)
->
isEmpty
())
{
return
[];
}
return
$this
->
get
(
self
::
ADMIN_FIELD
)
->
referencedEntities
();
}
/**
* Append an admin.
*
* If you are trying to append a user that is already in there, it will only
* be referenced once.
*/
public
function
appendAdmin
(
UserInterface
$user
)
:
self
{
if
(
!
$user
->
id
())
{
throw
new
\InvalidArgumentException
(
'Can not reference user id 0 when adding admins'
);
}
$admins
=
$this
->
getAdministrators
();
$already_has_it
=
(
bool
)
array_filter
(
$admins
,
function
(
UserInterface
$user_item
)
use
(
$user
)
{
return
$user
->
id
()
===
$user_item
->
id
();
});
if
(
$already_has_it
)
{
return
$this
;
}
$admins
[]
=
$user
;
$this
->
setAdmins
(
$admins
);
return
$this
;
}
/**
* Append a member.
*
...
...
@@ -62,6 +119,15 @@ class TeamNode extends Node {
return
$this
;
}
/**
* Set all of the admins of the node.
*/
public
function
setAdmins
(
array
$admins
)
:
self
{
$this
->
get
(
self
::
ADMIN_FIELD
)
->
setValue
(
$admins
);
return
$this
;
}
/**
* Helper to get all members of this team.
*
...
...
This diff is collapsed.
Click to expand it.
tests/src/Kernel/KernelTestBase.php
+
22
−
0
View file @
db2af846
...
...
@@ -65,6 +65,28 @@ abstract class KernelTestBase extends CoreKernelTestBase {
],
]);
$field
->
save
();
// Create the field for holding admins.
$fieldStorage
=
FieldStorageConfig
::
create
([
'field_name'
=>
'field_team_admins'
,
'entity_type'
=>
'node'
,
'type'
=>
'entity_reference'
,
'cardinality'
=>
-
1
,
'settings'
=>
[
'target_type'
=>
'user'
,
],
]);
$fieldStorage
->
save
();
$field
=
FieldConfig
::
create
([
'field_storage'
=>
$fieldStorage
,
'bundle'
=>
$this
->
nodeType
->
id
(),
'field_type'
=>
'entity_reference'
,
'required'
=>
FALSE
,
'settings'
=>
[
[
'handler'
=>
'default:user'
],
],
]);
$field
->
save
();
$this
->
installSchema
(
'user'
,
[
'users_data'
]);
}
...
...
This diff is collapsed.
Click to expand it.
tests/src/Kernel/TeamNodeTest.php
+
18
−
0
View file @
db2af846
...
...
@@ -41,4 +41,22 @@ class TeamNodeTest extends KernelTestBase {
self
::
assertCount
(
1
,
$team
->
getMembers
());
}
/**
* Tests getting admins from a node.
*/
public
function
testGetAdmins
()
{
/** @var \Drupal\violinist_teams\TeamNode $node */
$node
=
Node
::
create
([
'type'
=>
$this
->
nodeType
->
id
(),
]);
$user
=
User
::
create
([
'name'
=>
'test'
,
'mail'
=>
'test@example.com'
,
]);
$user
->
save
();
self
::
assertEquals
([],
$node
->
getAdministrators
());
$node
->
appendAdmin
(
$user
);
self
::
assertEquals
([
$user
->
id
()],
$node
->
getAdministratorIds
());
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment