You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.5 KiB

<?php
namespace App\Support\Miniapp;
use App\Models\MiniappUser;
class MiniappUserPermission
{
public const ROLE_NORMAL = 'normal';
public const ROLE_INCUBATION = 'incubation';
public const ROLE_PARTNER = 'partner';
public const ROLE_ADMIN = 'admin';
/**
* @return array{
* role: string,
* role_label: string,
* is_admin: bool,
* is_partner: bool,
* is_incubation: bool,
* can_see_radar: bool,
* can_see_papers: bool,
* can_access_demands: bool,
* can_use_crawler: bool
* }
*/
public static function resolve(MiniappUser $user): array
{
$user->loadMissing([
'teacher:id,is_partner,name',
'adminUser.roles:id,code',
]);
$isAdmin = $user->adminUser && (int) $user->adminUser->status === 1;
$teacherPartner = (bool) ($user->teacher?->is_partner ?? false);
$identityType = $user->identity_type;
if ($isAdmin) {
$role = self::ROLE_ADMIN;
} elseif ($identityType === self::ROLE_PARTNER || $teacherPartner) {
$role = self::ROLE_PARTNER;
} elseif ($identityType === self::ROLE_INCUBATION) {
$role = self::ROLE_INCUBATION;
} else {
$role = self::ROLE_NORMAL;
}
return [
'role' => $role,
'role_label' => self::roleLabel($role),
'is_admin' => $isAdmin,
'is_partner' => $role === self::ROLE_PARTNER,
'is_incubation' => $role === self::ROLE_INCUBATION,
'can_see_radar' => in_array($role, [self::ROLE_ADMIN, self::ROLE_PARTNER], true),
'can_see_papers' => in_array($role, [self::ROLE_ADMIN, self::ROLE_PARTNER], true),
'can_access_demands' => in_array($role, [self::ROLE_ADMIN, self::ROLE_PARTNER, self::ROLE_INCUBATION], true),
'can_use_crawler' => $role === self::ROLE_ADMIN,
];
}
public static function roleLabel(string $role): string
{
return match ($role) {
self::ROLE_ADMIN => '管理员',
self::ROLE_PARTNER => '合作伙伴',
self::ROLE_INCUBATION => '入孵用户',
default => '普通用户',
};
}
public static function identityTypeLabel(?string $identityType): ?string
{
return match ($identityType) {
self::ROLE_INCUBATION => '入孵用户',
self::ROLE_PARTNER => '合作伙伴',
default => null,
};
}
}