/
mikopbx
/
ModuleUsersGroups
Обзор
Документация
Войти
/
mikopbx
/
ModuleUsersGroups
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
v1.76
Models/UsersGroups.php
161 строка
4 KB
boffart
fix: prevent loss of group members and outbound rules on module disable (#34)
25 июн 2026, 10:07
25 июн 2026, 10:07
d9ff895
Код
Авторство
О чём код?
<?php /* * MikoPBX - free phone system for small business * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program. * If not, see <https://www.gnu.org/licenses/>. */ namespace Modules\ModuleUsersGroups\Models; use MikoPBX\Modules\Models\ModulesModelsBase; use Phalcon\Mvc\Model\Relation; class UsersGroups extends ModulesModelsBase { /** * @Primary * @Identity * @Column(type="integer", nullable=false) */ public $id; /** * Group name * * @Column(type="string", nullable=true) */ public $name; /** * Group description * * @Column(type="string", nullable=true) */ public $description; /** * Group patterns * * @Column(type="string", nullable=true) */ public $patterns; /** * Group isolate * * @Column(type="string", nullable=true) */ public $isolate; /** * Group isolatePickUp * * @Column(type="string", nullable=true) */ public $isolatePickUp; /** * There is default group * * @Column(type="string", nullable=true) */ public $defaultGroup; public function initialize(): void { $this->setSource('m_ModuleUsersGroups_UsersGroups'); parent::initialize(); // NOTE: these relations intentionally use NO_ACTION instead of ACTION_CASCADE. // // On module disable MikoPBX runs a "probe" (PbxExtensionState::makeBeforeDisableTest) // that calls beforeDelete() on every module record. For ACTION_CASCADE relations that // triggers a real cascade delete of the related rows. The probe wraps everything in a // transaction and rolls it back, but the rollback runs on the MAIN database connection // while this module stores its tables in a SEPARATE database file — so the cascade // deletes are never rolled back and all group members and outbound-rule links are lost // on every disable/enable cycle (issue #34). // // Cleanup of child records on real group deletion is therefore handled explicitly in // ModuleUsersGroupsController::deleteAction() instead of relying on the ORM cascade. $this->hasMany( 'id', GroupMembers::class, 'group_id', [ 'alias' => 'GroupMembers', 'foreignKey' => [ 'allowNulls' => true, 'action' => Relation::NO_ACTION, ], ] ); $this->hasMany( 'id', AllowedOutboundRules::class, 'group_id', [ 'alias' => 'AllowedOutboundRules', 'foreignKey' => [ 'allowNulls' => true, 'action' => Relation::NO_ACTION, ], ] ); } /** * Get user's group by user_id * * @param int|string $userId User ID from Users table * @return int|null Group ID or null if not found */ public static function getUserGroupId($userId): ?int { if (empty($userId)) { return null; } // Find user's group membership $groupMember = GroupMembers::findFirst([ 'conditions' => 'user_id = :user_id:', 'bind' => ['user_id' => $userId] ]); if ($groupMember !== null) { return (int)$groupMember->group_id; } // Return default group if user has no group assigned $defaultGroup = self::findFirst('defaultGroup=1'); if ($defaultGroup) { return (int)$defaultGroup->id; } return null; } /** * Get default group * * @return UsersGroups|null */ public static function getDefaultGroup(): ?UsersGroups { return self::findFirst('defaultGroup=1'); } }