コード例 #1
0
ファイル: Environment.php プロジェクト: df-arif/df-core
 protected static function getApps()
 {
     if (SessionUtilities::isAuthenticated()) {
         $user = SessionUtilities::user();
         $defaultAppId = $user->default_app_id;
         if (SessionUtilities::isSysAdmin()) {
             $appGroups = AppGroupModel::with(['app_by_app_to_app_group' => function ($q) {
                 $q->whereIsActive(1)->whereNotIn('type', [AppTypes::NONE]);
             }])->get();
             $apps = AppModel::whereIsActive(1)->whereNotIn('type', [AppTypes::NONE])->get();
         } else {
             $userId = $user->id;
             $userAppRoles = UserAppRole::whereUserId($userId)->whereNotNull('role_id')->get(['app_id']);
             $appIds = [];
             foreach ($userAppRoles as $uar) {
                 $appIds[] = $uar->app_id;
             }
             $appIdsString = implode(',', $appIds);
             $appIdsString = empty($appIdsString) ? '-1' : $appIdsString;
             $typeString = implode(',', [AppTypes::NONE]);
             $typeString = empty($typeString) ? '-1' : $typeString;
             $appGroups = AppGroupModel::with(['app_by_app_to_app_group' => function ($q) use($appIdsString, $typeString) {
                 $q->whereRaw("(app.id IN ({$appIdsString}) OR role_id > 0) AND is_active = 1 AND type NOT IN ({$typeString})");
             }])->get();
             $apps = AppModel::whereRaw("(app.id IN ({$appIdsString}) OR role_id > 0) AND is_active = 1 AND type NOT IN ({$typeString})")->get();
         }
     } else {
         $appGroups = AppGroupModel::with(['app_by_app_to_app_group' => function ($q) {
             $q->where('role_id', '>', 0)->whereIsActive(1)->whereNotIn('type', [AppTypes::NONE]);
         }])->get();
         $apps = AppModel::whereIsActive(1)->where('role_id', '>', 0)->whereNotIn('type', [AppTypes::NONE])->get();
     }
     if (empty($defaultAppId)) {
         $systemConfig = SystemConfig::first(['default_app_id']);
         $defaultAppId = !empty($systemConfig) ? $systemConfig->default_app_id : null;
     }
     $inGroups = [];
     $groupedApps = [];
     $noGroupedApps = [];
     foreach ($appGroups as $appGroup) {
         $appArray = $appGroup->getRelation('app_by_app_to_app_group')->toArray();
         if (!empty($appArray)) {
             $appInfo = [];
             foreach ($appArray as $app) {
                 $inGroups[] = $app['id'];
                 $appInfo[] = static::makeAppInfo($app, $defaultAppId);
             }
             $groupedApps[] = ['id' => $appGroup->id, 'name' => $appGroup->name, 'description' => $appGroup->description, 'app' => $appInfo];
         }
     }
     /** @type AppModel $app */
     foreach ($apps as $app) {
         if (!in_array($app->id, $inGroups)) {
             $noGroupedApps[] = static::makeAppInfo($app->toArray(), $defaultAppId);
         }
     }
     return [$groupedApps, $noGroupedApps];
 }
コード例 #2
0
ファイル: User.php プロジェクト: rajeshpillai/df-core
 /**
  * Assigns a role to a user for all apps in the system.
  *
  * @param $user
  * @param $defaultRole
  *
  * @return bool
  * @throws \Exception
  */
 public static function applyDefaultUserAppRole($user, $defaultRole)
 {
     $apps = App::all();
     if (count($apps) === 0) {
         return false;
     }
     foreach ($apps as $app) {
         if (!UserAppRole::whereUserId($user->id)->whereAppId($app->id)->exists()) {
             $userAppRoleData = ['user_id' => $user->id, 'app_id' => $app->id, 'role_id' => $defaultRole];
             UserAppRole::create($userAppRoleData);
         }
     }
     return true;
 }
コード例 #3
0
ファイル: Session.php プロジェクト: df-arif/df-core
 /**
  * Use this primarily in middle-ware or where no session is established yet.
  * Once session is established, the role id is accessible via Session.
  *
  * @param int $app_id
  * @param int $user_id
  *
  * @return null|int The role id or null for admin
  */
 public static function getRoleIdByAppIdAndUserId($app_id, $user_id)
 {
     $appIdUserIdToRoleIdMap = \Cache::get('appIdUserIdToRoleIdMap', []);
     if (isset($appIdUserIdToRoleIdMap[$app_id], $appIdUserIdToRoleIdMap[$app_id][$user_id])) {
         return $appIdUserIdToRoleIdMap[$app_id][$user_id];
     }
     $map = UserAppRole::whereUserId($user_id)->whereAppId($app_id)->first(['role_id']);
     if ($map) {
         $appIdUserIdToRoleIdMap[$app_id][$user_id] = $map->role_id;
         \Cache::put('appIdUserIdToRoleIdMap', $appIdUserIdToRoleIdMap, \Config::get('df.default_cache_ttl'));
         return $map->role_id;
     }
     return null;
 }