Commit d199c1c7 authored by Keven's avatar Keven
Browse files

Added domain type verification on responses

parent 89378561
......@@ -26,6 +26,12 @@ abstract class AccessCommand extends Command
*/
protected $objectClass = 'Libcast\Client\Entity\Access';
/**
*
* @var array of string
*/
protected $authorizedRoots = ['access', 'accesses'];
/**
*
* @param \Libcast\Client\Entity\Entity $entity
......@@ -119,4 +125,4 @@ abstract class AccessCommand extends Command
$this->setPayload($payload);
}
}
\ No newline at end of file
}
......@@ -20,6 +20,12 @@ abstract class ChannelCommand extends Command
*/
protected $objectClass = 'Libcast\Client\Entity\Channel';
/**
*
* @var array of string
*/
protected $authorizedRoots = ['channel', 'channels'];
/**
*
* @param MediaInterface $media Media instance, media URL or media unique name
......@@ -38,4 +44,4 @@ abstract class ChannelCommand extends Command
$this->setVar('media', $media);
}
}
}
\ No newline at end of file
}
......@@ -12,6 +12,7 @@ use Libcast\Client\Exception\UnauthorizedException;
use Libcast\Client\Exception\ForbiddenException;
use Libcast\Client\Exception\NotFoundException;
use Libcast\Client\Exception\ServerException;
use Libcast\Client\Exception\InvalidTypeException;
use JMS\Serializer\SerializerInterface;
......@@ -261,6 +262,8 @@ abstract class Command implements CommandInterface
case 500: throw new ServerException($response->getReasonPhrase());
}
$this->verifyResponse($response->getContent());
if ($this->collectionResponse) {
return new Collection($response->getContent(), $this->objectClass, $this->client, $serializer);
} else {
......@@ -273,6 +276,36 @@ abstract class Command implements CommandInterface
}
}
/**
*
* @param string $response
*/
protected function verifyResponse($response)
{
if (!$response) {
return;
}
try {
$xml = new \SimpleXMLElement($response);
} catch (\Exception $e) {
throw new ServerException('The server returned invalid XML content.');
}
if (isset($this->authorizedRoots) and !in_array($xml->getName(), $this->authorizedRoots)) {
throw new InvalidTypeException(sprintf('The returned content is not valid "%s" type (%s was expected but "%s" received).', $this->getExpectedType(), count($this->authorizedRoots) > 1 ? 'one of "'.implode('", "', $this->authorizedRoots).'"' : '"'.reset($this->authorizedRoots).'"', $xml->getName()));
}
}
/**
*
* @return string
*/
protected function getExpectedType()
{
return substr(strrchr($this->objectClass, '\\'), 1);
}
/**
*
* @return mixed
......
......@@ -25,7 +25,13 @@ class CreateUser extends Command
protected $objectClass = 'Libcast\Client\Entity\User';
/**
*
*
* @var array of string
*/
protected $authorizedRoots = ['user'];
/**
*
* @param UserInterface $user
* @param string $name Platform $name
*/
......@@ -54,7 +60,7 @@ class CreateUser extends Command
}
/**
*
*
* @todo This should use a real URI Template resolver
* @param string $path
* @param array $vars
......@@ -67,4 +73,4 @@ class CreateUser extends Command
return parent::resolvePath($path, $vars);
}
}
\ No newline at end of file
}
......@@ -4,6 +4,7 @@ namespace Libcast\Client\Command;
use Libcast\Client\Exception\BadParameterException;
use Libcast\Client\Model\FileInterface;
use Libcast\Client\Http\Response;
abstract class FileCommand extends Command
{
......@@ -19,6 +20,12 @@ abstract class FileCommand extends Command
*/
protected $objectClass = 'Libcast\Client\Entity\File';
/**
*
* @var array of string
*/
protected $authorizedRoots = ['file'];
/**
*
* @param FileInterface $file File instance, file URL or file unique slug
......
......@@ -19,6 +19,12 @@ abstract class FolderCommand extends Command
*/
protected $objectClass = 'Libcast\Client\Entity\Folder';
/**
*
* @var array of string
*/
protected $authorizedRoots = ['files', 'directory'];
/**
*
* @param FolderInterface $folder Folder instance, folder URL or folder unique slug
......@@ -38,4 +44,4 @@ abstract class FolderCommand extends Command
$this->setVar('folder', $folder);
}
}
}
\ No newline at end of file
}
......@@ -15,4 +15,10 @@ abstract class MediaCommand extends Command
* @var string
*/
protected $objectClass = 'Libcast\Client\Entity\Media';
/**
*
* @var array of string
*/
protected $authorizedRoots = ['media', 'medias'];
}
......@@ -15,4 +15,10 @@ abstract class PlatformCommand extends Command
* @var string
*/
protected $objectClass = 'Libcast\Client\Entity\Platform';
}
\ No newline at end of file
/**
*
* @var array of string
*/
protected $authorizedRoots = ['platform', 'platforms'];
}
......@@ -20,6 +20,12 @@ abstract class ProfileCommand extends Command
*/
protected $objectClass = 'Libcast\Client\Entity\Profile';
/**
*
* @var array of string
*/
protected $authorizedRoots = ['profile', 'profiles'];
/**
*
* @param PlatformInterface $platform Platform instance, platform URL or platform unique name
......@@ -39,4 +45,4 @@ abstract class ProfileCommand extends Command
$this->setVar('name', $platform);
}
}
}
\ No newline at end of file
}
......@@ -21,6 +21,12 @@ abstract class ResourceCommand extends Command
*/
protected $objectClass = 'Libcast\Client\Entity\Resource';
/**
*
* @var array of string
*/
protected $authorizedRoots = ['resource', 'resources'];
/**
*
* @param string $url
......
......@@ -17,7 +17,13 @@ class Role extends Command
protected $objectClass = 'Libcast\Client\Entity\Role';
/**
*
*
* @var array of string
*/
protected $authorizedRoots = ['role'];
/**
*
* @param string $name
*/
public function __construct($name)
......@@ -28,4 +34,4 @@ class Role extends Command
$this->setVar('name', $name);
}
}
}
\ No newline at end of file
}
......@@ -22,6 +22,12 @@ class Roles extends Command
*/
protected $collectionResponse = true;
/**
*
* @var array of string
*/
protected $authorizedRoots = ['roles'];
/**
*
* @param string $platform
......
......@@ -21,6 +21,12 @@ abstract class StreamCommand extends Command
*/
protected $objectClass = 'Libcast\Client\Entity\Stream';
/**
*
* @var array of string
*/
protected $authorizedRoots = ['stream', 'streams'];
/**
*
* @param MediaInterface $media Media instance, media URL or media unique name
......
......@@ -17,7 +17,13 @@ class User extends Command
protected $objectClass = 'Libcast\Client\Entity\User';
/**
*
*
* @var array of string
*/
protected $authorizedRoots = ['user'];
/**
*
* @param string $username
*/
public function __construct($username)
......
......@@ -22,6 +22,12 @@ class Users extends Command
*/
protected $collectionResponse = true;
/**
*
* @var array of string
*/
protected $authorizedRoots = ['users'];
/**
*
* @param string $platform
......@@ -59,4 +65,4 @@ class Users extends Command
return parent::resolvePath($path, $vars);
}
}
\ No newline at end of file
}
......@@ -15,4 +15,10 @@ abstract class WidgetCommand extends Command
* @var string
*/
protected $objectClass = 'Libcast\Client\Entity\Widget';
}
\ No newline at end of file
/**
*
* @var array of string
*/
protected $authorizedRoots = ['widget', 'widgets'];
}
<?php
namespace Libcast\Client\Exception;
class InvalidTypeException extends ClientException
{
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment