Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
twin-elements committed Aug 17, 2021
1 parent ae2c413 commit e799841
Show file tree
Hide file tree
Showing 98 changed files with 6,360 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/.idea/
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
##Installation

in `/config/packages/routes.yaml` add
```
admin_dashboard:
path: /admin/
controller: TwinElements\AdminBundle\Controller\DashboardController::index
methods: GET
admin_core:
resource: "@TwinElementsAdminBundle/Controller/"
type: annotation
prefix: /admin
requirements:
_locale: '%app_locales%'
defaults:
_locale: '%locale%'
_admin_locale: '%admin_locale%'
options: { i18n: false }
```

add in `/config/packages/security.yaml `
```
imports:
- { resource: '@TwinElementsAdminBundle/Resources/config/security.yaml' }
```

#### How create a new Roles?

1.Create class implements `TwinElements\AdminBundle\Role\RoleGroupInterface`

2.Register class in services.yaml
```
<service id="YOUR_CLASS">
<tag name="twin_elements.role" priority="2-999"/>
</service>
```
3.Roles automatically implemented to roles list type
4.Add to messages.LANG.yaml translations as
```
role:
role_name: Role name
```

#### How create a new AdminMenu items
1.Create class implements `TwinElements\AdminBundle\Menu\AdminMenuInterface`

2.Register class in services.yaml
```
<service id="YOUR_CLASS">
<tag name="twin_elements.admin_menu"/>
</service>
```
3.Menu items automatically added in main admin menu

22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "twin-elements/admin-bundle",
"description": "Admin bundle for CMS",
"type": "symfony-bundle",
"license": "MIT",
"authors": [
{
"name": "Władysław Dudko",
"email": "[email protected]"
}
],
"require": {
"php": "^7.1.3",
"symfony/framework-bundle": "^4.4",
"twin-elements/form-extensions": "^0.2"
},
"autoload": {
"psr-4": {
"TwinElements\\AdminBundle\\": "src/"
}
}
}
124 changes: 124 additions & 0 deletions src/Command/CreateSuperAdminCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace TwinElements\AdminBundle\Command;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use TwinElements\AdminBundle\Entity\AdminUser;

