- // remove all groups, replace with any groups specified
- foreach ( $this->user->getGroups() as $group ) {
- $this->user->removeGroup( $group );
+
+ // Adjust groups by adding any missing ones and removing any extras
+ $currentGroups = $this->user->getGroups();
+ foreach ( array_diff( $this->groups, $currentGroups ) as $group ) {
+ $this->user->addGroup( $group );