Skip to content

Commit 1088916

Browse files
authored
Merge pull request #390 from nextcloud/occ-user
More occ user management commands
2 parents 9f219f5 + 586c58e commit 1088916

6 files changed

Lines changed: 424 additions & 0 deletions

File tree

core/Command/Group/AddUser.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
4+
*
5+
* @license GNU AGPL version 3 or any later version
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
namespace OC\Core\Command\Group;
23+
24+
use OC\Core\Command\Base;
25+
use OCP\IGroupManager;
26+
use OCP\IUserManager;
27+
use Symfony\Component\Console\Command\Command;
28+
use Symfony\Component\Console\Input\InputArgument;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Input\InputOption;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
33+
class AddUser extends Base {
34+
/** @var IUserManager */
35+
protected $userManager;
36+
/** @var IGroupManager */
37+
protected $groupManager;
38+
39+
/**
40+
* @param IUserManager $userManager
41+
* @param IGroupManager $groupManager
42+
*/
43+
public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
44+
$this->userManager = $userManager;
45+
$this->groupManager = $groupManager;
46+
parent::__construct();
47+
}
48+
49+
protected function configure() {
50+
$this
51+
->setName('group:adduser')
52+
->setDescription('add a user to a group')
53+
->addArgument(
54+
'group',
55+
InputArgument::REQUIRED,
56+
'group to add the user to'
57+
)->addArgument(
58+
'user',
59+
InputArgument::REQUIRED,
60+
'user to add to the group'
61+
);
62+
}
63+
64+
protected function execute(InputInterface $input, OutputInterface $output) {
65+
$group = $this->groupManager->get($input->getArgument('group'));
66+
if (is_null($group)) {
67+
$output->writeln('<error>group not found</error>');
68+
return 1;
69+
}
70+
$user = $this->userManager->get($input->getArgument('user'));
71+
if (is_null($user)) {
72+
$output->writeln('<error>user not found</error>');
73+
return 1;
74+
}
75+
$group->addUser($user);
76+
}
77+
}

core/Command/Group/ListCommand.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
4+
*
5+
* @license GNU AGPL version 3 or any later version
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
namespace OC\Core\Command\Group;
23+
24+
use OC\Core\Command\Base;
25+
use OCP\IGroup;
26+
use OCP\IGroupManager;
27+
use OCP\IUser;
28+
use OCP\IUserManager;
29+
use Symfony\Component\Console\Command\Command;
30+
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Input\InputOption;
32+
use Symfony\Component\Console\Output\OutputInterface;
33+
34+
class ListCommand extends Base {
35+
/** @var IGroupManager */
36+
protected $groupManager;
37+
38+
/**
39+
* @param IGroupManager $groupManager
40+
*/
41+
public function __construct(IGroupManager $groupManager) {
42+
$this->groupManager = $groupManager;
43+
parent::__construct();
44+
}
45+
46+
protected function configure() {
47+
$this
48+
->setName('group:list')
49+
->setDescription('list configured groups')
50+
->addOption(
51+
'limit',
52+
'l',
53+
InputOption::VALUE_OPTIONAL,
54+
'Number of groups to retrieve',
55+
500
56+
)->addOption(
57+
'offset',
58+
'o',
59+
InputOption::VALUE_OPTIONAL,
60+
'Offset for retrieving groups',
61+
0
62+
)->addOption(
63+
'output',
64+
null,
65+
InputOption::VALUE_OPTIONAL,
66+
'Output format (plain, json or json_pretty, default is plain)',
67+
$this->defaultOutputFormat
68+
);
69+
}
70+
71+
protected function execute(InputInterface $input, OutputInterface $output) {
72+
$groups = $this->groupManager->search('', (int)$input->getOption('limit'), (int)$input->getOption('offset'));
73+
$this->writeArrayInOutputFormat($input, $output, $this->formatGroups($groups));
74+
}
75+
76+
/**
77+
* @param IGroup[] $groups
78+
* @return array
79+
*/
80+
private function formatGroups(array $groups) {
81+
$keys = array_map(function (IGroup $group) {
82+
return $group->getGID();
83+
}, $groups);
84+
$values = array_map(function (IGroup $group) {
85+
return array_keys($group->getUsers());
86+
}, $groups);
87+
return array_combine($keys, $values);
88+
}
89+
}

