public function run()
 {
     \DB::table('user_permission')->truncate();
     $adminUserID = \App\Clusters\AuthCluster\Models\User::where('username', '=', 'admin')->firstOrFail()->id;
     $usersRolesPermissions = [['user_id' => $adminUserID, 'permission_id' => '1'], ['user_id' => $adminUserID, 'permission_id' => '2'], ['user_id' => $adminUserID, 'permission_id' => '3']];
     foreach ($usersRolesPermissions as $permission) {
         \App\Clusters\AuthCluster\Models\AccessControl\UserPermission::create($permission);
     }
 }
 public function index(Request $request)
 {
     $perPage = $request->get('perPage') ? $request->get('perPage') : 20;
     //AJAX autocomplete
     if ($request->ajax()) {
         $username = $request->get('term');
         if ($username) {
             $data = [];
             $users = User::username($username)->take(10)->get(['username']);
             foreach ($users as $user) {
                 $data[] = $user->username;
             }
             return $data;
         }
         return [];
     }
     //Check for username query in URL
     $username = $request->get('username');
     if ($username && strlen($username) > 3) {
         $users = User::username($username)->paginate($perPage);
         $users->appends(['username' => $username]);
         goto composeView;
     }
     //Check for role query in URL
     $role = $request->get('roleId');
     if ($role) {
         $role = UserRole::where('role_id', '=', $role)->get();
         $userIds = [];
         foreach ($role as $r) {
             $userIds[] = $r->user_id;
         }
         $users = User::whereIn('id', $userIds)->paginate($perPage);
         $users->appends(['roleId' => $role]);
         goto composeView;
     }
     //Check for permission query in URL
     $permission = $request->get('permissionId');
     if ($permission) {
         $permission = UserPermission::where('permission_id', '=', $permission)->get();
         $userIds = [];
         foreach ($permission as $r) {
             $userIds[] = $r->user_id;
         }
         $users = User::whereIn('id', $userIds)->paginate($perPage);
         $users->appends(['permissionId' => $permission]);
         goto composeView;
     }
     //TODO to get the role of each user there is a separate query, this is not cool
     $users = User::paginate($perPage);
     composeView:
     $permissions = Permission::orderBy('name', 'asc')->get();
     $permissionsList = Permission::orderBy('name', 'asc')->lists('name', 'id')->all();
     $rolesList = Role::orderBy('name', 'asc')->lists('name', 'id')->all();
     return view('authcluster.dashboard.users.index', compact('users', 'permissions', 'permissionsList', 'rolesList'));
 }