* @license GPL2 or any later version * * @since Stud.IP 5.3 */ class UnitsCopy extends NonJsonApiController { public function __invoke(Request $request, Response $response, array $args) { $data = $request->getParsedBody()['data']; $sourceUnit = Unit::find($args['id']); $user = $this->getUser($request); $rangeId = $data['rangeId']; $rangeType = $data['rangeType']; $modified = $data['modified']; $duplicate = $data['duplicate']; try { $range = \RangeFactory::createRange($rangeType, $rangeId); } catch (\Exception $e) { throw new RecordNotFoundException('Range could not be found'); } if (!Authority::canCreateUnit($user, $range)) { throw new AuthorizationFailedException(); } $newUnit = $sourceUnit->copy($user, $rangeId, $rangeType, $modified, $duplicate); $response = $response->withHeader('Content-Type', 'application/json'); $response->getBody()->write((string) json_encode($newUnit)); return $response; } }