From a28626a0824fd81433b55181361948fb40977aec Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 9 Apr 2015 17:33:22 +0200 Subject: Initial commit --- .gitignore | 3 + composer.json | 9 ++ src/Console/Commands/SyncSplineAccountsCommand.php | 124 +++++++++++++++++++++ src/Hashing/HashServiceProvider.php | 34 ++++++ src/Hashing/SplineHasher.php | 60 ++++++++++ src/Models/User.php | 16 +++ src/Providers/ConsoleServiceProvider.php | 31 ++++++ 7 files changed, 277 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Console/Commands/SyncSplineAccountsCommand.php create mode 100644 src/Hashing/HashServiceProvider.php create mode 100644 src/Hashing/SplineHasher.php create mode 100644 src/Models/User.php create mode 100644 src/Providers/ConsoleServiceProvider.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99847a6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Laravel +bootstrap/compiled.php +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a2d9eb4 --- /dev/null +++ b/composer.json @@ -0,0 +1,9 @@ +{ + "name": "spline-cachet", + "description": "Spline extensions for cachet (open source status page in PHP).", + "autoload": { + "psr-4": { + "Spline\\Cachet\\": "src/" + } + } +} diff --git a/src/Console/Commands/SyncSplineAccountsCommand.php b/src/Console/Commands/SyncSplineAccountsCommand.php new file mode 100644 index 0000000..a83a73a --- /dev/null +++ b/src/Console/Commands/SyncSplineAccountsCommand.php @@ -0,0 +1,124 @@ +email, '@spline.de')) { + $users[$user->email] = $user; + } + } + $this->comment('Found ' . count($users) . ' existing users.'); + + return $users; + } + + protected function create_new_user($username, $email, $passwd) + { + $this->output->writeln("Creating new user: $email"); + $u = new User; + $u->username = $username; + $u->email = $email; + $u->setCryptedPassword($passwd); + $u->save(); + } + + protected function update_user($user, $passwd) + { + $this->output->writeln('Updating user: ' . $user->email . ''); + $user->setCryptedPassword($passwd); + $user->save(); + } + + protected function remove_users($users) + { + if (count($users) == 0) + return; + + if (count($users) == 1) { + $this->comment(count($users) . ' user is outdated.'); + } + else { + $this->comment(count($users) . ' users are outdated.'); + } + + foreach ($users as $uid => $user) { + $this->output->writeln("Deleting user: $uid"); + $user->delete(); + } + } + + protected function parse_input($input, $handler) + { + $fh = @fopen($input, 'r'); + if (!$fh) { + $this->error('Could not open input file.'); + return 1; + } + + while (($buffer = fgets($fh)) !== false) { + $buffer = trim($buffer); + if ($buffer == '' || $buffer[0] == '#' || strpos($buffer, ' ') === false) { + continue; + } + + list($username, $passwd) = explode(' ', $buffer, 2); + $handler($username, $passwd); + } + + if (!feof($fh)) { + $this->error('Unexpected fgets() fail.'); + return 1; + } + + fclose($fh); + return 0; + } + + public function fire() + { + $input = $this->argument('FILE'); + $users = $this->get_all_users(); + + $ret = $this->parse_input($input, function ($username, $passwd) use (&$users) { + $email = $username . '@spline.de'; + + if (array_key_exists($email, $users)) { + $u = $users[$email]; + unset($users[$email]); + + if ($u->password != $passwd) { + $this->update_user($u, $passwd); + } + } + else { + $this->create_new_user($username, $email, $passwd); + } + }); + + if ($ret != 0) { + return $ret; + } + + $this->remove_users($users); + return 0; + } + + protected function getArguments() + { + return array( + array('FILE', InputArgument::REQUIRED, 'Input file with users and password hashes.'), + ); + } +} diff --git a/src/Hashing/HashServiceProvider.php b/src/Hashing/HashServiceProvider.php new file mode 100644 index 0000000..e634de7 --- /dev/null +++ b/src/Hashing/HashServiceProvider.php @@ -0,0 +1,34 @@ +app->bindShared('hash', function() { return new SplineHasher; }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('hash'); + } + +} diff --git a/src/Hashing/SplineHasher.php b/src/Hashing/SplineHasher.php new file mode 100644 index 0000000..bf5d0d6 --- /dev/null +++ b/src/Hashing/SplineHasher.php @@ -0,0 +1,60 @@ +attributes['password'] = $password; + + return $this; + } +} + diff --git a/src/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php new file mode 100644 index 0000000..559dfaa --- /dev/null +++ b/src/Providers/ConsoleServiceProvider.php @@ -0,0 +1,31 @@ +commands('Spline\Cachet\Console\Commands\SyncSplineAccountsCommand'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->singleton('Spline\Cachet\Console\Commands\SyncSplineAccountsCommand', function ($app) { + return new SyncSplineAccountsCommand(); + }); + } +} -- cgit v1.2.3-1-g7c22