Skip to content

Commit

Permalink
Update to Nette 2.0 (stable) & Doctrine 2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtak-CZ committed Feb 8, 2012
1 parent 83d6601 commit 7672583
Show file tree
Hide file tree
Showing 522 changed files with 27,349 additions and 10,567 deletions.
45 changes: 16 additions & 29 deletions app/bootstrap.php
Expand Up @@ -3,55 +3,42 @@
/**
* My Application bootstrap file.
*/
use Nette\Diagnostics\Debugger,
Nette\Application\Routers\Route;
use Nette\Application\Routers\Route;


// Load Nette Framework
require LIBS_DIR . '/Nette/loader.php';


// Enable Nette Debugger for error visualisation & logging
Debugger::$logDirectory = __DIR__ . '/../log';
Debugger::$strictMode = TRUE;
Debugger::enable();


// Configure application
$configurator = new Nette\Config\Configurator;
$configurator->setTempDirectory(__DIR__ . '/../temp');

// Enable Nette Debugger for error visualisation & logging
//$configurator->setProductionMode($configurator::AUTO);
$configurator->enableDebugger(__DIR__ . '/../log');

// Enable RobotLoader - this will load all classes automatically
$configurator->setTempDirectory(__DIR__ . '/../temp');
$configurator->createRobotLoader()
->addDirectory(APP_DIR)
->addDirectory(LIBS_DIR)
->register();

// Register extensions
Nella\Config\Extensions\DoctrineExtension::register($configurator);
Nella\Config\Extensions\DoctrineMigrationsExtension::register($configurator);

\Nella\Addons\Doctrine\Config\Extension::register($configurator);
\Nella\Addons\Doctrine\Config\MigrationsExtension::register($configurator);

// Create Dependency Injection container from config.neon file
$configurator->addConfig(__DIR__ . '/config/config.neon');
$container = $configurator->createContainer();

// Opens already started session
if ($container->session->exists()) {
$container->session->start();
}

// Setup router
$router = $container->router;
$router[] = new Nella\Application\Routers\CliRouter($container);
$router[] = new Route('index.php', 'Homepage:default', Route::ONE_WAY);
$router[] = new Route('<presenter>/<action>[/<id>]', 'Homepage:default');
$container->router[] = new Route('index.php', 'Homepage:default', Route::ONE_WAY);
$container->router[] = new Route('<presenter>/<action>[/<id>]', 'Homepage:default');


