<commit_before><?php
App::uses('AppModel', 'Model');
App::uses('ConnectionManager', 'Model');

/**
 * @property Event $Event
 */
class Organisation extends AppModel
{
    public $useTable = 'organisations';

    public $recursive = -1;

    public $actsAs = array(
        'AuditLog',
        'Containable',
        'SysLogLogable.SysLogLogable' => array(	// TODO Audit, logable
                'roleModel' => 'Organisation',
                'roleKey' => 'organisation_id',
                'change' => 'full'
        ),
    );

    private $__orgCache = array();

    public $validate = array(
        'name' => array(
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'An organisation with this name already exists.'
            ),
            'valueNotEmpty' => array(
                'rule' => array('valueNotEmpty'),
            ),
        ),
        'uuid' => array(
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'An organisation with this UUID already exists.',
                'on' => 'create',
            ),
            'uuid' => array(
                'rule' => 'uuid',
                'message' => 'Please provide a valid RFC 4122 UUID',
                'allowEmpty' => true
            ),
            'valueNotEmpty' => array(
                'rule' => array('valueNotEmpty'),
            )
        )
    );

    public $hasMany = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'org_id'
        ),
        'SharingGroupOrg' => array(
            'className' => 'SharingGroupOrg',
            'foreignKey' => 'org_id',
            'dependent'=> true,
        ),
        'SharingGroup' => array(
            'className' => 'SharingGroup',
            'foreignKey' => 'org_id',
        ),
        'Event' => array(
            'className' => 'Event',
            'foreignKey' => 'orgc_id',
        ),
        'EventOwned' => array(
            'className' => 'Event',
            'foreignKey' => 'org_id',
        ),
    );

    public $organisationAssociations = array(
            'Correlation' => array('table' => 'correlations', 'fields' => array('org_id')),
            'Event' => array('table' => 'events', 'fields' => array('org_id', 'orgc_id')),
            'Job' => array('table' => 'jobs', 'fields' => array('org_id')),
            'Server' => array('table' => 'servers', 'fields' => array('org_id', 'remote_org_id')),
            'ShadowAttribute' =>array('table' => 'shadow_attributes', 'fields' => array('org_id', 'event_org_id')),
            'SharingGroup' => array('table' => 'sharing_groups', 'fields' => array('org_id')),
            'SharingGroupOrg' => array('table' => 'sharing_group_orgs', 'fields' => array('org_id')),
            'Thread' => array('table' => 'threads', 'fields' => array('org_id')),
            'User' => array('table' => 'users', 'fields' => array('org_id'))
    );

    public $genericMISPOrganisation = array(
        'id' => '0',
        'name' => 'MISP',
        'date_created' => '',
        'date_modified' => '',
        'description' => 'Automatically generated MISP organisation',
        'type' => '',
        'nationality' => 'Not specified',
        'sector' => '',
        'created_by' => '0',
        'uuid' => '0',
        'contacts' => '',
        'local' => true,
        'restricted_to_domain' => [],
        'landingpage' => null
    );

    public function beforeValidate($options = array())
    {
        parent::beforeValidate();
        if (empty($this->data['Organisation']['uuid'])) {
            $this->data['Organisation']['uuid'] = CakeText::uuid();
        } else {
            $this->data['Organisation']['uuid'] = strtolower(trim($this->data['Organisation']['uuid']));
        }
        $date = date('Y-m-d H:i:s');
        if (array_key_exists('restricted_to_domain', $this->data['Organisation'])) {
            if (!is_array($this->data['Organisation']['restricted_to_domain'])) {
                $this->data['Organisation']['restricted_to_domain'] = str_replace('\r', '', $this->data['Organisation']['restricted_to_domain']);
                $this->data['Organisation']['restricted_to_domain'] = explode('\n', $this->data['Organisation']['restricted_to_domain']);
            }
            
            $this->data['Organisation']['restricted_to_domain'] = array_values(
                array_filter(
                    array_map('trim', $this->data['Organisation']['restricted_to_domain'])
                )
            );

            $this->data['Organisation']['restricted_to_domain'] = json_encode($this->data['Organisation']['restricted_to_domain']);
        }
        if (!isset($this->data['Organisation']['id'])) {
            $this->data['Organisation']['date_created'] = $date;
        }
        $this->data['Organisation']['date_modified'] = $date;
        if (!isset($this->data['Organisation']['nationality']) || empty($this->data['Organisation']['nationality'])) {
            $this->data['Organisation']['nationality'] = '';
        }
        return true;
    }

    public function beforeDelete($cascade = false)
    {
        if ($this->User->find('count', array('conditions' => array('User.org_id' => $this->id))) != 0) {
            return false;
        }
        if ($this->Event->find('count', array('conditions' => array('OR' => array('Event.org_id' => $this->id, 'Event.orgc_id' => $this->id)))) != 0) {<commit_msg>fix: [organisations] correctly handle a list of org domain restrictions<commit_after><?php
App::uses('AppModel', 'Model');
App::uses('ConnectionManager', 'Model');

/**
 * @property Event $Event
 */
class Organisation extends AppModel
{
    public $useTable = 'organisations';

    public $recursive = -1;

    public $actsAs = array(
        'AuditLog',
        'Containable',
        'SysLogLogable.SysLogLogable' => array(	// TODO Audit, logable
                'roleModel' => 'Organisation',
                'roleKey' => 'organisation_id',
                'change' => 'full'
        ),
    );

    private $__orgCache = array();

    public $validate = array(
        'name' => array(
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'An organisation with this name already exists.'
            ),
            'valueNotEmpty' => array(
                'rule' => array('valueNotEmpty'),
            ),
        ),
        'uuid' => array(
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'An organisation with this UUID already exists.',
                'on' => 'create',
            ),
            'uuid' => array(
                'rule' => 'uuid',
                'message' => 'Please provide a valid RFC 4122 UUID',
                'allowEmpty' => true
            ),
            'valueNotEmpty' => array(
                'rule' => array('valueNotEmpty'),
            )
        )
    );

    public $hasMany = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'org_id'
        ),
        'SharingGroupOrg' => array(
            'className' => 'SharingGroupOrg',
            'foreignKey' => 'org_id',
            'dependent'=> true,
        ),
        'SharingGroup' => array(
            'className' => 'SharingGroup',
            'foreignKey' => 'org_id',
        ),
        'Event' => array(
            'className' => 'Event',
            'foreignKey' => 'orgc_id',
        ),
        'EventOwned' => array(
            'className' => 'Event',
            'foreignKey' => 'org_id',
        ),
    );

    public $organisationAssociations = array(
            'Correlation' => array('table' => 'correlations', 'fields' => array('org_id')),
            'Event' => array('table' => 'events', 'fields' => array('org_id', 'orgc_id')),
            'Job' => array('table' => 'jobs', 'fields' => array('org_id')),
            'Server' => array('table' => 'servers', 'fields' => array('org_id', 'remote_org_id')),
            'ShadowAttribute' =>array('table' => 'shadow_attributes', 'fields' => array('org_id', 'event_org_id')),
            'SharingGroup' => array('table' => 'sharing_groups', 'fields' => array('org_id')),
            'SharingGroupOrg' => array('table' => 'sharing_group_orgs', 'fields' => array('org_id')),
            'Thread' => array('table' => 'threads', 'fields' => array('org_id')),
            'User' => array('table' => 'users', 'fields' => array('org_id'))
    );

    public $genericMISPOrganisation = array(
        'id' => '0',
        'name' => 'MISP',
        'date_created' => '',
        'date_modified' => '',
        'description' => 'Automatically generated MISP organisation',
        'type' => '',
        'nationality' => 'Not specified',
        'sector' => '',
        'created_by' => '0',
        'uuid' => '0',
        'contacts' => '',
        'local' => true,
        'restricted_to_domain' => [],
        'landingpage' => null
    );

    public function beforeValidate($options = array())
    {
        parent::beforeValidate();
        if (empty($this->data['Organisation']['uuid'])) {
            $this->data['Organisation']['uuid'] = CakeText::uuid();
        } else {
            $this->data['Organisation']['uuid'] = strtolower(trim($this->data['Organisation']['uuid']));
        }
        $date = date('Y-m-d H:i:s');
        if (array_key_exists('restricted_to_domain', $this->data['Organisation'])) {
            if (!is_array($this->data['Organisation']['restricted_to_domain'])) {
                $this->data['Organisation']['restricted_to_domain'] = str_replace("\r", '', $this->data['Organisation']['restricted_to_domain']);
                $this->data['Organisation']['restricted_to_domain'] = explode("\n", $this->data['Organisation']['restricted_to_domain']);
            }

            $this->data['Organisation']['restricted_to_domain'] = array_values(
                array_filter(
                    array_map('trim', $this->data['Organisation']['restricted_to_domain'])
                )
            );

            $this->data['Organisation']['restricted_to_domain'] = json_encode($this->data['Organisation']['restricted_to_domain']);
        }
        if (!isset($this->data['Organisation']['id'])) {
            $this->data['Organisation']['date_created'] = $date;
        }
        $this->data['Organisation']['date_modified'] = $date;
        if (!isset($this->data['Organisation']['nationality']) || empty($this->data['Organisation']['nationality'])) {
            $this->data['Organisation']['nationality'] = '';
        }
        return true;
    }

    public function beforeDelete($cascade = false)
    {
        if ($this->User->find('count', array('conditions' => array('User.org_id' => $this->id))) != 0) {
            return false;
        }
        if ($this->Event->find('count', array('conditions' => array('OR' => array('Event.org_id' => $this->id, 'Event.orgc_id' => $this->id)))) != 0) {
{'fraction_non_alphanumeric': 0.19310533018009823, 'fraction_numerical': 0.0018191740949608877, 'mean_word_length': 2.190655832849681, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 225, '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': '3070200', 'n_tokens_mistral': 3258, 'n_tokens_neox': 3109, 'n_words': 555}