Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vcard services for import/export #1996

Merged
merged 45 commits into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
25abfda
import service
asbiin Nov 3, 2018
3804276
Apply fixes from StyleCI
asbiin Nov 3, 2018
6de8b83
wip
asbiin Nov 4, 2018
643f21b
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 4, 2018
6a031f8
Apply fixes from StyleCI
asbiin Nov 4, 2018
c63c9d4
fix psalm
asbiin Nov 4, 2018
0ae4e53
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 4, 2018
e9abca3
Apply fixes from StyleCI
asbiin Nov 4, 2018
99a405e
carddav is a paid feature
asbiin Nov 4, 2018
6f09db4
use middleware
asbiin Nov 4, 2018
57a5ab5
update
asbiin Nov 4, 2018
58958d1
Apply fixes from StyleCI
asbiin Nov 4, 2018
94d65ad
update
asbiin Nov 6, 2018
23d3f24
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 6, 2018
6f1dd9a
Apply fixes from StyleCI
asbiin Nov 6, 2018
ea963fe
fix tests
asbiin Nov 6, 2018
73c7d46
Apply fixes from StyleCI
asbiin Nov 6, 2018
ea58fdc
activate carddav
asbiin Nov 7, 2018
cb51578
revert bad commit
asbiin Nov 7, 2018
a20c412
fix page
asbiin Nov 7, 2018
afce9c6
update
asbiin Nov 7, 2018
c1c4e16
fix
asbiin Nov 7, 2018
939b657
add tests
asbiin Nov 7, 2018
76f7b0b
Apply fixes from StyleCI
asbiin Nov 7, 2018
002261c
Merge remote-tracking branch 'origin/master' into feat/vcard-import-e…
asbiin Nov 7, 2018
733924a
remove jeroendesloovere/vcard
asbiin Nov 7, 2018
7dacde2
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 7, 2018
d7f58b2
add some test
asbiin Nov 7, 2018
fb2fa8f
add tests
asbiin Nov 7, 2018
90529a8
Apply fixes from StyleCI
asbiin Nov 7, 2018
f078de9
update
asbiin Nov 7, 2018
c0212b2
rename addressbook url
asbiin Nov 7, 2018
a8eca02
Apply fixes from StyleCI
asbiin Nov 7, 2018
58ce6f5
updates
asbiin Nov 9, 2018
a1b590d
some improvments
asbiin Nov 10, 2018
89b0dfc
Apply fixes from StyleCI
asbiin Nov 10, 2018
bfcd83c
chore(assets): Update assets
MonicaBot Nov 10, 2018
7d2e7a0
improve
asbiin Nov 10, 2018
8d259d2
Apply fixes from StyleCI
asbiin Nov 10, 2018
d93381a
update functions names
asbiin Nov 10, 2018
f81972f
Apply fixes from StyleCI
asbiin Nov 10, 2018
6e48878
Merge remote-tracking branch 'origin/master' into feat/vcard-import-e…
asbiin Nov 10, 2018
2b47ff1
Merge branch 'feat/vcard-import-export-services' of github.com:monica…
asbiin Nov 10, 2018
d79a6b9
fix tests
asbiin Nov 10, 2018
31df324
Merge remote-tracking branch 'origin/master' into feat/vcard-import-e…
asbiin Nov 11, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions app/Http/Controllers/CardDAV/CardDAVController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace App\Http\Controllers\CardDAV;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use App\Http\Controllers\Controller;
use Sabre\DAV\Server as SabreServer;
use Sabre\DAVACL\Plugin as AclPlugin;
use Sabre\DAVACL\PrincipalCollection;
use Sabre\DAV\Auth\Plugin as AuthPlugin;
use Barryvdh\Debugbar\Facade as Debugbar;
use Sabre\CardDAV\Plugin as CardDAVPlugin;
use App\Models\CardDAV\MonicaAddressBookRoot;
use Sabre\DAV\Browser\Plugin as BrowserPlugin;
use App\Models\CardDAV\Backends\MonicaSabreBackend;
use App\Models\CardDAV\Backends\MonicaCardDAVBackend;
use App\Models\CardDAV\Backends\MonicaPrincipleBackend;

