This commit is contained in:
2022-04-11 16:05:12 +02:00
parent abaa95f71d
commit 6309faa898
37 changed files with 1263 additions and 7 deletions

@ -0,0 +1,28 @@
<?php
namespace App\Controller\Admin;
use App\Entity\Projects;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
/**
*
*/
class ProjectsCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Projects::class;
}
/*
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id'),
TextField::new('title'),
TextEditorField::new('description'),
];
}
*/
}

@ -0,0 +1,28 @@
<?php
namespace App\Controller\Admin;
use App\Entity\Quotes;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
/**
*
*/
class QuotesCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Quotes::class;
}
public function configureFields(string $pageName): iterable
{
return [
IdField::new(propertyName: 'id'),
TextField::new(propertyName: 'quote'),
];
}
}

@ -0,0 +1,37 @@
<?php
namespace App\Controller;
use App\Repository\ProjectsRepository;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
*
*/
class ProjectsController extends AbstractController
{
#[Route(path: '/projects/{name}', name: 'app_projects')]
public function index(ProjectsRepository $projectsRepository, string $name = ''): Response
{
if ($name == '') {
return $this->render(view: 'projects/index.html.twig', parameters: [
'projects' => $projectsRepository->findAll()
]);
} else {
if ($project = $projectsRepository->findOneByName(value: $name)) {
$readMe = file_get_contents(filename: $project->getURL() . '/raw/branch/master/README.md');
//$parsedReadMe = $markdownParser->transformMarkdown(text: $readMe);
return $this->render(view: 'projects/show.html.twig', parameters: [
'project' => $project,
'readme' => $readMe
]);
} else {
throw $this->createNotFoundException();
}
}
}
}

@ -2,17 +2,74 @@
namespace App\Controller;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
/**
* Class UserController
* @package App\Controller
*/
class UserController extends AbstractController
{
#[Route('/user', name: 'user')]
public function index(): Response
{
return $this->render('user/index.html.twig', [
'controller_name' => 'UserController',
]);
}
/**
* @param \App\Repository\UserRepository $userRepository
* @param string $userName
*
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route(path: '/profile/edit/{username}', name: 'app_profile_edit')]
public function editProfile(UserRepository $userRepository, string $username = ''): Response
{
/** var User $user */
if ($username === '') {
if ($this->isGranted(attribute: 'ROLE_USER')) {
$user = $this->getUser();
} else {
throw new AccessDeniedException('You need to be logged in.');
}
} else {
if ($this->isGranted(attribute: 'ROLE_ADMIN')) {
$user = $userRepository->findOneBy([
"username" => $username
]);
}
}
if (isset($user)) {
return $this->render(view: 'user/edit_profile.html.twig', parameters: [
'user' => $user,
]);
} else {
throw new UserNotFoundException();
}
}
/**
* @param \App\Repository\UserRepository $userRepository
* @param string $username
*
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route(path: '/profile/{username}', name: 'app_profile')]
public function showProfile(UserRepository $userRepository, string $username = ''): Response
{
/** var User $user */
if ($username === '') {
$user = $this->getUser();
} else {
$user = $userRepository->findOneBy([
"username" => $username
]);
}
return $this->render(view: 'user/show_profile.html.twig', parameters: [
'user' => $user,
]);
}
}

140
src/Entity/Projects.php Normal file

@ -0,0 +1,140 @@
<?php
namespace App\Entity;
use App\Repository\ProjectsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
*
*/
#[ORM\Entity(repositoryClass: ProjectsRepository::class)]
class Projects
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id;
#[ORM\Column(type: 'string', length: 255)]
private ?string $name;
#[ORM\Column(type: 'string', length: 255)]
private ?string $description;
#[ORM\Column(type: 'string', length: 255)]
private ?string $url;
#[ORM\Column(type: 'datetime_immutable')]
private ?\DateTimeImmutable $createdAt;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $teaserImage;
#[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'projects')]
private $developer;
public function __construct()
{
$this->developer = new ArrayCollection();
}
/**
* @return null|string
*/
public function __toString()
{
return $this->name;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $Name): self
{
$this->name = $Name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $Description): self
{
$this->description = $Description;
return $this;
}
public function getURL(): ?string
{
return $this->url;
}
public function setURL(string $url): self
{
$this->url = $url;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getTeaserImage(): ?string
{
return $this->teaserImage;
}
public function setTeaserImage(?string $teaserImage): self
{
$this->teaserImage = $teaserImage;
return $this;
}
/**
* @return Collection<int, User>
*/
public function getDeveloper(): Collection
{
return $this->developer;
}
public function addDeveloper(User $developer): self
{
if (!$this->developer->contains($developer)) {
$this->developer[] = $developer;
}
return $this;
}
public function removeDeveloper(User $developer): self
{
$this->developer->removeElement($developer);
return $this;
}
}

