aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/User/GetUser.php
blob: 402356c94c2bab398d5a47e1f7d92ca84401e364 (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
<?php

namespace Studip\Cli\Commands\User;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;
use User;

class GetUser extends Command
{
    protected static $defaultName = 'user:get';

    protected function configure(): void
    {
        $this->setDescription('Get data of Stud.IP users.');
        $this->setHelp('This command will return the user data.');
        $this->addOption(
            'field',
            'f',
            InputOption::VALUE_OPTIONAL,
            'In which database field should be searched',
            'username'
        );
        $this->addArgument('needle', InputArgument::REQUIRED, 'Value to search for a user.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $needle = $input->getArgument('needle');
        $field = $input->getOption('field');

        if ($field === 'id') {
            $field = 'user_id';
        }

        $users = User::findBySQL($field . ' = ?', [$needle]);

        if (empty($users)) {
            $output->writeln('<error>Could not find users</error>');
            return Command::FAILURE;
        }
        $data = [];
        foreach ($users as $user) {
            $data[] = [
                $user->id,
                $user->username,
                $user->vorname,
                $user->nachname,
                $user->email,
                $user->auth_plugin,
            ];
        }

        $table = new Table($output);
        $table->setHeaders(['Id', 'Username', 'Firstname', 'Lastname', 'Email', 'Auth-Plugin'])
            ->setRows($data)
            ->setStyle('box')
            ->render();

        return Command::SUCCESS;
    }
}