/** * Constructor * * If events are specified - do *not* merge them with existing events, * overwrite the events to listen on * * @param \Cake\ORM\Table $table The table this behavior is attached to. * @param array $config The config for this behavior. */ public function __construct(Table $table, array $config = []) { //Merge $config with application-wide scopeBehavior config $config = array_merge(MTApp::config('scopeBehavior'), $config); parent::__construct($table, $config); $this->_table = $table; }
/** * beforeFind callback * * inject where condition if context is 'tenant' * * @param \Cake\Event\Event $event The afterSave event that was fired. * @param \Cake\ORM\Query $query The query. * @return void */ public function beforeFind(Event $event, Query $query, $options) { // if context is tenant, add conditions to query if (MTApp::getContext() == 'tenant') { // check if find option has "skipTenant", recursive error fix if (!isset($options['skipTenantCheck']) || $options['skipTenantCheck'] !== true) { // secure the configured tenant table by adding a primary key condition if ($this->_table->alias() === MTApp::config('model')['className']) { $query->where([$this->_table->alias() . '.' . $this->_table->primaryKey() => MTApp::tenant()->id]); } else { $query->where([$this->_table->alias() . '.' . $this->config('foreign_key_field') => MTApp::tenant()->id]); } } } else { throw new DataScopeViolationException('Tenant Scoped accessed globally'); } return $query; }
<?php /** * MultiTenant Plugin * Copyright (c) PRONIQUE Software (http://pronique.com) * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) PRONIQUE Software (http://pronique.com) * @link http://github.com/pronique/multitenant MultiTenant Plugin Project * @since 0.5.1 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ namespace MultiTenant\Config; use MultiTenant\Core\MTApp; use Cake\Configure\Engine\PhpConfig; use Cake\Core\Configure; MTApp::config(Configure::consume('MultiTenant'));