<commit_before><?php
namespace RolesCapabilities\Controller\Component;

use Cake\Controller\Component;
use Cake\ORM\TableRegistry;

class CapabilityComponent extends Component
{
    /**
     * Allow flag
     */
    const ALLOW = true;

    /**
     * Deny flag
     */
    const DENY = false;

    public $components = ['Auth'];

    /**
     * Current controller
     * @var object
     */
    protected $_controller;

    /**
     * Current user details
     * @var array
     */
    protected $_user = [];

    /**
     * Initialize method
     * @param  array  $config configuration array
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->_controller = $this->_registry->getController();
        $this->_user = $this->Auth->user();
    }

    /**
     * Method that retrieves controller capabilities
     * @return array controller capabilities
     */
    protected function _getControllerCapabilities()
    {
        $caps = [];
        if (method_exists($this->_controller, 'getCapabilities')) {
            $caps = array_keys($this->_controller->getCapabilities());
        }

        return $caps;
    }

    /**
     * Method that retrieves specified user's groups
     * @param  string $userId user id
     * @return array
     */
    protected function _getUserGroups($userId = null)
    {
        // if not specified, get current user's id
        if (empty($userId)) {
            $userId = $this->Auth->user('id');
        }
        $groups = TableRegistry::get('Groups.Groups');

        $query = $groups->find('list', [
            'keyField' => 'id',
            'valueField' => 'name'
        ]);
        $query->matching('Users', function ($q) use ($userId) {
            return $q->where(['Users.id' => $userId]);
        });

        return $query->toArray();
    }
<commit_msg>added method that retrieves roles that group(s) belongs to (task #805)<commit_after><?php
namespace RolesCapabilities\Controller\Component;

use Cake\Controller\Component;
use Cake\ORM\TableRegistry;

class CapabilityComponent extends Component
{
    /**
     * Allow flag
     */
    const ALLOW = true;

    /**
     * Deny flag
     */
    const DENY = false;

    public $components = ['Auth'];

    /**
     * Current controller
     * @var object
     */
    protected $_controller;

    /**
     * Current user details
     * @var array
     */
    protected $_user = [];

    /**
     * Initialize method
     * @param  array  $config configuration array
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->_controller = $this->_registry->getController();
        $this->_user = $this->Auth->user();
    }

    /**
     * Method that retrieves controller capabilities
     * @return array controller capabilities
     */
    protected function _getControllerCapabilities()
    {
        $caps = [];
        if (method_exists($this->_controller, 'getCapabilities')) {
            $caps = array_keys($this->_controller->getCapabilities());
        }

        return $caps;
    }

    /**
     * Method that retrieves specified user's groups
     * @param  string $userId user id
     * @return array
     */
    protected function _getUserGroups($userId = null)
    {
        // if not specified, get current user's id
        if (empty($userId)) {
            $userId = $this->Auth->user('id');
        }

        $groups = TableRegistry::get('Groups.Groups');

        $query = $groups->find('list', [
            'keyField' => 'id',
            'valueField' => 'name'
        ]);
        $query->matching('Users', function ($q) use ($userId) {
            return $q->where(['Users.id' => $userId]);
        });

        return $query->toArray();
    }

    /**
     * Method that retrieves specified group(s) roles.
     * @param  array  $userGroups group(s) id(s)
     * @return array
     */
    protected function _getGroupsRoles(array $userGroups = [])
    {
        $result = [];

        if (!empty($userGroups)) {
            $roles = TableRegistry::get('RolesCapabilities.Roles');

            $query = $roles->find('list', [
                'keyField' => 'id',
                'valueField' => 'name'
            ]);
            $query->matching('Groups', function ($q) use ($userGroups) {
                return $q->where(['Groups.id IN' => array_keys($userGroups)]);
            });
            $result = $query->toArray();
        }

        return $result;
    }

{'fraction_non_alphanumeric': 0.14310837546713565, 'fraction_numerical': 0.0006594856012310398, 'mean_word_length': 2.019243530192435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5373', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1240, 'n_words': 293}