Browse Source

Added error message callback, make stream name and user_id unique

master
Shyim 6 years ago
parent
commit
b5731a28d9
  1. 15
      public/theme/src/components/ReCast/Endpoints/EditEndpoint.vue
  2. 15
      public/theme/src/components/ReCast/Streams/EditStream.vue
  3. 1
      src/Controller/Events.php
  4. 37
      src/Controller/Streams.php
  5. 10
      src/Entity/Streams.php
  6. 38
      src/Migrations/Version20180425210736.php
  7. 30
      src/Migrations/Version20180425213959.php
  8. 4
      src/Repository/StreamsRepository.php

15
public/theme/src/components/ReCast/Endpoints/EditEndpoint.vue

@ -80,7 +80,20 @@ @@ -80,7 +80,20 @@
save: function () {
this.axios.post('/streams/' + this.$route.params.streamId + '/endpoints/update', this.endpoint).then(() => {
this.$router.push('/ucp/streams/' + this.$route.params.streamId + '/');
})
}).catch(error => {
const notification = {
template: `<span>${error.response.data.message}</span>`
};
this.$notify(
{
component: notification,
icon: 'fa fa-exclamation-triangle',
horizontalAlign: 'right',
verticalAlign: 'top',
type: 'danger'
});
});
}
},
computed: {

15
public/theme/src/components/ReCast/Streams/EditStream.vue

@ -83,7 +83,20 @@ @@ -83,7 +83,20 @@
save: function () {
this.axios.post('/streams/update', this.stream).then(() => {
this.$router.push('/ucp/streams/');
})
}).catch(error => {
const notification = {
template: `<span>${error.response.data.message}</span>`
};
this.$notify(
{
component: notification,
icon: 'fa fa-exclamation-triangle',
horizontalAlign: 'right',
verticalAlign: 'top',
type: 'danger'
});
});
},
deleteEndpoint: function (endpoint) {
this.endpoints.splice(this.endpoints.indexOf(endpoint), 1);

1
src/Controller/Events.php

@ -4,7 +4,6 @@ namespace App\Controller; @@ -4,7 +4,6 @@ namespace App\Controller;
use App\Entity\Streams;
use App\Repository\StreamsRepository;
use Doctrine\Common\Util\Debug;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

37
src/Controller/Streams.php

@ -3,13 +3,12 @@ @@ -3,13 +3,12 @@
namespace App\Controller;
use App\Component\Forms\Endpoint as EndpointForm;
use App\Component\Forms\Streams as StreamsForm;
use App\Component\ServiceManager;
use App\Entity\Endpoint;
use App\Entity\User;
use App\Repository\EndpointRepository;
use App\Repository\StreamsRepository;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
@ -96,14 +95,22 @@ class Streams extends Controller @@ -96,14 +95,22 @@ class Streams extends Controller
return new Response('Access denied', 401);
}
if (empty($requestBody['name']) || !$this->isValidString($requestBody['name'])) {
return new JsonResponse(['message' => 'Name is empty or contains illegal strings'], 500);
}
$stream->setUser($this->getUser());
$stream->setName($requestBody['name']);
$stream->setActive($requestBody['active']);
$manager = $this->get('doctrine.orm.entity_manager');
$manager->persist($stream);
$manager->flush();
try {
$manager->persist($stream);
$manager->flush();
} catch (UniqueConstraintViolationException $e) {
return new JsonResponse(['message' => sprintf('"%s" is already taken', $stream->getName())], 500);
}
return new JsonResponse($stream);
}
@ -205,6 +212,18 @@ class Streams extends Controller @@ -205,6 +212,18 @@ class Streams extends Controller
return new JsonResponse([]);
}
if (empty($requestBody['name'])) {
return new JsonResponse(['message' => 'Name is empty'], 500);
}
if (empty($requestBody['server']) || !$this->isValidString($requestBody['server'])) {
return new JsonResponse(['message' => 'Server is empty or contains illegal strings'], 500);
}
if (!empty($requestBody['streamKey']) && !$this->isValidString($requestBody['streamKey'])) {
return new JsonResponse(['message' => 'Stream-Key contains illegal strings'], 500);
}
$endpoint = !empty($requestBody['id']) ? $this->endpointRepository->find($requestBody['id']) : new Endpoint();
$endpoint->setName($requestBody['name']);
@ -243,4 +262,14 @@ class Streams extends Controller @@ -243,4 +262,14 @@ class Streams extends Controller
return new JsonResponse();
}
/**
* @param null|string $string
* @return false|int
* @author Soner Sayakci <shyim@posteo.de>
*/
private function isValidString(?string $string)
{
return preg_match('/^[a-z|A-Z|a-z|A-Z|0-9|.|\-|_|]+$/', $string);
}
}

