Permalink
Browse files

Init from Nette Sandbox

  • Loading branch information...
chemix committed Jun 12, 2014
0 parents commit dafae017f01730c79da1ede8b2ba6c295ac79f61
Showing with 2,654 additions and 0 deletions.
  1. +12 −0 .gitignore
  2. +2 −0 app/.htaccess
  3. +22 −0 app/bootstrap.php
  4. +27 −0 app/config/config.neon
  5. +76 −0 app/model/UserManager.php
  6. +15 −0 app/presenters/BasePresenter.php
  7. +40 −0 app/presenters/ErrorPresenter.php
  8. +20 −0 app/presenters/HomepagePresenter.php
  9. +64 −0 app/presenters/SignPresenter.php
  10. +27 −0 app/router/RouterFactory.php
  11. +32 −0 app/templates/@layout.latte
  12. +7 −0 app/templates/Error/403.latte
  13. +8 −0 app/templates/Error/404.latte
  14. +6 −0 app/templates/Error/405.latte
  15. +6 −0 app/templates/Error/410.latte
  16. +4 −0 app/templates/Error/4xx.latte
  17. +20 −0 app/templates/Error/500.latte
  18. +135 −0 app/templates/Homepage/default.latte
  19. +6 −0 app/templates/Sign/in.latte
  20. +12 −0 app/templates/components/form.latte
  21. +16 −0 app/web.config
  22. +17 −0 bin/create-user.php
  23. +26 −0 composer.json
  24. +1,176 −0 composer.lock
  25. +49 −0 license.md
  26. +2 −0 log/.htaccess
  27. +16 −0 log/web.config
  28. +41 −0 readme.md
  29. +2 −0 temp/.htaccess
  30. +16 −0 temp/web.config
  31. +37 −0 tests/ExampleTest.phpt
  32. +21 −0 tests/bootstrap.php
  33. +2 −0 vendor/.htaccess
  34. 0 vendor/others/.gitignore
  35. +16 −0 vendor/web.config
  36. +25 −0 www/.htaccess
  37. +26 −0 www/.maintenance.php
  38. +4 −0 www/adminer/.htaccess
  39. 0 www/adminer/adminer.css
  40. +44 −0 www/adminer/index.php
  41. +12 −0 www/css/print.css
  42. +54 −0 www/css/screen.css
  43. BIN www/favicon.ico
  44. BIN www/images/spinner.gif
  45. +8 −0 www/index.php
  46. +6 −0 www/js/jquery.js
  47. +3 −0 www/js/main.js
  48. +474 −0 www/js/netteForms.js
  49. 0 www/robots.txt
  50. +20 −0 www/web.config
