Answer the question
In order to leave comments, you need to log in
php phalcon how to create access control lists for users?
I created a file in a project and defined it like this:
use Phalcon\Acl\Adapter\Memory as AclList;
use Phalcon\Acl;
use Phalcon\Acl\Role;
use Phalcon\Acl\Resource;
$acl = new AclList();
$acl->setDefaultAction(
Acl::DENY
);
$roleUsers = new Role("Users", "User role");
$roleGuests = new Role("Guests", "Guests role");
$acl->addRole($roleUsers);
$acl->addRole($roleGuests);
$profilesResource = new Resource("Profiles");
$acl->addResource(
$profilesResource,
[
"show"
]
);
$acl->allow("Users", "Profiles", "show");
$acl->deny("Guests", "Profiles", "show");
public function userShowAction()
{
$acl = new AclList();
if ($acl->isAllowed("Guests", "Profiles", "show")) {
$this->flashSession->error("Вы должны войти");
$this->response->redirect("/");
} elseif ($acl->isAllowed("Users", "Profiles", "show")) {
$user = Users::findFirst($this->session->get("auth-id"));
$this->view->name = $user->name;
$this->view->about = $user->about;
$this->view->email = $user->email;
$this->view->robots = $user->robots;
}
}
Answer the question
In order to leave comments, you need to log in
Documentation
https://olddocs.phalconphp.com/en/3.0.0/api/Phalco...
https://olddocs.phalconphp.com/en/3.0.0/api/Phalco
... described in the documentation is a specific implementation of getting the role of the current user.
You need to somehow determine that the current user has a role Guest
- this is beyond the scope of the documentation.
Option 1
You can store the role code in a separate column role
of the table user
Then it will be $acl->isAllowed($user->role, "Profiles", "show")
Option 2
You can set the available rights for each user in the ACL, for example
Then it will be
$acl->isAllowed($usr->username, "Profiles", "show")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question