protected function initGroups() { $groups = ['users' => 'Regular users', 'admin' => 'Administrators']; $adminGroup = null; foreach ($groups as $name => $description) { if (!Gatekeeper::findGroupByName($name)) { Gatekeeper::createGroup(['name' => $name, 'description' => $description]); } } }
public function upsertGroupProcessAction() { $id = $_POST['id'] ?? null; if ($id && !ctype_digit($id)) { $this->flasher->error('E01 Invalid group ID: ' . $id); $this->redirect('/users/groups'); } $group = null; if ($id) { $group = Gatekeeper::findGroupById($id); if (!$group) { $this->flasher->error('E02 Invalid group ID: ' . $id); } } try { v::alnum('-._')->setName('Group name')->check($_POST['name']); } catch (ValidationException $e) { $this->flasher->error($e->getMainMessage()); echo $this->twig->render('users/groups/upsert.twig', ['flashes' => $this->flasher->display(), 'group' => $group ?: $_POST]); return false; } if ($group) { $group->name = $_POST['name']; $group->description = $_POST['description']; $group->save(); } else { Gatekeeper::createGroup($_POST); if (Gatekeeper::getLastError()) { $this->flasher->error($this->site['debug'] ? Gatekeeper::getLastError() : "Could not create group!"); echo $this->twig->render('users/groups/upsert.twig', ['flashes' => $this->flasher->display(), 'user' => $group ?: $_POST]); return false; } $this->flasher->success('Successfully created group.'); $this->redirect('/users/groups'); } }
$post = $app->request->post(); $data = ['success' => true]; $group = g::findGroupById($groupId); $group->name = $post['name']; $group->description = $post['description']; $ds = g::getDatasource(); if ($ds->save($group) === false) { throw new \Exception('Error saving user.'); } $data['group'] = $group->toArray(); $view->render('groups/edit.php', $data); }); $app->post('/add', function () use($app, $view) { $data = $app->request->post(); try { $result = g::createGroup(array('name' => $data['name'], 'description' => $data['description'])); if ($result === false) { throw new \Exception('Error creating group'); } } catch (\Exception $e) { if (ACCEPT_JSON) { $app->response->setStatus(400); } $data = array('message' => $e->getMessage()); } echo $view->render('group/add.php', $data); }); $app->post('/permissions', function () use($app, $view) { $idList = $app->request->post('ids'); $groupName = $app->request->post('name'); $group = g::findGroupByName($groupName);