@@ -0,0 +1,12 @@
log/*
temp/*
vendor/*
!vendor/others
.DS_Store
!.gitignore
!.htaccess
!web.config
app/config/config.local.neon
.idea/*
@@ -0,0 +1,2 @@
Order Allow,Deny
Deny from all
@@ -0,0 +1,22 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
$configurator = new Nette\Configurator;
//$configurator->setDebugMode(TRUE); // debug mode MUST NOT be enabled on production server
$configurator->enableDebugger(__DIR__ . '/../log');
$configurator->setTempDirectory(__DIR__ . '/../temp');
$configurator->createRobotLoader()
->addDirectory(__DIR__)
->addDirectory(__DIR__ . '/../vendor/others')
->register();
$configurator->addConfig(__DIR__ . '/config/config.neon');
$configurator->addConfig(__DIR__ . '/config/config.local.neon');
$container = $configurator->createContainer();
return $container;
@@ -0,0 +1,27 @@
#
# SECURITY WARNING: it is CRITICAL that this file & directory are NOT accessible directly via a web browser!
#
# If you don't protect this directory from direct web access, anybody will be able to see your passwords.
# http://nette.org/security-warning
#
parameters:
php:
date.timezone: Europe/Prague
nette:
application:
errorPresenter: Error
mapping:
*: App\*Module\Presenters\*Presenter
session:
expiration: 14 days
services:
- App\Model\UserManager
- App\RouterFactory
router: @App\RouterFactory::createRouter
@@ -0,0 +1,76 @@
<?php
namespace App\Model;
use Nette,
Nette\Utils\Strings,
Nette\Security\Passwords;
/**
* Users management.
*/
class UserManager extends Nette\Object implements Nette\Security\IAuthenticator
{
const
TABLE_NAME = 'users',
COLUMN_ID = 'id',
COLUMN_NAME = 'username',
COLUMN_PASSWORD_HASH = 'password',
COLUMN_ROLE = 'role';
/** @var Nette\Database\Context */
private $database;
public function __construct(Nette\Database\Context $database)
{
$this->database = $database;
}
/**
* Performs an authentication.
* @return Nette\Security\Identity
* @throws Nette\Security\AuthenticationException
*/
public function authenticate(array $credentials)
{
list($username, $password) = $credentials;
$row = $this->database->table(self::TABLE_NAME)->where(self::COLUMN_NAME, $username)->fetch();
if (!$row) {
throw new Nette\Security\AuthenticationException('The username is incorrect.', self::IDENTITY_NOT_FOUND);
} elseif (!Passwords::verify($password, $row[self::COLUMN_PASSWORD_HASH])) {
throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
} elseif (Passwords::needsRehash($row[self::COLUMN_PASSWORD_HASH])) {
$row->update(array(
self::COLUMN_PASSWORD_HASH => Passwords::hash($password),
));
}
$arr = $row->toArray();
unset($arr[self::COLUMN_PASSWORD_HASH]);
return new Nette\Security\Identity($row[self::COLUMN_ID], $row[self::COLUMN_ROLE], $arr);
}
/**
* Adds new user.
* @param string
* @param string
* @return void
*/
public function add($username, $password)
{
$this->database->table(self::TABLE_NAME)->insert(array(
self::COLUMN_NAME => $username,
self::COLUMN_PASSWORD_HASH => Passwords::hash($password),
));
}
}
@@ -0,0 +1,15 @@
<?php
namespace App\Presenters;
use Nette,
App\Model;
/**
* Base presenter for all application presenters.
*/
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
}
@@ -0,0 +1,40 @@
<?php
namespace App\Presenters;
use Nette,
App\Model,
Nette\Diagnostics\Debugger;
/**
* Error presenter.
*/
class ErrorPresenter extends BasePresenter
{
/**
* @param Exception
* @return void
*/
public function renderDefault($exception)
{
if ($exception instanceof Nette\Application\BadRequestException) {
$code = $exception->getCode();
// 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
Debugger::log($exception, Debugger::ERROR); // and log exception
}
if ($this->isAjax()) { // AJAX request? Note this error in payload.
$this->payload->error = TRUE;
$this->terminate();
}
}
}
@@ -0,0 +1,20 @@
<?php
namespace App\Presenters;
use Nette,
App\Model;
/**
* Homepage presenter.
*/
class HomepagePresenter extends BasePresenter
{
public function renderDefault()
{
$this->template->anyVariable = 'any value';
}
}
@@ -0,0 +1,64 @@
<?php
namespace App\Presenters;
use Nette,
App\Model;
/**
* Sign in/out presenters.
*/
class SignPresenter extends BasePresenter
{
/**
* Sign-in form factory.
* @return Nette\Application\UI\Form
*/
protected function createComponentSignInForm()
{
$form = new Nette\Application\UI\Form;
$form->addText('username', 'Username:')
->setRequired('Please enter your username.');
$form->addPassword('password', 'Password:')
->setRequired('Please enter your password.');
$form->addCheckbox('remember', 'Keep me signed in');
$form->addSubmit('send', 'Sign in');
// call method signInFormSucceeded() on success
$form->onSuccess[] = $this->signInFormSucceeded;
return $form;
}
public function signInFormSucceeded($form, $values)
{
if ($values->remember) {
$this->getUser()->setExpiration('14 days', FALSE);
} else {
$this->getUser()->setExpiration('20 minutes', TRUE);
}
try {
$this->getUser()->login($values->username, $values->password);
$this->redirect('Homepage:');
} catch (Nette\Security\AuthenticationException $e) {
$form->addError($e->getMessage());
}
}
public function actionOut()
{
$this->getUser()->logout();
$this->flashMessage('You have been signed out.');
$this->redirect('in');
}
}
@@ -0,0 +1,27 @@
<?php
namespace App;
use Nette,
Nette\Application\Routers\RouteList,
Nette\Application\Routers\Route,
Nette\Application\Routers\SimpleRouter;
/**
* Router factory.
*/
class RouterFactory
{
/**
* @return \Nette\Application\IRouter
*/
public function createRouter()
{
$router = new RouteList();
$router[] = new Route('<presenter>/<action>[/<id>]', 'Homepage:default');
return $router;
}
}
@@ -0,0 +1,32 @@
{**
* @param string $basePath web base path
* @param array $flashes flash messages
*}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{ifset #title}{include title|striptags} | {/ifset}Nette Sandbox</title>
<link rel="stylesheet" media="screen,projection,tv" href="{$basePath}/css/screen.css">
<link rel="stylesheet" media="print" href="{$basePath}/css/print.css">
<link rel="shortcut icon" href="{$basePath}/favicon.ico">
{block head}{/block}
</head>
<body>
<script> document.documentElement.className+=' js' </script>
<div n:foreach="$flashes as $flash" class="flash {$flash->type}">{$flash->message}</div>
{include content}
{block scripts}
<script src="{$basePath}/js/jquery.js"></script>
<script src="{$basePath}/js/netteForms.js"></script>
<script src="{$basePath}/js/main.js"></script>
{/block}
</body>
</html>
@@ -0,0 +1,7 @@
{block content}
<h1 n:block=title>Access Denied</h1>
<p>You do not have permission to view this page. Please try contact the web
site administrator if you believe you should be able to view this page.</p>
<p><small>error 403</small></p>
@@ -0,0 +1,8 @@
{block content}
<h1 n:block=title>Page Not Found</h1>
<p>The page you requested could not be found. It is possible that the address is
incorrect, or that the page no longer exists. Please use a search engine to find
what you are looking for.</p>
<p><small>error 404</small></p>
@@ -0,0 +1,6 @@
{block content}
<h1 n:block=title>Method Not Allowed</h1>
<p>The requested method is not allowed for the URL.</p>
<p><small>error 405</small></p>
@@ -0,0 +1,6 @@
{block content}
<h1 n:block=title>Page Not Found</h1>
<p>The page you requested has been taken off the site. We apologize for the inconvenience.</p>
<p><small>error 410</small></p>
@@ -0,0 +1,4 @@
{block content}
<h1 n:block=title>Oops...</h1>
<p>Your browser sent a request that this server could not understand or process.</p>
Oops, something went wrong.

0 comments on commit dafae01

Please sign in to comment.