2013-03-08 13:16:02 +01:00
< ? php
App :: uses ( 'AppModel' , 'Model' );
2016-08-25 11:38:37 +02:00
2013-03-08 13:16:02 +01:00
class Role extends AppModel {
public $validate = array (
2017-07-31 15:21:14 +02:00
'valueNotEmpty' => array (
'rule' => array ( 'valueNotEmpty' ),
),
'name' => array (
'unique' => array (
'rule' => 'isUnique' ,
'message' => 'A role with this name already exists.'
),
2015-10-09 15:59:25 +02:00
'valueNotEmpty' => array (
'rule' => array ( 'valueNotEmpty' ),
2017-07-31 15:21:14 +02:00
),
2013-03-08 13:16:02 +01:00
),
);
public $hasMany = array (
'User' => array (
'className' => 'User' ,
'foreignKey' => 'role_id' ,
'dependent' => false ,
'conditions' => '' ,
'fields' => '' ,
'order' => '' ,
'limit' => '' ,
'offset' => '' ,
'exclusive' => '' ,
'finderQuery' => '' ,
'counterQuery' => ''
)
);
public $actsAs = array (
'Trim' ,
2013-03-07 11:51:43 +01:00
'SysLogLogable.SysLogLogable' => array ( // TODO Audit, logable
'roleModel' => 'Role' ,
'roleKey' => 'role_id' ,
'change' => 'full'
2013-03-08 13:16:02 +01:00
),
);
public $virtualFields = array (
2016-07-11 20:24:03 +02:00
'permission' => " CASE WHEN (Role.perm_add + Role.perm_modify + Role.perm_publish = 3) THEN '3' WHEN (Role.perm_add + Role.perm_modify_org = 2) THEN '2' WHEN (Role.perm_add = 1) THEN '1' ELSE '0' END " ,
2013-03-08 13:16:02 +01:00
);
2017-08-01 11:24:29 +02:00
public $permissionConstants = array (
'read_only' => 0 ,
'manage_own' => 1 ,
'manage_org' => 2 ,
'publish' => 3
);
2015-04-07 14:47:14 +02:00
public $permFlags = array (
2017-02-20 18:43:36 +01:00
'perm_admin' => array ( 'id' => 'RolePermAdmin' , 'text' => 'Admin' , 'readonlyenabled' => false ),
'perm_site_admin' => array ( 'id' => 'RolePermSiteAdmin' , 'text' => 'Site Admin' , 'readonlyenabled' => false ),
2017-03-02 11:11:18 +01:00
'perm_sync' => array ( 'id' => 'RolePermSync' , 'text' => 'Sync Actions' , 'readonlyenabled' => true ),
2017-02-20 18:43:36 +01:00
'perm_audit' => array ( 'id' => 'RolePermAudit' , 'text' => 'Audit Actions' , 'readonlyenabled' => true ),
'perm_auth' => array ( 'id' => 'RolePermAuth' , 'text' => 'Auth key access' , 'readonlyenabled' => true ),
'perm_regexp_access' => array ( 'id' => 'RolePermRegexpAccess' , 'text' => 'Regex Actions' , 'readonlyenabled' => false ),
'perm_tagger' => array ( 'id' => 'RolePermTagger' , 'text' => 'Tagger' , 'readonlyenabled' => false ),
'perm_tag_editor' => array ( 'id' => 'RolePermTagEditor' , 'text' => 'Tag Editor' , 'readonlyenabled' => false ),
'perm_template' => array ( 'id' => 'RolePermTemplate' , 'text' => 'Template Editor' , 'readonlyenabled' => false ),
'perm_sharing_group' => array ( 'id' => 'RolePermSharingGroup' , 'text' => 'Sharing Group Editor' , 'readonlyenabled' => false ),
'perm_delegate' => array ( 'id' => 'RolePermDelegate' , 'text' => 'Delegations Access' , 'readonlyenabled' => false ),
2017-07-02 00:05:15 +02:00
'perm_sighting' => array ( 'id' => 'RolePermSighting' , 'text' => 'Sighting Creator' , 'readonlyenabled' => true ),
2018-03-24 21:43:46 +01:00
'perm_object_template' => array ( 'id' => 'RolePermObjectTemplate' , 'text' => 'Object Template Editor' , 'readonlyenabled' => false )
2015-04-07 14:47:14 +02:00
);
2016-06-04 01:08:16 +02:00
2015-08-31 02:32:37 +02:00
public $premissionLevelName = array ( 'Read Only' , 'Manage Own Events' , 'Manage Organisation Events' , 'Manage and Publish Organisation Events' );
2013-04-25 14:04:08 +02:00
2013-03-08 13:16:02 +01:00
public function beforeSave ( $options = array ()) {
2017-02-20 18:43:36 +01:00
//Conversion from the named data access permission levels
2017-07-31 15:21:14 +02:00
if ( empty ( $this -> data [ 'Role' ][ 'permission' ])) {
$this -> data [ 'Role' ][ 'permission' ] = 0 ;
2017-08-01 11:24:29 +02:00
} else if ( ! is_numeric ( $this -> data [ 'Role' ][ 'permission' ])) {
// If a constant was passed via the API, convert it to the numeric value
// For invalid entries, choose permission level 0
if ( isset ( $this -> permissionConstants [ $this -> data [ 'Role' ][ 'permission' ]])) {
$this -> data [ 'Role' ][ 'permission' ] = $this -> permissionConstants [ $this -> data [ 'Role' ][ 'permission' ]];
} else {
$this -> data [ 'Role' ][ 'permission' ] = 0 ;
}
2017-07-31 15:21:14 +02:00
}
2013-03-08 13:16:02 +01:00
switch ( $this -> data [ 'Role' ][ 'permission' ]) {
case '0' :
2016-07-11 20:22:42 +02:00
$this -> data [ 'Role' ][ 'perm_add' ] = 0 ;
$this -> data [ 'Role' ][ 'perm_modify' ] = 0 ;
$this -> data [ 'Role' ][ 'perm_modify_org' ] = 0 ;
$this -> data [ 'Role' ][ 'perm_publish' ] = 0 ;
2013-03-08 13:16:02 +01:00
break ;
case '1' :
2016-07-11 20:22:42 +02:00
$this -> data [ 'Role' ][ 'perm_add' ] = 1 ;
2017-02-20 18:43:36 +01:00
$this -> data [ 'Role' ][ 'perm_modify' ] = 1 ;
2016-07-11 20:22:42 +02:00
$this -> data [ 'Role' ][ 'perm_modify_org' ] = 0 ;
$this -> data [ 'Role' ][ 'perm_publish' ] = 0 ;
2013-03-08 13:16:02 +01:00
break ;
case '2' :
2016-07-11 20:22:42 +02:00
$this -> data [ 'Role' ][ 'perm_add' ] = 1 ;
$this -> data [ 'Role' ][ 'perm_modify' ] = 1 ;
$this -> data [ 'Role' ][ 'perm_modify_org' ] = 1 ;
$this -> data [ 'Role' ][ 'perm_publish' ] = 0 ;
2013-03-08 13:16:02 +01:00
break ;
case '3' :
2016-07-11 20:22:42 +02:00
$this -> data [ 'Role' ][ 'perm_add' ] = 1 ;
2017-02-20 18:43:36 +01:00
$this -> data [ 'Role' ][ 'perm_modify' ] = 1 ;
$this -> data [ 'Role' ][ 'perm_modify_org' ] = 1 ;
2016-07-11 20:22:42 +02:00
$this -> data [ 'Role' ][ 'perm_publish' ] = 1 ;
2013-03-08 13:16:02 +01:00
break ;
default :
break ;
}
2017-07-31 15:21:14 +02:00
if ( empty ( $this -> data [ 'Role' ][ 'id' ])) {
foreach ( array_keys ( $this -> permFlags ) as $permFlag ) {
if ( ! isset ( $this -> data [ 'Role' ][ $permFlag ])) {
$this -> data [ 'Role' ][ $permFlag ] = 0 ;
}
}
2018-03-24 21:43:46 +01:00
if ( ! isset ( $this -> data [ 'Role' ][ 'max_execution_time' ])) {
$this -> data [ 'Role' ][ 'max_execution_time' ] = '' ;
} else if ( $this -> data [ 'Role' ][ 'max_execution_time' ] !== '' ) {
$this -> data [ 'Role' ][ 'max_execution_time' ] = intval ( $this -> data [ 'Role' ][ 'max_execution_time' ]);
}
if ( ! isset ( $this -> data [ 'Role' ][ 'memory_limit' ])) {
$this -> data [ 'Role' ][ 'memory_limit' ] = '' ;
} else if (
$this -> data [ 'Role' ][ 'memory_limit' ] !== '' &&
! preg_match ( '/^[0-9]+[MG]$/i' , $this -> data [ 'Role' ][ 'memory_limit' ]) &&
$this -> data [ 'Role' ][ 'memory_limit' ] != - 1
) {
$this -> data [ 'Role' ][ 'memory_limit' ] = '' ;
}
2017-07-31 15:21:14 +02:00
}
2013-03-08 13:16:02 +01:00
return true ;
}
2017-07-31 15:21:14 +02:00
public function afterFind ( $results , $primary = false ) {
foreach ( $results as $key => $val ) {
2017-08-01 11:24:29 +02:00
if ( isset ( $results [ $key ][ 'Role' ])) {
unset ( $results [ $key ][ 'Role' ][ 'perm_full' ]);
2017-08-01 11:34:25 +02:00
if ( isset ( $results [ $key ][ 'Role' ][ 'permission' ])) {
$results [ $key ][ 'Role' ][ 'permission_description' ] =
array_flip ( $this -> permissionConstants )[ $results [ $key ][ 'Role' ][ 'permission' ]];
}
2017-08-01 11:24:29 +02:00
}
2017-07-31 15:21:14 +02:00
}
return $results ;
}
2016-06-06 10:09:55 +02:00
}