2014-12-04 13:15:55 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Lukas Reschke
|
|
|
|
* @copyright 2014 Lukas Reschke lukas@owncloud.com
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Settings\Controller;
|
|
|
|
|
2014-12-08 14:32:59 +00:00
|
|
|
use OC\AppFramework\Http;
|
2014-12-09 17:36:40 +00:00
|
|
|
use OC\User\Manager;
|
2014-12-04 13:15:55 +00:00
|
|
|
use OC\User\User;
|
|
|
|
use \OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
Expose backend type via REST API
This change will expose the user backend via the REST API which is a pre-requisite for https://github.com/owncloud/core/issues/12620.
For example:
````json
[{"name":"9707A09E-CA9A-4ABE-A66A-3F632F16C409","displayname":"Document Conversion User Account","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/9707A09E-CA9A-4ABE-A66A-3F632F16C409","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"ED86733E-745C-4E4D-90CB-278A9737DB3C","displayname":"Hacker","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/ED86733E-745C-4E4D-90CB-278A9737DB3C","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"71CDF45B-E125-450D-983C-D9192F36EC88","displayname":"admin","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/71CDF45B-E125-450D-983C-D9192F36EC88","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"admin","displayname":"admin","groups":["admin"],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/admin","lastLogin":"1418057287","backend":"OC_User_Database"},{"name":"test","displayname":"test","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/test","lastLogin":0,"backend":"OC_User_Database"}]
```
2014-12-08 21:38:54 +00:00
|
|
|
use OCP\IUser;
|
2014-12-04 13:15:55 +00:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package OC\Settings\Controller
|
|
|
|
*/
|
|
|
|
class UsersController extends Controller {
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
/** @var bool */
|
|
|
|
private $isAdmin;
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IGroupManager $groupManager
|
|
|
|
* @param IUserSession $userSession
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param bool $isAdmin
|
|
|
|
* @param IL10N $l10n
|
|
|
|
*/
|
|
|
|
public function __construct($appName,
|
|
|
|
IRequest $request,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IGroupManager $groupManager,
|
|
|
|
IUserSession $userSession,
|
|
|
|
IConfig $config,
|
|
|
|
$isAdmin,
|
|
|
|
IL10N $l10n) {
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->isAdmin = $isAdmin;
|
|
|
|
$this->l10n = $l10n;
|
|
|
|
}
|
|
|
|
|
Expose backend type via REST API
This change will expose the user backend via the REST API which is a pre-requisite for https://github.com/owncloud/core/issues/12620.
For example:
````json
[{"name":"9707A09E-CA9A-4ABE-A66A-3F632F16C409","displayname":"Document Conversion User Account","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/9707A09E-CA9A-4ABE-A66A-3F632F16C409","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"ED86733E-745C-4E4D-90CB-278A9737DB3C","displayname":"Hacker","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/ED86733E-745C-4E4D-90CB-278A9737DB3C","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"71CDF45B-E125-450D-983C-D9192F36EC88","displayname":"admin","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/71CDF45B-E125-450D-983C-D9192F36EC88","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"admin","displayname":"admin","groups":["admin"],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/admin","lastLogin":"1418057287","backend":"OC_User_Database"},{"name":"test","displayname":"test","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/test","lastLogin":0,"backend":"OC_User_Database"}]
```
2014-12-08 21:38:54 +00:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @param array $userGroups
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function formatUserForIndex(IUser $user, array $userGroups = null) {
|
|
|
|
return array(
|
|
|
|
'name' => $user->getUID(),
|
|
|
|
'displayname' => $user->getDisplayName(),
|
|
|
|
'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
|
|
|
|
'subadmin' => \OC_SubAdmin::getSubAdminsGroups($user->getUID()),
|
|
|
|
'quota' => $this->config->getUserValue($user->getUID(), 'files', 'quota', 'default'),
|
|
|
|
'storageLocation' => $user->getHome(),
|
|
|
|
'lastLogin' => $user->getLastLogin(),
|
|
|
|
'backend' => $user->getBackendClassName()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-12-09 17:36:40 +00:00
|
|
|
/**
|
|
|
|
* @param array $userIDs
|
|
|
|
* @return IUser[]
|
|
|
|
*/
|
|
|
|
private function getUsersForUID(array $userIDs) {
|
|
|
|
$users = [];
|
|
|
|
foreach ($userIDs as $uid) {
|
|
|
|
$users[] = $this->userManager->get($uid);
|
|
|
|
}
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2014-12-04 13:15:55 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
Expose backend type via REST API
This change will expose the user backend via the REST API which is a pre-requisite for https://github.com/owncloud/core/issues/12620.
For example:
````json
[{"name":"9707A09E-CA9A-4ABE-A66A-3F632F16C409","displayname":"Document Conversion User Account","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/9707A09E-CA9A-4ABE-A66A-3F632F16C409","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"ED86733E-745C-4E4D-90CB-278A9737DB3C","displayname":"Hacker","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/ED86733E-745C-4E4D-90CB-278A9737DB3C","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"71CDF45B-E125-450D-983C-D9192F36EC88","displayname":"admin","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/71CDF45B-E125-450D-983C-D9192F36EC88","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"admin","displayname":"admin","groups":["admin"],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/admin","lastLogin":"1418057287","backend":"OC_User_Database"},{"name":"test","displayname":"test","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/test","lastLogin":0,"backend":"OC_User_Database"}]
```
2014-12-08 21:38:54 +00:00
|
|
|
*
|
2014-12-04 13:15:55 +00:00
|
|
|
* @param int $offset
|
|
|
|
* @param int $limit
|
2014-12-09 17:36:40 +00:00
|
|
|
* @param string $gid GID to filter for
|
|
|
|
* @param string $pattern Pattern to search for in the username
|
|
|
|
* @param string $backend Backend to filter for (class-name)
|
2014-12-04 13:15:55 +00:00
|
|
|
* @return DataResponse
|
|
|
|
*
|
|
|
|
* TODO: Tidy up and write unit tests - code is mainly static method calls
|
|
|
|
*/
|
2014-12-09 17:36:40 +00:00
|
|
|
public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
|
2014-12-04 13:15:55 +00:00
|
|
|
// FIXME: The JS sends the group '_everyone' instead of no GID for the "all users" group.
|
|
|
|
if($gid === '_everyone') {
|
|
|
|
$gid = '';
|
|
|
|
}
|
2014-12-09 17:36:40 +00:00
|
|
|
|
|
|
|
// Remove backends
|
|
|
|
if(!empty($backend)) {
|
|
|
|
$activeBackends = $this->userManager->getBackends();
|
|
|
|
$this->userManager->clearBackends();
|
|
|
|
foreach($activeBackends as $singleActiveBackend) {
|
|
|
|
if($backend === get_class($singleActiveBackend)) {
|
|
|
|
$this->userManager->registerBackend($singleActiveBackend);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 13:15:55 +00:00
|
|
|
$users = array();
|
|
|
|
if ($this->isAdmin) {
|
2014-12-09 17:36:40 +00:00
|
|
|
|
2014-12-04 13:15:55 +00:00
|
|
|
if($gid !== '') {
|
2014-12-09 17:36:40 +00:00
|
|
|
$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
|
2014-12-04 13:15:55 +00:00
|
|
|
} else {
|
2014-12-09 17:36:40 +00:00
|
|
|
$batch = $this->userManager->search('');
|
2014-12-04 13:15:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:36:40 +00:00
|
|
|
foreach ($batch as $user) {
|
|
|
|
$users[] = $this->formatUserForIndex($user);
|
2014-12-04 13:15:55 +00:00
|
|
|
}
|
2014-12-09 17:36:40 +00:00
|
|
|
|
2014-12-04 13:15:55 +00:00
|
|
|
} else {
|
2014-12-09 17:36:40 +00:00
|
|
|
if($gid !== '' && !in_array($gid, \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()))) {
|
|
|
|
$gid = '';
|
2014-12-04 13:15:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:36:40 +00:00
|
|
|
$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
|
|
|
|
foreach ($batch as $user) {
|
2014-12-04 13:15:55 +00:00
|
|
|
// Only add the groups, this user is a subadmin of
|
Expose backend type via REST API
This change will expose the user backend via the REST API which is a pre-requisite for https://github.com/owncloud/core/issues/12620.
For example:
````json
[{"name":"9707A09E-CA9A-4ABE-A66A-3F632F16C409","displayname":"Document Conversion User Account","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/9707A09E-CA9A-4ABE-A66A-3F632F16C409","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"ED86733E-745C-4E4D-90CB-278A9737DB3C","displayname":"Hacker","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/ED86733E-745C-4E4D-90CB-278A9737DB3C","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"71CDF45B-E125-450D-983C-D9192F36EC88","displayname":"admin","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/71CDF45B-E125-450D-983C-D9192F36EC88","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"admin","displayname":"admin","groups":["admin"],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/admin","lastLogin":"1418057287","backend":"OC_User_Database"},{"name":"test","displayname":"test","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/test","lastLogin":0,"backend":"OC_User_Database"}]
```
2014-12-08 21:38:54 +00:00
|
|
|
$userGroups = array_intersect($this->groupManager->getUserGroupIds($user),
|
|
|
|
\OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()));
|
|
|
|
$users[] = $this->formatUserForIndex($user, $userGroups);
|
2014-12-04 13:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Expose backend type via REST API
This change will expose the user backend via the REST API which is a pre-requisite for https://github.com/owncloud/core/issues/12620.
For example:
````json
[{"name":"9707A09E-CA9A-4ABE-A66A-3F632F16C409","displayname":"Document Conversion User Account","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/9707A09E-CA9A-4ABE-A66A-3F632F16C409","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"ED86733E-745C-4E4D-90CB-278A9737DB3C","displayname":"Hacker","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/ED86733E-745C-4E4D-90CB-278A9737DB3C","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"71CDF45B-E125-450D-983C-D9192F36EC88","displayname":"admin","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/71CDF45B-E125-450D-983C-D9192F36EC88","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"admin","displayname":"admin","groups":["admin"],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/admin","lastLogin":"1418057287","backend":"OC_User_Database"},{"name":"test","displayname":"test","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/test","lastLogin":0,"backend":"OC_User_Database"}]
```
2014-12-08 21:38:54 +00:00
|
|
|
return new DataResponse($users);
|
2014-12-04 13:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @param array $groups
|
|
|
|
* @return DataResponse
|
|
|
|
*
|
|
|
|
* TODO: Tidy up and write unit tests - code is mainly static method calls
|
|
|
|
*/
|
|
|
|
public function create($username, $password, array $groups) {
|
Expose backend type via REST API
This change will expose the user backend via the REST API which is a pre-requisite for https://github.com/owncloud/core/issues/12620.
For example:
````json
[{"name":"9707A09E-CA9A-4ABE-A66A-3F632F16C409","displayname":"Document Conversion User Account","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/9707A09E-CA9A-4ABE-A66A-3F632F16C409","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"ED86733E-745C-4E4D-90CB-278A9737DB3C","displayname":"Hacker","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/ED86733E-745C-4E4D-90CB-278A9737DB3C","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"71CDF45B-E125-450D-983C-D9192F36EC88","displayname":"admin","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/71CDF45B-E125-450D-983C-D9192F36EC88","lastLogin":0,"backend":"OCA\\user_ldap\\USER_LDAP"},{"name":"admin","displayname":"admin","groups":["admin"],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/admin","lastLogin":"1418057287","backend":"OC_User_Database"},{"name":"test","displayname":"test","groups":[],"subadmin":[],"quota":"default","storageLocation":"\/Users\/lreschke\/Programming\/core\/data\/test","lastLogin":0,"backend":"OC_User_Database"}]
```
2014-12-08 21:38:54 +00:00
|
|
|
|
2014-12-04 13:15:55 +00:00
|
|
|
if (!$this->isAdmin) {
|
|
|
|
if (!empty($groups)) {
|
|
|
|
foreach ($groups as $key => $group) {
|
|
|
|
if (!\OC_SubAdmin::isGroupAccessible($this->userSession->getUser()->getUID(), $group)) {
|
|
|
|
unset($groups[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($groups)) {
|
|
|
|
$groups = \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$user = $this->userManager->createUser($username, $password);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
return new DataResponse(
|
|
|
|
array(
|
2014-12-08 15:35:13 +00:00
|
|
|
'message' => (string)$this->l10n->t('Unable to create user.')
|
2014-12-08 14:32:59 +00:00
|
|
|
),
|
|
|
|
Http::STATUS_FORBIDDEN
|
2014-12-04 13:15:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($user instanceof User) {
|
|
|
|
foreach( $groups as $groupName ) {
|
|
|
|
$group = $this->groupManager->get($groupName);
|
|
|
|
|
|
|
|
if(empty($group)) {
|
|
|
|
$group = $this->groupManager->createGroup($groupName);
|
|
|
|
}
|
|
|
|
$group->addUser($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse(
|
|
|
|
array(
|
2014-12-08 15:35:13 +00:00
|
|
|
'username' => $username,
|
|
|
|
'groups' => $this->groupManager->getUserGroupIds($user),
|
|
|
|
'storageLocation' => $user->getHome()
|
2014-12-08 14:32:59 +00:00
|
|
|
),
|
|
|
|
Http::STATUS_CREATED
|
2014-12-04 13:15:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return DataResponse
|
|
|
|
*
|
|
|
|
* TODO: Tidy up and write unit tests - code is mainly static method calls
|
|
|
|
*/
|
|
|
|
public function destroy($id) {
|
|
|
|
if($this->userSession->getUser()->getUID() === $id) {
|
|
|
|
return new DataResponse(
|
|
|
|
array(
|
|
|
|
'status' => 'error',
|
|
|
|
'data' => array(
|
|
|
|
'message' => (string)$this->l10n->t('Unable to delete user.')
|
|
|
|
)
|
2014-12-08 14:32:59 +00:00
|
|
|
),
|
|
|
|
Http::STATUS_FORBIDDEN
|
2014-12-04 13:15:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Remove this static function call at some point…
|
|
|
|
if(!$this->isAdmin && !\OC_SubAdmin::isUserAccessible($this->userSession->getUser()->getUID(), $id)) {
|
|
|
|
return new DataResponse(
|
|
|
|
array(
|
|
|
|
'status' => 'error',
|
|
|
|
'data' => array(
|
2014-12-08 14:32:59 +00:00
|
|
|
'message' => (string)$this->l10n->t('Authentication error')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
Http::STATUS_FORBIDDEN
|
2014-12-04 13:15:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $this->userManager->get($id);
|
|
|
|
if($user) {
|
|
|
|
if($user->delete()) {
|
|
|
|
return new DataResponse(
|
|
|
|
array(
|
|
|
|
'status' => 'success',
|
|
|
|
'data' => array(
|
|
|
|
'username' => $id
|
|
|
|
)
|
2014-12-08 14:32:59 +00:00
|
|
|
),
|
|
|
|
Http::STATUS_NO_CONTENT
|
2014-12-04 13:15:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse(
|
|
|
|
array(
|
|
|
|
'status' => 'error',
|
|
|
|
'data' => array(
|
|
|
|
'message' => (string)$this->l10n->t('Unable to delete user.')
|
|
|
|
)
|
2014-12-08 14:32:59 +00:00
|
|
|
),
|
|
|
|
Http::STATUS_FORBIDDEN
|
2014-12-04 13:15:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|