core/Command/Group/RemoveUser.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
4+
*
5+
* @license GNU AGPL version 3 or any later version
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
namespace OC\Core\Command\Group;
23+
24+
use OC\Core\Command\Base;
25+
use OCP\IGroupManager;
26+
use OCP\IUserManager;
27+
use Symfony\Component\Console\Command\Command;
28+
use Symfony\Component\Console\Input\InputArgument;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Input\InputOption;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
33+
class RemoveUser extends Base {
34+
/** @var IUserManager */
35+
protected $userManager;
36+
/** @var IGroupManager */
37+
protected $groupManager;
38+
39+
/**
40+
* @param IUserManager $userManager
41+
* @param IGroupManager $groupManager
42+
*/
43+
public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
44+
$this->userManager = $userManager;
45+
$this->groupManager = $groupManager;
46+
parent::__construct();
47+
}
48+
49+
protected function configure() {
50+
$this
51+
->setName('group:removeuser')
52+
->setDescription('remove a user from a group')
53+
->addArgument(
54+
'group',
55+
InputArgument::REQUIRED,
56+
'group to remove the user from'
57+
)->addArgument(
58+
'user',
59+
InputArgument::REQUIRED,
60+
'user to remove from the group'
61+
);
62+
}
63+
64+
protected function execute(InputInterface $input, OutputInterface $output) {
65+
$group = $this->groupManager->get($input->getArgument('group'));
66+
if (is_null($group)) {
67+
$output->writeln('<error>group not found</error>');
68+
return 1;
69+
}
70+
$user = $this->userManager->get($input->getArgument('user'));
71+
if (is_null($user)) {
72+
$output->writeln('<error>user not found</error>');
73+
return 1;
74+
}
75+
$group->removeUser($user);
76+
}
77+
}

core/Command/User/Info.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
4+
*
5+
* @license GNU AGPL version 3 or any later version
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
namespace OC\Core\Command\User;
23+
24+
use OC\Core\Command\Base;
25+
use OCP\IGroupManager;
26+
use OCP\IUser;
27+
use OCP\IUserManager;
28+
use Symfony\Component\Console\Command\Command;
29+
use Symfony\Component\Console\Input\InputArgument;
30+
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Input\InputOption;
32+
use Symfony\Component\Console\Output\OutputInterface;
33+
34+
class Info extends Base {
35+
/** @var IUserManager */
36+
protected $userManager;
37+
/** @var IGroupManager */
38+
protected $groupManager;
39+
40+
/**
41+
* @param IUserManager $userManager
42+
* @param IGroupManager $groupManager
43+
*/
44+
public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
45+
$this->userManager = $userManager;
46+
$this->groupManager = $groupManager;
47+
parent::__construct();
48+
}
49+
50+
protected function configure() {
51+
$this
52+
->setName('user:info')
53+
->setDescription('show user info')
54+
->addArgument(
55+
'user',
56+
InputArgument::REQUIRED,
57+
'user to show'
58+
)->addOption(
59+
'output',
60+
null,
61+
InputOption::VALUE_OPTIONAL,
62+
'Output format (plain, json or json_pretty, default is plain)',
63+
$this->defaultOutputFormat
64+
);
65+
}
66+
67+
protected function execute(InputInterface $input, OutputInterface $output) {
68+
$user = $this->userManager->get($input->getArgument('user'));
69+
if (is_null($user)) {
70+
$output->writeln('<error>user not found</error>');
71+
return 1;
72+
}
73+
$groups = $this->groupManager->getUserGroupIds($user);
74+
$data = [
75+
'user_id' => $user->getUID(),
76+
'display_name' => $user->getDisplayName(),
77+
'email' => ($user->getEMailAddress()) ? $user->getEMailAddress() : '',
78+
'cloud_id' => $user->getCloudId(),
79+
'enabled' => $user->isEnabled(),
80+
'groups' => $groups,
81+
'quota' => $user->getQuota(),
82+
'last_seen' => date(\DateTime::ATOM, $user->getLastLogin()), // ISO-8601
83+
'user_directory' => $user->getHome(),
84+
'backend' => $user->getBackendClassName()
85+
];
86+
$this->writeArrayInOutputFormat($input, $output, $data);
87+
}
88+
}

0 commit comments

Comments
 (0)