2015-02-23 11:33:38 +01:00
|
|
|
<?php
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
|
|
|
class SharingGroup extends AppModel {
|
|
|
|
|
|
|
|
public $actsAs = array('Containable');
|
|
|
|
public $validate = array(
|
|
|
|
'name' => array(
|
|
|
|
'unique' => array(
|
|
|
|
'rule' => 'isUnique',
|
|
|
|
'message' => 'A sharing group with this name already exists.'
|
|
|
|
),
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => array('notempty'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'uuid' => array(
|
|
|
|
'uuid' => array(
|
|
|
|
'rule' => array('uuid'),
|
|
|
|
'message' => 'Please provide a valid UUID'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
public $hasMany = array(
|
2015-04-07 00:24:44 +02:00
|
|
|
'SharingGroupOrg' => array(
|
|
|
|
'className' => 'SharingGroupOrg',
|
|
|
|
'foreignKey' => 'sharing_group_id',
|
|
|
|
'dependent' => true, // cascade deletes
|
|
|
|
),
|
|
|
|
'SharingGroupServer' => array(
|
|
|
|
'className' => 'SharingGroupServer',
|
2015-02-23 11:33:38 +01:00
|
|
|
'foreignKey' => 'sharing_group_id',
|
|
|
|
'dependent' => true, // cascade deletes
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
public $belongsTo = array(
|
|
|
|
'Organisation' => array(
|
|
|
|
'className' => 'Organisation',
|
|
|
|
'foreignKey' => false,
|
|
|
|
'conditions' => array('Organisation.uuid = SharingGroup.organisation_uuid'),
|
|
|
|
)
|
|
|
|
);
|
2015-04-07 14:47:14 +02:00
|
|
|
|
2015-02-23 11:33:38 +01:00
|
|
|
|
|
|
|
public function beforeValidate($options = array()) {
|
|
|
|
parent::beforeValidate();
|
|
|
|
if (empty($this->data['SharingGroup']['uuid'])) {
|
|
|
|
$this->data['SharingGroup']['uuid'] = String::uuid();
|
|
|
|
}
|
|
|
|
$date = date('Y-m-d H:i:s');
|
|
|
|
if (empty($this->data['SharingGroup']['date_created'])) {
|
|
|
|
$this->data['SharingGroup']['date_created'] = $date;
|
|
|
|
}
|
|
|
|
$this->data['SharingGroup']['date_modified'] = $date;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-07 00:24:44 +02:00
|
|
|
// returns a list of all sharing groups that the user is allowed to see
|
2015-04-09 17:27:34 +02:00
|
|
|
// pass true to get the actual SG objects
|
|
|
|
public function fetchAllAuthorised($user, $scope = false) {
|
2015-04-07 00:24:44 +02:00
|
|
|
if ($user['Role']['perm_site_admin']) {
|
|
|
|
$sgs = $this->find('all', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'fields' => array('id'),
|
|
|
|
));
|
|
|
|
$ids = array();
|
|
|
|
foreach ($sgs as $sg) $ids[] = $sg['SharingGroup']['id'];
|
|
|
|
} else {
|
|
|
|
$ids = array_unique(array_merge($this->SharingGroupServer->fetchAllAuthorised(), $this->SharingGroupOrg->fetchAllAuthorised($user['Organisation']['id'])));
|
2015-02-23 11:33:38 +01:00
|
|
|
}
|
2015-04-09 17:27:34 +02:00
|
|
|
if ($scope === 'full') {
|
|
|
|
$sgs = $this->find('all', array(
|
|
|
|
'contain' => array('SharingGroupServer' => array('Server'), 'SharingGroupOrg' => array('Organisation'), 'Organisation'),
|
|
|
|
'conditions' => array('SharingGroup.id' => $ids),
|
|
|
|
'order' => 'name ASC'
|
|
|
|
));
|
|
|
|
return $sgs;
|
|
|
|
} else if ($scope == 'name') {
|
|
|
|
$sgs = $this->find('list', array(
|
|
|
|
'recursive' => -1,
|
|
|
|
'fields' => array('id', 'name'),
|
|
|
|
'order' => 'name ASC'
|
|
|
|
));
|
|
|
|
return $sgs;
|
|
|
|
} else {
|
|
|
|
return $ids;
|
|
|
|
}
|
2015-02-23 11:33:38 +01:00
|
|
|
}
|
|
|
|
|
2015-04-07 00:24:44 +02:00
|
|
|
// returns true if the SG exists and the user is allowed to see it
|
|
|
|
public function checkIfAuthorised($user, $id) {
|
|
|
|
if (!isset($user['id'])) throw new MethodNotAllowedException('Invalid user.');
|
|
|
|
$this->id = $id;
|
|
|
|
if (!$this->exists()) return false;
|
|
|
|
if ($user['Role']['perm_site_admin'] || $this->SharingGroupServer->checkIfAuthorised($id) || $this->SharingGroupOrg->checkIfAuthorised($id, $user['Organisation']['id'])) return true;
|
|
|
|
return false;
|
2015-02-23 11:33:38 +01:00
|
|
|
}
|
|
|
|
|
2015-04-07 00:24:44 +02:00
|
|
|
public function checkIfOwner($user, $id) {
|
|
|
|
if (!isset($user['id'])) throw new MethodNotAllowedException('Invalid user.');
|
|
|
|
$this->id = $id;
|
|
|
|
if (!$this->exists()) return false;
|
|
|
|
if ($user['Role']['perm_site_admin']) return true;
|
|
|
|
$sg = $this->find('first', array(
|
|
|
|
'conditions' => array('SharingGroup.id' => $id),
|
|
|
|
'recursive' => -1,
|
|
|
|
'fields' => array('id', 'organisation_uuid'),
|
|
|
|
));
|
|
|
|
return ($sg['SharingGroup']['organisation_uuid'] === $user['Organisation']['uuid']);
|
|
|
|
}
|
2015-02-23 11:33:38 +01:00
|
|
|
}
|