Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin cannot be demoted or removed from a group when he's the only one left #110

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions features/group-member.feature
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,39 @@ Feature: Manage BuddyPress Group Members
Then STDOUT should be a number
And save STDOUT as {MEMBER_ID}

When I run `wp user create randon [email protected] --porcelain`
Then STDOUT should be a number
And save STDOUT as {RANDON_MEMBER_ID}

When I run `wp user create anothermod [email protected] --porcelain`
Then STDOUT should be a number
And save STDOUT as {ANOTHER_MEMBER_ID}

When I run `wp bp group create --name="Totally Cool Group" --creator-id={CREATOR_ID} --porcelain`
Then STDOUT should be a number
And save STDOUT as {GROUP_ID}

When I run `wp bp group member create --group-id={GROUP_ID} --user-id={MEMBER_ID}`
When I run `wp bp group meta add {GROUP_ID} invite_status 'public'`
Then STDOUT should not be empty

When I run `wp bp group member add --group-id={GROUP_ID} --user-id={MEMBER_ID}`
Then STDOUT should contain:
"""
Success: Added user #{MEMBER_ID} to group #{GROUP_ID} as member.
"""

When I run `wp bp group member create --group-id={GROUP_ID} --user-id={ANOTHER_MEMBER_ID}`
Then STDOUT should contain:
"""
Success: Added user #{ANOTHER_MEMBER_ID} to group #{GROUP_ID} as member.
"""

When I run `wp bp group member list {GROUP_ID} --fields=id`
Then STDOUT should be a table containing rows:
| id |
| {CREATOR_ID} |
| {MEMBER_ID} |
| id |
| {CREATOR_ID} |
| {MEMBER_ID} |
| {ANOTHER_MEMBER_ID} |

When I run `wp bp group member promote --group-id={GROUP_ID} --user-id={MEMBER_ID} --role=mod`
Then STDOUT should contain:
Expand All @@ -45,6 +63,13 @@ Feature: Manage BuddyPress Group Members
| id |
| {MEMBER_ID} |

When I try `wp bp group member demote --group-id={GROUP_ID} --user-id={RANDON_MEMBER_ID}`
Then the return code should be 1
Then STDERR should be:
"""
Error: User is not a member of the group.
"""

When I run `wp bp group member demote --group-id={GROUP_ID} --user-id={MEMBER_ID}`
Then STDOUT should contain:
"""
Expand All @@ -54,18 +79,36 @@ Feature: Manage BuddyPress Group Members
When I try `wp bp group member list {GROUP_ID} --fields=user_id --role=mod`
Then the return code should be 1

When I run `wp bp group member ban --group-id={GROUP_ID} --user-id={MEMBER_ID}`
When I run `wp bp group member promote --group-id={GROUP_ID} --user-id={MEMBER_ID} --role=admin`
Then STDOUT should contain:
"""
Success: Member promoted to new role successfully.
"""

When I run `wp bp group member promote --group-id={GROUP_ID} --user-id={ANOTHER_MEMBER_ID} --role=admin`
Then STDOUT should contain:
"""
Success: Member promoted to new role successfully.
"""

When I run `wp bp group member demote --group-id={GROUP_ID} --user-id={ANOTHER_MEMBER_ID}`
Then STDOUT should contain:
"""
Success: User demoted to the "member" status.
"""

When I run `wp bp group member ban --group-id={GROUP_ID} --user-id={ANOTHER_MEMBER_ID}`
Then STDOUT should contain:
"""
Success: Member banned from the group.
"""

When I run `wp bp group member list {GROUP_ID} --fields=user_id --role=banned`
Then STDOUT should be a table containing rows:
| user_id |
| {MEMBER_ID} |
| user_id |
| {ANOTHER_MEMBER_ID} |

When I run `wp bp group member unban --group-id={GROUP_ID} --user-id={MEMBER_ID}`
When I run `wp bp group member unban --group-id={GROUP_ID} --user-id={ANOTHER_MEMBER_ID}`
Then STDOUT should contain:
"""
Success: Member unbanned from the group.
Expand All @@ -74,8 +117,8 @@ Feature: Manage BuddyPress Group Members
When I try `wp bp group member list {GROUP_ID} --fields=user_id --role=banned`
Then the return code should be 1

