From 762a1f34301ce63cc6d5e0db14b3f0e94d14019f Mon Sep 17 00:00:00 2001 From: Evann Regnault Date: Thu, 20 Jun 2024 01:29:02 +0200 Subject: [PATCH] Dockerfile --- .dockerignore | 5 +++++ .env.example | 2 +- Dockerfile | 21 +++++++++++++++++++++ composer.json | 14 ++++++-------- docker-compose.yml | 15 +++++++++++++++ index.php | 9 +++++---- src/Collections/HomeCollection.php | 14 +++++++------- src/Collections/RolesCollection.php | 14 +++++++------- 8 files changed, 67 insertions(+), 27 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..49e2fda --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +public +data +vendor +.env +composer.lock \ No newline at end of file diff --git a/.env.example b/.env.example index 95f22f0..d7d13e0 100644 --- a/.env.example +++ b/.env.example @@ -2,7 +2,7 @@ client_id=client client_secret=secret keycloak_token_url=https://keycloak.example.com/auth/realms/master/protocol/openid-connect/token realm=master -users_path=/srv/dav/public +data_path=/srv/dav/public base_uri=/ redis_host=localhost redis_port=6379 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..54c4e8b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM php:8.3-fpm-alpine + +RUN apk add --no-cache php-xml php-curl php-pear + +RUN apk --no-cache add pcre-dev ${PHPIZE_DEPS} \ + && pecl install redis \ + && docker-php-ext-enable redis \ + && apk del pcre-dev ${PHPIZE_DEPS} \ + && rm -rf /tmp/pear + +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + +RUN mkdir /app + +WORKDIR /app + +COPY . . + +RUN composer update + +ENTRYPOINT [ "php", "-S", "0.0.0.0:8080", "index.php" ] \ No newline at end of file diff --git a/composer.json b/composer.json index e32305a..28b40f9 100755 --- a/composer.json +++ b/composer.json @@ -1,20 +1,18 @@ { - "name": "jlucki/docker-php-dev-env", - "description": "A simple web development environment using Docker with NGINX, PHP, MySQL and Xdebug.", + "name": "estym/keycloak-webdav", + "description": "A simple webdav server with a Keycloak integration", "type": "project", "keywords": [ "docker", - "development", - "environment", + "webdav", "php", - "xdebug", - "nginx" + "phpredis" ], "license": "MIT", "authors": [ { - "name": "Jan Lucki", - "email": "jan@lucki.dev" + "name": "Evann (Estym) Regnault", + "email": "contact@regnault.dev" } ], "require": { diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9c8ea80 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +services: + webdav: + build: . + ports: + - 8080:8080 + env_file: + - .env + environment: + - redis_host=redis + - data_path=/data/ + volumes: + - ./public:/data + + redis: + image: redis:7.0.15-alpine diff --git a/index.php b/index.php index 64e2d61..760b9a8 100755 --- a/index.php +++ b/index.php @@ -9,9 +9,10 @@ use Dotenv\Dotenv; // The autoloader require 'vendor/autoload.php'; -$dotenv = Dotenv::createImmutable(__DIR__); -$dotenv->load(); - +if (is_file(".env")){ + $dotenv = Dotenv::createImmutable(__DIR__); + $dotenv->load(); +} $redis_client = new Redis(); $redis_client->connect($_ENV['redis_host'], intval($_ENV['redis_port'])); @@ -23,7 +24,7 @@ $authBackend = new Keycloak\KeycloakAuth($redis_client, $principalBackend,$_ENV[ $authBackend->setRealm($_ENV['realm']); $authPlugin = new DAV\Auth\Plugin($authBackend); -$path = $_ENV['users_path']; +$path = $_ENV['data_path']; // The server object is responsible for making sense out of the WebDAV protocol $server = new DAV\Server([new HomeCollection($authPlugin, $path), new RolesCollection($principalBackend, $path)]); diff --git a/src/Collections/HomeCollection.php b/src/Collections/HomeCollection.php index b63bebd..55322bd 100644 --- a/src/Collections/HomeCollection.php +++ b/src/Collections/HomeCollection.php @@ -9,26 +9,26 @@ use Sabre\DAV\FS\Directory; class HomeCollection extends Collection { private $plugin; - private $userPath; + private $dataPath; - public function __construct(AuthPlugin $authPlugin, string $userPath) + public function __construct(AuthPlugin $authPlugin, string $dataPath) { $this->plugin = $authPlugin; - $this->userPath = $userPath; + $this->dataPath = $dataPath; } public function getChildren(): array { $principal = $this->plugin->getCurrentPrincipal(); $username = explode("/", $principal)[1]; - $path = $this->userPath; + $path = $this->dataPath; - if (!is_dir($path.$username)) { - mkdir($path.$username, 0777 , true); + if (!is_dir($path . '/users/' . $username)) { + mkdir($path . '/users/' . $username, 0777 , true); } - return [new Directory($path.$username, $username)]; + return [new Directory($path . '/users/' . $username, $username)]; } public function getName(): string diff --git a/src/Collections/RolesCollection.php b/src/Collections/RolesCollection.php index 5a8926e..94b40ec 100644 --- a/src/Collections/RolesCollection.php +++ b/src/Collections/RolesCollection.php @@ -9,22 +9,22 @@ use Sabre\DAV\FSExt\Directory as SabreDirectory; class RolesCollection extends Collection { private RolesBackend $principalBackend; - private $dataRoot; + private string $dataPath; - public function __construct(RolesBackend $principal_backend, string $dataRoot) + public function __construct(RolesBackend $principal_backend, string $dataPath) { - $this->$dataRoot = $dataRoot; + $this->dataPath = $dataPath; $this->principalBackend = $principal_backend; } public function getChildren() : array { - $path = $this->dataRoot; + $path = $this->dataPath; $dirs = []; foreach ($this->principalBackend->roles as $role) { - if (!is_dir($path . 'public/' . $role)){ - mkdir($path . 'public/' . $role, 0777, true); + if (!is_dir($path . '/groups/' . $role)){ + mkdir($path . '/groups/' . $role, 0777, true); } - $dirs[] = new SabreDirectory($path . 'public/' . $role, $role); + $dirs[] = new SabreDirectory($path . '/groups/' . $role, $role); } return $dirs;