diff --git a/.editorconfig b/.editorconfig index 43357f8..b599f35 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,6 +3,7 @@ root = true [*] end_of_line = lf insert_final_newline = true +trim_trailing_whitespace = true charset = utf-8 ## Why tabs? diff --git a/composer.json b/composer.json index afce8a6..304ddf0 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,9 @@ "kint-php/kint": "dev-master", "filp/whoops": "^2.6@dev", "composer/composer": "^1.10@dev", - "guzzlehttp/guzzle": "^7.0@dev" + "guzzlehttp/guzzle": "^7.0@dev", + "illuminate/support": "^7.0@dev", + "illuminate/filesystem": "^7.0@dev" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 0e83497..3401a35 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e3552987835f60bf0cbcb9d8ba20ba25", + "content-hash": "7c10ed1f9d18614a8998ba652c6f1efa", "packages": [ { "name": "composer/ca-bundle", @@ -68,12 +68,12 @@ "source": { "type": "git", "url": "https://github.com/composer/composer.git", - "reference": "dbcf592ea22553d90cf27e81501fe279345473f6" + "reference": "88cff792cc6f2fa4df736e9abb612b955b783087" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/composer/zipball/dbcf592ea22553d90cf27e81501fe279345473f6", - "reference": "dbcf592ea22553d90cf27e81501fe279345473f6", + "url": "https://api.github.com/repos/composer/composer/zipball/88cff792cc6f2fa4df736e9abb612b955b783087", + "reference": "88cff792cc6f2fa4df736e9abb612b955b783087", "shasum": "" }, "require": { @@ -140,7 +140,7 @@ "dependency", "package" ], - "time": "2020-01-14T15:33:39+00:00" + "time": "2020-01-18T12:54:01+00:00" }, { "name": "composer/semver", @@ -307,6 +307,73 @@ ], "time": "2019-11-06T16:40:04+00:00" }, + { + "name": "doctrine/inflector", + "version": "1.3.x-dev", + "source": { + "type": "git", + "url": "https://github.com/doctrine/inflector.git", + "reference": "ec3a55242203ffa6a4b27c58176da97ff0a7aec1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/inflector/zipball/ec3a55242203ffa6a4b27c58176da97ff0a7aec1", + "reference": "ec3a55242203ffa6a4b27c58176da97ff0a7aec1", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^6.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\Inflector\\": "lib/Doctrine/Common/Inflector" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Common String Manipulations with regard to casing and singular/plural rules.", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "inflection", + "pluralize", + "singularize", + "string" + ], + "time": "2019-10-30T19:59:35+00:00" + }, { "name": "filp/whoops", "version": "2.7.1", @@ -374,12 +441,12 @@ "source": { "type": "git", "url": "https://github.com/guzzle/guzzle.git", - "reference": "2c1455e8dd44c4cea97e7fcc8969dc7815a23eae" + "reference": "192d1f1945e63239b45abfcfd606556f1673ab79" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/guzzle/zipball/2c1455e8dd44c4cea97e7fcc8969dc7815a23eae", - "reference": "2c1455e8dd44c4cea97e7fcc8969dc7815a23eae", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/192d1f1945e63239b45abfcfd606556f1673ab79", + "reference": "192d1f1945e63239b45abfcfd606556f1673ab79", "shasum": "" }, "require": { @@ -397,6 +464,7 @@ "psr/log": "^1.1" }, "suggest": { + "ext-curl": "Required for CURL handler support", "ext-intl": "Required for Internationalized Domain Name (IDN) support", "psr/log": "Required for using the Log middleware" }, @@ -441,7 +509,7 @@ "rest", "web service" ], - "time": "2020-01-16T13:29:20+00:00" + "time": "2020-01-19T19:33:25+00:00" }, { "name": "guzzlehttp/promises", @@ -565,6 +633,164 @@ ], "time": "2019-12-21T15:35:40+00:00" }, + { + "name": "illuminate/contracts", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/illuminate/contracts.git", + "reference": "05a204408fb1bb872e0d0c5fcf6d73fe2f32d815" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/contracts/zipball/05a204408fb1bb872e0d0c5fcf6d73fe2f32d815", + "reference": "05a204408fb1bb872e0d0c5fcf6d73fe2f32d815", + "shasum": "" + }, + "require": { + "php": "^7.2.5", + "psr/container": "^1.0", + "psr/simple-cache": "^1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "7.x-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Contracts\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Contracts package.", + "homepage": "https://laravel.com", + "time": "2020-01-08T21:11:01+00:00" + }, + { + "name": "illuminate/filesystem", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/illuminate/filesystem.git", + "reference": "a72a740e320d3e1572f9c265791146b4195c0a42" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/filesystem/zipball/a72a740e320d3e1572f9c265791146b4195c0a42", + "reference": "a72a740e320d3e1572f9c265791146b4195c0a42", + "shasum": "" + }, + "require": { + "illuminate/contracts": "^7.0", + "illuminate/support": "^7.0", + "php": "^7.2.5", + "symfony/finder": "^5.0" + }, + "suggest": { + "league/flysystem": "Required to use the Flysystem local and FTP drivers (^1.0).", + "league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^1.0).", + "league/flysystem-cached-adapter": "Required to use the Flysystem cache (^1.0).", + "league/flysystem-sftp": "Required to use the Flysystem SFTP driver (^1.0).", + "psr/http-message": "Required to allow Storage::put to accept a StreamInterface (^1.0)" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "7.x-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Filesystem\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Filesystem package.", + "homepage": "https://laravel.com", + "time": "2020-01-22T15:13:53+00:00" + }, + { + "name": "illuminate/support", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/illuminate/support.git", + "reference": "aa0c7a842a7b42c91098a14e9ca932eaf16e8a93" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/support/zipball/aa0c7a842a7b42c91098a14e9ca932eaf16e8a93", + "reference": "aa0c7a842a7b42c91098a14e9ca932eaf16e8a93", + "shasum": "" + }, + "require": { + "doctrine/inflector": "^1.1", + "ext-json": "*", + "ext-mbstring": "*", + "illuminate/contracts": "^7.0", + "nesbot/carbon": "^2.17", + "php": "^7.2.5", + "voku/portable-ascii": "^1.2.3" + }, + "conflict": { + "tightenco/collect": "<5.5.33" + }, + "suggest": { + "illuminate/filesystem": "Required to use the composer class (^7.0).", + "moontoast/math": "Required to use ordered UUIDs (^1.1).", + "ramsey/uuid": "Required to use Str::uuid() (^3.7).", + "symfony/process": "Required to use the composer class (^5.0).", + "symfony/var-dumper": "Required to use the dd function (^5.0).", + "vlucas/phpdotenv": "Required to use the Env class and env helper (^4.0)." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "7.x-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Support\\": "" + }, + "files": [ + "helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Support package.", + "homepage": "https://laravel.com", + "time": "2020-01-16T23:05:47+00:00" + }, { "name": "justinrainbow/json-schema", "version": "5.2.9", @@ -701,6 +927,76 @@ ], "time": "2020-01-14T20:03:41+00:00" }, + { + "name": "nesbot/carbon", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/briannesbitt/Carbon.git", + "reference": "e509be5bf2d703390e69e14496d9a1168452b0a2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/e509be5bf2d703390e69e14496d9a1168452b0a2", + "reference": "e509be5bf2d703390e69e14496d9a1168452b0a2", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": "^7.1.8 || ^8.0", + "symfony/translation": "^3.4 || ^4.0 || ^5.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.14 || ^3.0", + "kylekatarnls/multi-tester": "^1.1", + "phpmd/phpmd": "^2.8", + "phpstan/phpstan": "^0.11", + "phpunit/phpunit": "^7.5 || ^8.0", + "squizlabs/php_codesniffer": "^3.4" + }, + "bin": [ + "bin/carbon" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.x-dev" + }, + "laravel": { + "providers": [ + "Carbon\\Laravel\\ServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Carbon\\": "src/Carbon/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Brian Nesbitt", + "email": "brian@nesbot.com", + "homepage": "http://nesbot.com" + }, + { + "name": "kylekatarnls", + "homepage": "http://github.com/kylekatarnls" + } + ], + "description": "An API extension for DateTime that supports 281 different languages.", + "homepage": "http://carbon.nesbot.com", + "keywords": [ + "date", + "datetime", + "time" + ], + "time": "2020-01-21T09:36:43+00:00" + }, { "name": "phpoption/phpoption", "version": "dev-master", @@ -951,6 +1247,54 @@ ], "time": "2019-11-12T16:45:05+00:00" }, + { + "name": "psr/simple-cache", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "time": "2017-10-23T01:57:42+00:00" + }, { "name": "ralouphie/getallheaders", "version": "3.0.3", @@ -1090,12 +1434,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "2f6d80f531af0f6c3f3ad53e5ed8aab0486e50cd" + "reference": "b71643142b48c7470ed071374f8ad138d09b00b2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/2f6d80f531af0f6c3f3ad53e5ed8aab0486e50cd", - "reference": "2f6d80f531af0f6c3f3ad53e5ed8aab0486e50cd", + "url": "https://api.github.com/repos/symfony/console/zipball/b71643142b48c7470ed071374f8ad138d09b00b2", + "reference": "b71643142b48c7470ed071374f8ad138d09b00b2", "shasum": "" }, "require": { @@ -1158,7 +1502,7 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2020-01-10T21:57:55+00:00" + "time": "2020-01-21T07:39:56+00:00" }, { "name": "symfony/filesystem", @@ -1166,12 +1510,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "c2e47941544b6efedc551f7cf3e9b1202ccbbf36" + "reference": "a75a71f1267bf3779b41b659dc0ba3859a1271de" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/c2e47941544b6efedc551f7cf3e9b1202ccbbf36", - "reference": "c2e47941544b6efedc551f7cf3e9b1202ccbbf36", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/a75a71f1267bf3779b41b659dc0ba3859a1271de", + "reference": "a75a71f1267bf3779b41b659dc0ba3859a1271de", "shasum": "" }, "require": { @@ -1208,7 +1552,7 @@ ], "description": "Symfony Filesystem Component", "homepage": "https://symfony.com", - "time": "2020-01-04T14:20:45+00:00" + "time": "2020-01-21T08:44:52+00:00" }, { "name": "symfony/finder", @@ -1541,6 +1885,140 @@ ], "time": "2020-01-04T14:08:26+00:00" }, + { + "name": "symfony/translation", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/symfony/translation.git", + "reference": "94ea74a34015e4b6961b79d78534cd91da9cdd07" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/translation/zipball/94ea74a34015e4b6961b79d78534cd91da9cdd07", + "reference": "94ea74a34015e4b6961b79d78534cd91da9cdd07", + "shasum": "" + }, + "require": { + "php": "^7.2.5", + "symfony/polyfill-mbstring": "~1.0", + "symfony/translation-contracts": "^2" + }, + "conflict": { + "symfony/config": "<4.4", + "symfony/dependency-injection": "<5.0", + "symfony/http-kernel": "<5.0", + "symfony/twig-bundle": "<5.0", + "symfony/yaml": "<4.4" + }, + "provide": { + "symfony/translation-implementation": "2.0" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "^4.4|^5.0", + "symfony/console": "^4.4|^5.0", + "symfony/dependency-injection": "^5.0", + "symfony/finder": "^4.4|^5.0", + "symfony/http-kernel": "^5.0", + "symfony/intl": "^4.4|^5.0", + "symfony/service-contracts": "^1.1.2|^2", + "symfony/yaml": "^4.4|^5.0" + }, + "suggest": { + "psr/log-implementation": "To use logging capability in translator", + "symfony/config": "", + "symfony/yaml": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.1-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Translation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Translation Component", + "homepage": "https://symfony.com", + "time": "2020-01-21T08:44:52+00:00" + }, + { + "name": "symfony/translation-contracts", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/symfony/translation-contracts.git", + "reference": "50c101c6e7147d3284a945f71eb0959bd5736f1b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/translation-contracts/zipball/50c101c6e7147d3284a945f71eb0959bd5736f1b", + "reference": "50c101c6e7147d3284a945f71eb0959bd5736f1b", + "shasum": "" + }, + "require": { + "php": "^7.2.5" + }, + "suggest": { + "symfony/translation-implementation": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Contracts\\Translation\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Generic abstractions related to translation", + "homepage": "https://symfony.com", + "keywords": [ + "abstractions", + "contracts", + "decoupling", + "interfaces", + "interoperability", + "standards" + ], + "time": "2020-01-04T14:08:26+00:00" + }, { "name": "vlucas/phpdotenv", "version": "dev-master", @@ -1598,6 +2076,55 @@ "environment" ], "time": "2020-01-13T12:50:29+00:00" + }, + { + "name": "voku/portable-ascii", + "version": "1.4.6", + "source": { + "type": "git", + "url": "https://github.com/voku/portable-ascii.git", + "reference": "8b23920025c9b040301e3d1443b302547560f48d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/voku/portable-ascii/zipball/8b23920025c9b040301e3d1443b302547560f48d", + "reference": "8b23920025c9b040301e3d1443b302547560f48d", + "shasum": "" + }, + "require": { + "php": ">=7.0.0" + }, + "require-dev": { + "phpunit/phpunit": "~6.0 || ~7.0" + }, + "suggest": { + "ext-intl": "Use Intl for transliterator_transliterate() support" + }, + "type": "library", + "autoload": { + "psr-4": { + "voku\\": "src/voku/", + "voku\\tests\\": "tests/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Lars Moelleken", + "homepage": "http://www.moelleken.org/" + } + ], + "description": "Portable ASCII library - performance optimized (ascii) string functions for php.", + "homepage": "https://github.com/voku/portable-ascii", + "keywords": [ + "ascii", + "clean", + "php" + ], + "time": "2019-12-23T12:30:26+00:00" } ], "packages-dev": [], @@ -1608,7 +2135,9 @@ "kint-php/kint": 20, "filp/whoops": 20, "composer/composer": 20, - "guzzlehttp/guzzle": 20 + "guzzlehttp/guzzle": 20, + "illuminate/support": 20, + "illuminate/filesystem": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/src/Auth.php b/src/Auth.php index 47a9e0d..db8d078 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -5,6 +5,8 @@ use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7\Request; +// use Illuminate\Filesystem\Filesystem; + use BlueRaster\PowerBIAuthProxy\Exceptions\MissingConfigException; require_once(__DIR__.'/boot.php'); @@ -46,7 +48,9 @@ public function __construct(){ } static::$framework = $this->register_framework(); - UserProxy::handle(static::$framework->getUser()); + // UserProxy handles middleware functions. + // Aborts the request if authorization is not valid + UserProxy::handle(static::$framework->getUserProvider()); foreach(static::config() as $key => $value){ $this->{$key} = $value; @@ -73,13 +77,8 @@ public static function config(){ } private function register_framework(){ - $framework = false; - $classes = [ - 'CodeIgniter', - 'Mock', - ]; - foreach($classes as $classname){ - $class = "BlueRaster\\PowerBIAuthProxy\\Frameworks\\$classname"; + foreach(Filesystem::list_classes('Frameworks') as $class){ + $class = "$class"; if($class::test()){ return new $class; } @@ -87,22 +86,6 @@ private function register_framework(){ } - private function register_user_providers(){ - $framework = false; - $classes = [ - 'Prologin', - 'MockUser', - ]; - foreach($classes as $classname){ - $class = "BlueRaster\\PowerBIAuthProxy\\UserProviders\\$classname"; - if($framework = $class::test()){ - return new $class; - } - } - } - - - public function getAuthToken(){ if(!$this->oauth_token){ $client = new GuzzleClient; diff --git a/src/Exceptions/IllegalClassDirectoryListException.php b/src/Exceptions/IllegalClassDirectoryListException.php new file mode 100644 index 0000000..21888b6 --- /dev/null +++ b/src/Exceptions/IllegalClassDirectoryListException.php @@ -0,0 +1,8 @@ +files($dir)); + } + + public static function allFiles($dir){ + return collect((new IlluminateFilesystem)->allFiles($dir)); + } + + public static function list_classes($dir){ + $sanitized_dir = basename($dir); + if(!in_array($sanitized_dir, self::$allowed_class_directories)){ + throw new IllegalClassDirectoryListException; + } + + return Filesystem::files(__DIR__."/$sanitized_dir")->map(function($splFile) use($sanitized_dir){ + $name = $splFile->getFilenameWithoutExtension(); + return $name !== Str::singular($sanitized_dir) ? new ClassIterable("$sanitized_dir\\$name") : null; + })->filter(); + } + +} + + +class ClassIterable{ + + public $classname; + +// public $classname; + + public function __construct($classname){ + $namespace = (new \ReflectionClass($this))->getNamespaceName(); + $this->classname = "$namespace\\$classname"; + } + + public function __toString(){ + return $this->classname; + } +} diff --git a/src/Frameworks/CodeIgniter.php b/src/Frameworks/CodeIgniter.php index 6f27f15..060e7bc 100644 --- a/src/Frameworks/CodeIgniter.php +++ b/src/Frameworks/CodeIgniter.php @@ -4,11 +4,12 @@ class CodeIgniter extends Framework{ - protected $providers = ['Prologin']; + protected $user_providers = ['Prologin']; public static $ci; public function __construct(){ + parent::__construct(); if(! static::$ci =& get_instance() ) { new \Ci_Controller; static::$ci =& get_instance(); diff --git a/src/Frameworks/Framework.php b/src/Frameworks/Framework.php index d898188..e071d80 100644 --- a/src/Frameworks/Framework.php +++ b/src/Frameworks/Framework.php @@ -2,40 +2,32 @@ namespace BlueRaster\PowerBIAuthProxy\Frameworks; -class Framework{ +abstract class Framework{ protected $user; - protected $providers; + protected $user_providers; + + public function __construct(Array $config = ['user' => null]){ + [ 'user' => $user ] = $config; + if($user) $this->user = $user; + } public static function test(){ return false; } - private function getProvider(){ - $tests = [ - 'ci_ehris' => function(){ - $props = $this->user_model_reflection->getProperties(); - if(preg_match('/^.*?\/application\/libraries\/User.php$/', $this->user_model_reflection->getFileName()) ){ - return true; - } - return false; + public function getUserProvider(){ + return collect($this->user_providers)->map(function($classname){ + $p = "BlueRaster\\PowerBIAuthProxy\\UserProviders\\$classname"; + if($p::test($this)){ + return new $p($this->getUser()); } - ]; - - $found = array_filter($tests, 'call_user_func'); - if(empty($found)){ - throw new MissingUserProviderException; - } - - foreach($tests as $key => $test_fn){ - if($test_fn() === true){ - $this->getHandlerForProvider($key); - } - } + })->filter()->first(); } - + // Provides the currently used "user" object that the framework is utilizing. + // public function getUser(){ return $this->user; } diff --git a/src/Frameworks/Laravel.php b/src/Frameworks/Laravel.php new file mode 100644 index 0000000..01b1979 --- /dev/null +++ b/src/Frameworks/Laravel.php @@ -0,0 +1,18 @@ +user = auth()->user() ?? new \Illuminate\Foundation\Auth\User; + } + + public static function test(){ + return defined('LARAVEL_START'); + } +} diff --git a/src/Frameworks/Wordpress.php b/src/Frameworks/Wordpress.php new file mode 100644 index 0000000..cb13b35 --- /dev/null +++ b/src/Frameworks/Wordpress.php @@ -0,0 +1,18 @@ +user = $user ? $user : new \WP_User; + } + + public static function test(){ + return function_exists( 'wp_get_current_user' ); + } +} diff --git a/src/UserProviders/Eloquent.php b/src/UserProviders/Eloquent.php new file mode 100644 index 0000000..24c7a27 --- /dev/null +++ b/src/UserProviders/Eloquent.php @@ -0,0 +1,22 @@ +user; + } + + public function can($ability = '*'){ + return false; + } + + public static function test(Framework $framework){ + $user = $framework->getUser(); + if($user) return $user instanceof Illuminate\Foundation\Auth\User; + return false; + } +} diff --git a/src/UserProviders/MockUser.php b/src/UserProviders/MockUser.php index 43a2dca..fe41e37 100644 --- a/src/UserProviders/MockUser.php +++ b/src/UserProviders/MockUser.php @@ -2,7 +2,9 @@ namespace BlueRaster\PowerBIAuthProxy\UserProviders; -class Prologin extends UserProvider{ +use BlueRaster\PowerBIAuthProxy\Frameworks\Framework; + +class MockUser extends UserProvider{ public function logged_in(){ return !!$this->user->loggedin; @@ -12,7 +14,7 @@ public function can($ability = '*'){ return false; } - public static function test(){ + public static function test(Framework $framework){ return false; } } diff --git a/src/UserProviders/Prologin.php b/src/UserProviders/Prologin.php index 3daece3..1b725c3 100644 --- a/src/UserProviders/Prologin.php +++ b/src/UserProviders/Prologin.php @@ -1,6 +1,8 @@ user_reflection->getProperties(); - if(preg_match('/^.*?\/application\/libraries\/User.php$/', $this->user_reflection->getFileName()) ){ + public static function test(Framework $framework){ + $user = $framework->getUser(); + $reflection = new \ReflectionClass($user); + $props = $reflection->getProperties(); + if(preg_match('/^.*?\/application\/libraries\/User.php$/', $reflection->getFileName()) ){ return true; } return false; diff --git a/src/UserProviders/UserProvider.php b/src/UserProviders/UserProvider.php index 641e4a4..5708747 100644 --- a/src/UserProviders/UserProvider.php +++ b/src/UserProviders/UserProvider.php @@ -2,15 +2,23 @@ namespace BlueRaster\PowerBIAuthProxy\UserProviders; +use BlueRaster\PowerBIAuthProxy\Frameworks\Framework; + class UserProvider{ - private $user; + protected $user; + + protected $user_reflection; public function __construct($user){ $this->user = $user; $this->user_reflection = new \ReflectionClass($user); } + public function getUser(){ + return $this->user; + } + public function logged_in(){ return false; } @@ -19,7 +27,7 @@ public function can($ability = '*'){ return false; } - public static function test(){ + public static function test(Framework $framework){ return false; } } diff --git a/src/UserProviders/WordpressUser.php b/src/UserProviders/WordpressUser.php new file mode 100644 index 0000000..fa5175c --- /dev/null +++ b/src/UserProviders/WordpressUser.php @@ -0,0 +1,22 @@ +getUser(); + if($user) return $user instanceof \WP_User; + return function_exists( 'wp_get_current_user' ); + } +} diff --git a/src/UserProxy.php b/src/UserProxy.php index 0ff4b7a..904d349 100644 --- a/src/UserProxy.php +++ b/src/UserProxy.php @@ -8,27 +8,37 @@ class UserProxy{ - private $user_model; + private $user; - private $user_model_reflection; + private $user_reflection; - public function __construct(UserProvider $user_model){ - $this->user_model = $user_model; - $this->user_model_reflection = new \ReflectionClass($user_model); - dd($this); + public function __construct(UserProvider $user){ + $this->user = $user; + $this->user_reflection = new \ReflectionClass($user); } - public static function handle($user_model){ - (new self($user_model))->getProvider(); + public static function handle(UserProvider $user){ + $instance = new self($user); + if( ! $instance->user->logged_in() ){ + $instance->abort(); + } } + public function abort(){ + header("HTTP/1.1 403 Forbidden"); + echo "Forbidden"; + exit; + } +} + +/* private function getProvider(){ $tests = [ 'ci_ehris' => function(){ - $props = $this->user_model_reflection->getProperties(); - if(preg_match('/^.*?\/application\/libraries\/User.php$/', $this->user_model_reflection->getFileName()) ){ + $props = $this->user_reflection->getProperties(); + if(preg_match('/^.*?\/application\/libraries\/User.php$/', $this->user_reflection->getFileName()) ){ return true; } return false; @@ -51,8 +61,8 @@ private function getProvider(){ private function getHandlerForProvider($key){ $handlers = [ 'ci_ehris' => function(){ - if(isset($this->user_model->loggedin)){ - return $this->user_model->loggedin; + if(isset($this->user->loggedin)){ + return $this->user->loggedin; } return false; } @@ -68,12 +78,6 @@ private function getHandlerForProvider($key){ } } +*/ - private function abort(){ - header("HTTP/1.1 403 Forbidden"); - echo "Forbidden"; - exit; - } - -}