加固房间准入与消息广播边界
This commit is contained in:
+60
-4
@@ -13,7 +13,11 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* 类功能:承载聊天室房间资料与准入规则。
|
||||
*/
|
||||
class Room extends Model
|
||||
{
|
||||
/**
|
||||
@@ -56,31 +60,83 @@ class Room extends Model
|
||||
];
|
||||
}
|
||||
|
||||
// ---- 兼容新版逻辑和 Blade 视图的访问器 ----
|
||||
|
||||
/**
|
||||
* 兼容新版视图访问器:返回房间名称。
|
||||
*/
|
||||
public function getNameAttribute(): string
|
||||
{
|
||||
return $this->room_name ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容新版视图访问器:返回房间介绍。
|
||||
*/
|
||||
public function getDescriptionAttribute(): string
|
||||
{
|
||||
return $this->room_des ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容新版视图访问器:返回房主用户名。
|
||||
*/
|
||||
public function getMasterAttribute(): string
|
||||
{
|
||||
return $this->room_owner ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容新版视图访问器:判断是否系统房间。
|
||||
*/
|
||||
public function getIsSystemAttribute(): bool
|
||||
{
|
||||
return (bool) $this->room_keep;
|
||||
}
|
||||
|
||||
// 同样可为主讲人关联提供便捷方法
|
||||
public function masterUser()
|
||||
/**
|
||||
* 关联房间房主用户。
|
||||
*/
|
||||
public function masterUser(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'room_owner', 'username');
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断指定用户是否允许进入当前房间。
|
||||
*/
|
||||
public function canUserEnter(User $user): bool
|
||||
{
|
||||
if ($this->userCanBypassEntryRestrictions($user)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $this->door_open) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $user->user_level >= (int) ($this->permit_level ?? 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回用户被拒绝进入房间时的中文提示。
|
||||
*/
|
||||
public function entryDeniedMessage(User $user): string
|
||||
{
|
||||
if (! $this->door_open && ! $this->userCanBypassEntryRestrictions($user)) {
|
||||
return '该房间当前已关闭,暂不允许进入。';
|
||||
}
|
||||
|
||||
return '您的等级不足,暂时无法进入该房间。';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断用户是否可绕过房间开放状态与等级限制。
|
||||
*/
|
||||
private function userCanBypassEntryRestrictions(User $user): bool
|
||||
{
|
||||
$superLevel = (int) Sysparam::getValue('superlevel', '100');
|
||||
|
||||
return $user->id === 1
|
||||
|| $user->username === $this->master
|
||||
|| $user->user_level >= $superLevel;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user