class CreateSuperAdminCommand extends Command
{
protected static $defaultName = 'te:admin:create_super_admin';

/**
* @var EntityManagerInterface $em
*/
private $em;

/**
* @var UserPasswordEncoderInterface $encoder
*/
private $encoder;

/**
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em, UserPasswordEncoderInterface $encoder)
{
$this->em = $em;
$this->encoder = $encoder;

parent::__construct();
}

protected function configure()
{
$this
->setDescription('Create new Super Admin')
->setDefinition([
new InputArgument('username', InputArgument::REQUIRED, 'User name'),
new InputArgument('email', InputArgument::REQUIRED, 'User email'),
new InputArgument('password', InputArgument::REQUIRED, 'User password')
]);
}

protected function interact(InputInterface $input, OutputInterface $output)
{
$questions = [];

if (!$input->getArgument('username')) {
$question = new Question('Please choose a username:');
$question->setValidator(function ($username) {
if (empty($username)) {
throw new \Exception('Username can not be empty');
}

return $username;
});
$questions['username'] = $question;
}

if (!$input->getArgument('email')) {
$question = new Question('Please choose an email:');
$question->setValidator(function ($email) {
if (empty($email)) {
throw new \Exception('Email can not be empty');
}

return $email;
});
$questions['email'] = $question;
}

if (!$input->getArgument('password')) {
$question = new Question('Please choose a password:');
$question->setValidator(function ($password) {
if (empty($password)) {
throw new \Exception('Password can not be empty');
}

return $password;
});
$question->setHidden(true);
$questions['password'] = $question;
}

foreach ($questions as $name => $question) {
$answer = $this->getHelper('question')->ask($input, $output, $question);
$input->setArgument($name, $answer);
}
}

protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$username = $input->getArgument('username');
$email = $input->getArgument('email');
$password = $input->getArgument('password');

$user = new AdminUser();
$user->setEmail($email);
$user->setUsername($username);
$user->setEnabled(true);
$password = $this->encoder->encodePassword($user, $password);
$user->setPassword($password);
$user->setRoles(['ROLE_SUPER_ADMIN']);

$this->em->persist($user);
$this->em->flush();

$output->writeln(sprintf('Created user <comment>%s</comment>', $username));

return 0;

} catch (\Exception $exception) {
$output->writeln($exception->getMessage());
return 1;
}
}
}
153 changes: 153 additions & 0 deletions src/Controller/AdminUserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace TwinElements\AdminBundle\Controller;

use TwinElements\AdminBundle\Entity\AdminUser;
use TwinElements\AdminBundle\Form\AdminUserType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use TwinElements\AdminBundle\Model\CrudControllerTrait;
use TwinElements\AdminBundle\Role\AdminUserRole;
use TwinElements\AdminBundle\Service\AdminTranslator;

/**
* @Route("user")
*/
class AdminUserController extends AbstractController
{

use CrudControllerTrait;

/**
* @Route("/", name="user_index", methods={"GET"})
*/
public function indexAction(AdminTranslator $translator)
{
$em = $this->getDoctrine()->getManager();

$adminUsers = $em->getRepository(AdminUser::class)->findAll();

$this->breadcrumbs->setItems([
$translator->translate('admin.user.users') => null
]);

return $this->render('@TwinElementsAdmin/adminuser/index.html.twig', array(
'adminUsers' => $adminUsers,
));
}

/**
* @Route("/new", name="user_new", methods={"GET", "POST"})
*/
public function newAction(Request $request, UserPasswordEncoderInterface $passwordEncoder, AdminTranslator $translator)
{
$this->denyAccessUnlessGranted(AdminUserRole::ROLE_ADMIN);

$adminUser = new AdminUser();
$form = $this->createForm(AdminUserType::class, $adminUser);
$form->handleRequest($request);

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

$password = $passwordEncoder->encodePassword($adminUser, $adminUser->getPassword());
$adminUser->setPassword($password);

$em = $this->getDoctrine()->getManager();
$em->persist($adminUser);
$em->flush();
$this->flashes->successMessage($translator->translate('admin.user.user_has_been_added'));
return $this->redirectToRoute('user_index');
}

$this->breadcrumbs->setItems([
$translator->translate('admin.user.users') => $this->generateUrl('user_index'),
$translator->translate('admin.user.add_new_user') => null
]);

return $this->render('@TwinElementsAdmin/adminuser/new.html.twig', array(
'adminUser' => $adminUser,
'form' => $form->createView(),
));
}

/**
* Displays a form to edit an existing adminUser entity.
*
* @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"})
*
*/
public function editAction(Request $request, AdminUser $adminUser, UserPasswordEncoderInterface $passwordEncoder)
{
$this->denyAccessUnlessGranted(AdminUserRole::ROLE_ADMIN);

$oldPassword = $adminUser->getPassword();

$deleteForm = $this->createDeleteForm($adminUser);
$editForm = $this->createForm(AdminUserType::class, $adminUser);
$editForm->handleRequest($request);

if ($editForm->isSubmitted() && $editForm->isValid()) {
if (null === $editForm->getData()->getPassword()) {
$adminUser->setPassword($oldPassword);
} else {
$password = $passwordEncoder->encodePassword($adminUser, $adminUser->getPassword());
$adminUser->setPassword($password);
}

$this->getDoctrine()->getManager()->flush();
$this->flashes->successMessage();

return $this->redirectToRoute('user_edit', array('id' => $adminUser->getId()));
}

$this->breadcrumbs->setItems([
'cms.users' => $this->generateUrl('user_index'),
$adminUser->getUsername() => null
]);

return $this->render('@TwinElementsAdmin/adminuser/edit.html.twig', array(
'adminUser' => $adminUser,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));

}

/**
* @Route("/{id}", name="user_delete", methods={"DELETE"})
*/
public function deleteAction(Request $request, AdminUser $adminUser, AdminTranslator $translator)
{
$this->denyAccessUnlessGranted(AdminUserRole::ROLE_ADMIN);

$form = $this->createDeleteForm($adminUser);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($adminUser);
$em->flush();

$this->flashes->successMessage($translator->translate('admin.user.user_has_been_deleted'));

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

throw new \Exception('Form is not submitted');
}

/**
* @param AdminUser $adminUser The adminUser entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(AdminUser $adminUser)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('user_delete', array('id' => $adminUser->getId())))
->setMethod('DELETE')
->getForm();
}
}
13 changes: 13 additions & 0 deletions src/Controller/DashboardController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace TwinElements\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class DashboardController extends AbstractController
{
public function index()
{
return $this->render('@TwinElementsAdmin/dashboard.html.twig');
}
}
Loading

0 comments on commit e799841

Please sign in to comment.