diff --git a/app/Controller/AttributesController.php b/app/Controller/AttributesController.php index ec6f6c49c..2479c5cd1 100644 --- a/app/Controller/AttributesController.php +++ b/app/Controller/AttributesController.php @@ -2641,11 +2641,8 @@ class AttributesController extends AppController $tag_id_list = array($tag_id); } - $conditions = ['Tag.id' => $tag_id_list]; - if (!$this->_isSiteAdmin()) { - $conditions['Tag.org_id'] = array(0, $this->Auth->user('org_id')); - $conditions['Tag.user_id'] = array(0, $this->Auth->user('id')); - } + $conditions = $this->Attribute->AttributeTag->Tag->createConditions($this->Auth->user()); + $conditions['Tag.id'] = $tag_id_list; $tags = $this->Attribute->AttributeTag->Tag->find('list', array( 'conditions' => $conditions, 'fields' => ['Tag.id', 'Tag.name'], diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index 4a3eb1596..b0b446766 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -3754,11 +3754,8 @@ class EventsController extends AppController $this->loadModel('Taxonomy'); foreach ($tag_id_list as $tag_id) { - $conditions = ['Tag.id' => $tag_id]; - if (!$this->_isSiteAdmin()) { - $conditions['Tag.org_id'] = array('0', $this->Auth->user('org_id')); - $conditions['Tag.user_id'] = array('0', $this->Auth->user('id')); - } + $conditions = $this->Event->EventTag->Tag->createConditions($this->Auth->user()); + $conditions['Tag.id'] = $tag_id; $tag = $this->Event->EventTag->Tag->find('first', array( 'conditions' => $conditions, 'recursive' => -1, diff --git a/app/Controller/TagCollectionsController.php b/app/Controller/TagCollectionsController.php index 512c65765..0131790ab 100644 --- a/app/Controller/TagCollectionsController.php +++ b/app/Controller/TagCollectionsController.php @@ -247,11 +247,7 @@ class TagCollectionsController extends AppController } $tag_id = $this->request->data['tag']; } - $tagConditions = array(); - if (!$this->_isSiteAdmin()) { - $tagConditions['Tag.org_id'] = array('0', $this->Auth->user('org_id')); - $tagConditions['Tag.user_id'] = array('0', $this->Auth->user('id')); - } + $tagConditions = $this->TagCollection->TagCollectionTag->Tag->createConditions($this->Auth->user()); if (!is_numeric($tag_id)) { $tag_ids = json_decode($tag_id); $tag_lookups = array(); @@ -306,11 +302,8 @@ class TagCollectionsController extends AppController } foreach ($tag_id_list as $tag_id) { - $tagConditions = ['Tag.id' => $tag_id]; - if (!$this->_isSiteAdmin()) { - $tagConditions['Tag.org_id'] = array('0', $this->Auth->user('org_id')); - $tagConditions['Tag.user_id'] = array('0', $this->Auth->user('id')); - } + $tagConditions = $this->TagCollection->TagCollectionTag->Tag->createConditions($this->Auth->user()); + $tagConditions['Tag.id'] = $tag_id; $tag = $this->TagCollection->TagCollectionTag->Tag->find('first', array( 'conditions' => $tagConditions, 'recursive' => -1, diff --git a/app/Controller/TagsController.php b/app/Controller/TagsController.php index f0c2ce258..8bfa3dee6 100644 --- a/app/Controller/TagsController.php +++ b/app/Controller/TagsController.php @@ -507,12 +507,9 @@ class TagsController extends AppController $expanded = $tags; } elseif ($taxonomy_id === 'favourites') { $tags = array(); - $conditions = array( - 'FavouriteTag.user_id' => $user['id'], - 'Tag.org_id' => array(0, $user['org_id']), - 'Tag.user_id' => array(0, $user['id']), - 'Tag.hide_tag' => 0, - ); + $conditions = $this->Tag->createConditions($user); + $conditions['FavouriteTag.user_id'] = $user['id']; + $conditions['Tag.hide_tag'] = 0; if (!$local_tag) { $conditions['Tag.local_only'] = 0; } @@ -527,14 +524,9 @@ class TagsController extends AppController $expanded = $tags; } } elseif ($taxonomy_id === 'all') { // all tags - $conditions = [ - 'Tag.is_galaxy' => 0, - 'Tag.hide_tag' => 0, - ]; - if (!$this->_isSiteAdmin()) { - $conditions['Tag.org_id'] = array(0, $user['org_id']); - $conditions['Tag.user_id'] = array(0, $user['id']); - } + $conditions = $this->Tag->createConditions($user); + $conditions['Tag.is_galaxy'] = 0; + $conditions['Tag.hide_tag'] = 0; if (!$local_tag) { $conditions['Tag.local_only'] = 0; } diff --git a/app/Model/Tag.php b/app/Model/Tag.php index b58fd7c75..0eee6b821 100644 --- a/app/Model/Tag.php +++ b/app/Model/Tag.php @@ -173,11 +173,9 @@ class Tag extends AppModel */ public function lookupTagIdForUser(array $user, $tagName) { - $conditions = ['LOWER(Tag.name)' => mb_strtolower($tagName)]; - if (!$user['Role']['perm_site_admin']) { - $conditions['Tag.org_id'] = [0, $user['org_id']]; - $conditions['Tag.user_id'] = [0, $user['id']]; - } + $conditions = $this->createConditions($user); + $conditions['LOWER(Tag.name)'] = mb_strtolower($tagName); + $tagId = $this->find('first', array( 'conditions' => $conditions, 'recursive' => -1, @@ -849,4 +847,18 @@ class Tag extends AppModel return $data; } + + /** + * @param array $user + * @return array + */ + public function createConditions(array $user) + { + $conditions = []; + if (!$user['Role']['perm_site_admin']) { + $conditions['Tag.org_id'] = [0, $user['org_id']]; + $conditions['Tag.user_id'] = [0, $user['id']]; + } + return $conditions; + } } diff --git a/app/Model/TagCollection.php b/app/Model/TagCollection.php index e4835794a..7b57c9293 100644 --- a/app/Model/TagCollection.php +++ b/app/Model/TagCollection.php @@ -2,6 +2,9 @@ App::uses('AppModel', 'Model'); +/** + * @property TagCollectionTag $TagCollectionTag + */ class TagCollection extends AppModel { public $useTable = 'tag_collections'; diff --git a/app/Model/TagCollectionTag.php b/app/Model/TagCollectionTag.php index 8baf03cb5..858f5bfb4 100644 --- a/app/Model/TagCollectionTag.php +++ b/app/Model/TagCollectionTag.php @@ -2,6 +2,9 @@ App::uses('AppModel', 'Model'); +/** + * @property Tag $Tag + */ class TagCollectionTag extends AppModel { public $useTable = 'tag_collection_tags'; @@ -25,8 +28,4 @@ class TagCollectionTag extends AppModel 'className' => 'Tag', ) ); - - public $validate = array( - - ); }