Пример #1
0
 private function manageableRolesForUser()
 {
     $roles = $this->roleResultToList(Role::with('perms')->get());
     if (Entrust::can(PermissionNames::ManageGlobalPermissions())) {
         return $roles;
     }
     //Filter out global permissions
     $roles = array_filter($roles, function ($r) {
         $globalPerms = PermissionNames::AllGlobalPermissions();
         foreach ($r->perms as $p) {
             if (in_array($p->name, $globalPerms)) {
                 return false;
             }
         }
         return true;
     });
     $confPermNamePart = PermissionNames::normalizePermissionName(PermissionNames::ConferencePermissionManagement(1));
     $evtPermNamePart = PermissionNames::normalizePermissionName(PermissionNames::EventPermissionManagement(1));
     //Get the permissions this user has which are permissions management
     //permissions
     $currentPermManagement = Permission::whereHas("roles", function ($query) {
         $query->whereHas("users", function ($query) {
             //on Account table
             $query->where("id", Auth::user()->id);
         });
     })->where(function ($query) use($confPermNamePart, $evtPermNamePart) {
         $query->where('name', 'like', $confPermNamePart . '%');
         $query->orWhere('name', 'like', $evtPermNamePart . '%');
     })->get();
     $conferences = [];
     $events = [];
     foreach ($currentPermManagement as $perm) {
         if (PermissionNames::isConferencePermission($perm->name)) {
             $conferences[] = PermissionNames::extractPermissionData($perm->name)->idPart;
         } else {
             $events[] = PermissionNames::extractPermissionData($perm->name)->idPart;
         }
     }
     $ownedEvents = Event::whereIn('conferenceID', $conferences)->select('id')->get();
     $ownedEvents = array_map(function ($e) {
         return $e['id'];
     }, $ownedEvents->toArray());
     $events = array_merge($events, $ownedEvents);
     //Filter out permissions not associated with the conferences/events
     //this user can control.
     $roles = array_filter($roles, function ($r) use($events, $conferences) {
         foreach ($r->perms as $p) {
             if (PermissionNames::isConferencePermission($p->name)) {
                 $confId = PermissionNames::extractPermissionData($p->name)->idPart;
                 if (!in_array($confId, $conferences)) {
                     return false;
                 }
             } else {
                 if (PermissionNames::isEventPermission($p->name)) {
                     $evtId = PermissionNames::extractPermissionData($p->name)->idPart;
                     if (!in_array($evtId, $events)) {
                         return false;
                     }
                 }
             }
             return true;
         }
     });
     return $roles;
 }