10
src/Entity/Streams.php

@ -5,9 +5,17 @@ namespace App\Entity; @@ -5,9 +5,17 @@ namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\UniqueConstraint;
/**
* @ORM\Entity(repositoryClass="App\Repository\StreamsRepository")
* @ORM\Table(name="streams",
* uniqueConstraints={
* @UniqueConstraint(name="stream_name",
* columns={"name", "user_id"})
* }
* )
* @ORM\HasLifecycleCallbacks()
*/
class Streams implements \JsonSerializable
@ -21,7 +29,7 @@ class Streams implements \JsonSerializable @@ -21,7 +29,7 @@ class Streams implements \JsonSerializable
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=false)
* @ORM\Column(type="string", length=50, nullable=false)
* @var string
*/
private $name;

38
src/Migrations/Version20180425210736.php

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
<?php declare(strict_types = 1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20180425210736 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('CREATE TABLE streams (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, name VARCHAR(255) NOT NULL, active TINYINT(1) NOT NULL, live TINYINT(1) DEFAULT \'0\' NOT NULL, stream_key VARCHAR(255) NOT NULL, INDEX IDX_FFF7AFAA76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(100) NOT NULL, password VARCHAR(255) NOT NULL, email VARCHAR(100) NOT NULL, role VARCHAR(255) DEFAULT \'user\' NOT NULL, created_at DATETIME NOT NULL, UNIQUE INDEX UNIQ_8D93D649F85E0677 (username), UNIQUE INDEX UNIQ_8D93D649E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE endpoint (id INT AUTO_INCREMENT NOT NULL, stream_id INT DEFAULT NULL, active TINYINT(1) DEFAULT \'1\' NOT NULL, name VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, server VARCHAR(255) NOT NULL, streamKey VARCHAR(255) NOT NULL, INDEX IDX_C4420F7BD0ED463E (stream_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('CREATE TABLE queue (id INT AUTO_INCREMENT NOT NULL, task VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('ALTER TABLE streams ADD CONSTRAINT FK_FFF7AFAA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE endpoint ADD CONSTRAINT FK_C4420F7BD0ED463E FOREIGN KEY (stream_id) REFERENCES streams (id)');
}
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE endpoint DROP FOREIGN KEY FK_C4420F7BD0ED463E');
$this->addSql('ALTER TABLE streams DROP FOREIGN KEY FK_FFF7AFAA76ED395');
$this->addSql('DROP TABLE streams');
$this->addSql('DROP TABLE user');
$this->addSql('DROP TABLE endpoint');
$this->addSql('DROP TABLE queue');
}
}

30
src/Migrations/Version20180425213959.php

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
<?php declare(strict_types = 1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20180425213959 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE streams CHANGE name name VARCHAR(50) NOT NULL');
$this->addSql('CREATE UNIQUE INDEX stream_name ON streams (name, user_id)');
}
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('DROP INDEX stream_name ON streams');
$this->addSql('ALTER TABLE streams CHANGE name name VARCHAR(255) NOT NULL COLLATE utf8mb4_unicode_ci');
}
}

4
src/Repository/StreamsRepository.php

@ -7,7 +7,6 @@ use App\Entity\User; @@ -7,7 +7,6 @@ use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Bundle\MakerBundle\Str;
/**
* @method Streams|null find($id, $lockMode = null, $lockVersion = null)
@ -67,12 +66,9 @@ class StreamsRepository extends ServiceEntityRepository @@ -67,12 +66,9 @@ class StreamsRepository extends ServiceEntityRepository
public function getStreamByNameAndUsername(string $streamName, string $userName, string $streamKey): ?Streams
{
$qb = $this->createQueryBuilder('streams')
->addSelect('endpoints')
->addSelect('user')
->leftJoin('streams.endpoints', 'endpoints')
->innerJoin('streams.user', 'user')
->andWhere('streams.active = true')
->andWhere('endpoints.active = true')
->andWhere('streams.name = :streamName')
->andWhere('user.username = :userName')
->andWhere('streams.streamKey = :streamKey')

Loading…
Cancel
Save