mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 20:40:49 +08:00
staff message add permission
This commit is contained in:
@@ -2,11 +2,18 @@
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Message;
|
||||
use App\Models\Setting;
|
||||
use App\Models\StaffMessage;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class MessageRepository extends BaseRepository
|
||||
{
|
||||
const STAFF_MESSAGE_TOTAL_CACHE_KEY = 'staff_message_count';
|
||||
|
||||
const STAFF_MESSAGE_NEW_CACHE_KEY = 'staff_new_message_count';
|
||||
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = Message::query();
|
||||
@@ -40,4 +47,48 @@ class MessageRepository extends BaseRepository
|
||||
$result = $model->delete();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function countStaffMessage($uid, $answered = null): int
|
||||
{
|
||||
return self::buildStaffMessageQuery($uid, $answered)->count();
|
||||
}
|
||||
|
||||
public static function buildStaffMessageQuery($uid, $answered = null): \Illuminate\Database\Eloquent\Builder
|
||||
{
|
||||
$query = StaffMessage::query();
|
||||
if ($answered !== null) {
|
||||
$query->where('answered', $answered);
|
||||
}
|
||||
if (!user_can('staffmem', false, $uid)) {
|
||||
//Not staff member only can see authorized
|
||||
$permissions = ToolRepository::listUserAllPermissions($uid);
|
||||
$query->whereIn('permission', $permissions);
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
public static function updateStaffMessageCountCache($uid = 0, $type = '', $value = '')
|
||||
{
|
||||
if ($uid === false) {
|
||||
NexusDB::cache_del(self::STAFF_MESSAGE_NEW_CACHE_KEY);
|
||||
NexusDB::cache_del(self::STAFF_MESSAGE_TOTAL_CACHE_KEY);
|
||||
} else {
|
||||
$redis = NexusDB::redis();
|
||||
match ($type) {
|
||||
'total' => $redis->hSet(self::STAFF_MESSAGE_TOTAL_CACHE_KEY, $uid, $value),
|
||||
'new' => $redis->hSet(self::STAFF_MESSAGE_NEW_CACHE_KEY, $uid, $value),
|
||||
default => throw new \InvalidArgumentException("Invalid type: $type")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static function getStaffMessageCountCache($uid = 0, $type = '')
|
||||
{
|
||||
$redis = NexusDB::redis();
|
||||
return match ($type) {
|
||||
'total' => $redis->hGet(self::STAFF_MESSAGE_TOTAL_CACHE_KEY, $uid),
|
||||
'new' => $redis->hGet(self::STAFF_MESSAGE_NEW_CACHE_KEY, $uid),
|
||||
default => throw new \InvalidArgumentException("Invalid type: $type")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,11 @@ use App\Models\PollAnswer;
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Nexus\Database\NexusDB;
|
||||
use Nexus\Plugin\Plugin;
|
||||
use NexusPlugin\Permission\PermissionRepository;
|
||||
use Symfony\Component\Mailer\Transport\Dsn;
|
||||
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
|
||||
use Symfony\Component\Mailer\Mailer;
|
||||
@@ -360,5 +364,27 @@ class ToolRepository extends BaseRepository
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function listUserClassPermissions($uid): array
|
||||
{
|
||||
$userInfo = get_user_row($uid);
|
||||
$prefix = "authority";
|
||||
$excludes = collect(Setting::$permissionMustHaveClass)->map(fn ($p) => "$prefix.$p")->toArray();
|
||||
return Setting::query()
|
||||
->where("name", "like", "$prefix.%")
|
||||
->whereNotIn('name', $excludes)
|
||||
->where('value', '<=', $userInfo['class'])
|
||||
->where('value', '>=', User::CLASS_PEASANT)
|
||||
->pluck('name')
|
||||
->map(fn ($name) => str_replace("$prefix.", "", $name))
|
||||
->toArray();
|
||||
}
|
||||
|
||||
|
||||
public static function listUserAllPermissions($uid): array
|
||||
{
|
||||
return NexusDB::remember("user_{$uid}_permissions", 600, function () use ($uid) {
|
||||
$classPermissions = self::listUserClassPermissions($uid);
|
||||
return apply_filter('user_permissions', $classPermissions, $uid);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user