class CardDAVController extends Controller
{
private const BASE_URI = '/carddav/';

/**
* Display the specified resource.
*/
public function init(Request $request)
{
// Disable debugger for caldav output
if (config('app.debug')) {
Debugbar::disable();
}

$server = $this->getServer($request);

$this->addPlugins($server);

return $this->server($server);
}

private function getNodes() : array
{
// Initiate custom backends for link between Sabre and Monica
$principalBackend = new MonicaPrincipleBackend(); // User rights
$carddavBackend = new MonicaCardDAVBackend(); // Contacts

return [
new PrincipalCollection($principalBackend),
new MonicaAddressBookRoot($principalBackend, $carddavBackend),
];
}

private function getServer(Request $request)
{
$nodes = $this->getNodes();

// Initiate Sabre server
$server = new SabreServer($nodes);
$server->sapi = new SapiServerMock();

$server->setBaseUri(self::BASE_URI);
$server->debugExceptions = true;

// Set Url with trailing slash
$server->httpRequest->setUrl($this->fullUrl($request));

if (App::environment('testing')) {
// Testing needs request to be set manually
$server->httpRequest->setMethod($request->method());
$server->httpRequest->setBody($request->getContent(true));
$server->httpRequest->setHeaders($request->headers->all());
}

return $server;
}

/**
* Get the full URL for the request.
*
* @return string
*/
public function fullUrl(Request $request)
{
$query = $request->getQueryString();
$url = str_finish($request->getPathInfo(), '/');

return $query ? $url.'?'.$query : $url;
}

/**
* Add required plugins.
*/
private function addPlugins(SabreServer $server)
{
// Authentication backend
$authBackend = new MonicaSabreBackend();
$server->addPlugin(new AuthPlugin($authBackend, 'SabreDAV'));

// CardDAV plugin
$server->addPlugin(new CardDAVPlugin());

// ACL plugnin
$aclPlugin = new AclPlugin();
$aclPlugin->allowUnauthenticatedAccess = false;
$aclPlugin->hideNodesFromListings = true;
$server->addPlugin($aclPlugin);

// In debug mode add browser plugin
if (App::environment('local')) {
$server->addPlugin(new BrowserPlugin());
}
}

private function server(SabreServer $server)
{
// Execute sabre requests
$server->exec();

// Transform to Laravel response
$content = $server->httpResponse->getBodyAsString();
$status = $server->httpResponse->getStatus();
$headers = $server->httpResponse->getHeaders();

return response($content, $status)
->withHeaders($headers);
}
}
13 changes: 13 additions & 0 deletions app/Http/Controllers/CardDAV/SapiServerMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Http\Controllers\CardDAV;

use Sabre\HTTP\Sapi;
use Sabre\HTTP\ResponseInterface;

class SapiServerMock extends Sapi
{
public static function sendResponse(ResponseInterface $response)
{
}
}
82 changes: 0 additions & 82 deletions app/Http/Controllers/CardDAVController.php

This file was deleted.

1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Kernel extends HttpKernel
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'2fa' => \PragmaRX\Google2FALaravel\Middleware::class,
'u2f' => \Lahaxearnaud\U2f\Http\Middleware\U2f::class,
'limitations' => \App\Http\Middleware\CheckLimitations::class,
'locale' => \App\Http\Middleware\CheckLocale::class,
'sentry.context' => \App\Http\Middleware\SentryContext::class,
'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class,
Expand Down
8 changes: 5 additions & 3 deletions app/Http/Middleware/AuthenticateWithTokenOnBasicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private function authenticate($request)
}

// Try Bearer authentication, with token in 'password' field on basic auth
/*
if (! $request->bearerToken()) {
$password = $request->getPassword();
$request->headers->set('Authorization', 'Bearer '.$password);
Expand All @@ -67,8 +68,9 @@ private function authenticate($request)
if ($user && (! $request->getUser() || $request->getUser() === $user->email)) {
$this->auth->setUser($user);
} else {
// Basic authentication
$this->auth->onceBasic();
}
*/
// Basic authentication
$this->auth->onceBasic();
//}
}
}
25 changes: 25 additions & 0 deletions app/Http/Middleware/CheckLimitations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class CheckLimitations
asbiin marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::check() && auth()->user()->account->hasLimitations()) {
abort(402);
}

return $next($request);
}
}
3 changes: 2 additions & 1 deletion app/Jobs/AddContactFromVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Bus\Queueable;
use App\Models\Account\ImportJob;
use App\Services\VCard\ImportVCard;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -21,7 +22,7 @@ class AddContactFromVCard implements ShouldQueue
*
* @return void
*/
public function __construct(ImportJob $importJob, $behaviour = ImportJob::BEHAVIOUR_ADD)
public function __construct(ImportJob $importJob, $behaviour = ImportVCard::BEHAVIOUR_ADD)
{
$this->importJob = $importJob;
$this->behaviour = $behaviour;
Expand Down
Loading