When I run `wp bp group member remove --group-id={GROUP_ID} --user-id={MEMBER_ID}`
When I run `wp bp group member remove --group-id={GROUP_ID} --user-id={ANOTHER_MEMBER_ID}`
Then STDOUT should contain:
"""
Success: Member #{MEMBER_ID} removed from the group #{GROUP_ID}.
Success: Member #{ANOTHER_MEMBER_ID} removed from the group #{GROUP_ID}.
"""
16 changes: 15 additions & 1 deletion features/group.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Feature: Manage BuddyPress Groups

When I try `wp bp group get i-do-not-exist`
Then the return code should be 1
Then STDERR should be:
"""
Error: No group found by that slug or ID.
"""

When I run `wp bp group update {GROUP_ID} --description=foo`
Then STDOUT should not be empty
Expand All @@ -46,17 +50,27 @@ Feature: Manage BuddyPress Groups

When I try `wp bp group get {GROUP_ID}`
Then the return code should be 1
Then STDERR should be:
"""
Error: No group found by that slug or ID.
"""

Scenario: Group list

When I run `wp bp group create --name="ZZZ Group 1" --slug=group1 --porcelain`
Then STDOUT should be a number
And save STDOUT as {GROUP_ONE_ID}

When I run `wp bp group meta add {GROUP_ONE_ID} invite_status 'public'`
Then STDOUT should not be empty

When I run `wp bp group create --name="AAA Group 2" --slug=group2 --porcelain`
Then STDOUT should be a number
And save STDOUT as {GROUP_TWO_ID}

When I run `wp bp group meta add {GROUP_TWO_ID} invite_status 'public'`
Then STDOUT should not be empty

When I run `wp bp group list --fields=id,name,slug`
Then STDOUT should be a table containing rows:
| id | name | slug |
Expand All @@ -82,7 +96,7 @@ Feature: Manage BuddyPress Groups
When I try `wp bp group list --fields=id --user-id={MEMBER_ID}`
Then the return code should be 1

When I run `wp bp group member create --group-id={GROUP_ONE_ID} --user-id={MEMBER_ID}`
When I run `wp bp group member add --group-id={GROUP_ONE_ID} --user-id={MEMBER_ID}`
Then the return code should be 0

When I run `wp bp group list --fields=id --user-id={MEMBER_ID}`
Expand Down
71 changes: 52 additions & 19 deletions src/group-member.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public function create( $args, $assoc_args ) {
}

if ( 'member' !== $role ) {
groups_promote_member( $user->ID, $group_id, $role );
$group_member = new \BP_Groups_Member( $user->ID, $group_id );
$group_member->promote( $role );
}

