Skip to content

Commit a686682

Browse files
committed
fix: add security check permissions on groups add / remove
Change-Id: I01b63a2ee5004d9200f08ba48a620dea5bf05fd0
1 parent 0ee6f2a commit a686682

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

app/Services/OpenId/UserService.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ public function update(int $id, array $payload): IEntity
255255

256256
$user = $this->repository->getById($id);
257257

258-
if (is_null($user) || !$user instanceof User)
259-
throw new EntityNotFoundException("user not found");
258+
if (!$user instanceof User)
259+
throw new EntityNotFoundException("User not found.");
260260

261261
$former_email = $user->getEmail();
262262
$former_password = $user->getPassword();
@@ -298,8 +298,7 @@ public function update(int $id, array $payload): IEntity
298298
foreach ($payload['groups'] as $group_id) {
299299
$group = $this->group_repository->getById($group_id);
300300
if (!$group instanceof Group)
301-
throw new EntityNotFoundException("group not found");
302-
301+
throw new EntityNotFoundException("Group not found");
303302
$user->addToGroup($group);
304303
}
305304
}

app/libs/Auth/Models/User.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use App\libs\Utils\TextUtils;
2323
use Doctrine\ORM\Event\PreUpdateEventArgs;
2424
use GuzzleHttp\Exception\RequestException;
25+
use Illuminate\Support\Facades\Auth;
2526
use Illuminate\Support\Facades\Cache;
2627
use Illuminate\Support\Facades\Config;
2728
use Illuminate\Support\Facades\Event;
@@ -717,8 +718,50 @@ public function belongToGroup(string $slug): bool
717718
*/
718719
public function addToGroup(Group $group)
719720
{
721+
Log::debug
722+
(
723+
sprintf
724+
(
725+
"User::addToGroup user %s user current groups %s group 2 add %s",
726+
$this->id,
727+
$this->getGroupsNice(),
728+
$group->getSlug()
729+
)
730+
);
731+
732+
$current_user = Auth::user();
733+
if($current_user instanceof User){
734+
Log::debug
735+
(
736+
sprintf
737+
(
738+
"User::addToGroup current user %s current user groups %s user %s user current groups %s group 2 add %s",
739+
$current_user->getId(),
740+
$current_user->getGroupsNice(),
741+
$this->id,
742+
$this->getGroupsNice(),
743+
$group->getSlug()
744+
)
745+
);
746+
747+
if(!$current_user->isActive())
748+
throw new ValidationException("Current User is not active.");
749+
750+
if(!$current_user->isSuperAdmin() && $group->getSlug() != IGroupSlugs::RawUsersGroup) {
751+
$current_user->deActivate();
752+
throw new ValidationException
753+
(
754+
sprintf(
755+
"Only Super Admins can add users to groups other than %s.",
756+
IGroupSlugs::RawUsersGroup
757+
)
758+
);
759+
}
760+
}
761+
720762
if ($this->groups->contains($group))
721763
throw new ValidationException("User is already assigned to this group.");
764+
722765
$this->groups->add($group);
723766
}
724767

@@ -727,6 +770,43 @@ public function addToGroup(Group $group)
727770
*/
728771
public function removeFromGroup(Group $group)
729772
{
773+
Log::debug
774+
(
775+
sprintf
776+
(
777+
"User::removeFromGroup user %s user current groups %s group 2 remove %s",
778+
$this->id,
779+
$this->getGroupsNice(),
780+
$group->getSlug()
781+
)
782+
);
783+
$current_user = Auth::user();
784+
if($current_user instanceof User){
785+
Log::debug
786+
(
787+
sprintf
788+
(
789+
"User::removeFromGroup current user %s current user groups %s user %s user current groups %s group 2 remove %s",
790+
$current_user->getId(),
791+
$current_user->getGroupsNice(),
792+
$this->id,
793+
$this->getGroupsNice(),
794+
$group->getSlug()
795+
)
796+
);
797+
798+
if(!$current_user->isActive())
799+
throw new ValidationException("Current User is not active.");
800+
801+
if(!$current_user->isSuperAdmin()) {
802+
$current_user->deActivate();
803+
throw new ValidationException
804+
(
805+
"Only Super Admins can remove users from groups",
806+
);
807+
}
808+
}
809+
730810
if (!$this->groups->contains($group)) return;
731811
$this->groups->removeElement($group);
732812
}

0 commit comments

Comments
 (0)