35
src/Entity/Quotes.php Normal file

@ -0,0 +1,35 @@
<?php
namespace App\Entity;
use App\Repository\QuotesRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: QuotesRepository::class)]
class Quotes
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'text')]
private $quote;
public function getId(): ?int
{
return $this->id;
}
public function getQuote(): ?string
{
return $this->quote;
}
public function setQuote(string $quote): self
{
$this->quote = $quote;
return $this;
}
}

@ -0,0 +1,73 @@
<?php
namespace App\Repository;
use App\Entity\Projects;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Projects|null find($id, $lockMode = null, $lockVersion = null)
* @method Projects|null findOneBy(array $criteria, array $orderBy = null)
* @method Projects[] findAll()
* @method Projects[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ProjectsRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct(registry: $registry, entityClass: Projects::class);
}
/**
*/
public function add(Projects $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
*/
public function remove(Projects $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
// /**
// * @return Projects[] Returns an array of Projects objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->orderBy('p.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
public function findOneByName($value): ?Projects
{
try {
return $this->createQueryBuilder(alias: 'q')
->andWhere('q.name = :val')
->setParameter(key: 'val', value: $value)
->getQuery()
->getOneOrNullResult();
} catch(NonUniqueResultException $e) {
dd($e->getMessage());
}
}
}

@ -0,0 +1,94 @@
<?php
namespace App\Repository;
use App\Entity\Quotes;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Quotes|null find($id, $lockMode = null, $lockVersion = null)
* @method Quotes|null findOneBy(array $criteria, array $orderBy = null)
* @method Quotes[] findAll()
* @method Quotes[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class QuotesRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct(registry: $registry, entityClass: Quotes::class);
}
/**
*/
public function add(Quotes $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
*/
public function remove(Quotes $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @return null|float|int|mixed|string
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneRandom(): mixed
{
$idLimits = $this->createQueryBuilder(alias: 'q')
->select('MIN(q.id)', 'MAX(q.id)')
->getQuery()
->getOneOrNullResult();
$randomPossibleId = rand(min: $idLimits[1], max: $idLimits[2]);
return $this->createQueryBuilder(alias: 'q')
->where(predicates: 'q.id >= :random_id')
->setParameter(key: 'random_id', value: $randomPossibleId)
->setMaxResults(maxResults: 1)
->getQuery()
->getOneOrNullResult();
}
// /**
// * @return Quotes[] Returns an array of Quotes objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('q')
->andWhere('q.exampleField = :val')
->setParameter('val', $value)
->orderBy('q.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Quotes
{
return $this->createQueryBuilder('q')
->andWhere('q.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

@ -0,0 +1,97 @@
<?php
namespace App\Security;
use App\Entity\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use App\Repository\UserRepository;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use function mysql_xdevapi\getSession;
/**
*
*/
class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
{
use TargetPathTrait;
/**
* @var \App\Repository\UserRepository
*/
private UserRepository $userRepository;
/**
* @var \Symfony\Component\Routing\RouterInterface
*/
private RouterInterface $router;
public function __construct(UserRepository $userRepository, RouterInterface $router)
{
$this->userRepository = $userRepository;
$this->router = $router;
}
public function authenticate(Request $request): Passport
{
$username = $request->request->get(key: 'username');
$password = $request->request->get(key: 'password');
$csrfToken = $request->request->get(key: '_csrf_token');
$request->getSession()->set(name: Security::LAST_USERNAME, value: $username);
return new Passport(
userBadge: new UserBadge(userIdentifier: $username, userLoader: function ($userIdentifier) {
$user = $this->userRepository->findOneBy(['username' => $userIdentifier]);
if (!$user) {
$user = $this->userRepository->findOneBy(['email' => $userIdentifier]);
}
if (!$user) {
throw new UserNotFoundException();
}
return $user;
}),
credentials: new CustomCredentials(customCredentialsChecker: function ($credentials, User $user) {
return $credentials === 'test';
}, credentials : $password),
// new PasswordCredentials($password),
badges: [
new CsrfTokenBadge(csrfTokenId: 'authenticate', csrfToken: $csrfToken),
new RememberMeBadge()
]
);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
if ($target = $this->getTargetPath(session: $request->getSession(), firewallName: $firewallName)) {
return new RedirectResponse(url: $target);
}
return new RedirectResponse(
url: $this->router->generate(name: 'app_main')
);
}
protected function getLoginUrl(Request $request): string
{
return $this->router->generate(name: 'app_login');
}
}