2222use App \libs \Utils \TextUtils ;
2323use Doctrine \ORM \Event \PreUpdateEventArgs ;
2424use GuzzleHttp \Exception \RequestException ;
25+ use Illuminate \Support \Facades \Auth ;
2526use Illuminate \Support \Facades \Cache ;
2627use Illuminate \Support \Facades \Config ;
2728use 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