summaryrefslogtreecommitdiffstats
path: root/src/Console/Commands/SyncSplineAccountsCommand.php
blob: a83a73a9a36e0a80023e741af47bf032eaf3a46b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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.'),
		);
	}
}