<commit_before><?php
namespace MapasCulturais\Repositories;

use MapasCulturais\Entities;

class User extends \MapasCulturais\Repository{

    /**
     * Retorna um usuário pelo auth_uid e auth_provider
     * @param string $auth_uid
     * @param int $auth_provider
     * @return \MapasCulturais\Entities\User|null
     */
    public function getByAuth($auth_provider, $auth_uid){
        $user_query = $this->_em->createQuery('SELECT u FROM MapasCulturais\Entities\User u WHERE u.authProvider=:auth_provider AND u.authUid = :auth_uid');

        $user_query->setParameter('auth_provider', $auth_provider);
        $user_query->setParameter('auth_uid', $auth_uid);
        $user = $user_query->getOneOrNullResult();
        return $user;
    }
    
    public function getByRole($role,$subsite_id = 0) {
        $join_subsite = "";
        if($subsite_id > 0) {
            $join_subsite .= " JOIN r.subsite s WITH s.id =:subsite_id " ; 
        }
        
        $user_query = $this->_em->createQuery('SELECT r,u,a FROM MapasCulturais\Entities\Role r 
                JOIN r.user u WITH r.name =:role' . $join_subsite . ' JOIN u.profile a ORDER BY a.name');

        $user_query->setParameter('role', $role);
        if($subsite_id > 0) {
            $user_query->setParameter('subsite_id', $subsite_id);
        }
        $users = $user_query->getResult();
        return $users;

    }


    public function getAdmins($subsite_id){
        $class = $this->getClassName();

        $q = $this->_em->createQuery();

        if(is_null($subsite_id)){
            $_dql = 'r.subsiteId IS NULL';
        } else {
            $_dql = 'r.subsiteId = :subsiteId';
            $q->setParameter('subsiteId', $subsite_id);
        }

        $dql = "
            SELECT
                e
            FROM
                {$class} e
                JOIN e.roles r
                    WITH r.name IN ('saasSuperAdmin', 'saasAdmin') OR
                         (r.name IN ('superAdmin', 'admin') AND {$_dql})";


        $q->setDQL($dql);

        return $q->getResult();
    }
}<commit_msg>adiciona hook para possibilitar a filtragem dos admin do subsite. hook: repo(User).getAdmins<commit_after><?php
namespace MapasCulturais\Repositories;

use MapasCulturais\Entities;
use MapasCulturais\App;

class User extends \MapasCulturais\Repository{

    /**
     * Retorna um usuário pelo auth_uid e auth_provider
     * @param string $auth_uid
     * @param int $auth_provider
     * @return \MapasCulturais\Entities\User|null
     */
    public function getByAuth($auth_provider, $auth_uid){
        $user_query = $this->_em->createQuery('SELECT u FROM MapasCulturais\Entities\User u WHERE u.authProvider=:auth_provider AND u.authUid = :auth_uid');

        $user_query->setParameter('auth_provider', $auth_provider);
        $user_query->setParameter('auth_uid', $auth_uid);
        $user = $user_query->getOneOrNullResult();
        return $user;
    }
    
    public function getByRole($role,$subsite_id = 0) {
        $join_subsite = "";
        if($subsite_id > 0) {
            $join_subsite .= " JOIN r.subsite s WITH s.id =:subsite_id " ; 
        }
        
        $user_query = $this->_em->createQuery('SELECT r,u,a FROM MapasCulturais\Entities\Role r 
                JOIN r.user u WITH r.name =:role' . $join_subsite . ' JOIN u.profile a ORDER BY a.name');

        $user_query->setParameter('role', $role);
        if($subsite_id > 0) {
            $user_query->setParameter('subsite_id', $subsite_id);
        }
        $users = $user_query->getResult();
        return $users;

    }


    public function getAdmins($subsite_id){
        $class = $this->getClassName();

        $q = $this->_em->createQuery();

        if(is_null($subsite_id)){
            $_dql = 'r.subsiteId IS NULL';
        } else {
            $_dql = 'r.subsiteId = :subsiteId';
            $q->setParameter('subsiteId', $subsite_id);
        }

        $dql = "
            SELECT
                e
            FROM
                {$class} e
                JOIN e.roles r
                    WITH r.name IN ('saasSuperAdmin', 'saasAdmin') OR
                         (r.name IN ('superAdmin', 'admin') AND {$_dql})";


        $q->setDQL($dql);

        $admins = $q->getResult();
        
        App::i()->applyHookBoundTo($this, "repo(User).getAdmins", [$subsite_id, &$admins]);

        return $admins;
    }
}
{'fraction_non_alphanumeric': 0.13460663938153705, 'fraction_numerical': 0.001364256480218281, 'mean_word_length': 2.1602011494252875, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '375265', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1416, 'n_words': 283}