realtebo
realtebo

Reputation: 25671

Laravel file cache: can we set file permissions?

I ask you if there is the possibility to add a permission mask to config/cache.php so new cache file are created with 664 and not with 644 as file permission mask.

My shell use is also member of www-data group, but with a 644, shell user cannot delete cache.

Upvotes: 0

Views: 1805

Answers (2)

Nico Rudolf
Nico Rudolf

Reputation: 56

If you are using the file store driver, laravel creates the driver with reading a setting for file permissions from your config here:

protected function createFileDriver(array $config)
{
    return $this->repository(new FileStore($this->app['files'], $config['path'], $config['permission'] ?? null));
}

That means you can specify file permission by adding the key "permission" in config/cache.php like:

    'stores' =>[
        ...
        'file' => [
            'driver' => 'file',
            ...
            'permission' => 0664,
        ],
     ]

Make sure to specify the permission with a leading 0 so it is correctly interpreted. That is as phps chmod function is used internally.

I tested this with laravel 9, but did not check if this behavior is different in older versions.

Upvotes: 4

andychukse
andychukse

Reputation: 592

You can create a new file cache store instance or custom cache driver. Then set your permission.

Laravel File Store Documentation

__construct(Filesystem $files, string $directory, int|null $filePermission = null)

$filePermission accepts int|null

namespace App\Extensions;

use Illuminate\Contracts\Cache\Store;

class CustomStore implements Store
{
  public function __construct(Filesystem $files, string $directory, int|null $filePermission = null) {}
  public function get($key) {}
  public function many(array $keys) {}
  public function put($key, $value, $seconds) {}
  public function putMany(array $values, $seconds) {}
  public function increment($key, $value = 1) {}
  public function decrement($key, $value = 1) {}
  public function forever($key, $value) {}
  public function forget($key) {}
  public function flush() {}
  public function getPrefix() {}
}

After creating a custom cache driver, you can register it see instructions

Upvotes: 1

Related Questions