Skip to content

Commit

Permalink
Merge branch 'pu/pm/CalendarResourceGrantsViaAdminContainer' into '20…
Browse files Browse the repository at this point in the history
…22.11'

tweak(Calendar) resource grants editable vie admin container

See merge request tine20/tine20!6033
  • Loading branch information
paulmhh committed Oct 8, 2024
2 parents d6f9f01 + 91dd90a commit 012dd47
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 77 deletions.
85 changes: 9 additions & 76 deletions tine20/Calendar/Controller/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,66 +92,6 @@ public static function destroyInstance()
self::$_instance = null;
}

/**
* we don't want the normal admin grant to be set ever
*
* @param Tinebase_Record_RecordSet $_grants
* @return Tinebase_Record_RecordSet
*/
protected function convertToEventGrants(Tinebase_Record_RecordSet $_grants)
{
/** @var Calendar_Model_ResourceGrants $grant */
foreach ($_grants as $grant) {
// unset all default grants
foreach (Tinebase_Model_Grants::getAllGrants() as $key) {
$grant->{$key} = false;
}

// enforce implicit resource grants
if ($grant->{Calendar_Model_ResourceGrants::RESOURCE_ADMIN}) {
$grant->{Calendar_Model_ResourceGrants::RESOURCE_EDIT} = true;
$grant->{Calendar_Model_ResourceGrants::RESOURCE_EXPORT} = true;
$grant->{Calendar_Model_ResourceGrants::RESOURCE_INVITE} = true;
$grant->{Calendar_Model_ResourceGrants::RESOURCE_READ} = true;
$grant->{Calendar_Model_ResourceGrants::RESOURCE_SYNC} = true;
} else {
if ($grant->{Calendar_Model_ResourceGrants::RESOURCE_EDIT}) {
$grant->{Calendar_Model_ResourceGrants::RESOURCE_READ} = true;
}
if ($grant->{Calendar_Model_ResourceGrants::RESOURCE_STATUS}) {
$grant->{Calendar_Model_ResourceGrants::RESOURCE_INVITE} = true;
$grant->{Calendar_Model_ResourceGrants::EVENTS_FREEBUSY} = true;
}
}


// apply event grants
if ($grant->{Calendar_Model_ResourceGrants::EVENTS_ADD}) {
$grant->{Tinebase_Model_Grants::GRANT_ADD} = true;
}
if ($grant->{Calendar_Model_ResourceGrants::EVENTS_DELETE}) {
$grant->{Tinebase_Model_Grants::GRANT_DELETE} = true;
}
if ($grant->{Calendar_Model_ResourceGrants::EVENTS_EDIT}) {
$grant->{Tinebase_Model_Grants::GRANT_EDIT} = true;
}
if ($grant->{Calendar_Model_ResourceGrants::EVENTS_EXPORT}) {
$grant->{Tinebase_Model_Grants::GRANT_EXPORT} = true;
}
if ($grant->{Calendar_Model_ResourceGrants::EVENTS_FREEBUSY}) {
$grant->{Calendar_Model_EventPersonalGrants::GRANT_FREEBUSY} = true;
}
if ($grant->{Calendar_Model_ResourceGrants::EVENTS_READ}) {
$grant->{Tinebase_Model_Grants::GRANT_READ} = true;
}
if ($grant->{Calendar_Model_ResourceGrants::EVENTS_SYNC}) {
$grant->{Tinebase_Model_Grants::GRANT_SYNC} = true;
}
}

return $_grants;
}

/**
* add one record
*
Expand All @@ -172,17 +112,13 @@ public function create(Tinebase_Record_Interface $_record, $_duplicateCheck = tr
try {
$transactionId = Tinebase_TransactionManager::getInstance()->startTransaction($db);

$grants = null;
if (is_array($_record->grants) && !empty($_record->grants)) {
$grants = $this->convertToEventGrants(
new Tinebase_Record_RecordSet(Calendar_Model_ResourceGrants::class, $_record->grants));
} else {
$grants = new Tinebase_Record_RecordSet(Calendar_Model_ResourceGrants::class,
[new Calendar_Model_ResourceGrants([
if (!is_array($_record->grants) || empty($_record->grants)) {
$_record->grants = [[
'account_id' => '0',
'account_type' => Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE,
])]);
]];
}
$grants = new Tinebase_Record_RecordSet(Calendar_Model_ResourceGrants::class, $_record->grants);
unset($_record->grants);

$appId = Tinebase_Application::getInstance()->getApplicationByName($this->_applicationName)->getId();
Expand Down Expand Up @@ -283,17 +219,14 @@ public function update(Tinebase_Record_Interface $_record, $_duplicateCheck = tr
$_record->container_id = $currentRecord->container_id;

// get container
/** @var Tinebase_Model_Container $container */
$container = Tinebase_Container::getInstance()->getContainerById($_record->container_id);
/** @var Tinebase_Model_Container $eventContainer */

if (is_array($_record->grants) && Tinebase_Core::getUser()
$grants = $_record->grants;
unset($_record->grants);
if (is_array($grants) && Tinebase_Core::getUser()
->hasGrant($container, Calendar_Model_ResourceGrants::RESOURCE_ADMIN)) {
$grants = $this->convertToEventGrants(
new Tinebase_Record_RecordSet(Calendar_Model_ResourceGrants::class, $_record->grants));

Tinebase_Container::getInstance()->setGrants($container->getId(), $grants, true, false);
Tinebase_Container::getInstance()->setGrants($container, new Tinebase_Record_RecordSet(Calendar_Model_ResourceGrants::class, $grants), true, false);
}
unset($_record->grants);

$result = parent::update($_record, true, $_updateDeleted);

Expand Down
51 changes: 51 additions & 0 deletions tine20/Calendar/Model/ResourceGrants.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,57 @@ public static function addCustomGetSharedContainerSQL(Zend_Db_Select $_select,
);
}

