function index($id, Request $request, Application $app)
 {
     $this->build($id, $request, $app);
     if ($request->request->get('action') == "addpermission" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $extension = $app['extensions']->getExtensionById($request->request->get("extension"));
         if ($extension) {
             $permission = $extension->getUserPermission($request->request->get("permission"));
             if ($permission) {
                 $ugr = new UserGroupRepository();
                 $ugr->addPermissionToGroup($permission, $this->parameters['usergroup'], $app['currentUser']);
                 return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
             }
         }
     } else {
         if ($request->request->get('action') == "removepermission" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
             $extension = $app['extensions']->getExtensionById($request->request->get("extension"));
             if ($extension) {
                 $permission = $extension->getUserPermission($request->request->get("permission"));
                 if ($permission) {
                     $ugr = new UserGroupRepository();
                     $ugr->removePermissionFromGroup($permission, $this->parameters['usergroup'], $app['currentUser']);
                     return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                 }
             }
         }
     }
     $form = $app['form.factory']->create(new ActionForm());
     if ('POST' == $request->getMethod()) {
         $form->bind($request);
         if ($form->isValid()) {
             $data = $form->getData();
             $action = new ActionParser($data['action']);
             if ($action->getCommand() == 'addusername') {
                 $uar = new UserAccountRepository();
                 $user = $uar->loadByUserName($action->getParam(0));
                 if ($user) {
                     $ugr = new UserGroupRepository();
                     $ugr->addUserToGroup($user, $this->parameters['usergroup'], $app['currentUser']);
                     return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                 }
             } else {
                 if ($action->getCommand() == 'removeusername') {
                     $uar = new UserAccountRepository();
                     $user = $uar->loadByUserName($action->getParam(0));
                     if ($user) {
                         $ugr = new UserGroupRepository();
                         $ugr->removeUserFromGroup($user, $this->parameters['usergroup'], $app['currentUser']);
                         return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                     }
                 } else {
                     if ($action->getCommand() == 'includesanonymous') {
                         $ugr = new UserGroupRepository();
                         $this->parameters['usergroup']->setIsIncludesAnonymous($action->getParamBoolean(0));
                         $ugr->editIsIncludesAnonymous($this->parameters['usergroup'], $app['currentUser']);
                         return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                     } else {
                         if ($action->getCommand() == 'includesusers') {
                             $ugr = new UserGroupRepository();
                             $this->parameters['usergroup']->setIsIncludesUsers($action->getParamBoolean(0));
                             $ugr->editIsIncludesUser($this->parameters['usergroup'], $app['currentUser']);
                             return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                         } else {
                             if ($action->getCommand() == 'includesverifiedusers') {
                                 $ugr = new UserGroupRepository();
                                 $this->parameters['usergroup']->setIsIncludesVerifiedUsers($action->getParamBoolean(0));
                                 $ugr->editIsIncludesVerifiedUser($this->parameters['usergroup'], $app['currentUser']);
                                 return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                             }
                         }
                     }
                 }
             }
         }
     }
     $this->parameters['form'] = $form->createView();
     $urb = new UserAccountRepositoryBuilder();
     $urb->setInUserGroup($this->parameters['usergroup']);
     $this->parameters['users'] = $urb->fetchAll();
     $r = new UserPermissionsRepository($app['extensions']);
     $this->parameters['userpermissions'] = $r->getPermissionsForUserGroup($this->parameters['usergroup'], false);
     $this->parameters['userpermissionstoadd'] = array();
     foreach ($app['extensions']->getExtensionsIncludingCore() as $ext) {
         foreach ($ext->getUserPermissions() as $key) {
             $per = $ext->getUserPermission($key);
             if ($per->isForIndex() && !in_array($per, $this->parameters['userpermissions'])) {
                 $this->parameters['userpermissionstoadd'][] = $per;
             }
         }
     }
     return $app['twig']->render('sysadmin/usergroup/index.html.twig', $this->parameters);
 }
 function users($id, Request $request, Application $app)
 {
     if (!$this->build($id, $request, $app)) {
         $app->abort(404, "User Group does not exist.");
     }
     if ($request->request->get('action') == "removeuser" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $ur = new UserAccountRepository();
         $user = $ur->loadById($request->request->get('id'));
         if ($user) {
             $ugr = new UserGroupRepository();
             $ugr->removeUserFromGroup($user, $this->parameters['usergroup'], $app['currentUser']);
             return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
         }
     } else {
         if ($request->request->get('action') == "adduser" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
             $ur = new UserAccountRepository();
             $user = $ur->loadByUserName($request->request->get('username'));
             if ($user) {
                 $ugr = new UserGroupRepository();
                 $ugr->addUserToGroup($user, $this->parameters['usergroup'], $app['currentUser']);
                 return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
             } else {
                 $app['flashmessages']->addError("Could not find user");
             }
         } else {
             if ($request->request->get('action') == "removeanonymous" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                 $this->parameters['usergroup']->setIsIncludesAnonymous(false);
                 $ugr = new UserGroupRepository();
                 $ugr->editIsIncludesAnonymous($this->parameters['usergroup'], $app['currentUser']);
                 return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
             } else {
                 if ($request->request->get('action') == "addanonymous" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                     $this->parameters['usergroup']->setIsIncludesAnonymous(true);
                     $ugr = new UserGroupRepository();
                     $ugr->editIsIncludesAnonymous($this->parameters['usergroup'], $app['currentUser']);
                     return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                 } else {
                     if ($request->request->get('action') == "removeusers" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                         $this->parameters['usergroup']->setIsIncludesUsers(false);
                         $ugr = new UserGroupRepository();
                         $ugr->editIsIncludesUser($this->parameters['usergroup'], $app['currentUser']);
                         return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                     } else {
                         if ($request->request->get('action') == "addusers" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                             $this->parameters['usergroup']->setIsIncludesUsers(true);
                             $ugr = new UserGroupRepository();
                             $ugr->editIsIncludesUser($this->parameters['usergroup'], $app['currentUser']);
                             return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                         } else {
                             if ($request->request->get('action') == "removeverifiedusers" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                                 $this->parameters['usergroup']->setIsIncludesVerifiedUsers(false);
                                 $ugr = new UserGroupRepository();
                                 $ugr->editIsIncludesVerifiedUser($this->parameters['usergroup'], $app['currentUser']);
                                 return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                             } else {
                                 if ($request->request->get('action') == "addverifiedusers" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                                     $this->parameters['usergroup']->setIsIncludesVerifiedUsers(true);
                                     $ugr = new UserGroupRepository();
                                     $ugr->editIsIncludesVerifiedUser($this->parameters['usergroup'], $app['currentUser']);
                                     return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $urb = new UserAccountRepositoryBuilder();
     $urb->setInUserGroup($this->parameters['usergroup']);
     $this->parameters['users'] = $urb->fetchAll();
     $r = new UserPermissionsRepository($app['extensions']);
     $this->parameters['userpermissions'] = $r->getPermissionsForUserGroup($this->parameters['usergroup'], false);
     return $app['twig']->render('site/adminusergroup/users.html.twig', $this->parameters);
 }