This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added examples
- Loading branch information
Showing
69 changed files
with
2,514 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Deny from all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
|
||
/** | ||
* Load Nette | ||
*/ | ||
require_once dirname(__FILE__) . '/../../../Nette/loader.php'; | ||
|
||
|
||
/** | ||
* Configure application | ||
*/ | ||
Environment::loadConfig(); | ||
|
||
|
||
/** | ||
* Prepare & setup | ||
*/ | ||
Debug::enable(E_ALL | E_STRICT); | ||
|
||
require_once 'dibi.compact.php'; | ||
dibi::connect(Environment::getConfig('database')); | ||
|
||
|
||
$application = Environment::getApplication(); | ||
$router = $application->getRouter(); | ||
$router[] = new Route('index.php', array( | ||
'presenter' => 'default', | ||
), Route::ONE_WAY); | ||
|
||
$router[] = new Route('<presenter>/<view>/<id>', array( | ||
'presenter' => 'default', | ||
'view' => 'default', | ||
'id' => NULL, | ||
)); | ||
|
||
|
||
|
||
/** | ||
* Run! | ||
*/ | ||
$application->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[common] | ||
set> date.timezone = "Europe/Prague" | ||
set> iconv.internal_encoding = "%encoding%" | ||
set> mbstring.internal_encoding = "%encoding%" | ||
set> include_path = "%appDir%/../libs;%appDir%;" | ||
|
||
[production < common] | ||
database> driver = sqlite | ||
database> file = "%modelsDir%/demo.db" | ||
database> lazy = TRUE | ||
database> result:objects = TRUE | ||
|
||
service> Nette::Security::IAuthenticator = Users | ||
|
||
[development < production] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
|
||
|
||
class Albums extends DibiTable | ||
{ | ||
|
||
protected $blankRow = array( | ||
'artist' => '', | ||
'title' => '', | ||
); | ||
|
||
/* CREATE TABLE [albums] ( | ||
[id] INTEGER NOT NULL PRIMARY KEY, | ||
[artist] VARCHAR(100) NOT NULL, | ||
[title] VARCHAR(100) NOT NULL | ||
) */ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
|
||
|
||
class Users extends DibiTable implements /*Nette::Security::*/IAuthenticator | ||
{ | ||
|
||
/* CREATE TABLE [users] ( | ||
[id] INTEGER NULL PRIMARY KEY, | ||
[username] VARCHAR(50) UNIQUE NOT NULL, | ||
[password] VARCHAR(50) NOT NULL, | ||
[real_name] VARCHAR(100) NOT NULL | ||
) */ | ||
|
||
|
||
/** | ||
* Performs an authentication | ||
* @param array | ||
* @return void | ||
* @throws AuthenticationException | ||
*/ | ||
public function authenticate(array $credentials) | ||
{ | ||
$row = $this->fetch(array('username' => $credentials['username'])); | ||
if (!$row) { | ||
throw new AuthenticationException('', AuthenticationException::IDENTITY_NOT_FOUND); | ||
} | ||
|
||
if ($row->password !== $credentials['password']) { | ||
throw new AuthenticationException('', AuthenticationException::INVALID_CREDENTIAL); | ||
} | ||
|
||
unset($row->password); | ||
return new Identity($row->username, array(), $row); | ||
} | ||
|
||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
require_once dirname(__FILE__) . '/BasePresenter.php'; | ||
|
||
|
||
class AuthPresenter extends BasePresenter | ||
{ | ||
/** @persistent */ | ||
public $backlink = ''; | ||
|
||
|
||
protected function startup() | ||
{ | ||
require_once 'models/Users.php'; | ||
|
||
parent::startup(); | ||
} | ||
|
||
|
||
|
||
/********************* view Default *********************/ | ||
|
||
|
||
|
||
public function prepareDefault() | ||
{ | ||
$this->forward('login'); | ||
} | ||
|
||
|
||
|
||
/********************* view Login *********************/ | ||
|
||
|
||
|
||
public function renderLogin($backlink) | ||
{ | ||
$this->backlink = $backlink; | ||
$this->template->title = "Log in"; | ||
} | ||
|
||
|
||
|
||
public function handleLogin() | ||
{ | ||
$request = $this->request; | ||
if (!$request->isPost()) return; | ||
|
||
// collect the data from the user | ||
$username = trim($request->post['username']); | ||
$password = trim($request->post['password']); | ||
|
||
if (empty($username)) { | ||
$this->template->message = 'Please provide a username.'; | ||
} else { | ||
try { | ||
$user = Environment::getUser(); | ||
$user->authenticate($username, $password); | ||
$this->redirect($this->backlink); | ||
|
||
} catch (AuthenticationException $e) { | ||
$this->template->message = 'Login failed.'; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
|
||
abstract class BasePresenter extends Presenter | ||
{ | ||
|
||
protected function startup() | ||
{ | ||
$this->template->registerFilter('TemplateFilters::curlyBrackets'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
|
||
require_once dirname(__FILE__) . '/BasePresenter.php'; | ||
|
||
|
||
class DefaultPresenter extends BasePresenter | ||
{ | ||
|
||
protected function startup() | ||
{ | ||
require_once 'models/Albums.php'; | ||
|
||
// user authentication | ||
$user = Environment::getUser(); | ||
if (!$user->isAuthenticated()) { | ||
$this->redirect('auth:login', $this->backlink()); | ||
} | ||
|
||
$this->template->user = $user->getIdentity(); | ||
parent::startup(); | ||
} | ||
|
||
|
||
|
||
/********************* view Default *********************/ | ||
|
||
|
||
|
||
public function renderDefault() | ||
{ | ||
$this->template->title = "My Albums"; | ||
$album = new Albums(); | ||
$this->template->albums = $album->findAll('artist', 'title'); | ||
} | ||
|
||
|
||
|
||
/********************* view Add *********************/ | ||
|
||
|
||
|
||
public function renderAdd() | ||
{ | ||
$this->template->title = "Add New Album"; | ||
|
||
// set up an "empty" album | ||
$album = new Albums(); | ||
$this->template->album = $album->createBlank(); | ||
|
||
// additional view fields required by form | ||
$this->template->action = $this->link('add!'); | ||
$this->template->buttonText = 'Add'; | ||
} | ||
|
||
|
||
|
||
public function handleAdd() | ||
{ | ||
$request = $this->request; | ||
if (!$request->isPost()) return; | ||
|
||
$artist = trim($request->post['artist']); | ||
$title = trim($request->post['title']); | ||
|
||
if ($artist != '' && $title != '') { | ||
$data = array( | ||
'artist' => $artist, | ||
'title' => $title, | ||
); | ||
$album = new Albums(); | ||
$album->insert($data); | ||
|
||
$this->redirect('default'); | ||
} | ||
} | ||
|
||
|
||
|
||
/********************* view Edit *********************/ | ||
|
||
|
||
|
||
public function renderEdit($id = 0) | ||
{ | ||
$this->template->title = "Edit Album"; | ||
$album = new Albums(); | ||
if ($id > 0) { | ||
$this->template->album = $album->fetch($id); | ||
} else { | ||
$this->template->album = $album->createBlank(); | ||
} | ||
|
||
// additional view fields required by form | ||
$this->template->action = $this->link('save!', $id); | ||
$this->template->buttonText = 'Update'; | ||
} | ||
|
||
|
||
|
||
public function handleSave($id = 0) | ||
{ | ||
$request = $this->request; | ||
if (!$request->isPost()) return; | ||
|
||
$artist = trim($request->post['artist']); | ||
$title = trim($request->post['title']); | ||
|
||
if ($id !== 0) { | ||
if ($artist != '' && $title != '') { | ||
$data = array( | ||
'artist' => $artist, | ||
'title' => $title, | ||
); | ||
$album = new Albums(); | ||
$album->update($id, $data); | ||
|
||
$this->redirect('default'); | ||
|
||
} else { | ||
//$this->template->album = $album->fetch($id); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
/********************* view Delete *********************/ | ||
|
||
|
||
|
||
public function renderDelete($id = 0) | ||
{ | ||
$this->template->title = "Delete Album"; | ||
|
||
if ($id > 0) { | ||
// only render if we have an id and can find the album. | ||
$album = new Albums(); | ||
$this->template->album = $album->fetch($id); | ||
if (!$this->template->album) { | ||
$this->redirect('default'); | ||
} | ||
} | ||
|
||
$this->template->action = $this->link('delete!', $id); | ||
} | ||
|
||
|
||
|
||
public function handleDelete($id = 0) | ||
{ | ||
$request = $this->request; | ||
if (!$request->isPost()) return; | ||
|
||
$del = $request->post['del']; | ||
if ($del == 'Yes' && $id > 0) { | ||
$album = new Albums(); | ||
$album->delete($id); | ||
} | ||
$this->redirect('default'); | ||
} | ||
|
||
|
||
|
||
/********************* common commands *********************/ | ||
|
||
|
||
|
||
function handleLogout() | ||
{ | ||
Environment::getUser()->signOut(); | ||
$this->redirect('default'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<h1>{$title}</h1> | ||
|
||
{if !empty($message)} | ||
<div id="message"> | ||
{$message} | ||
</div> | ||
{/if} | ||
|
||
<form action="{$component->link('login!')}" method="post"> | ||
<div> | ||
<label for="username">Username</label> | ||
<input type="text" name="username" value=""/> | ||
</div> | ||
<div> | ||
<label for="password">Password</label> | ||
<input type="password" name="password" value=""/> | ||
</div> | ||
<div id="formbutton"> | ||
<input type="submit" name="login" value="Login" /> | ||
</div> | ||
</form> |
Oops, something went wrong.