2015-04-07 00:24:44 +02:00
< ? php
App :: uses ( 'AppModel' , 'Model' );
2021-10-20 18:08:42 +02:00
2018-07-19 11:48:22 +02:00
class SharingGroupOrg extends AppModel
{
2021-01-22 13:01:23 +01:00
public $actsAs = array ( 'AuditLog' , 'Containable' );
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public $belongsTo = array (
2021-10-20 18:08:42 +02:00
'SharingGroup' => array (
'className' => 'SharingGroup' ,
'foreignKey' => 'sharing_group_id'
),
'Organisation' => array (
'className' => 'Organisation' ,
'foreignKey' => 'org_id' ,
//'conditions' => array('SharingGroupElement.organisation_uuid' => 'Organisation.uuid')
)
2018-07-19 11:48:22 +02:00
);
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function beforeValidate ( $options = array ())
{
2021-10-20 18:08:42 +02:00
$data = $this -> data [ $this -> alias ];
$conditions = [
'sharing_group_id' => $data [ 'sharing_group_id' ],
'org_id' => $data [ 'org_id' ],
];
if ( isset ( $data [ 'id' ])) {
$conditions [ 'id !=' ] = $data [ 'id' ];
}
if ( $this -> hasAny ( $conditions )) {
$this -> log ( " Trying to save duplicate organisation ` { $data [ 'org_id' ] } ` for sharing group ` { $data [ 'sharing_group_id' ] } . This should never happened. " );
$this -> invalidate ( 'org_id' , 'The same organisation is already assigned to this sharing group.' );
return false ;
}
2018-07-19 11:48:22 +02:00
}
2015-04-07 00:24:44 +02:00
2018-07-19 11:48:22 +02:00
public function updateOrgsForSG ( $id , $new_orgs , $old_orgs , $user )
{
// Loop through all of the organisations we want to add.
foreach ( $new_orgs as $org ) {
$SgO = array (
'sharing_group_id' => $id ,
'org_id' => $org [ 'id' ],
'extend' => $org [ 'extend' ]
);
$found = false ;
// If there is a match between a new org and an old org, keep the org in $found and unset it in the old org array.
foreach ( $old_orgs as $k => $old_org ) {
if ( $old_org [ 'org_id' ] == $org [ 'id' ]) {
$found = $old_orgs [ $k ];
unset ( $old_orgs [ $k ]);
break ;
}
}
// If we have not found the org previously, create a new sharing group org object.
// Otherwise, if we have found it check whether the extended field has been altered, if not just continue without saving
if ( ! $found ) {
$this -> create ();
$isChange = false ;
} else {
if ( $found [ 'extend' ] == $SgO [ 'extend' ]) {
continue ;
}
$SgO [ 'id' ] = $found [ 'id' ];
$isChange = true ;
}
if ( $this -> save ( $SgO )) {
if ( $isChange ) {
2021-10-20 18:08:42 +02:00
$this -> loadLog () -> createLogEntry ( $user , 'edit' , 'SharingGroupOrg' , $this -> id , 'Sharing group (' . $id . '): Modified right to alter sharing group for organisation (' . $org [ 'id' ] . ').' , ( $org [ 'extend' ] ? 'Organisation (' . $org [ 'id' ] . ') can now extend the sharing group.' : 'Organisation (' . $org [ 'id' ] . ') can no longer extend the sharing group.' ));
2018-07-19 11:48:22 +02:00
} else {
2021-10-20 18:08:42 +02:00
$this -> loadLog () -> createLogEntry ( $user , 'add' , 'SharingGroupOrg' , $this -> id , 'Sharing group (' . $id . '): Added organisation (' . $org [ 'id' ] . ').' , 'Organisation (' . $org [ 'id' ] . ') added to Sharing group.' . ( $org [ 'extend' ] ? ' Organisation (' . $org [ 'id' ] . ') can extend the sharing group.' : '' ));
2018-07-19 11:48:22 +02:00
}
}
}
// We are left with some "old orgs" that are not in the new list. This means that they can be safely deleted.
foreach ( $old_orgs as $old_org ) {
if ( $this -> delete ( $old_org [ 'id' ])) {
2021-10-20 18:08:42 +02:00
$this -> loadLog () -> createLogEntry ( $user , 'delete' , 'SharingGroupOrg' , $old_org [ 'id' ], 'Sharing group (' . $id . '): Removed organisation (' . $old_org [ 'id' ] . ').' , 'Organisation (' . $org [ 'id' ] . ') removed from Sharing group.' );
2018-07-19 11:48:22 +02:00
}
}
}
2016-06-04 01:08:16 +02:00
2020-07-19 16:22:16 +02:00
/**
* Returns sharing groups IDs that are accessible by given organisation ID .
*
* @ param int $org_id
* @ return array
*/
2018-07-19 11:48:22 +02:00
public function fetchAllAuthorised ( $org_id )
{
2020-12-27 15:37:31 +01:00
$sgs = $this -> find ( 'column' , array (
2018-07-19 11:48:22 +02:00
'conditions' => array ( 'org_id' => $org_id ),
2020-12-27 15:37:31 +01:00
'fields' => array ( 'SharingGroupOrg.sharing_group_id' ),
2018-07-19 11:48:22 +02:00
));
2020-12-27 15:37:31 +01:00
return $sgs ;
2018-07-19 11:48:22 +02:00
}
2016-06-04 01:08:16 +02:00
2021-10-20 18:08:42 +02:00
/**
* Pass a sharing group ID and an organisation ID , returns true if it has a matching attached organisation object
* @ param int $id
* @ param int $org_id
* @ return bool
*/
2018-07-19 11:48:22 +02:00
public function checkIfAuthorised ( $id , $org_id )
{
2021-10-10 11:37:36 +02:00
return $this -> hasAny ([
'sharing_group_id' => $id ,
'org_id' => $org_id ,
]);
2018-07-19 11:48:22 +02:00
}
2015-04-07 00:24:44 +02:00
}