S
S
Sergey08082017-06-17 16:52:13
PHP
Sergey0808, 2017-06-17 16:52:13

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");

Then I try to make the check into action:
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;
        }


    }

As a result, it does not let me go to this page. I understand that the code is incorrect, but how to do the correct user verification?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Volintsev, 2017-06-22
@copist

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 roleof 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 question

Ask a Question

731 491 924 answers to any question