芝麻web文件管理V1.00
编辑当前文件:/home/mgatv524/public_html/mctv/lib/Controller/UserGroup.php
. */ namespace Xibo\Controller; use Slim\Http\Response as Response; use Slim\Http\ServerRequest as Request; use Slim\Views\Twig; use Xibo\Entity\Permission; use Xibo\Entity\User; use Xibo\Factory\PermissionFactory; use Xibo\Factory\UserFactory; use Xibo\Factory\UserGroupFactory; use Xibo\Helper\ByteFormatter; use Xibo\Helper\SanitizerService; use Xibo\Service\ConfigServiceInterface; use Xibo\Service\LogServiceInterface; use Xibo\Support\Exception\AccessDeniedException; use Xibo\Support\Exception\InvalidArgumentException; /** * Class UserGroup * @package Xibo\Controller */ class UserGroup extends Base { /** * @var UserGroupFactory */ private $userGroupFactory; /** * @var PermissionFactory */ private $permissionFactory; /** * @var UserFactory */ private $userFactory; /** * Set common dependencies. * @param LogServiceInterface $log * @param SanitizerService $sanitizerService * @param \Xibo\Helper\ApplicationState $state * @param \Xibo\Entity\User $user * @param \Xibo\Service\HelpServiceInterface $help * @param ConfigServiceInterface $config * @param UserGroupFactory $userGroupFactory * @param PermissionFactory $permissionFactory * @param UserFactory $userFactory * @param Twig $view */ public function __construct($log, $sanitizerService, $state, $user, $help, $config, $userGroupFactory, $permissionFactory, $userFactory, Twig $view) { $this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $config, $view); $this->userGroupFactory = $userGroupFactory; $this->permissionFactory = $permissionFactory; $this->userFactory = $userFactory; } /** * Display page logic * @param Request $request * @param Response $response * @return \Psr\Http\Message\ResponseInterface|Response * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException */ function displayPage(Request $request, Response $response) { $this->getState()->template = 'usergroup-page'; return $this->render($request, $response); } /** * Group Grid * @SWG\Get( * path="/group", * operationId="userGroupSearch", * tags={"usergroup"}, * summary="UserGroup Search", * description="Search User Groups", * @SWG\Parameter( * name="userGroupId", * in="query", * description="Filter by UserGroup Id", * type="integer", * required=false * ), * @SWG\Parameter( * name="userGroup", * in="query", * description="Filter by UserGroup Name", * type="string", * required=false * ), * @SWG\Response( * response=200, * description="successful operation", * @SWG\Schema( * type="array", * @SWG\Items(ref="#/definitions/UserGroup") * ) * ) * ) * @param Request $request * @param Response $response * @return \Psr\Http\Message\ResponseInterface|Response * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException */ function grid(Request $request, Response $response) { $sanitizedQueryParams = $this->getSanitizer($request->getQueryParams()); $filterBy = [ 'groupId' => $sanitizedQueryParams->getInt('userGroupId'), 'group' => $sanitizedQueryParams->getString('userGroup'), 'isUserSpecific' => 0 ]; $groups = $this->userGroupFactory->query( $this->gridRenderSort($sanitizedQueryParams), $this->gridRenderFilter($filterBy, $sanitizedQueryParams) ); foreach ($groups as $group) { /* @var \Xibo\Entity\UserGroup $group */ $group->libraryQuotaFormatted = ByteFormatter::format($group->libraryQuota * 1024); if ($this->isApi($request)) continue; // we only want to show certain buttons, depending on the user logged in if ($this->getUser()->featureEnabled('usergroup.modify') && $this->isEditable($group) ) { // Edit $group->buttons[] = array( 'id' => 'usergroup_button_edit', 'url' => $this->urlFor($request,'group.edit.form', ['id' => $group->groupId]), 'text' => __('Edit') ); if ($this->getUser()->isSuperAdmin()) { // Delete $group->buttons[] = array( 'id' => 'usergroup_button_delete', 'url' => $this->urlFor($request,'group.delete.form', ['id' => $group->groupId]), 'text' => __('Delete') ); $group->buttons[] = ['divider' => true]; // Copy $group->buttons[] = array( 'id' => 'usergroup_button_copy', 'url' => $this->urlFor($request,'group.copy.form', ['id' => $group->groupId]), 'text' => __('Copy') ); $group->buttons[] = ['divider' => true]; } // Members $group->buttons[] = array( 'id' => 'usergroup_button_members', 'url' => $this->urlFor($request,'group.members.form', ['id' => $group->groupId]), 'text' => __('Members') ); if ($this->getUser()->isSuperAdmin()) { // Features $group->buttons[] = ['divider' => true]; $group->buttons[] = array( 'id' => 'usergroup_button_page_security', 'url' => $this->urlFor($request,'group.acl.form', ['id' => $group->groupId]), 'text' => __('Features'), 'title' => __('Turn Features on/off for this User') ); } } } $this->getState()->template = 'grid'; $this->getState()->recordsTotal = $this->userGroupFactory->countLast(); $this->getState()->setData($groups); return $this->render($request, $response); } /** * Form to Add a Group * @param Request $request * @param Response $response * @return \Psr\Http\Message\ResponseInterface|Response * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException */ function addForm(Request $request, Response $response) { $this->getState()->template = 'usergroup-form-add'; $this->getState()->setData([ 'help' => [ 'add' => $this->getHelp()->link('UserGroup', 'Add') ] ]); return $this->render($request, $response); } /** * Form to Edit a Group * @param Request $request * @param Response $response * @param $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\NotFoundException */ function editForm(Request $request, Response $response, $id) { $group = $this->userGroupFactory->getById($id); if (!$this->isEditable($group)) { throw new AccessDeniedException(); } $this->getState()->template = 'usergroup-form-edit'; $this->getState()->setData([ 'group' => $group, 'help' => [ 'add' => $this->getHelp()->link('UserGroup', 'Edit') ] ]); return $this->render($request, $response); } /** * Shows the Delete Group Form * @param Request $request * @param Response $response * @param $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\NotFoundException */ function deleteForm(Request $request, Response $response, $id) { $group = $this->userGroupFactory->getById($id); if (!$this->isEditable($group)) { throw new AccessDeniedException(); } $this->getState()->template = 'usergroup-form-delete'; $this->getState()->setData([ 'group' => $group, 'help' => [ 'delete' => $this->getHelp()->link('UserGroup', 'Delete') ] ]); return $this->render($request, $response); } /** * Add User Group * @SWG\Post( * path="/group", * operationId="userGroupAdd", * tags={"usergroup"}, * summary="UserGroup Add", * description="Add User Group", * @SWG\Parameter( * name="group", * in="formData", * description="Name of the User Group", * type="string", * required=true * ), * @SWG\Parameter( * name="decription", * in="formData", * description="A description of the User Group", * type="string", * required=false * ), * @SWG\Parameter( * name="libraryQuota", * in="formData", * description="The quota that should be applied (KiB). Provide 0 for no quota", * type="string", * required=false * ), * @SWG\Parameter( * name="isSystemNotification", * in="formData", * description="Flag (0, 1), should members of this Group receive system notifications?", * type="integer", * required=false * ), * @SWG\Parameter( * name="isDisplayNotification", * in="formData", * description="Flag (0, 1), should members of this Group receive Display notifications for Displays they have permissions to see", * type="integer", * required=false * ), * @SWG\Parameter( * name="isShownForAddUser", * in="formData", * description="Flag (0, 1), should this Group be shown in the Add User onboarding form.", * type="integer", * required=false * ), * @SWG\Parameter( * name="defaultHomePageId", * in="formData", * description="If this user has been created via the onboarding form, this should be the default home page", * type="integer", * required=false * ), * @SWG\Response( * response=200, * description="successful operation", * @SWG\Schema( * type="array", * @SWG\Items(ref="#/definitions/UserGroup") * ) * ) * ) * @param Request $request * @param Response $response * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\DuplicateEntityException * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\InvalidArgumentException */ function add(Request $request, Response $response) { $sanitizedParams = $this->getSanitizer($request->getParams()); // Check permissions if (!$this->getUser()->isSuperAdmin()) { throw new AccessDeniedException(); } // Build a user entity and save it $group = $this->userGroupFactory->createEmpty(); $group->group = $sanitizedParams->getString('group'); $group->description = $sanitizedParams->getString('description'); $group->libraryQuota = $sanitizedParams->getInt('libraryQuota'); if ($this->getUser()->userTypeId == 1) { $group->isSystemNotification = $sanitizedParams->getCheckbox('isSystemNotification'); $group->isDisplayNotification = $sanitizedParams->getCheckbox('isDisplayNotification'); $group->isShownForAddUser = $sanitizedParams->getCheckbox('isShownForAddUser'); $group->defaultHomepageId = $sanitizedParams->getString('defaultHomepageId'); } // Save $group->save(); // icondashboard does not need features, otherwise assign the feature matching selected homepage. if ($group->defaultHomepageId !== 'icondashboard.view' && !empty($group->defaultHomepageId)) { $group->features[] = $this->userGroupFactory->getHomepageByName($group->defaultHomepageId)->feature; $group->saveFeatures(); } // Return $this->getState()->hydrate([ 'message' => sprintf(__('Added %s'), $group->group), 'id' => $group->groupId, 'data' => $group ]); return $this->render($request, $response); } /** * Edit User Group * @SWG\Put( * path="/group/{userGroupId}", * operationId="userGroupEdit", * tags={"usergroup"}, * summary="UserGroup Edit", * description="Edit User Group", * @SWG\Parameter( * name="userGroupId", * in="path", * description="ID of the User Group", * type="integer", * required=true * ), * @SWG\Parameter( * name="group", * in="formData", * description="Name of the User Group", * type="string", * required=true * ), * @SWG\Parameter( * name="decription", * in="formData", * description="A description of the User Group", * type="string", * required=false * ), * @SWG\Parameter( * name="libraryQuota", * in="formData", * description="The quota that should be applied (KiB). Provide 0 for no quota", * type="string", * required=false * ), * @SWG\Parameter( * name="isSystemNotification", * in="formData", * description="Flag (0, 1), should members of this Group receive system notifications?", * type="integer", * required=false * ), * @SWG\Parameter( * name="isDisplayNotification", * in="formData", * description="Flag (0, 1), should members of this Group receive Display notifications for Displays they have permissions to see", * type="integer", * required=false * ), * @SWG\Parameter( * name="isShownForAddUser", * in="formData", * description="Flag (0, 1), should this Group be shown in the Add User onboarding form.", * type="integer", * required=false * ), * @SWG\Parameter( * name="defaultHomePageId", * in="formData", * description="If this user has been created via the onboarding form, this should be the default home page", * type="integer", * required=false * ), * @SWG\Response( * response=200, * description="successful operation", * @SWG\Schema( * type="array", * @SWG\Items(ref="#/definitions/UserGroup") * ) * ) * ) * @param Request $request * @param Response $response * @param $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\DuplicateEntityException * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\InvalidArgumentException * @throws \Xibo\Support\Exception\NotFoundException */ function edit(Request $request, Response $response, $id) { // Check permissions if (!$this->getUser()->isSuperAdmin() && !$this->getUser()->isGroupAdmin()) { throw new AccessDeniedException(); } $sanitizedParams = $this->getSanitizer($request->getParams()); $group = $this->userGroupFactory->getById($id); if (!$this->isEditable($group)) { throw new AccessDeniedException(); } $group->load(); $group->group = $sanitizedParams->getString('group'); $group->description = $sanitizedParams->getString('description'); $group->libraryQuota = $sanitizedParams->getInt('libraryQuota'); if ($this->getUser()->userTypeId == 1) { $group->isSystemNotification = $sanitizedParams->getCheckbox('isSystemNotification'); $group->isDisplayNotification = $sanitizedParams->getCheckbox('isDisplayNotification'); $group->isShownForAddUser = $sanitizedParams->getCheckbox('isShownForAddUser'); $group->defaultHomepageId = $sanitizedParams->getString('defaultHomepageId'); // if we have homepage set assign matching feature if it does not already exist if (!in_array($this->userGroupFactory->getHomepageByName($group->defaultHomepageId)->feature, $group->features) && $group->defaultHomepageId !== 'icondashboard.view' && !empty($group->defaultHomepageId) ) { $group->features[] = $this->userGroupFactory->getHomepageByName($group->defaultHomepageId)->feature; $group->saveFeatures(); } } // Save $group->save(); // Return $this->getState()->hydrate([ 'message' => sprintf(__('Edited %s'), $group->group), 'id' => $group->groupId, 'data' => $group ]); return $this->render($request, $response); } /** * Delete User Group * @param Request $request * @param Response $response * @param $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\NotFoundException * @SWG\Delete( * path="/group/{userGroupId}", * operationId="userGroupDelete", * tags={"usergroup"}, * summary="Delete User Group", * description="Delete User Group", * @SWG\Parameter( * name="userGroupId", * in="path", * description="The user Group ID to Delete", * type="integer", * required=true * ), * @SWG\Response( * response=204, * description="successful operation" * ) * ) */ function delete(Request $request, Response $response, $id) { // Check permissions if (!$this->getUser()->isSuperAdmin()) { throw new AccessDeniedException(); } $group = $this->userGroupFactory->getById($id); if (!$this->isEditable($group)) { throw new AccessDeniedException(); } $group->delete(); // Return $this->getState()->hydrate([ 'message' => sprintf(__('Deleted %s'), $group->group), 'id' => $group->groupId ]); return $this->render($request, $response); } /** * ACL Form for the provided GroupId * @param Request $request * @param Response $response * @param $id * @param int|null $userId * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\NotFoundException */ public function aclForm(Request $request, Response $response, $id, $userId = null) { // Check permissions to this function if (!$this->getUser()->isSuperAdmin()) { throw new AccessDeniedException(); } // Get permissions for the group provided $group = $this->userGroupFactory->getById($id); $inheritedFeatures = ($userId !== null) ? $this->userGroupFactory->getGroupFeaturesForUser($this->userFactory->getById($userId), false) : []; $data = [ 'groupId' => $id, 'group' => $group->group, 'isUserSpecific' => $group->isUserSpecific, 'features' => $group->features, 'inheritedFeatures' => $inheritedFeatures, 'userGroupFactory' => $this->userGroupFactory, 'help' => $this->getHelp()->link('User', 'Acl') ]; $this->getState()->template = 'usergroup-form-acl'; $this->getState()->setData($data); return $this->render($request, $response); } /** * ACL update * @param Request $request * @param Response $response * @param int $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\NotFoundException */ public function acl(Request $request, Response $response, $id) { // Check permissions to this function if (!$this->getUser()->isSuperAdmin()) { throw new AccessDeniedException(); } // Load the Group we are working on // Get the object if ($id == 0) { throw new InvalidArgumentException(__('Features form requested without a User Group'), 'id'); } $features = $request->getParam('features', null); if (!is_array($features)) { $features = []; } $group = $this->userGroupFactory->getById($id); $group->features = $features; $group->saveFeatures(); // Return $this->getState()->hydrate([ 'message' => sprintf(__('Features updated for %s'), $group->group), 'id' => $group->groupId ]); return $this->render($request, $response); } /** * Shows the Members of a Group * @param Request $request * @param Response $response * @param $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\NotFoundException */ public function membersForm(Request $request, Response $response, $id) { $group = $this->userGroupFactory->getById($id); if (!$this->isEditable($group)) { throw new AccessDeniedException(); } // Users in group $usersAssigned = $this->userFactory->query(null, ['groupIds' => [$id]]); // Users not in group $allUsers = $this->userFactory->query(); // The available users are all users except users already in assigned users $checkboxes = []; foreach ($allUsers as $user) { /* @var User $user */ // Check to see if it exists in $usersAssigned $exists = false; foreach ($usersAssigned as $userAssigned) { /* @var User $userAssigned */ if ($userAssigned->userId == $user->userId) { $exists = true; break; } } // Store this checkbox $checkbox = array( 'id' => $user->userId, 'name' => $user->userName, 'value_checked' => (($exists) ? 'checked' : '') ); $checkboxes[] = $checkbox; } $this->getState()->template = 'usergroup-form-members'; $this->getState()->setData([ 'group' => $group, 'checkboxes' => $checkboxes, 'help' => $this->getHelp()->link('UserGroup', 'Members') ]); return $this->render($request, $response); } /** * Assign User to the User Group * @SWG\Post( * path="/group/members/assign/{userGroupId}", * operationId="userGroupAssign", * tags={"usergroup"}, * summary="Assign User to User Group", * description="Assign User to User Group", * @SWG\Parameter( * name="userGroupId", * in="path", * description="ID of the user group to which assign the user", * type="integer", * required=true * ), * @SWG\Parameter( * name="userId", * in="formData", * description="Array of userIDs to assign", * type="array", * required=true, * @SWG\Items(type="integer") * ), * @SWG\Response( * response=200, * description="successful operation", * @SWG\Schema( * type="array", * @SWG\Items(ref="#/definitions/UserGroup") * ) * ) * ) * @param Request $request * @param Response $response * @param $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\DuplicateEntityException * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\InvalidArgumentException * @throws \Xibo\Support\Exception\NotFoundException */ public function assignUser(Request $request, Response $response, $id) { $this->getLog()->debug(sprintf('Assign User for groupId %d', $id)); $sanitizedPaarams = $this->getSanitizer($request->getParams()); $group = $this->userGroupFactory->getById($id); $group->load(); if (!$this->isEditable($group)) { throw new AccessDeniedException(); } $users = $sanitizedPaarams->getIntArray('userId', ['default' => []]); foreach ($users as $userId) { $this->getLog()->debug(sprintf('Assign User %d for groupId %d', $userId, $id)); $user = $this->userFactory->getById($userId); if (!$this->getUser()->checkViewable($user)) { throw new AccessDeniedException(__('Access Denied to User')); } $group->assignUser($user); $group->save(['validate' => false]); } // Check to see if unassign has been provided. $users = $sanitizedPaarams->getIntArray('unassignUserId', ['default' => []]); foreach ($users as $userId) { $this->getLog()->debug(sprintf('Unassign User %d for groupId %d', $userId, $id)); $user = $this->userFactory->getById($userId); if (!$this->getUser()->checkViewable($user)) { throw new AccessDeniedException(__('Access Denied to User')); } $group->unassignUser($user); $group->save(['validate' => false]); } // Return $this->getState()->hydrate([ 'message' => sprintf(__('Membership set for %s'), $group->group), 'id' => $group->groupId ]); return $this->render($request, $response); } /** * Unassign User to the User Group * @SWG\Post( * path="/group/members/unassign/{userGroupId}", * operationId="userGroupUnassign", * tags={"usergroup"}, * summary="Unassign User from User Group", * description="Unassign User from User Group", * @SWG\Parameter( * name="userGroupId", * in="path", * description="ID of the user group from which to unassign the user", * type="integer", * required=true * ), * @SWG\Parameter( * name="userId", * in="formData", * description="Array of userIDs to unassign", * type="array", * required=true, * @SWG\Items(type="integer") * ), * @SWG\Response( * response=200, * description="successful operation", * @SWG\Schema( * type="array", * @SWG\Items(ref="#/definitions/UserGroup") * ) * ) * ) * @param Request $request * @param Response $response * @param $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\DuplicateEntityException * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\InvalidArgumentException * @throws \Xibo\Support\Exception\NotFoundException */ public function unassignUser(Request $request, Response $response, $id) { $group = $this->userGroupFactory->getById($id); $sanitizedParams = $this->getSanitizer($request->getParams()); if (!$this->isEditable($group)) { throw new AccessDeniedException(); } $users = $sanitizedParams->getIntArray('userId'); foreach ($users as $userId) { $group->unassignUser($this->userFactory->getById($userId)); } $group->save(['validate' => false]); // Return $this->getState()->hydrate([ 'message' => sprintf(__('Membership set for %s'), $group->group), 'id' => $group->groupId ]); return $this->render($request, $response); } /** * Form to Copy Group * @param Request $request * @param Response $response * @param $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\NotFoundException */ function copyForm(Request $request, Response $response, $id) { $group = $this->userGroupFactory->getById($id); if (!$this->isEditable($group)) { throw new AccessDeniedException(); } $this->getState()->template = 'usergroup-form-copy'; $this->getState()->setData([ 'group' => $group ]); return $this->render($request, $response); } /** * @SWG\Post( * path="/group/{userGroupId}/copy", * operationId="userGroupCopy", * tags={"usergroup"}, * summary="Copy User Group", * description="Copy an user group, optionally copying the group members", * @SWG\Parameter( * name="userGroupId", * in="path", * description="The User Group ID to Copy", * type="integer", * required=true * ), * @SWG\Parameter( * name="group", * in="formData", * description="The Group Name", * type="string", * required=true * ), * @SWG\Parameter( * name="copyMembers", * in="formData", * description="Flag indicating whether to copy group members", * type="integer", * required=false * ), * @SWG\Parameter( * name="copyFeatures", * in="formData", * description="Flag indicating whether to copy group features", * type="integer", * required=false * ), * @SWG\Response( * response=201, * description="successful operation", * @SWG\Schema(ref="#/definitions/UserGroup"), * @SWG\Header( * header="Location", * description="Location of the new record", * type="string" * ) * ) * ) * * @param Request $request * @param Response $response * @param $id * @return \Psr\Http\Message\ResponseInterface|Response * @throws AccessDeniedException * @throws \Xibo\Support\Exception\ControllerNotImplemented * @throws \Xibo\Support\Exception\DuplicateEntityException * @throws \Xibo\Support\Exception\GeneralException * @throws \Xibo\Support\Exception\InvalidArgumentException * @throws \Xibo\Support\Exception\NotFoundException */ public function copy(Request $request, Response $response, $id) { $group = $this->userGroupFactory->getById($id); $sanitizedParams = $this->getSanitizer($request->getParams()); // Check we have permission to view this group if (!$this->isEditable($group)) { throw new AccessDeniedException(); } // Clone the group $group->load([ 'loadUsers' => ($sanitizedParams->getCheckbox('copyMembers') == 1) ]); $newGroup = clone $group; $newGroup->group = $sanitizedParams->getString('group'); $newGroup->save(); // Save features? if ($sanitizedParams->getCheckbox('copyFeatures')) { $newGroup->saveFeatures(); } else { $newGroup->features = []; } // Copy permissions foreach ($this->permissionFactory->getByGroupId('Page', $group->groupId) as $permission) { /* @var Permission $permission */ $permission = clone $permission; $permission->groupId = $newGroup->groupId; $permission->save(); } $this->getState()->hydrate([ 'httpStatus' => 201, 'message' => sprintf(__('Copied %s'), $group->group), 'id' => $newGroup->groupId, 'data' => $newGroup ]); return $this->render($request, $response); } /** * @param \Xibo\Entity\UserGroup $group * @return bool */ private function isEditable($group) { return $this->getUser()->isSuperAdmin() || ($this->getUser()->isGroupAdmin() && count(array_intersect($this->getUser()->groups, [$group]))); } }