public function setFromArray(array &$_data)
{
// unset all default grants
foreach (Tinebase_Model_Grants::getAllGrants() as $key) {
$_data[$key] = false;
}

// enforce implicit resource grants
if ($_data[Calendar_Model_ResourceGrants::RESOURCE_ADMIN] ?? false) {
$_data[Calendar_Model_ResourceGrants::RESOURCE_EDIT] = true;
$_data[Calendar_Model_ResourceGrants::RESOURCE_EXPORT] = true;
$_data[Calendar_Model_ResourceGrants::RESOURCE_INVITE] = true;
$_data[Calendar_Model_ResourceGrants::RESOURCE_READ] = true;
$_data[Calendar_Model_ResourceGrants::RESOURCE_SYNC] = true;
} else {
if ($_data[Calendar_Model_ResourceGrants::RESOURCE_EDIT] ?? false) {
$_data[Calendar_Model_ResourceGrants::RESOURCE_READ] = true;
}
if ($_data[Calendar_Model_ResourceGrants::RESOURCE_STATUS] ?? false) {
$_data[Calendar_Model_ResourceGrants::RESOURCE_INVITE] = true;
$_data[Calendar_Model_ResourceGrants::EVENTS_FREEBUSY] = true;
}
}


// apply event grants
if ($_data[Calendar_Model_ResourceGrants::EVENTS_ADD] ?? false) {
$_data[Tinebase_Model_Grants::GRANT_ADD] = true;
}
if ($_data[Calendar_Model_ResourceGrants::EVENTS_DELETE] ?? false) {
$_data[Tinebase_Model_Grants::GRANT_DELETE] = true;
}
if ($_data[Calendar_Model_ResourceGrants::EVENTS_EDIT] ?? false) {
$_data[Tinebase_Model_Grants::GRANT_EDIT] = true;
}
if ($_data[Calendar_Model_ResourceGrants::EVENTS_EXPORT] ?? false) {
$_data[Tinebase_Model_Grants::GRANT_EXPORT] = true;
}
if ($_data[Calendar_Model_ResourceGrants::EVENTS_FREEBUSY] ?? false) {
$_data[Calendar_Model_EventPersonalGrants::GRANT_FREEBUSY] = true;
}
if ($_data[Calendar_Model_ResourceGrants::EVENTS_READ] ?? false) {
$_data[Tinebase_Model_Grants::GRANT_READ] = true;
}
if ($_data[Calendar_Model_ResourceGrants::EVENTS_SYNC] ?? false) {
$_data[Tinebase_Model_Grants::GRANT_SYNC] = true;
}

parent::setFromArray($_data);
}

protected static $_modelConfiguration = null;

/**
Expand Down
18 changes: 18 additions & 0 deletions tine20/Calendar/Setup/Update/15.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Calendar_Setup_Update_15 extends Setup_Update_Abstract
const RELEASE015_UPDATE002 = __CLASS__ . '::update002';
const RELEASE015_UPDATE003 = __CLASS__ . '::update003';
const RELEASE015_UPDATE004 = __CLASS__ . '::update004';
const RELEASE015_UPDATE005 = __CLASS__ . '::update005';

static protected $_allUpdates = [
self::PRIO_NORMAL_APP_UPDATE => [
Expand All @@ -29,6 +30,10 @@ class Calendar_Setup_Update_15 extends Setup_Update_Abstract
self::CLASS_CONST => self::class,
self::FUNCTION_CONST => 'update003',
],
self::RELEASE015_UPDATE005 => [
self::CLASS_CONST => self::class,
self::FUNCTION_CONST => 'update005',
],
],
self::PRIO_NORMAL_APP_STRUCTURE => [
self::RELEASE015_UPDATE001 => [
Expand Down Expand Up @@ -118,4 +123,17 @@ public function update004()
$this->_db->query('UPDATE ' . SQL_TABLE_PREFIX . Calendar_Model_Event::TABLE_NAME . ' SET `status` = "CANCELLED" WHERE `status` = "CANCELED"');
$this->addApplicationUpdate('Calendar', '15.4', self::RELEASE015_UPDATE004);
}

public function update005()
{
Tinebase_Container::getInstance()->doSearchAclFilter(false);
foreach (Tinebase_Container::getInstance()->search(Tinebase_Model_Filter_FilterGroup::getFilterForModel(Tinebase_Model_Container::class, [
['field' => 'application_id', 'operator' => 'equals', 'value' => Tinebase_Application::getInstance()->getApplicationByName(Calendar_Config::APP_NAME)->getId()],
['field' => 'model', 'operator' => 'equals', 'value' => Calendar_Model_Resource::class],
]), null, false, ['id']) as $id) {
$grants = Tinebase_Container::getInstance()->getGrantsOfContainer($id, true);
Tinebase_Container::getInstance()->setGrants($id, $grants, true, false);
}
$this->addApplicationUpdate('Calendar', '15.5', self::RELEASE015_UPDATE005);
}
}
2 changes: 1 addition & 1 deletion tine20/Calendar/Setup/setup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<application>
<name>Calendar</name>
<!-- gettext('Calendar') -->
<version>15.4</version>
<version>15.5</version>
<order>15</order>
<status>enabled</status>
<tables>
Expand Down

0 comments on commit 012dd47

Please sign in to comment.