src/ApplicationBundle/Modules/LeadGen/Service/LeadgenAccess.php line 81

Open in your IDE?
  1. <?php
  2. namespace ApplicationBundle\Modules\LeadGen\Service;
  3. use ApplicationBundle\Modules\Authentication\Constants\UserConstants;
  4. /**
  5.  * LG-B2 — the LeadGen-operator access slice. Today every /leadgen mutation gated on "is this the
  6.  * central box + any authenticated user"; handing every salesperson full super-admin is not on.
  7.  * So two tiers, BOTH still requiring the central box + an authenticated session:
  8.  *
  9.  *   OPERATE (view/segment/enrich/draft/approve/send): a super-admin OR a whitelisted LeadGen
  10.  *           operator (acc_setting `leadgen_operator_user_ids` = comma-separated user ids).
  11.  *   ADMIN   (sender-identity profiles, platform settings): super-admin ONLY.
  12.  *
  13.  * Pure decision helpers (session values + the operator list passed in) so the matrix is
  14.  * self-testable; the controller supplies the live session + acc_setting.
  15.  */
  16. class LeadgenAccess
  17. {
  18.     /** Super-admin = the same posture the CC/super-admin console uses. */
  19.     public static function isSuperAdmin($userType$isBuddybeeAdmin$allModuleAccess)
  20.     {
  21.         $allowedTypes = [
  22.             UserConstants::USER_TYPE_SYSTEM,
  23.             UserConstants::USER_TYPE_MANAGEMENT_USER,
  24.             UserConstants::USER_TYPE_GENERAL,
  25.         ];
  26.         return (int) $isBuddybeeAdmin === 1
  27.             || (int) $allModuleAccess === 1
  28.             || in_array((int) $userType$allowedTypestrue);
  29.     }
  30.     /** Parse the acc_setting operator list ("12, 34,56") → int[]. */
  31.     public static function parseOperatorIds($csv)
  32.     {
  33.         $ids = [];
  34.         foreach (explode(',', (string) $csv) as $part) {
  35.             $n = (int) trim($part);
  36.             if ($n 0) { $ids[] = $n; }
  37.         }
  38.         return $ids;
  39.     }
  40.     /** May this user OPERATE the LeadGen console? (super-admin OR listed operator). Pure. */
  41.     public static function canOperate($userId$userType$isBuddybeeAdmin$allModuleAccess, array $operatorIds)
  42.     {
  43.         if ((int) $userId <= 0) {
  44.             return false;
  45.         }
  46.         if (self::isSuperAdmin($userType$isBuddybeeAdmin$allModuleAccess)) {
  47.             return true;
  48.         }
  49.         return in_array((int) $userId$operatorIdstrue);
  50.     }
  51.     /** May this user ADMIN identities/settings? (super-admin ONLY). Pure. */
  52.     public static function canAdmin($userId$userType$isBuddybeeAdmin$allModuleAccess)
  53.     {
  54.         return (int) $userId && self::isSuperAdmin($userType$isBuddybeeAdmin$allModuleAccess);
  55.     }
  56.     // ── Live wrappers (read the session + the operator-list acc_setting) ────
  57.     /** Operator list from acc_setting `leadgen_operator_user_ids` (guarded → []). */
  58.     public static function operatorIds($em)
  59.     {
  60.         try {
  61.             $row $em->getRepository('ApplicationBundle\\Entity\\AccSettings')
  62.                 ->findOneBy(['name' => 'leadgen_operator_user_ids']);
  63.             return $row !== null self::parseOperatorIds($row->getData()) : [];
  64.         } catch (\Throwable $e) {
  65.             return [];
  66.         }
  67.     }
  68.     public static function sessionCanOperate($request$em)
  69.     {
  70.         $s $request->getSession();
  71.         return self::canOperate(
  72.             $s->get(UserConstants::USER_ID0),
  73.             $s->get(UserConstants::USER_TYPE0),
  74.             $s->get(UserConstants::IS_BUDDYBEE_ADMIN0),
  75.             $s->get(UserConstants::ALL_MODULE_ACCESS_FLAG0),
  76.             self::operatorIds($em)
  77.         );
  78.     }
  79.     public static function sessionCanAdmin($request)
  80.     {
  81.         $s $request->getSession();
  82.         return self::canAdmin(
  83.             $s->get(UserConstants::USER_ID0),
  84.             $s->get(UserConstants::USER_TYPE0),
  85.             $s->get(UserConstants::IS_BUDDYBEE_ADMIN0),
  86.             $s->get(UserConstants::ALL_MODULE_ACCESS_FLAG0)
  87.         );
  88.     }
  89. }