WP_CLI::success(
Expand Down Expand Up @@ -117,12 +118,20 @@ public function create( $args, $assoc_args ) {
* @alias trash
*/
public function delete( $args, $assoc_args ) {
$group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
$member = new \BP_Groups_Member( $user->ID, $group_id );
$group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
$group_member = new \BP_Groups_Member( $user->ID, $group_id );

// Check if the user is the only admin of the group.
if ( (bool) $group_member->is_admin ) {
$group_admins = groups_get_group_admins( $group_id );
if ( 1 === count( $group_admins ) ) {
WP_CLI::error( 'Cannot remove the only admin of the group.' );
}
}

// True on success.
if ( $member->remove() ) {
if ( $group_member->remove() ) {
WP_CLI::success( sprintf( 'Member #%d removed from the group #%d.', $user->ID, $group_id ) );
} else {
WP_CLI::error( 'Could not remove member from the group.' );
Expand Down Expand Up @@ -217,6 +226,10 @@ public function list_( $args, $assoc_args ) {

$members = $members_query['members'];

if ( empty( $members ) ) {
WP_CLI::error( 'No group members found.' );
}

// Make 'role' human-readable.
foreach ( $members as &$member ) {
$role = 'member';
Expand All @@ -229,10 +242,6 @@ public function list_( $args, $assoc_args ) {
$member->role = $role;
}

if ( empty( $members ) ) {
WP_CLI::error( 'No group members found.' );
}

if ( empty( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = [
'id',
Expand Down Expand Up @@ -277,11 +286,11 @@ public function list_( $args, $assoc_args ) {
* Success: Member promoted to new role successfully.
*/
public function promote( $args, $assoc_args ) {
$group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
$member = new \BP_Groups_Member( $user->ID, $group_id );
$group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
$group_member = new \BP_Groups_Member( $user->ID, $group_id );

if ( $member->promote( $assoc_args['role'] ) ) {
if ( $group_member->promote( $assoc_args['role'] ) ) {
WP_CLI::success( 'Member promoted to new role successfully.' );
} else {
WP_CLI::error( 'Could not promote the member.' );
Expand All @@ -301,20 +310,38 @@ public function promote( $args, $assoc_args ) {
*
* ## EXAMPLES
*
* # Demote a user to the "member" status.
* # Demote a user to the "member" status using numeric IDs.
* $ wp bp group member demote --group-id=3 --user-id=10
* Success: User demoted to the "member" status.
*
* # Demote a user to the "member" status.
* # Demote a user to the "member" status using slugs.
* $ wp bp group member demote --group-id=foo --user-id=admin
* Success: User demoted to the "member" status.
*
* # Demote a user not part of the group.
* $ wp bp group member demote --group-id=foo --user-id=admin
* Error: User is not a member of the group.
*/
public function demote( $args, $assoc_args ) {
$group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
$member = new \BP_Groups_Member( $user->ID, $group_id );

if ( $member->demote() ) {
// Check if the user is a member of the group.
if ( ! groups_is_user_member( $user->ID, $group_id ) ) {
WP_CLI::error( 'User is not a member of the group.' );
}

$group_member = new \BP_Groups_Member( $user->ID, $group_id );

// Check if the user is the only admin of the group.
if ( (bool) $group_member->is_admin ) {
$group_admins = groups_get_group_admins( $group_id );
if ( 1 === count( $group_admins ) ) {
WP_CLI::error( 'Cannot demote the only admin of the group.' );
}
}

if ( $group_member->demote() ) {
WP_CLI::success( 'User demoted to the "member" status.' );
} else {
WP_CLI::error( 'Could not demote the member.' );
Expand Down Expand Up @@ -345,9 +372,15 @@ public function demote( $args, $assoc_args ) {
public function ban( $args, $assoc_args ) {
$group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
$member = new \BP_Groups_Member( $user->ID, $group_id );

if ( $member->ban() ) {
// Check if the user is a member of the group.
if ( ! groups_is_user_member( $user->ID, $group_id ) ) {
WP_CLI::error( 'User is not a member of the group.' );
}

$group_member = new \BP_Groups_Member( $user->ID, $group_id );

if ( $group_member->ban() ) {
WP_CLI::success( 'Member banned from the group.' );
} else {
WP_CLI::error( 'Could not ban the member.' );
Expand Down
5 changes: 4 additions & 1 deletion src/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ public function create( $args, $assoc_args ) {
]
);

// Auto-generate some stuff.
// Auto-generate slug.
if ( empty( $r['slug'] ) ) {
$r['slug'] = groups_check_slug( sanitize_title( $r['name'] ) );
}

// Auto-generate description.
if ( empty( $r['description'] ) ) {
$r['description'] = sprintf( 'Description for group "%s"', $r['name'] );
}
Expand Down Expand Up @@ -364,7 +365,9 @@ function ( $group_id ) {
*
* ## EXAMPLES
*
* # Update a group.
* $ wp bp group update 35 --description="What a cool group!" --name="Group of Cool People"
* Success: Group updated.
*/
public function update( $args, $assoc_args ) {
parent::_update(
Expand Down
Loading