Refactor admin/user isolation logic and model methods
Moved admin/user isolation checks and related methods from Domain and NotificationGroup models to User model for better separation of concerns. Replaced direct database queries in controllers and services with new model methods. Added methods for assigning unassigned domains/groups, searching domains, and clearing old notification logs. Updated views for improved UI consistency.
This commit is contained in:
@@ -8,6 +8,14 @@ class NotificationGroup extends Model
|
||||
{
|
||||
protected static string $table = 'notification_groups';
|
||||
|
||||
/**
|
||||
* Get User model instance
|
||||
*/
|
||||
private function getUserModel(): \App\Models\User
|
||||
{
|
||||
return new \App\Models\User();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all groups with channel count
|
||||
*/
|
||||
@@ -20,7 +28,7 @@ class NotificationGroup extends Model
|
||||
LEFT JOIN notification_channels nc ON ng.id = nc.notification_group_id
|
||||
LEFT JOIN domains d ON ng.id = d.notification_group_id";
|
||||
|
||||
if ($userId && !$this->isAdmin($userId)) {
|
||||
if ($userId && !$this->getUserModel()->isAdmin($userId)) {
|
||||
$sql .= " WHERE ng.user_id = ?";
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt->execute([$userId]);
|
||||
@@ -44,7 +52,7 @@ class NotificationGroup extends Model
|
||||
}
|
||||
|
||||
// Check if user has access to this group
|
||||
if ($userId && !$this->isAdmin($userId) && $group['user_id'] != $userId) {
|
||||
if ($userId && !$this->getUserModel()->isAdmin($userId) && $group['user_id'] != $userId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -54,7 +62,7 @@ class NotificationGroup extends Model
|
||||
|
||||
// Get domains (filtered by user if needed)
|
||||
$domainModel = new Domain();
|
||||
if ($userId && !$this->isAdmin($userId)) {
|
||||
if ($userId && !$this->getUserModel()->isAdmin($userId)) {
|
||||
$group['domains'] = $domainModel->where('notification_group_id', $id, $userId);
|
||||
} else {
|
||||
$group['domains'] = $domainModel->where('notification_group_id', $id);
|
||||
@@ -77,28 +85,15 @@ class NotificationGroup extends Model
|
||||
return $this->delete($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user is admin
|
||||
*/
|
||||
private function isAdmin(?int $userId): bool
|
||||
{
|
||||
if (!$userId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$stmt = $this->db->prepare("SELECT role FROM users WHERE id = ?");
|
||||
$stmt->execute([$userId]);
|
||||
$user = $stmt->fetch();
|
||||
return $user && $user['role'] === 'admin';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get first admin user
|
||||
* Assign all notification groups without user_id to a specific user
|
||||
*/
|
||||
public function getFirstAdminUser(): ?array
|
||||
public function assignUnassignedGroupsToUser(int $userId): int
|
||||
{
|
||||
$stmt = $this->db->query("SELECT * FROM users WHERE role = 'admin' ORDER BY id ASC LIMIT 1");
|
||||
return $stmt->fetch() ?: null;
|
||||
$stmt = $this->db->prepare("UPDATE notification_groups SET user_id = ? WHERE user_id IS NULL");
|
||||
$stmt->execute([$userId]);
|
||||
return $stmt->rowCount();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user