refactoring
This commit is contained in:
@@ -3,26 +3,18 @@
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Models\OrganizationModel;
|
||||
use App\Models\OrganizationUserModel;
|
||||
use App\Models\UserModel;
|
||||
use App\Services\AccessService;
|
||||
|
||||
class Organizations extends BaseController
|
||||
{
|
||||
protected OrganizationUserModel $orgUserModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->orgUserModel = new OrganizationUserModel();
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$orgModel = new OrganizationModel();
|
||||
$userId = session()->get('user_id');
|
||||
$userId = $this->getCurrentUserId();
|
||||
|
||||
// Получаем организации пользователя через связующую таблицу
|
||||
$userOrgLinks = $this->orgUserModel->where('user_id', $userId)->findAll();
|
||||
$userOrgLinks = $this->getOrgUserModel()->where('user_id', $userId)->findAll();
|
||||
|
||||
// Нам нужно получить сами данные организаций
|
||||
$orgIds = array_column($userOrgLinks, 'organization_id');
|
||||
@@ -70,7 +62,7 @@ class Organizations extends BaseController
|
||||
|
||||
// Создаем организацию
|
||||
$orgId = $orgModel->insert([
|
||||
'owner_id' => session()->get('user_id'),
|
||||
'owner_id' => $this->getCurrentUserId(),
|
||||
'name' => $this->request->getPost('name'),
|
||||
'type' => 'business',
|
||||
'requisites' => json_encode($requisites),
|
||||
@@ -78,17 +70,17 @@ class Organizations extends BaseController
|
||||
]);
|
||||
|
||||
// Привязываем владельца
|
||||
$this->orgUserModel->insert([
|
||||
$this->getOrgUserModel()->insert([
|
||||
'organization_id' => $orgId,
|
||||
'user_id' => session()->get('user_id'),
|
||||
'user_id' => $this->getCurrentUserId(),
|
||||
'role' => 'owner',
|
||||
'status' => 'active',
|
||||
'joined_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
// Сразу переключаемся на неё
|
||||
session()->set('active_org_id', $orgId);
|
||||
session()->setFlashdata('success', 'Организация успешно создана!');
|
||||
$this->session->set('active_org_id', $orgId);
|
||||
$this->session->setFlashdata('success', 'Организация успешно создана!');
|
||||
|
||||
return redirect()->to('/');
|
||||
}
|
||||
@@ -119,9 +111,9 @@ class Organizations extends BaseController
|
||||
|
||||
// Получаем статистику организации
|
||||
$stats = [
|
||||
'users_total' => $this->orgUserModel->where('organization_id', $orgId)->countAllResults(),
|
||||
'users_active' => $this->orgUserModel->where('organization_id', $orgId)->where('status', 'active')->countAllResults(),
|
||||
'users_blocked' => $this->orgUserModel->where('organization_id', $orgId)->where('status', 'blocked')->countAllResults(),
|
||||
'users_total' => $this->getOrgUserModel()->where('organization_id', $orgId)->countAllResults(),
|
||||
'users_active' => $this->getOrgUserModel()->where('organization_id', $orgId)->where('status', 'active')->countAllResults(),
|
||||
'users_blocked' => $this->getOrgUserModel()->where('organization_id', $orgId)->where('status', 'blocked')->countAllResults(),
|
||||
];
|
||||
|
||||
// Проверяем права для отображения пунктов меню
|
||||
@@ -194,7 +186,7 @@ class Organizations extends BaseController
|
||||
'requisites' => json_encode($newRequisites),
|
||||
]);
|
||||
|
||||
session()->setFlashdata('success', 'Организация успешно обновлена!');
|
||||
$this->session->setFlashdata('success', 'Организация успешно обновлена!');
|
||||
return redirect()->to('/organizations');
|
||||
}
|
||||
|
||||
@@ -232,17 +224,17 @@ class Organizations extends BaseController
|
||||
// Если это POST с подтверждением — удаляем
|
||||
if ($this->request->getMethod() === 'POST') {
|
||||
// Удаляем связи с пользователями через forCurrentOrg()
|
||||
$this->orgUserModel->forCurrentOrg()->delete();
|
||||
$this->getOrgUserModel()->forCurrentOrg()->delete();
|
||||
|
||||
// Мягкое удаление организации
|
||||
$orgModel->delete($orgId);
|
||||
|
||||
// Если удаляли активную организацию — очищаем
|
||||
if (session()->get('active_org_id') == $orgId) {
|
||||
session()->remove('active_org_id');
|
||||
if ($this->session->get('active_org_id') == $orgId) {
|
||||
$this->session->remove('active_org_id');
|
||||
}
|
||||
|
||||
session()->setFlashdata('success', 'Организация "' . $organization['name'] . '" удалена');
|
||||
$this->session->setFlashdata('success', 'Организация "' . $organization['name'] . '" удалена');
|
||||
return redirect()->to('/organizations');
|
||||
}
|
||||
|
||||
@@ -254,11 +246,11 @@ class Organizations extends BaseController
|
||||
|
||||
public function switch($orgId)
|
||||
{
|
||||
$userId = session()->get('user_id');
|
||||
$userId = $this->getCurrentUserId();
|
||||
$orgId = (int) $orgId;
|
||||
|
||||
// Проверяем доступ
|
||||
$membership = $this->orgUserModel
|
||||
$membership = $this->getOrgUserModel()
|
||||
->where('organization_id', $orgId)
|
||||
->where('user_id', $userId)
|
||||
->first();
|
||||
@@ -267,11 +259,11 @@ class Organizations extends BaseController
|
||||
// Сбрасываем кэш AccessService при смене организации
|
||||
$this->access->resetCache();
|
||||
|
||||
session()->set('active_org_id', $orgId);
|
||||
session()->setFlashdata('success', 'Организация изменена');
|
||||
$this->session->set('active_org_id', $orgId);
|
||||
$this->session->setFlashdata('success', 'Организация изменена');
|
||||
return redirect()->to('/');
|
||||
} else {
|
||||
session()->setFlashdata('error', 'Доступ запрещен');
|
||||
$this->session->setFlashdata('error', 'Доступ запрещен');
|
||||
return redirect()->to('/organizations');
|
||||
}
|
||||
}
|
||||
@@ -309,14 +301,14 @@ class Organizations extends BaseController
|
||||
$tableHtml = $this->renderTable($this->getUsersTableConfig($orgId));
|
||||
|
||||
// Получаем данные пользователей для статистики
|
||||
$users = $this->orgUserModel->getOrganizationUsers($orgId);
|
||||
$users = $this->getOrgUserModel()->getOrganizationUsers($orgId);
|
||||
|
||||
return $this->renderTwig('organizations/users', [
|
||||
'organization' => $organization,
|
||||
'organization_id' => $orgId,
|
||||
'tableHtml' => $tableHtml,
|
||||
'users' => $users,
|
||||
'current_user_id' => session()->get('user_id'),
|
||||
'current_user_id' => $this->getCurrentUserId(),
|
||||
'can_manage_users' => $this->access->canManageUsers(),
|
||||
'current_role' => $membership['role'],
|
||||
]);
|
||||
@@ -329,11 +321,11 @@ class Organizations extends BaseController
|
||||
{
|
||||
// Проверяем права для кнопок действий
|
||||
$canManage = $this->access->canManageUsers();
|
||||
|
||||
|
||||
return [
|
||||
'id' => 'users-table',
|
||||
'url' => '/organizations/' . $orgId . '/users/table',
|
||||
'model' => $this->orgUserModel,
|
||||
'model' => $this->getOrgUserModel(),
|
||||
'columns' => [
|
||||
'user_email' => [
|
||||
'label' => 'Пользователь',
|
||||
@@ -495,7 +487,7 @@ class Organizations extends BaseController
|
||||
$orgId,
|
||||
$email,
|
||||
$role,
|
||||
session()->get('user_id')
|
||||
$this->getCurrentUserId()
|
||||
);
|
||||
|
||||
return $this->response->setJSON($result);
|
||||
@@ -519,7 +511,7 @@ class Organizations extends BaseController
|
||||
}
|
||||
|
||||
// Нельзя заблокировать владельца
|
||||
$targetMembership = $this->orgUserModel
|
||||
$targetMembership = $this->getOrgUserModel()
|
||||
->where('organization_id', $orgId)
|
||||
->where('user_id', $userId)
|
||||
->first();
|
||||
@@ -532,9 +524,9 @@ class Organizations extends BaseController
|
||||
return $this->redirectWithError('Нельзя заблокировать владельца', "/organizations/users/{$orgId}");
|
||||
}
|
||||
|
||||
$this->orgUserModel->blockUser($targetMembership['id']);
|
||||
$this->getOrgUserModel()->blockUser($targetMembership['id']);
|
||||
|
||||
session()->setFlashdata('success', 'Пользователь заблокирован');
|
||||
$this->session->setFlashdata('success', 'Пользователь заблокирован');
|
||||
return redirect()->to("/organizations/users/{$orgId}");
|
||||
}
|
||||
|
||||
@@ -551,7 +543,7 @@ class Organizations extends BaseController
|
||||
return $this->redirectWithError('Доступ запрещен', '/organizations');
|
||||
}
|
||||
|
||||
$targetMembership = $this->orgUserModel
|
||||
$targetMembership = $this->getOrgUserModel()
|
||||
->where('organization_id', $orgId)
|
||||
->where('user_id', $userId)
|
||||
->first();
|
||||
@@ -560,9 +552,9 @@ class Organizations extends BaseController
|
||||
return $this->redirectWithError('Пользователь не найден', "/organizations/users/{$orgId}");
|
||||
}
|
||||
|
||||
$this->orgUserModel->unblockUser($targetMembership['id']);
|
||||
$this->getOrgUserModel()->unblockUser($targetMembership['id']);
|
||||
|
||||
session()->setFlashdata('success', 'Пользователь разблокирован');
|
||||
$this->session->setFlashdata('success', 'Пользователь разблокирован');
|
||||
return redirect()->to("/organizations/users/{$orgId}");
|
||||
}
|
||||
|
||||
@@ -584,21 +576,21 @@ class Organizations extends BaseController
|
||||
}
|
||||
|
||||
// Удаляем из организации
|
||||
$this->orgUserModel->delete($membership['id']);
|
||||
$this->getOrgUserModel()->delete($membership['id']);
|
||||
|
||||
// Если это была активная организация - переключаем на другую
|
||||
if (session()->get('active_org_id') == $orgId) {
|
||||
$userId = session()->get('user_id');
|
||||
$otherOrgs = $this->orgUserModel->where('user_id', $userId)->where('status', 'active')->findAll();
|
||||
if ($this->session->get('active_org_id') == $orgId) {
|
||||
$userId = $this->getCurrentUserId();
|
||||
$otherOrgs = $this->getOrgUserModel()->where('user_id', $userId)->where('status', 'active')->findAll();
|
||||
|
||||
if (!empty($otherOrgs)) {
|
||||
session()->set('active_org_id', $otherOrgs[0]['organization_id']);
|
||||
$this->session->set('active_org_id', $otherOrgs[0]['organization_id']);
|
||||
} else {
|
||||
session()->remove('active_org_id');
|
||||
$this->session->remove('active_org_id');
|
||||
}
|
||||
}
|
||||
|
||||
session()->setFlashdata('success', 'Вы покинули организацию');
|
||||
$this->session->setFlashdata('success', 'Вы покинули организацию');
|
||||
return redirect()->to('/organizations');
|
||||
}
|
||||
|
||||
@@ -618,9 +610,9 @@ class Organizations extends BaseController
|
||||
$result = $invitationService->resendInvitation($invitationId, $orgId);
|
||||
|
||||
if ($result['success']) {
|
||||
session()->setFlashdata('success', 'Приглашение отправлено повторно');
|
||||
$this->session->setFlashdata('success', 'Приглашение отправлено повторно');
|
||||
} else {
|
||||
session()->setFlashdata('error', $result['message']);
|
||||
$this->session->setFlashdata('error', $result['message']);
|
||||
}
|
||||
|
||||
return redirect()->to("/organizations/users/{$orgId}");
|
||||
@@ -642,9 +634,9 @@ class Organizations extends BaseController
|
||||
$result = $invitationService->cancelInvitation($invitationId, $orgId);
|
||||
|
||||
if ($result['success']) {
|
||||
session()->setFlashdata('success', 'Приглашение отозвано');
|
||||
$this->session->setFlashdata('success', 'Приглашение отозвано');
|
||||
} else {
|
||||
session()->setFlashdata('error', $result['message']);
|
||||
$this->session->setFlashdata('error', $result['message']);
|
||||
}
|
||||
|
||||
return redirect()->to("/organizations/users/{$orgId}");
|
||||
@@ -669,7 +661,7 @@ class Organizations extends BaseController
|
||||
}
|
||||
|
||||
// Нельзя изменить роль владельца
|
||||
$targetMembership = $this->orgUserModel
|
||||
$targetMembership = $this->getOrgUserModel()
|
||||
->where('organization_id', $orgId)
|
||||
->where('user_id', $userId)
|
||||
->first();
|
||||
@@ -691,11 +683,11 @@ class Organizations extends BaseController
|
||||
return redirect()->back()->withInput()->with('error', 'Недопустимая роль');
|
||||
}
|
||||
|
||||
$this->orgUserModel->update($targetMembership['id'], [
|
||||
$this->getOrgUserModel()->update($targetMembership['id'], [
|
||||
'role' => $newRole,
|
||||
]);
|
||||
|
||||
session()->setFlashdata('success', 'Роль изменена');
|
||||
$this->session->setFlashdata('success', 'Роль изменена');
|
||||
return redirect()->to("/organizations/users/{$orgId}");
|
||||
}
|
||||
|
||||
@@ -717,7 +709,7 @@ class Organizations extends BaseController
|
||||
{
|
||||
$orgId = (int) $orgId;
|
||||
$userId = (int) $userId;
|
||||
$currentUserId = session()->get('user_id');
|
||||
$currentUserId = $this->getCurrentUserId();
|
||||
$membership = $this->getMembership($orgId);
|
||||
|
||||
if (!$membership) {
|
||||
@@ -730,7 +722,7 @@ class Organizations extends BaseController
|
||||
}
|
||||
|
||||
// Нельзя удалить владельца
|
||||
$targetMembership = $this->orgUserModel
|
||||
$targetMembership = $this->getOrgUserModel()
|
||||
->where('organization_id', $orgId)
|
||||
->where('user_id', $userId)
|
||||
->first();
|
||||
@@ -749,51 +741,9 @@ class Organizations extends BaseController
|
||||
}
|
||||
|
||||
// Удаляем пользователя из организации
|
||||
$this->orgUserModel->delete($targetMembership['id']);
|
||||
$this->getOrgUserModel()->delete($targetMembership['id']);
|
||||
|
||||
session()->setFlashdata('success', 'Пользователь удалён из организации');
|
||||
$this->session->setFlashdata('success', 'Пользователь удалён из организации');
|
||||
return redirect()->to("/organizations/users/{$orgId}");
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// Вспомогательные методы
|
||||
// ========================================
|
||||
|
||||
/**
|
||||
* Получение membership пользователя для организации
|
||||
*
|
||||
* @param int $orgId
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getMembership(int $orgId): ?array
|
||||
{
|
||||
$userId = session()->get('user_id');
|
||||
if (!$userId || !$orgId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->orgUserModel
|
||||
->where('organization_id', $orgId)
|
||||
->where('user_id', $userId)
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Редирект с сообщением об ошибке
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $redirectUrl
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function redirectWithError(string $message, string $redirectUrl)
|
||||
{
|
||||
if ($this->request->isAJAX()) {
|
||||
return service('response')
|
||||
->setStatusCode(403)
|
||||
->setJSON(['error' => $message]);
|
||||
}
|
||||
|
||||
session()->setFlashdata('error', $message);
|
||||
return redirect()->to($redirectUrl);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user