<?php
namespace ApplicationBundle\Modules\LeadGen\Service;
use ApplicationBundle\Modules\Authentication\Constants\UserConstants;
/**
* LG-B2 — the LeadGen-operator access slice. Today every /leadgen mutation gated on "is this the
* central box + any authenticated user"; handing every salesperson full super-admin is not on.
* So two tiers, BOTH still requiring the central box + an authenticated session:
*
* OPERATE (view/segment/enrich/draft/approve/send): a super-admin OR a whitelisted LeadGen
* operator (acc_setting `leadgen_operator_user_ids` = comma-separated user ids).
* ADMIN (sender-identity profiles, platform settings): super-admin ONLY.
*
* Pure decision helpers (session values + the operator list passed in) so the matrix is
* self-testable; the controller supplies the live session + acc_setting.
*/
class LeadgenAccess
{
/** Super-admin = the same posture the CC/super-admin console uses. */
public static function isSuperAdmin($userType, $isBuddybeeAdmin, $allModuleAccess)
{
$allowedTypes = [
UserConstants::USER_TYPE_SYSTEM,
UserConstants::USER_TYPE_MANAGEMENT_USER,
UserConstants::USER_TYPE_GENERAL,
];
return (int) $isBuddybeeAdmin === 1
|| (int) $allModuleAccess === 1
|| in_array((int) $userType, $allowedTypes, true);
}
/** Parse the acc_setting operator list ("12, 34,56") → int[]. */
public static function parseOperatorIds($csv)
{
$ids = [];
foreach (explode(',', (string) $csv) as $part) {
$n = (int) trim($part);
if ($n > 0) { $ids[] = $n; }
}
return $ids;
}
/** May this user OPERATE the LeadGen console? (super-admin OR listed operator). Pure. */
public static function canOperate($userId, $userType, $isBuddybeeAdmin, $allModuleAccess, array $operatorIds)
{
if ((int) $userId <= 0) {
return false;
}
if (self::isSuperAdmin($userType, $isBuddybeeAdmin, $allModuleAccess)) {
return true;
}
return in_array((int) $userId, $operatorIds, true);
}
/** May this user ADMIN identities/settings? (super-admin ONLY). Pure. */
public static function canAdmin($userId, $userType, $isBuddybeeAdmin, $allModuleAccess)
{
return (int) $userId > 0 && self::isSuperAdmin($userType, $isBuddybeeAdmin, $allModuleAccess);
}
// ── Live wrappers (read the session + the operator-list acc_setting) ────
/** Operator list from acc_setting `leadgen_operator_user_ids` (guarded → []). */
public static function operatorIds($em)
{
try {
$row = $em->getRepository('ApplicationBundle\\Entity\\AccSettings')
->findOneBy(['name' => 'leadgen_operator_user_ids']);
return $row !== null ? self::parseOperatorIds($row->getData()) : [];
} catch (\Throwable $e) {
return [];
}
}
public static function sessionCanOperate($request, $em)
{
$s = $request->getSession();
return self::canOperate(
$s->get(UserConstants::USER_ID, 0),
$s->get(UserConstants::USER_TYPE, 0),
$s->get(UserConstants::IS_BUDDYBEE_ADMIN, 0),
$s->get(UserConstants::ALL_MODULE_ACCESS_FLAG, 0),
self::operatorIds($em)
);
}
public static function sessionCanAdmin($request)
{
$s = $request->getSession();
return self::canAdmin(
$s->get(UserConstants::USER_ID, 0),
$s->get(UserConstants::USER_TYPE, 0),
$s->get(UserConstants::IS_BUDDYBEE_ADMIN, 0),
$s->get(UserConstants::ALL_MODULE_ACCESS_FLAG, 0)
);
}
}