summaryrefslogtreecommitdiffstats
path: root/src/Console/Commands/SyncSplineAccountsCommand.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Console/Commands/SyncSplineAccountsCommand.php')
-rw-r--r--src/Console/Commands/SyncSplineAccountsCommand.php124
1 files changed, 124 insertions, 0 deletions
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 @@
+<?php
+
+namespace Spline\Cachet\Console\Commands;
+
+use Illuminate\Console\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Spline\Cachet\Models\User;
+
+class SyncSplineAccountsCommand extends Command {
+
+ protected $name = 'cachet:sync-spline';
+ protected $description = 'Sync the spline user into the laravel backend.';
+
+ protected function get_all_users()
+ {
+ $users = array();
+ foreach (User::all() as $user) {
+ if (ends_with($user->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: <info>$email</info>");
+ $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: <info>' . $user->email . '</info>');
+ $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: <info>$uid</info>");
+ $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.'),
+ );
+ }
+}