200 lines
5.9 KiB
PHP
200 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\Portal\Utils;
|
|
|
|
use App\Business;
|
|
use App\User;
|
|
use App\Utils\ModuleUtil;
|
|
|
|
class PortalUtil
|
|
{
|
|
public const TYPE_CUSTOMER = 'user_customer';
|
|
|
|
public const TYPE_SUPPLIER = 'user_supplier';
|
|
|
|
public const TYPE_MARKETER = 'user_marketer';
|
|
|
|
public const PORTAL_CUSTOMER = 'customer';
|
|
|
|
public const PORTAL_SUPPLIER = 'supplier';
|
|
|
|
public const PORTAL_MARKETER = 'marketer';
|
|
|
|
public static function portalTypes(): array
|
|
{
|
|
return [
|
|
self::TYPE_CUSTOMER,
|
|
self::TYPE_SUPPLIER,
|
|
self::TYPE_MARKETER,
|
|
];
|
|
}
|
|
|
|
public static function portals(): array
|
|
{
|
|
return [
|
|
self::PORTAL_CUSTOMER,
|
|
self::PORTAL_SUPPLIER,
|
|
self::PORTAL_MARKETER,
|
|
];
|
|
}
|
|
|
|
public static function isPortalUser(?User $user): bool
|
|
{
|
|
return $user && in_array($user->user_type, self::portalTypes(), true);
|
|
}
|
|
|
|
public static function homeUrl(?User $user): string
|
|
{
|
|
if (! $user) {
|
|
return '/login';
|
|
}
|
|
|
|
return match ($user->user_type) {
|
|
self::TYPE_CUSTOMER => '/customer/dashboard',
|
|
self::TYPE_SUPPLIER => '/supplier/dashboard',
|
|
self::TYPE_MARKETER => '/marketer/dashboard',
|
|
default => '/home',
|
|
};
|
|
}
|
|
|
|
public static function userTypeForPortal(string $portal): string
|
|
{
|
|
return match ($portal) {
|
|
self::PORTAL_SUPPLIER => self::TYPE_SUPPLIER,
|
|
self::PORTAL_MARKETER => self::TYPE_MARKETER,
|
|
default => self::TYPE_CUSTOMER,
|
|
};
|
|
}
|
|
|
|
public static function portalForUserType(string $userType): ?string
|
|
{
|
|
return match ($userType) {
|
|
self::TYPE_CUSTOMER => self::PORTAL_CUSTOMER,
|
|
self::TYPE_SUPPLIER => self::PORTAL_SUPPLIER,
|
|
self::TYPE_MARKETER => self::PORTAL_MARKETER,
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
public static function userTypeForContact(string $contactType): string
|
|
{
|
|
if (in_array($contactType, ['supplier'], true)) {
|
|
return self::TYPE_SUPPLIER;
|
|
}
|
|
|
|
return self::TYPE_CUSTOMER;
|
|
}
|
|
|
|
public static function portalForContactType(?string $contactType): string
|
|
{
|
|
return $contactType === 'supplier' ? self::PORTAL_SUPPLIER : self::PORTAL_CUSTOMER;
|
|
}
|
|
|
|
public static function subscriptionKey(string $portal): string
|
|
{
|
|
return match ($portal) {
|
|
self::PORTAL_SUPPLIER => 'supplier_portal_module',
|
|
self::PORTAL_MARKETER => 'marketer_portal_module',
|
|
default => 'customer_portal_module',
|
|
};
|
|
}
|
|
|
|
public static function businessSettingKey(string $portal): string
|
|
{
|
|
return 'portal_enable_'.$portal;
|
|
}
|
|
|
|
public static function adminPermission(string $portal): string
|
|
{
|
|
return 'portal.'.$portal.'.access';
|
|
}
|
|
|
|
public static function portalTypeLabel(string $userType): string
|
|
{
|
|
return match ($userType) {
|
|
self::TYPE_CUSTOMER => __('portal::lang.customer_portal'),
|
|
self::TYPE_SUPPLIER => __('portal::lang.supplier_portal'),
|
|
self::TYPE_MARKETER => __('portal::lang.marketer_portal'),
|
|
default => $userType,
|
|
};
|
|
}
|
|
|
|
public static function portalLabel(string $portal): string
|
|
{
|
|
return self::portalTypeLabel(self::userTypeForPortal($portal));
|
|
}
|
|
|
|
public static function hasPortalSubscription(int $businessId, string $portal, ?ModuleUtil $moduleUtil = null): bool
|
|
{
|
|
$moduleUtil ??= app(ModuleUtil::class);
|
|
|
|
$business = Business::find($businessId);
|
|
if ($business) {
|
|
$common = $business->common_settings ?? [];
|
|
$settingKey = self::businessSettingKey($portal);
|
|
if (array_key_exists($settingKey, $common) && empty($common[$settingKey])) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
$subscriptionKey = self::subscriptionKey($portal);
|
|
if ($moduleUtil->hasThePermissionInSubscription($businessId, $subscriptionKey)) {
|
|
return true;
|
|
}
|
|
|
|
if (in_array($portal, [self::PORTAL_CUSTOMER, self::PORTAL_SUPPLIER], true)) {
|
|
return $moduleUtil->hasThePermissionInSubscription($businessId, 'crm_module');
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function assertPortalAccess(User $user, string $portal): void
|
|
{
|
|
if ($user->user_type !== self::userTypeForPortal($portal)) {
|
|
abort(403, __('portal::lang.portal_access_denied'));
|
|
}
|
|
|
|
if (! self::hasPortalSubscription((int) $user->business_id, $portal)) {
|
|
abort(403, __('portal::lang.portal_subscription_required', ['portal' => self::portalLabel($portal)]));
|
|
}
|
|
}
|
|
|
|
public static function loginDeniedMessage(string $portal): string
|
|
{
|
|
return __('portal::lang.portal_login_denied', ['portal' => self::portalLabel($portal)]);
|
|
}
|
|
|
|
public static function adminCanManagePortal(User $user, int $businessId, string $portal): bool
|
|
{
|
|
if ($user->can('superadmin') || $user->can('portal.settings.manage')) {
|
|
return self::hasPortalSubscription($businessId, $portal);
|
|
}
|
|
|
|
if (! self::hasPortalSubscription($businessId, $portal)) {
|
|
return false;
|
|
}
|
|
|
|
return $user->can(self::adminPermission($portal));
|
|
}
|
|
|
|
public static function adminCanManageContactLogin(User $user, int $businessId, ?string $contactType = null): bool
|
|
{
|
|
$portal = self::portalForContactType($contactType);
|
|
|
|
if ($user->can('superadmin') || $user->can('portal.settings.manage')) {
|
|
return self::hasPortalSubscription($businessId, $portal);
|
|
}
|
|
|
|
if (! self::hasPortalSubscription($businessId, $portal)) {
|
|
return false;
|
|
}
|
|
|
|
if ($user->can(self::adminPermission($portal))) {
|
|
return true;
|
|
}
|
|
|
|
return $user->can('crm.access_contact_login');
|
|
}
|
|
}
|