155 lines
7.9 KiB
PHP
155 lines
7.9 KiB
PHP
<?php
|
|
/**
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Settings;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class Capabilities {
|
|
|
|
/**
|
|
* Capability definitions registry.
|
|
*
|
|
* @return array<string, array{label:string, exclude_roles?:string[], default?:array<string,mixed>}>
|
|
*/
|
|
public static function cap_definitions(): array {
|
|
return [
|
|
'user_report' => ['label' => __('举报其它用户', 'aksrt'), 'default' => ['logged' => true]],
|
|
'balance_transfer' => ['label' => __('余额转账给其他用户', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'points_transfer' => ['label' => __('积分转账给其他用户', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'balance_withdraw' => ['label' => __('余额提现', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_add' => ['label' => __('发布新文章', 'aksrt'), 'default' => ['logged' => true]],
|
|
'new_post_audit_no' => ['label' => __('投稿无需审核直接发布', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_audit_no_manual' => ['label' => __('无需人工审核直接发布', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_upload_video' => ['label' => __('投稿允许上传视频', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_iframe_video' => ['label' => __('投稿允许插入 iframe 视频', 'aksrt'), 'exclude_roles' => ['all'], 'default' => ['logged' => true]],
|
|
'new_post_hide' => ['label' => __('投稿允许发布隐藏内容', 'aksrt'), 'default' => ['logged' => true]],
|
|
'new_post_image_cover' => ['label' => __('发帖允许设置图片封面', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_slide_cover' => ['label' => __('允许设置幻灯片封面', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_video_cover' => ['label' => __('允许设置视频封面', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_pay' => ['label' => __('投稿允许设置付费内容', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_pay_download' => ['label' => __('投稿允许设置付费下载', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'new_post_edit' => ['label' => __('修改自己发布的投稿', 'aksrt'), 'exclude_roles' => ['all'], 'default' => ['logged' => true]],
|
|
'new_post_delete' => ['label' => __('删除自己发布的投稿', 'aksrt'), 'exclude_roles' => ['all']],
|
|
|
|
'comment_view' => ['label' => __('查看评论', 'aksrt'), 'default' => ['all' => true]],
|
|
'comment_add' => ['label' => __('发表评论', 'aksrt'), 'default' => ['logged' => true]],
|
|
'comment_edit' => ['label' => __('修改自己发布的评论', 'aksrt'), 'exclude_roles' => ['all'], 'default' => ['logged' => true]],
|
|
'comment_delete' => ['label' => __('删除自己发布的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_audit_no' => ['label' => __('评论无需审核直接发布', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_audit_no_manual' => ['label' => __('评论无需人工审核', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_set_topping_my_post' => ['label' => __('置顶自己文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_set_topping_other' => ['label' => __('置顶其他文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_edit_my_post' => ['label' => __('修改自己文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_edit_other' => ['label' => __('修改其他文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_audit_my_post' => ['label' => __('审核自己文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_audit_other' => ['label' => __('审核其他文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_delete_my_post' => ['label' => __('删除自己文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'comment_delete_other' => ['label' => __('删除其他文章下的评论', 'aksrt'), 'exclude_roles' => ['all']],
|
|
|
|
'upload_submit_image' => ['label' => __('投稿图片上传', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'upload_comment_image' => ['label' => __('评论图片上传', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'upload_resource_attachment'=> ['label' => __('资源附件上传', 'aksrt'), 'exclude_roles' => ['all']],
|
|
'resource_download' => ['label' => __('资源下载', 'aksrt'), 'default' => ['logged' => true]],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* All role columns in the capability matrix.
|
|
*/
|
|
public static function cap_role_columns(): array {
|
|
return ['all', 'logged', 'level', 'vip', 'auth'];
|
|
}
|
|
|
|
/**
|
|
* Read the user_caps matrix from settings.
|
|
*
|
|
* @return array<string, array<string, mixed>>
|
|
*/
|
|
public static function get_user_capabilities(): array {
|
|
$s = Settings::get_all();
|
|
$stored = is_array($s['user_caps'] ?? null) ? $s['user_caps'] : [];
|
|
$defs = self::cap_definitions();
|
|
$cols = self::cap_role_columns();
|
|
|
|
$result = [];
|
|
foreach ($defs as $cap_id => $def) {
|
|
$defaults = $def['default'] ?? [];
|
|
$excludes = $def['exclude_roles'] ?? [];
|
|
|
|
$row = [];
|
|
foreach ($cols as $col) {
|
|
if (in_array($col, $excludes, true)) {
|
|
$row[$col] = false;
|
|
continue;
|
|
}
|
|
|
|
if (isset($stored[$cap_id][$col])) {
|
|
$val = $stored[$cap_id][$col];
|
|
$row[$col] = in_array($col, ['level', 'vip'], true) ? (int) $val : (bool) $val;
|
|
} else {
|
|
$row[$col] = $defaults[$col] ?? (in_array($col, ['level', 'vip'], true) ? -1 : false);
|
|
}
|
|
}
|
|
$result[$cap_id] = $row;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Resolve whether a user has a specific capability.
|
|
*/
|
|
public static function user_can(string $cap, int $user_id = 0, ?int $context = null): bool {
|
|
if ($user_id > 0 && user_can($user_id, 'manage_options')) {
|
|
return true;
|
|
}
|
|
|
|
$caps = self::get_user_capabilities();
|
|
if (!isset($caps[$cap])) {
|
|
return false;
|
|
}
|
|
|
|
$row = $caps[$cap];
|
|
|
|
if (!empty($row['all'])) {
|
|
return true;
|
|
}
|
|
|
|
if ($user_id <= 0) {
|
|
return false;
|
|
}
|
|
|
|
if (!empty($row['logged'])) {
|
|
return true;
|
|
}
|
|
|
|
$level_threshold = (int) ($row['level'] ?? -1);
|
|
if ($level_threshold >= 0) {
|
|
$user_level = class_exists('\AKSRT\User\Level') ? \AKSRT\User\Level::get_level($user_id) : 0;
|
|
if ($user_level >= $level_threshold) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
$vip_threshold = (int) ($row['vip'] ?? -1);
|
|
if ($vip_threshold >= 0) {
|
|
$user_vip = function_exists('aksrt_get_user_vip_level') ? aksrt_get_user_vip_level($user_id) : 0;
|
|
if ($user_vip >= $vip_threshold) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if (!empty($row['auth'])) {
|
|
if (class_exists('\\AKSRT\\User\\AuthApply') && \AKSRT\User\AuthApply::is_verified($user_id)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|