public function run()
 {
     $superadmin = Role::whereName('SuperAdmin')->get()->first();
     $actions = Action::all();
     $ownership = Ownership::whereName('any')->get()->first();
     $entities = Entity::all();
     foreach ($entities as $entity) {
         foreach ($actions as $action) {
             //                foreach ($ownerships as $ownership) {
             $permission = new Permission();
             $permission->role()->associate($superadmin);
             $permission->action()->associate($action);
             $permission->ownership()->associate($ownership);
             $permission->entity()->associate($entity);
             $permission->save();
             //                    $superadmin->permissions()->attach($permission);
             //                }
         }
     }
     //        $superadmin->save();
 }
 public function run()
 {
     Ownership::create(['name' => 'any']);
     Ownership::create(['name' => 'own']);
     Ownership::create(['name' => 'other']);
 }
 /**
  * @param $name
  * @return mixed
  */
 public function update($name)
 {
     if (Auth::user()->hasAccessTo('change', 'any', 'role')) {
         $role = Role::find($name);
         $allActions = Action::all();
         $allOwnerships = Ownership::all();
         $allEntities = Entity::all();
         if (Input::has('permissions')) {
             $role->permissions()->delete();
             foreach (Input::get('permissions') as $entity => $actions) {
                 foreach ($actions as $action => $ownership) {
                     if ('no' != $ownership) {
                         $currentAction = $allActions->find($action);
                         $currentOwnership = $allOwnerships->find($ownership);
                         $currentEntity = $allEntities->find($entity);
                         $currentPermission = new Permission();
                         $currentPermission->ownership()->associate($currentOwnership);
                         $currentPermission->action()->associate($currentAction);
                         $currentPermission->role()->associate($role);
                         $currentPermission->entity()->associate($currentEntity);
                         $currentPermission->save();
                     }
                 }
             }
         }
         $role->fill(Input::only(['name', 'description']));
         $role->save();
         return redirect()->route('roles.index');
     }
 }
 /**
  * @param $ownership
  * @throws InvalidOwnershipException
  */
 private function checkIfOwnershipExists($ownerships)
 {
     $allOwnerships = Ownership::all()->lists('name')->toArray();
     foreach ($ownerships as $ownership) {
         if (!in_array($ownership, $allOwnerships)) {
             $exception = new InvalidOwnershipException('The Ownership "' . $ownership . '" does not exist. Use one of: ' . implode(', ', $allOwnerships) . '.');
             $exception->ownership = $ownership;
             throw $exception;
         }
     }
 }