Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * @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;
     }
 }
Exemplo n.º 3
0
 /**
  * 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;
 }
Exemplo n.º 4
0
 /**
  * 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;
     }
 }
Exemplo n.º 5
0
 /**
  * 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;
     }
 }
Exemplo n.º 6
0
 /**
  * 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;
     });
 }