<commit_before><?php

namespace MapasCulturais\Entities;

use Doctrine\ORM\Mapping as ORM;
use MapasCulturais\App;
use Doctrine\Common\Collections\Criteria;


/**
 * User
 *
 * @property-read \MapasCulturais\Entities\Agent[] $agents Active Agents
 * @property-read \MapasCulturais\Entities\Space[] $spaces Active Spaces
 * @property-read \MapasCulturais\Entities\Project[] $projects Active Projects
 * @property-read \MapasCulturais\Entities\Event[] $events Active Events
 *
 * @property-read \MapasCulturais\Entities\Agent $profile User Profile Agent
 *
 * @ORM\Table(name="usr")
 * @ORM\Entity
 * @ORM\entity(repositoryClass="MapasCulturais\Repositories\User")
 * @ORM\HasLifecycleCallbacks
 */
class User extends \MapasCulturais\Entity
{
    const STATUS_ENABLED = 1;


    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="usr_id_seq", allocationSize=1, initialValue=1)
     */
    protected $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="auth_provider", type="smallint", nullable=false)
     */
    protected $authProvider;

    /**
     * @var string
     *
     * @ORM\Column(name="auth_uid", type="string", length=512, nullable=false)
     */
    protected $authUid;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=false)
     */
    protected $email;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="last_login_timestamp", type="datetime", nullable=true)
     */
    protected $lastLoginTimestamp;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_timestamp", type="datetime", nullable=false)
     */
    protected $createTimestamp;

    /**
     * @var integer
     *
     * @ORM\Column(name="status", type="smallint", nullable=false)
     */
    protected $status = self::STATUS_ENABLED;


    /**
     *
     * @var \MapasCulturais\Entities\Role[] User Roles
     * @ORM\OneToMany(targetEntity="MapasCulturais\Entities\Role", mappedBy="user", cascade="remove", orphanRemoval=true, fetch="EAGER")
     */
    protected $roles = [];

    /**
     * @ORM\OneToMany(targetEntity="MapasCulturais\Entities\Agent", mappedBy="user", cascade="remove", orphanRemoval=true, fetch="EAGER")
     * @ORM\OrderBy({"createTimestamp" = "ASC"})
     */
    protected $agents;

    /**
     * @var \MapasCulturais\Entities\Agent
     *
     * @ORM\ManyToOne(targetEntity="MapasCulturais\Entities\Agent", fetch="EAGER")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
     * })
     */
    protected $profile;


    public function __construct() {
        parent::__construct();

        $this->agents = new \Doctrine\Common\Collections\ArrayCollection();
        $this->lastLoginTimestamp = new \DateTime;
    }

    function getOwnerUser(){
        return $this;
    }

    function setAuthProvider($provider_name){
        $this->authProvider = App::i()->getRegisteredAuthProviderId($provider_name);
    }

    function setProfile(Agent $agent){
        $this->checkPermission('changeProfile');

        if(!$this->equals($agent->user))
            throw new \Exception ('error');

        $this->profile = $agent;
    }

    function jsonSerialize() {
        $result = parent::jsonSerialize();
        unset($result['authUid']);
        return $result;
    }

    function addRole($role_name){
        if(method_exists($this, 'canUserAddRole' . $role_name))
            $this->checkPermission('addRole' . $role_name);
        else
            $this->checkPermission('addRole');

        if(!$this->is($role_name)){
            $role = new Role;
            $role->user = $this;
            $role->name = $role_name;
            $role->save(true);
            return true;
        }

        return false;
    }

    function removeRole($role_name){
        if(method_exists($this, 'canUserRemoveRole' . $role_name))
            $this->checkPermission('removeRole' . $role_name);
        else
            $this->checkPermission('removeRole');


        foreach($this->roles as $role){
            if($role->name == $role_name){
                $role->delete(true);
                return true;
            }
        }

        return false;
    }

    protected function canUserAddRole($user){
        return $user->is('admin') && $user->id != $this->id;
    }

    protected function canUserAddRoleAdmin($user){
        return $user->is('superAdmin') && $user->id != $this->id;
    }

    protected function canUserAddRoleSuperAdmin($user){<commit_msg>removes ' = []; ' of roles declaration to fix joins with roles<commit_after><?php

namespace MapasCulturais\Entities;

use Doctrine\ORM\Mapping as ORM;
use MapasCulturais\App;
use Doctrine\Common\Collections\Criteria;


/**
 * User
 *
 * @property-read \MapasCulturais\Entities\Agent[] $agents Active Agents
 * @property-read \MapasCulturais\Entities\Space[] $spaces Active Spaces
 * @property-read \MapasCulturais\Entities\Project[] $projects Active Projects
 * @property-read \MapasCulturais\Entities\Event[] $events Active Events
 *
 * @property-read \MapasCulturais\Entities\Agent $profile User Profile Agent
 *
 * @ORM\Table(name="usr")
 * @ORM\Entity
 * @ORM\entity(repositoryClass="MapasCulturais\Repositories\User")
 * @ORM\HasLifecycleCallbacks
 */
class User extends \MapasCulturais\Entity
{
    const STATUS_ENABLED = 1;


    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="usr_id_seq", allocationSize=1, initialValue=1)
     */
    protected $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="auth_provider", type="smallint", nullable=false)
     */
    protected $authProvider;

    /**
     * @var string
     *
     * @ORM\Column(name="auth_uid", type="string", length=512, nullable=false)
     */
    protected $authUid;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=false)
     */
    protected $email;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="last_login_timestamp", type="datetime", nullable=true)
     */
    protected $lastLoginTimestamp;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_timestamp", type="datetime", nullable=false)
     */
    protected $createTimestamp;

    /**
     * @var integer
     *
     * @ORM\Column(name="status", type="smallint", nullable=false)
     */
    protected $status = self::STATUS_ENABLED;


    /**
     *
     * @var \MapasCulturais\Entities\Role[] User Roles
     * @ORM\OneToMany(targetEntity="MapasCulturais\Entities\Role", mappedBy="user", cascade="remove", orphanRemoval=true, fetch="EAGER")
     */
    protected $roles;

    /**
     * @ORM\OneToMany(targetEntity="MapasCulturais\Entities\Agent", mappedBy="user", cascade="remove", orphanRemoval=true, fetch="EAGER")
     * @ORM\OrderBy({"createTimestamp" = "ASC"})
     */
    protected $agents;

    /**
     * @var \MapasCulturais\Entities\Agent
     *
     * @ORM\ManyToOne(targetEntity="MapasCulturais\Entities\Agent", fetch="EAGER")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
     * })
     */
    protected $profile;


    public function __construct() {
        parent::__construct();

        $this->agents = new \Doctrine\Common\Collections\ArrayCollection();
        $this->lastLoginTimestamp = new \DateTime;
    }

    function getOwnerUser(){
        return $this;
    }

    function setAuthProvider($provider_name){
        $this->authProvider = App::i()->getRegisteredAuthProviderId($provider_name);
    }

    function setProfile(Agent $agent){
        $this->checkPermission('changeProfile');

        if(!$this->equals($agent->user))
            throw new \Exception ('error');

        $this->profile = $agent;
    }

    function jsonSerialize() {
        $result = parent::jsonSerialize();
        unset($result['authUid']);
        return $result;
    }

    function addRole($role_name){
        if(method_exists($this, 'canUserAddRole' . $role_name))
            $this->checkPermission('addRole' . $role_name);
        else
            $this->checkPermission('addRole');

        if(!$this->is($role_name)){
            $role = new Role;
            $role->user = $this;
            $role->name = $role_name;
            $role->save(true);
            return true;
        }

        return false;
    }

    function removeRole($role_name){
        if(method_exists($this, 'canUserRemoveRole' . $role_name))
            $this->checkPermission('removeRole' . $role_name);
        else
            $this->checkPermission('removeRole');


        foreach($this->roles as $role){
            if($role->name == $role_name){
                $role->delete(true);
                return true;
            }
        }

        return false;
    }
    
    function can($action, \MapasCulturais\Entity $entity){
        return $entity->canUser($action, $this);
    }

    protected function canUserAddRole($user){
        return $user->is('admin') && $user->id != $this->id;
    }

    protected function canUserAddRoleAdmin($user){
        return $user->is('superAdmin') && $user->id != $this->id;
    }

    protected function canUserAddRoleSuperAdmin($user){
{'fraction_non_alphanumeric': 0.15268544898174527, 'fraction_numerical': 0.001899335232668566, 'mean_word_length': 2.715405723245786, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, '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': '7320009', 'n_tokens_mistral': 2955, 'n_tokens_neox': 2914, 'n_words': 472}