// Configure and run the application!
$application = $container->application;
if (PHP_SAPI === 'cli') {
$application->catchExceptions = FALSE;
$application->allowedMethods = FALSE;
if (PHP_SAPI == 'cli') {
$container->console->run();
} else {
$container->application->run();
}
//$application->catchExceptions = TRUE;
$application->errorPresenter = 'Error';
$application->run();
48 changes: 40 additions & 8 deletions app/config/config.neon
Expand Up @@ -7,25 +7,57 @@
common:
parameters:
database:
driver: pdo_sqlite
path: %appDir%/models/db.sqlite
driver: pdo_mysql
host: localhost
dbname: test
user:
password:
charset: utf8
collation: utf8_czech_ci


php:
date.timezone: Europe/Prague
# session.save_path: "%tempDir%/sessions"
session.save_path: "%tempDir%/sessions"
# zlib.output_compression: yes


nette:
session:
autoStart: smart

doctrine:
connections:
default: %database%

entityManagers:
default:
connection: default

console:
entityManager: default

migrations:
connection: @doctrine.connections.default


services:
authenticator:
class: Authenticator(@entityManager::getRepository('User'))
console: @doctrine.console

database: @doctrine.entityManagers.default

authenticator: Authenticator( @database::getRepository('User') )


factories:


production < common:


development < common:
services:
cacheStorage:
class: Nette\Caching\Storages\MemoryStorage
parameters:
database:
dbname: test
user: ace
password: ace
3 changes: 3 additions & 0 deletions app/migrations/.gitignore
@@ -0,0 +1,3 @@
*
!.*
!web.config
2 changes: 2 additions & 0 deletions app/migrations/.htaccess
@@ -0,0 +1,2 @@
Order Allow,Deny
Deny from all
16 changes: 16 additions & 0 deletions app/migrations/web.config
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Deny Rule 1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{URL}" pattern="*" />
</conditions>
<action type="CustomResponse" statusCode="403" statusReason="Forbidden: Access is denied." statusDescription="You do not have permission to view this directory or page using the credentials that you supplied." />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
13 changes: 8 additions & 5 deletions app/models/Authenticator.php
Expand Up @@ -12,13 +12,13 @@
class Authenticator extends Nette\Object implements NS\IAuthenticator
{
/** @var \Doctrine\ORM\EntityRepository */
private $repository;
private $users;



public function __construct(\Doctrine\ORM\EntityRepository $repository)
public function __construct(\Doctrine\ORM\EntityRepository $users)
{
$this->repository = $repository;
$this->users = $users;
}


Expand All @@ -32,7 +32,7 @@ public function __construct(\Doctrine\ORM\EntityRepository $repository)
public function authenticate(array $credentials)
{
list($username, $password) = $credentials;
$user = $this->repository->findOneByUsername($username);
$user = $this->users->findOneBy(array('username' => $username));

if (!$user) {
throw new NS\AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
Expand All @@ -42,7 +42,10 @@ public function authenticate(array $credentials)
throw new NS\AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
}

return new NS\Identity($user->id, array(), array('username' => $user->username));
return new NS\Identity($user->id, $user->role, array(
'username' => $user->username,
'email' => $user->email,
));
}


Expand Down
109 changes: 95 additions & 14 deletions app/models/User.php
@@ -1,51 +1,132 @@
<?php

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM:entity
* @ORM\Entity
*
* @property-read int $id
* @property-read string $username
* @property string $email
* @property string $password
* @property string $role
*/
class User extends \Nette\Object
{
/**
* @ORM:id
* @ORM:column(type="integer")
* @ORM:generatedValue
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @var int
*/
private $id;
/**
* @ORM:column
* @ORM\Column(unique=true)
* @var string
*/
private $username;
/**
* @ORM:column
* @ORM\Column
* @var string
*/
private $email;
/**
* @ORM\Column
* @var string
*/
private $password;

/**
* @ORM\Column
* @var string
*/
private $role;

/**
* @param string
* @return User
*/
public function __construct($username)
{
$this->username = $username;
$this->username = static::normalizeString($username);
}


/**
* @return int
*/
public function getId()
{
return $this->id;
}


/**
* @return string
*/
public function getUsername()
{
return $this->username;
}


/**
* @return string
*/
public function getPassword()
{
return $this->password;
}


/**
* @param string
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
$this->password = static::normalizeString($password);
return $this;
}
}

/**
* @return string
*/
public function getEmail()
{
return $this->email;
}

/**
* @param string
* @return User
*/
public function setEmail($email)
{
$this->email = static::normalizeString($email);
return $this;
}

/**
* @return string
*/
public function getRole()
{
return $this->role;
}

/**
* @param string
* @return User
*/
public function setRole($role)
{
$this->role = static::normalizeString($role);
return $this;
}

/**
* @param string
* @return string
*/
protected static function normalizeString($s)
{
$s = trim($s);
return $s === "" ? NULL : $s;
}
}
Binary file removed app/models/db.sqlite
Binary file not shown.
6 changes: 4 additions & 2 deletions app/presenters/ErrorPresenter.php
Expand Up @@ -26,8 +26,10 @@ public function renderDefault($exception)

} elseif ($exception instanceof NA\BadRequestException) {
$code = $exception->getCode();
$this->setView(in_array($code, array(403, 404, 405, 410, 500)) ? $code : '4xx'); // load template 403.latte or 404.latte or ... 4xx.latte
Debugger::log("HTTP code $code", 'access'); // log to access.log
// load template 403.latte or 404.latte or ... 4xx.latte
$this->setView(in_array($code, array(403, 404, 405, 410, 500)) ? $code : '4xx');
// log to access.log
Debugger::log("HTTP code $code: {$exception->getMessage()} in {$exception->getFile()}:{$exception->getLine()}", 'access');

} else {
$this->setView('500'); // load template 500.latte
Expand Down
20 changes: 20 additions & 0 deletions app/presenters/HomepagePresenter.php
Expand Up @@ -8,6 +8,26 @@
*/
class HomepagePresenter extends BasePresenter
{
public function actionCreateDefaultUser()
{
$em = $this->getContext()->database;

$user = new User('admin');
$user->setPassword($this->getContext()->authenticator->calculateHash('traktor'));
$user->setEmail('info@nella-project.org')->setRole('admin');

$em->persist($user);
try {
$em->flush();
} catch(\PDOException $e) {
dump($e);
$this->terminate();
}

$this->sendResponse(new \Nette\Application\Responses\TextResponse('OK'));
$this->terminate();
}


public function renderDefault()
{
Expand Down
3 changes: 3 additions & 0 deletions app/proxies/.gitignore
@@ -0,0 +1,3 @@
*
!.*
!web.config

0 comments on commit 7672583

Please sign in to comment.