lonnieezell / myth-auth

One-stop Auth package for CodeIgniter 4
MIT License
637 stars 208 forks source link

User Entitiy can() method #555

Closed ghassanhjr closed 2 years ago

ghassanhjr commented 2 years ago

https://github.com/lonnieezell/myth-auth/blob/0ab551af4cd97e675cb77e9f8b44b54c354b3daa/src/Entities/User.php#L227

$this->getPermissions() returns a multi-dimensional array(two levels ) where in_array can't find the permission name for example,

I suggest having a multi to multi-dimensional search for $permission for the returned value.

public function can(string $permission)
    {
        foreach ($this->getPermissions() as  $value) {
            if (in_array(strtolower($permission), $value, true)) {
                return true;
            }
        }
        return false;
    }
manageruz commented 2 years ago

$this->getPermissions() returns a multi-dimensional array(two levels ) where in_array can't find the permission name for example

Hi @ghassanhjr.
getPermissions() method of User entity returns array like

[ permission_id => permission_name, permission_id=> permission_name, ]

not a multi-dimentional array (two levels) as you said. So return in_array(strtolower($permission), $this->getPermissions(), true); code works well.

manageruz commented 2 years ago

permissions

ghassanhjr commented 2 years ago

yes, you are correct, $this->permissions or getPermissions(), both return an array of other arrays that's why inside the method public function can(string $permission), can't return the correct answer, , it has a search function in_array(strtolower($permission), $this->getPermissions(), true); , where in_array() function search only in one dimension, and it's always returning false image

MGatner commented 2 years ago

@ghassanhjr can you share some example output of getPermissions() where this doesn't work? @manageruz shows a clear example of it not being a multi-dimensional array, and we have a test case for this (although not very robust): https://github.com/lonnieezell/myth-auth/blob/develop/tests/unit/UserEntityTest.php

ghassanhjr commented 2 years ago

there you are, the method where I can inside the permissions variable

method dumbing the permissions variable: image

result in page body: image

the result from the CodeIgniter : image

manageruz commented 2 years ago

You are clearly confusing something. Is sidebar is a user's permission? And there are two 'sidebar' permissions for one user? In your current database how many permissions you have (look at the auth_permissions database table)? Can you find there 'sidebar'? And attached photo doesn't look like var_dump function result. Can you show full content?

manageruz commented 2 years ago

And show me var_dump($this->getPermissions()); die(); result instead of yours. Before to do all above manipulations run php spark cache:clear command in terminal or manually delete cache files if there is one (cache files located in your_project_root/writable/cache . Delete all files except index.html)