Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ security:
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/task/*, roles: ROLE_USER }
- { path: ^/project/*, roles: ROLE_USER }

# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
124 changes: 124 additions & 0 deletions src/Controller/ProjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace App\Controller;

use App\Entity\Project;
use App\Type\ProjectType;
use App\Entity\Task;
use App\Type\TaskType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Type\TaskFilterType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

use Symfony\Component\Security\Core\Security;

class ProjectController extends AbstractController
{
/**
* @var Security
*/
protected $security;

public function __construct(Security $security)
{
$this->security = $security;
}

/**
* @Route("/project/list", name="project_list")
*/
public function showPorjects(Request $request): Response
{
$user = $this->getUser();

if($this->isGranted('ROLE_ADMIN'))
{
/** @var $projects */
$projects = $this->getDoctrine()->getManager()
->getRepository(Project::class)
->findBy([], []);
}
else
{
/** @var $projects */
$projects = $this->getDoctrine()->getManager()
->getRepository(Project::class)
->findBy(['author' => $user->getId()]);
}



return $this->render('project/index.html.twig', [
'projects' => $projects,
]);
}

/**
* @Route("/project/create", name="project_create")
*/
public function createProject(Request $request): Response
{
$project = new Project();
$form = $this->createForm(ProjectType::class, $project);

$form->handleRequest($request);
$user = $this->getUser();


if ($form->isSubmitted() && $form->isValid()) {

$project->setAuthor($user->getId());

$this->getDoctrine()->getManager()->persist($project);
$this->getDoctrine()->getManager()->flush();

return $this->redirectToRoute('project_list');
}

return $this->render('project/createForm.html.twig', [
'form' => $form->createView(),
]);
}


/**
* @Route("/project/{id}/", name="project_byId")
*/
public function projectById($id): Response
{
/** @var Task $project */
$project = $this->getDoctrine()->getManager()->find(Project::class, $id);

if ($project === null) {
throw $this->createNotFoundException(sprintf("Project with id %s not found", $id));
}
$Flag=False;
$user = $this->getUser();
foreach ($user->getRoles() as $role)
{
if($role === 'ROLE_ADMIN')
{
$Flag=True;
}
}

if(!$Flag)
{
if($user->getId() !== $project->getAuthor())
{
throw $this->createAccessDeniedException();
}
}

$tasks = $this->getDoctrine()->getRepository(Task::class)
->findBy(['project' => $id], []);

return $this->render('project/project.html.twig',[
'id' => $id,
'tasks' => $tasks,
]);
}
}
69 changes: 59 additions & 10 deletions src/Controller/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Controller;

use App\Entity\Project;
use App\Entity\Task;
use App\Type\TaskFilterType;
use App\Type\TaskType;
Expand All @@ -22,7 +23,11 @@ class TaskController extends AbstractController
public function create(Request $request): Response
{
$task = new Task();
$form = $this->createForm(TaskType::class, $task);
$option = [
'userId' => $this->getUser()->getId(),
'userRole' => $this->getUser()->getRoles(),
];
$form = $this->createForm(TaskType::class, $task, $option);

$form->handleRequest($request);

Expand All @@ -48,6 +53,20 @@ public function create(Request $request): Response
*/
public function list(Request $request): Response
{
$Flag=False;
$user = $this->getUser();
foreach ($user->getRoles() as $role)
{
if($role === 'ROLE_ADMIN')
{
$Flag=True;
}
}
if(!$Flag)
{
$ids = $this->getProjectID($user->getId());
}

$taskFilterForm = $this->createForm(TaskFilterType::class);

$taskFilterForm->handleRequest($request);
Expand All @@ -58,23 +77,36 @@ public function list(Request $request): Response
if ($filter['isCompleted'] === null) {
unset($filter['isCompleted']);
}

if(!$Flag)
{
$filter['project'] = $ids;
}
$tasks = $this->getDoctrine()->getRepository(Task::class)
->findBy($filter, [
'dueDate' => 'DESC'
]);

} else {
/** @var $tasks */
$tasks = $this->getDoctrine()->getManager()
->getRepository(Task::class)
->findBy([], [
'dueDate' => 'DESC'
]);
if($Flag)
{
/** @var $tasks */
$tasks = $this->getDoctrine()->getManager()
->getRepository(Task::class)
->findBy([], [
'dueDate' => 'DESC'
]);
}
else
{
/** @var $tasks */
$tasks = $this->getDoctrine()->getManager()
->getRepository(Task::class)
->findBy(['project' => $ids], [
'dueDate' => 'DESC'
]);
}
}



return $this->render('task/list.html.twig', [
'tasks' => $tasks,
'filterForm' => $taskFilterForm->createView()
Expand Down Expand Up @@ -104,4 +136,21 @@ public function complete($id): Response

return $this->redirectToRoute('task_list');
}

private function getProjectID(int $id)
{
$returnId = [];

/** @var $projects */
$projects = $this->getDoctrine()->getManager()
->getRepository(Project::class)
->findBy(['author' => $id]);

foreach ($projects as $project)
{
array_push($returnId, $project->getId());
}

return $returnId;
}
}
75 changes: 75 additions & 0 deletions src/Entity/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Entity;

use App\Repository\ProjectRepository;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass=ProjectRepository::class)
*/
class Project
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=6)
*/
private $token;

/**
* @ORM\Column(type="string", length=255)
*/
private $name;

/**
* @ORM\Column(type="integer")
*/
private $author;

public function getId(): ?int
{
return $this->id;
}

public function getToken(): ?string
{
return $this->token;
}

public function setToken(string $token): self
{
$this->token = $token;

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getAuthor(): ?int
{
return $this->author;
}

public function setAuthor(int $author): self
{
$this->author = $author;

return $this;
}
}
24 changes: 24 additions & 0 deletions src/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class Task
*/
protected $author;

/**
* @ORM\ManyToOne(targetEntity="App\Entity\Project", inversedBy="project")
* @var Project
*/
protected $project;

/**
* Create empty task
*/
Expand Down Expand Up @@ -85,6 +91,24 @@ public function getAuthor()
return $this->author;
}

/**
* Set task Project
* @param Project|null $project
* @return void
*/
public function setProject(Project $project = null)
{
$this->project = $project;
}

/**
* Return task project
* @return Project|null
*/
public function getProject()
{
return $this->project;
}

/**
* @return mixed
Expand Down
Loading