public static function manage() { Controller::requirePermissions(["AdminAccessDashboard", "AdminUserGroups"]); $permissions = Permission::get(); if (!empty($_POST)) { UserGroupModel::update($_POST["id"], $_POST["name"]); foreach ($permissions as $permission) { if (isset($_POST["permission-" . $permission->id])) { Permission::grantToGroup($_POST["id"], $permission->id); } else { Permission::revokeFromGroup($_POST["id"], $permission->id); } } Controller::addAlert(new Alert("success", "User group updated successfully")); } View::load("acp/user_group_manage.twig", ["object" => current(UserGroupModel::get($_GET["id"])), "permissions" => $permissions]); }
/** * Add a user group * * @param string $name group name * @param array|boolean $permissionNames array of permission names to grant or boolean true to * grant all available permissions * @param bool $special set to true if this is a special group (i.e. not deletable) * @return int group id */ public static function add($name, $permissionNames = [], $special = false) { $db = Database::getConnection(); (new InsertQuery($db))->into("user_groups")->fields(["name", "special"])->values("(?,?)", [$name, $special])->prepare()->execute(); $id = $db->lastInsertId(); if ($permissionNames === true) { $permissions = Permission::get(); } else { if (!empty($permissionNames)) { $permissions = Permission::get(null, $permissionNames); } else { $permissions = false; } } if (is_array($permissions) && !empty($permissions)) { $permissionIds = []; foreach ($permissions as $permission) { $permissionIds[] = $permission->id; } Permission::grantToGroup($id, $permissionIds); } return $id; }
/** * Load initial data */ protected static function loadInitialData() { $allPermissions = Permission::get(); $allPermissionIds = []; foreach ($allPermissions as $permission) { $allPermissionIds[] = $permission->id; } UserGroup::add("Root Admin", true, true); UserGroup::add("Committee", ["AccessAdminDashboard"]); UserGroup::add("Head Coach"); UserGroup::add("Coach"); UserGroup::add("Guest", [], true); }