1. Go to this page and download the library: Download nette/security library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nette / security example snippets
try {
$user->login($username, $password);
} catch (Nette\Security\AuthenticationException $e) {
$this->flashMessage('The username or password you entered is incorrect.');
}
$user->logout();
echo $user->isLoggedIn() ? 'yes' : 'no';
// login expires after 30 minutes of inactivity
$user->setExpiration('30 minutes');
// cancel expiration
$user->setExpiration(null);
protected function startup()
{
parent::startup();
if (!$this->getUser()->isLoggedIn()) {
$this->redirect('Sign:in');
}
}
$authenticator = new Nette\Security\SimpleAuthenticator([
# name => password
'johndoe' => 'secret123',
'kathy' => 'evenmoresecretpassword',
]);
use Nette;
class MyAuthenticator implements Nette\Security\Authenticator
{
private $database;
private $passwords;
public function __construct(Nette\Database\Context $database, Nette\Security\Passwords $passwords)
{
$this->database = $database;
$this->passwords = $passwords;
}
public function authenticate($username, $password): Nette\Security\IIdentity
{
$row = $this->database->table('users')
->where('username', $username)
->fetch();
if (!$row) {
throw new Nette\Security\AuthenticationException('User not found.');
}
if (!$this->passwords->verify($password, $row->password)) {
throw new Nette\Security\AuthenticationException('Invalid password.');
}
return new Nette\Security\SimpleIdentity(
$row->id,
$row->role, // or array of roles
['name' => $row->username]
);
}
}
$user->onLoggedIn[] = function () {
// user has just logged in
};
$user->getIdentity()->getId();
// also works shortcut $user->getId();
$user->getIdentity()->getRoles();
// user data can be access as properties
// the name we passed on in MyAuthenticator
$user->getIdentity()->name;
if ($user->isLoggedIn()) { // is user logged in?
deleteItem(); // if so, he may delete an item
}
if ($user->isInRole('admin')) { // is the admin role assigned to the user?
deleteItem(); // if so, he may delete an item
}
class MyAuthorizator implements Nette\Security\Authorizator
{
public function isAllowed($role, $resource, $operation): bool
{
if ($role === 'admin') {
return true;
}
if ($role === 'user' && $resource === 'article') {
return true;
}
...
return false;
}
}
if ($user->isAllowed('file')) { // is user allowed to do everything with resource 'file'?
useFile();
}
if ($user->isAllowed('file', 'delete')) { // is user allowed to delete a resource 'file'?
deleteFile();
}
$acl = new Nette\Security\Permission;
$acl->addRole('guest');
$acl->addRole('registered', 'guest'); // registered inherits from guest
$acl->addRole('administrator', 'registered'); // and administrator inherits from registered
// everything is denied now
// let the guest view articles, comments and polls
$acl->allow('guest', ['article', 'comment', 'poll'], 'view');
// and also vote in polls
$acl->allow('guest', 'poll', 'vote');
// the registered inherits the permissions from guesta, we will also let him to comment
$acl->allow('registered', 'comment', 'add');
// the administrator can view and edit anything
$acl->allow('administrator', $acl::All, ['view', 'edit', 'add']);
// administrator cannot edit polls, that would be undemocractic.
$acl->deny('administrator', 'poll', 'edit');
// can guest view articles?
$acl->isAllowed('guest', 'article', 'view'); // true
// can guest edit an article?
$acl->isAllowed('guest', 'article', 'edit'); // false
// can guest vote in polls?
$acl->isAllowed('guest', 'poll', 'vote'); // true
// may guest add comments?
$acl->isAllowed('guest', 'comment', 'add'); // false
class Registered implements Nette\Security\IRole
{
public $id;
public function getRoleId(): string
{
return 'registered';
}
}
class Article implements Nette\Security\IResource
{
public $authorId;
public function getResourceId(): string
{
return 'article';
}
}
$user = new Registered(...);
$article = new Article(...);
$acl->isAllowed($user, $article, 'edit');
$acl = new Nette\Security\Permission;
$acl->addRole('admin');
$acl->addRole('guest');
$acl->addResource('backend');
$acl->allow('admin', 'backend');
$acl->deny('guest', 'backend');
// example A: role admin has lower weight than role guest
$acl->addRole('john', ['admin', 'guest']);
$acl->isAllowed('john', 'backend'); // false
// example B: role admin has greater weight than role guest
$acl->addRole('mary', ['guest', 'admin']);
$acl->isAllowed('mary', 'backend'); // true
$user->getStorage()->setNamespace('forum');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.