/** * @param array $schema */ protected static function prepareConfigSchemaField(array &$schema) { parent::prepareConfigSchemaField($schema); switch ($schema['name']) { case 'public_path': $schema['type'] = 'array'; $schema['items'] = 'string'; $schema['description'] = 'An array of paths to make public.' . ' All folders and files under these paths will be available as public but read-only via the server\'s URL.'; break; case 'container': $values = []; $defaultDiskName = \Config::get('filesystems.default'); $disks = \Config::get('filesystems.disks'); foreach ($disks as $disk) { $default = false; if ($defaultDiskName === $disk['driver']) { $default = true; } $values[] = ['name' => $disk['driver'], 'label' => $disk['driver'], 'default' => $default]; } $schema['type'] = 'picklist'; $schema['description'] = 'Select a disk configuration to use for local file service.'; $schema['values'] = $values; break; } }
/** * Returns role info cached, or reads from db if not present. * Pass in a key to return a portion/index of the cached data. * * @param int $id * @param null|string $key * @param null $default * * @return mixed|null */ public static function getCachedInfo($id, $key = null, $default = null) { $cacheKey = 'role:' . $id; try { $result = \Cache::remember($cacheKey, \Config::get('df.default_cache_ttl'), function () use($id) { $role = Role::with(['role_lookup_by_role_id', 'role_service_access_by_role_id', 'service_by_role_service_access'])->whereId($id)->first(); if (empty($role)) { throw new NotFoundException("Role not found."); } if (!$role->is_active) { throw new ForbiddenException("Role is not active."); } $roleInfo = $role->toArray(); $services = ArrayUtils::get($roleInfo, 'service_by_role_service_access'); unset($roleInfo['service_by_role_service_access']); foreach ($roleInfo['role_service_access_by_role_id'] as $key => $value) { $serviceName = ArrayUtils::findByKeyValue($services, 'id', ArrayUtils::get($value, 'service_id'), 'name'); $component = ArrayUtils::get($value, 'component'); $roleInfo['role_service_access_by_role_id'][$key]['service'] = $serviceName; $roleInfo['role_service_access_by_role_id'][$key]['component'] = trim($component, '/'); } return $roleInfo; }); if (is_null($result)) { return $default; } } catch (ModelNotFoundException $ex) { return $default; } if (is_null($key)) { return $result; } return isset($result[$key]) ? $result[$key] : $default; }
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]; }
/** * Returns system lookups cached, or reads from db if not present. * Pass in a key to return a portion/index of the cached data. * * @param null|string $key * @param null $default * * @return mixed|null */ public static function getCachedLookups($key = null, $default = null) { $cacheKey = 'system_lookups'; try { $result = \Cache::remember($cacheKey, \Config::get('df.default_cache_ttl'), function () { return Lookup::all()->toArray(); }); if (is_null($result)) { return $default; } } catch (ModelNotFoundException $ex) { return $default; } if (is_null($key)) { return $result; } return isset($result[$key]) ? $result[$key] : $default; }
/** * Creates first admin user. * * @param array $data * * @return User|boolean */ public static function createFirstAdmin(array &$data) { $validationRules = ['name' => 'required|max:255', 'first_name' => 'required|max:255', 'last_name' => 'required|max:255', 'email' => 'required|email|max:255|unique:user', 'password' => 'required|confirmed|min:6']; $validator = Validator::make($data, $validationRules); if ($validator->fails()) { $errors = $validator->getMessageBag()->all(); $data = array_merge($data, ['errors' => $errors, 'version' => \Config::get('df.version')]); return false; } else { /** @type User $user */ $attributes = array_only($data, ['name', 'first_name', 'last_name', 'email']); $attributes['is_active'] = 1; $user = static::create($attributes); $user->password = ArrayUtils::get($data, 'password'); $user->is_sys_admin = 1; $user->save(); // Register user RegisterContact::registerUser($user); // Reset admin_exists flag in cache. \Cache::forever('admin_exists', true); return $user; } }
/** * Use this primarily in middle-ware or where no session is established yet. * * @param string $api_key * * @return int The app id */ public static function getAppIdByApiKey($api_key) { $cacheKey = 'apikey2appid:' . $api_key; try { return \Cache::remember($cacheKey, \Config::get('df.default_cache_ttl'), function () use($api_key) { return App::whereApiKey($api_key)->firstOrFail()->id; }); } catch (ModelNotFoundException $ex) { return null; } }
/** * Returns service name cached, or reads from db if not present. * Pass in a key to return a portion/index of the cached data. * * @param int $id * * @return string|null */ public static function getCachedNameById($id) { $cacheKey = 'service_id:' . $id; return \Cache::remember($cacheKey, \Config::get('df.default_cache_ttl'), function () use($id) { $service = static::whereId($id)->first(['name']); if (empty($service)) { throw new NotFoundException("Could not find a service for id {$id}"); } return $service->name; }); }