@@ -25,47 +25,47 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | 27 | return [ |
| 28 | - 'ocs' => [ |
|
| 29 | - // Apps |
|
| 30 | - ['root' => '/cloud', 'name' => 'Apps#getApps', 'url' => '/apps', 'verb' => 'GET'], |
|
| 31 | - ['root' => '/cloud', 'name' => 'Apps#getAppInfo', 'url' => '/apps/{app}', 'verb' => 'GET'], |
|
| 32 | - ['root' => '/cloud', 'name' => 'Apps#enable', 'url' => '/apps/{app}', 'verb' => 'POST'], |
|
| 33 | - ['root' => '/cloud', 'name' => 'Apps#disable', 'url' => '/apps/{app}', 'verb' => 'DELETE'], |
|
| 28 | + 'ocs' => [ |
|
| 29 | + // Apps |
|
| 30 | + ['root' => '/cloud', 'name' => 'Apps#getApps', 'url' => '/apps', 'verb' => 'GET'], |
|
| 31 | + ['root' => '/cloud', 'name' => 'Apps#getAppInfo', 'url' => '/apps/{app}', 'verb' => 'GET'], |
|
| 32 | + ['root' => '/cloud', 'name' => 'Apps#enable', 'url' => '/apps/{app}', 'verb' => 'POST'], |
|
| 33 | + ['root' => '/cloud', 'name' => 'Apps#disable', 'url' => '/apps/{app}', 'verb' => 'DELETE'], |
|
| 34 | 34 | |
| 35 | - // Groups |
|
| 36 | - ['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'], |
|
| 37 | - ['root' => '/cloud', 'name' => 'Groups#getGroupsDetails', 'url' => '/groups/details', 'verb' => 'GET'], |
|
| 38 | - ['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET'], |
|
| 39 | - ['root' => '/cloud', 'name' => 'Groups#getGroupUsers', 'url' => '/groups/{groupId}/users', 'verb' => 'GET'], |
|
| 40 | - ['root' => '/cloud', 'name' => 'Groups#getGroupUsersDetails', 'url' => '/groups/{groupId}/users/details', 'verb' => 'GET'], |
|
| 41 | - ['root' => '/cloud', 'name' => 'Groups#addGroup', 'url' => '/groups', 'verb' => 'POST'], |
|
| 42 | - ['root' => '/cloud', 'name' => 'Groups#deleteGroup', 'url' => '/groups/{groupId}', 'verb' => 'DELETE'], |
|
| 43 | - ['root' => '/cloud', 'name' => 'Groups#getSubAdminsOfGroup', 'url' => '/groups/{groupId}/subadmins', 'verb' => 'GET'], |
|
| 35 | + // Groups |
|
| 36 | + ['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'], |
|
| 37 | + ['root' => '/cloud', 'name' => 'Groups#getGroupsDetails', 'url' => '/groups/details', 'verb' => 'GET'], |
|
| 38 | + ['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET'], |
|
| 39 | + ['root' => '/cloud', 'name' => 'Groups#getGroupUsers', 'url' => '/groups/{groupId}/users', 'verb' => 'GET'], |
|
| 40 | + ['root' => '/cloud', 'name' => 'Groups#getGroupUsersDetails', 'url' => '/groups/{groupId}/users/details', 'verb' => 'GET'], |
|
| 41 | + ['root' => '/cloud', 'name' => 'Groups#addGroup', 'url' => '/groups', 'verb' => 'POST'], |
|
| 42 | + ['root' => '/cloud', 'name' => 'Groups#deleteGroup', 'url' => '/groups/{groupId}', 'verb' => 'DELETE'], |
|
| 43 | + ['root' => '/cloud', 'name' => 'Groups#getSubAdminsOfGroup', 'url' => '/groups/{groupId}/subadmins', 'verb' => 'GET'], |
|
| 44 | 44 | |
| 45 | - // Users |
|
| 46 | - ['root' => '/cloud', 'name' => 'Users#getUsers', 'url' => '/users', 'verb' => 'GET'], |
|
| 47 | - ['root' => '/cloud', 'name' => 'Users#getUsersDetails', 'url' => '/users/details', 'verb' => 'GET'], |
|
| 48 | - ['root' => '/cloud', 'name' => 'Users#addUser', 'url' => '/users', 'verb' => 'POST'], |
|
| 49 | - ['root' => '/cloud', 'name' => 'Users#getUser', 'url' => '/users/{userId}', 'verb' => 'GET'], |
|
| 50 | - ['root' => '/cloud', 'name' => 'Users#getCurrentUser', 'url' => '/user', 'verb' => 'GET'], |
|
| 51 | - ['root' => '/cloud', 'name' => 'Users#getEditableFields', 'url' => '/user/fields', 'verb' => 'GET'], |
|
| 52 | - ['root' => '/cloud', 'name' => 'Users#editUser', 'url' => '/users/{userId}', 'verb' => 'PUT'], |
|
| 53 | - ['root' => '/cloud', 'name' => 'Users#deleteUser', 'url' => '/users/{userId}', 'verb' => 'DELETE'], |
|
| 54 | - ['root' => '/cloud', 'name' => 'Users#enableUser', 'url' => '/users/{userId}/enable', 'verb' => 'PUT'], |
|
| 55 | - ['root' => '/cloud', 'name' => 'Users#disableUser', 'url' => '/users/{userId}/disable', 'verb' => 'PUT'], |
|
| 56 | - ['root' => '/cloud', 'name' => 'Users#getUsersGroups', 'url' => '/users/{userId}/groups', 'verb' => 'GET'], |
|
| 57 | - ['root' => '/cloud', 'name' => 'Users#addToGroup', 'url' => '/users/{userId}/groups', 'verb' => 'POST'], |
|
| 58 | - ['root' => '/cloud', 'name' => 'Users#removeFromGroup', 'url' => '/users/{userId}/groups', 'verb' => 'DELETE'], |
|
| 59 | - ['root' => '/cloud', 'name' => 'Users#getUserSubAdminGroups', 'url' => '/users/{userId}/subadmins', 'verb' => 'GET'], |
|
| 60 | - ['root' => '/cloud', 'name' => 'Users#addSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'POST'], |
|
| 61 | - ['root' => '/cloud', 'name' => 'Users#removeSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'DELETE'], |
|
| 62 | - ['root' => '/cloud', 'name' => 'Users#resendWelcomeMessage', 'url' => '/users/{userId}/welcome', 'verb' => 'POST'], |
|
| 45 | + // Users |
|
| 46 | + ['root' => '/cloud', 'name' => 'Users#getUsers', 'url' => '/users', 'verb' => 'GET'], |
|
| 47 | + ['root' => '/cloud', 'name' => 'Users#getUsersDetails', 'url' => '/users/details', 'verb' => 'GET'], |
|
| 48 | + ['root' => '/cloud', 'name' => 'Users#addUser', 'url' => '/users', 'verb' => 'POST'], |
|
| 49 | + ['root' => '/cloud', 'name' => 'Users#getUser', 'url' => '/users/{userId}', 'verb' => 'GET'], |
|
| 50 | + ['root' => '/cloud', 'name' => 'Users#getCurrentUser', 'url' => '/user', 'verb' => 'GET'], |
|
| 51 | + ['root' => '/cloud', 'name' => 'Users#getEditableFields', 'url' => '/user/fields', 'verb' => 'GET'], |
|
| 52 | + ['root' => '/cloud', 'name' => 'Users#editUser', 'url' => '/users/{userId}', 'verb' => 'PUT'], |
|
| 53 | + ['root' => '/cloud', 'name' => 'Users#deleteUser', 'url' => '/users/{userId}', 'verb' => 'DELETE'], |
|
| 54 | + ['root' => '/cloud', 'name' => 'Users#enableUser', 'url' => '/users/{userId}/enable', 'verb' => 'PUT'], |
|
| 55 | + ['root' => '/cloud', 'name' => 'Users#disableUser', 'url' => '/users/{userId}/disable', 'verb' => 'PUT'], |
|
| 56 | + ['root' => '/cloud', 'name' => 'Users#getUsersGroups', 'url' => '/users/{userId}/groups', 'verb' => 'GET'], |
|
| 57 | + ['root' => '/cloud', 'name' => 'Users#addToGroup', 'url' => '/users/{userId}/groups', 'verb' => 'POST'], |
|
| 58 | + ['root' => '/cloud', 'name' => 'Users#removeFromGroup', 'url' => '/users/{userId}/groups', 'verb' => 'DELETE'], |
|
| 59 | + ['root' => '/cloud', 'name' => 'Users#getUserSubAdminGroups', 'url' => '/users/{userId}/subadmins', 'verb' => 'GET'], |
|
| 60 | + ['root' => '/cloud', 'name' => 'Users#addSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'POST'], |
|
| 61 | + ['root' => '/cloud', 'name' => 'Users#removeSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'DELETE'], |
|
| 62 | + ['root' => '/cloud', 'name' => 'Users#resendWelcomeMessage', 'url' => '/users/{userId}/welcome', 'verb' => 'POST'], |
|
| 63 | 63 | |
| 64 | - // Config |
|
| 65 | - ['name' => 'AppConfig#getApps', 'url' => '/api/v1/config/apps', 'verb' => 'GET'], |
|
| 66 | - ['name' => 'AppConfig#getKeys', 'url' => '/api/v1/config/apps/{app}', 'verb' => 'GET'], |
|
| 67 | - ['name' => 'AppConfig#getValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'GET'], |
|
| 68 | - ['name' => 'AppConfig#setValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'POST'], |
|
| 69 | - ['name' => 'AppConfig#deleteKey', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'DELETE'], |
|
| 70 | - ], |
|
| 64 | + // Config |
|
| 65 | + ['name' => 'AppConfig#getApps', 'url' => '/api/v1/config/apps', 'verb' => 'GET'], |
|
| 66 | + ['name' => 'AppConfig#getKeys', 'url' => '/api/v1/config/apps/{app}', 'verb' => 'GET'], |
|
| 67 | + ['name' => 'AppConfig#getValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'GET'], |
|
| 68 | + ['name' => 'AppConfig#setValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'POST'], |
|
| 69 | + ['name' => 'AppConfig#deleteKey', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'DELETE'], |
|
| 70 | + ], |
|
| 71 | 71 | ]; |
@@ -74,7 +74,6 @@ |
||
| 74 | 74 | * @param IUserSession $userSession |
| 75 | 75 | * @param AccountManager $accountManager |
| 76 | 76 | * @param ILogger $logger |
| 77 | - * @param UsersController $userController |
|
| 78 | 77 | */ |
| 79 | 78 | public function __construct(string $appName, |
| 80 | 79 | IRequest $request, |
@@ -45,235 +45,235 @@ |
||
| 45 | 45 | |
| 46 | 46 | class GroupsController extends AUserData { |
| 47 | 47 | |
| 48 | - /** @var ILogger */ |
|
| 49 | - private $logger; |
|
| 48 | + /** @var ILogger */ |
|
| 49 | + private $logger; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param string $appName |
|
| 53 | - * @param IRequest $request |
|
| 54 | - * @param IUserManager $userManager |
|
| 55 | - * @param IConfig $config |
|
| 56 | - * @param IGroupManager $groupManager |
|
| 57 | - * @param IUserSession $userSession |
|
| 58 | - * @param AccountManager $accountManager |
|
| 59 | - * @param ILogger $logger |
|
| 60 | - * @param UsersController $userController |
|
| 61 | - */ |
|
| 62 | - public function __construct(string $appName, |
|
| 63 | - IRequest $request, |
|
| 64 | - IUserManager $userManager, |
|
| 65 | - IConfig $config, |
|
| 66 | - IGroupManager $groupManager, |
|
| 67 | - IUserSession $userSession, |
|
| 68 | - AccountManager $accountManager, |
|
| 69 | - ILogger $logger) { |
|
| 70 | - parent::__construct($appName, |
|
| 71 | - $request, |
|
| 72 | - $userManager, |
|
| 73 | - $config, |
|
| 74 | - $groupManager, |
|
| 75 | - $userSession, |
|
| 76 | - $accountManager); |
|
| 51 | + /** |
|
| 52 | + * @param string $appName |
|
| 53 | + * @param IRequest $request |
|
| 54 | + * @param IUserManager $userManager |
|
| 55 | + * @param IConfig $config |
|
| 56 | + * @param IGroupManager $groupManager |
|
| 57 | + * @param IUserSession $userSession |
|
| 58 | + * @param AccountManager $accountManager |
|
| 59 | + * @param ILogger $logger |
|
| 60 | + * @param UsersController $userController |
|
| 61 | + */ |
|
| 62 | + public function __construct(string $appName, |
|
| 63 | + IRequest $request, |
|
| 64 | + IUserManager $userManager, |
|
| 65 | + IConfig $config, |
|
| 66 | + IGroupManager $groupManager, |
|
| 67 | + IUserSession $userSession, |
|
| 68 | + AccountManager $accountManager, |
|
| 69 | + ILogger $logger) { |
|
| 70 | + parent::__construct($appName, |
|
| 71 | + $request, |
|
| 72 | + $userManager, |
|
| 73 | + $config, |
|
| 74 | + $groupManager, |
|
| 75 | + $userSession, |
|
| 76 | + $accountManager); |
|
| 77 | 77 | |
| 78 | - $this->logger = $logger; |
|
| 79 | - } |
|
| 78 | + $this->logger = $logger; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * returns a list of groups |
|
| 83 | - * |
|
| 84 | - * @NoAdminRequired |
|
| 85 | - * |
|
| 86 | - * @param string $search |
|
| 87 | - * @param int $limit |
|
| 88 | - * @param int $offset |
|
| 89 | - * @return DataResponse |
|
| 90 | - */ |
|
| 91 | - public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 92 | - $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 93 | - $groups = array_map(function($group) { |
|
| 94 | - /** @var IGroup $group */ |
|
| 95 | - return $group->getGID(); |
|
| 96 | - }, $groups); |
|
| 81 | + /** |
|
| 82 | + * returns a list of groups |
|
| 83 | + * |
|
| 84 | + * @NoAdminRequired |
|
| 85 | + * |
|
| 86 | + * @param string $search |
|
| 87 | + * @param int $limit |
|
| 88 | + * @param int $offset |
|
| 89 | + * @return DataResponse |
|
| 90 | + */ |
|
| 91 | + public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 92 | + $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 93 | + $groups = array_map(function($group) { |
|
| 94 | + /** @var IGroup $group */ |
|
| 95 | + return $group->getGID(); |
|
| 96 | + }, $groups); |
|
| 97 | 97 | |
| 98 | - return new DataResponse(['groups' => $groups]); |
|
| 99 | - } |
|
| 98 | + return new DataResponse(['groups' => $groups]); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * returns a list of groups details with ids and displaynames |
|
| 103 | - * |
|
| 104 | - * @NoAdminRequired |
|
| 105 | - * |
|
| 106 | - * @param string $search |
|
| 107 | - * @param int $limit |
|
| 108 | - * @param int $offset |
|
| 109 | - * @return DataResponse |
|
| 110 | - */ |
|
| 111 | - public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 112 | - $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 113 | - $groups = array_map(function($group) { |
|
| 114 | - /** @var IGroup $group */ |
|
| 115 | - return ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()]; |
|
| 116 | - }, $groups); |
|
| 101 | + /** |
|
| 102 | + * returns a list of groups details with ids and displaynames |
|
| 103 | + * |
|
| 104 | + * @NoAdminRequired |
|
| 105 | + * |
|
| 106 | + * @param string $search |
|
| 107 | + * @param int $limit |
|
| 108 | + * @param int $offset |
|
| 109 | + * @return DataResponse |
|
| 110 | + */ |
|
| 111 | + public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse { |
|
| 112 | + $groups = $this->groupManager->search($search, $limit, $offset); |
|
| 113 | + $groups = array_map(function($group) { |
|
| 114 | + /** @var IGroup $group */ |
|
| 115 | + return ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()]; |
|
| 116 | + }, $groups); |
|
| 117 | 117 | |
| 118 | - return new DataResponse(['groups' => $groups]); |
|
| 119 | - } |
|
| 118 | + return new DataResponse(['groups' => $groups]); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * @NoAdminRequired |
|
| 123 | - * |
|
| 124 | - * @param string $groupId |
|
| 125 | - * @return DataResponse |
|
| 126 | - * @throws OCSException |
|
| 127 | - * |
|
| 128 | - * @deprecated 14 Use getGroupUsers |
|
| 129 | - */ |
|
| 130 | - public function getGroup(string $groupId): DataResponse { |
|
| 131 | - return $this->getGroupUsers($groupId); |
|
| 132 | - } |
|
| 121 | + /** |
|
| 122 | + * @NoAdminRequired |
|
| 123 | + * |
|
| 124 | + * @param string $groupId |
|
| 125 | + * @return DataResponse |
|
| 126 | + * @throws OCSException |
|
| 127 | + * |
|
| 128 | + * @deprecated 14 Use getGroupUsers |
|
| 129 | + */ |
|
| 130 | + public function getGroup(string $groupId): DataResponse { |
|
| 131 | + return $this->getGroupUsers($groupId); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * returns an array of users in the specified group |
|
| 136 | - * |
|
| 137 | - * @NoAdminRequired |
|
| 138 | - * |
|
| 139 | - * @param string $groupId |
|
| 140 | - * @return DataResponse |
|
| 141 | - * @throws OCSException |
|
| 142 | - */ |
|
| 143 | - public function getGroupUsers(string $groupId): DataResponse { |
|
| 144 | - $user = $this->userSession->getUser(); |
|
| 145 | - $isSubadminOfGroup = false; |
|
| 134 | + /** |
|
| 135 | + * returns an array of users in the specified group |
|
| 136 | + * |
|
| 137 | + * @NoAdminRequired |
|
| 138 | + * |
|
| 139 | + * @param string $groupId |
|
| 140 | + * @return DataResponse |
|
| 141 | + * @throws OCSException |
|
| 142 | + */ |
|
| 143 | + public function getGroupUsers(string $groupId): DataResponse { |
|
| 144 | + $user = $this->userSession->getUser(); |
|
| 145 | + $isSubadminOfGroup = false; |
|
| 146 | 146 | |
| 147 | - // Check the group exists |
|
| 148 | - $group = $this->groupManager->get($groupId); |
|
| 149 | - if ($group !== null) { |
|
| 150 | - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 151 | - } else { |
|
| 152 | - throw new OCSNotFoundException('The requested group could not be found'); |
|
| 153 | - } |
|
| 147 | + // Check the group exists |
|
| 148 | + $group = $this->groupManager->get($groupId); |
|
| 149 | + if ($group !== null) { |
|
| 150 | + $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 151 | + } else { |
|
| 152 | + throw new OCSNotFoundException('The requested group could not be found'); |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - // Check subadmin has access to this group |
|
| 156 | - if($this->groupManager->isAdmin($user->getUID()) |
|
| 157 | - || $isSubadminOfGroup) { |
|
| 158 | - $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 159 | - $users = array_map(function($user) { |
|
| 160 | - /** @var IUser $user */ |
|
| 161 | - return $user->getUID(); |
|
| 162 | - }, $users); |
|
| 163 | - $users = array_values($users); |
|
| 164 | - return new DataResponse(['users' => $users]); |
|
| 165 | - } |
|
| 155 | + // Check subadmin has access to this group |
|
| 156 | + if($this->groupManager->isAdmin($user->getUID()) |
|
| 157 | + || $isSubadminOfGroup) { |
|
| 158 | + $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 159 | + $users = array_map(function($user) { |
|
| 160 | + /** @var IUser $user */ |
|
| 161 | + return $user->getUID(); |
|
| 162 | + }, $users); |
|
| 163 | + $users = array_values($users); |
|
| 164 | + return new DataResponse(['users' => $users]); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - throw new OCSForbiddenException(); |
|
| 168 | - } |
|
| 167 | + throw new OCSForbiddenException(); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * returns an array of users details in the specified group |
|
| 172 | - * |
|
| 173 | - * @NoAdminRequired |
|
| 174 | - * |
|
| 175 | - * @param string $groupId |
|
| 176 | - * @param int $limit |
|
| 177 | - * @param int $offset |
|
| 178 | - * @return DataResponse |
|
| 179 | - * @throws OCSException |
|
| 180 | - */ |
|
| 181 | - public function getGroupUsersDetails(string $groupId, int $limit = null, int $offset = 0): DataResponse { |
|
| 182 | - $user = $this->userSession->getUser(); |
|
| 183 | - $isSubadminOfGroup = false; |
|
| 170 | + /** |
|
| 171 | + * returns an array of users details in the specified group |
|
| 172 | + * |
|
| 173 | + * @NoAdminRequired |
|
| 174 | + * |
|
| 175 | + * @param string $groupId |
|
| 176 | + * @param int $limit |
|
| 177 | + * @param int $offset |
|
| 178 | + * @return DataResponse |
|
| 179 | + * @throws OCSException |
|
| 180 | + */ |
|
| 181 | + public function getGroupUsersDetails(string $groupId, int $limit = null, int $offset = 0): DataResponse { |
|
| 182 | + $user = $this->userSession->getUser(); |
|
| 183 | + $isSubadminOfGroup = false; |
|
| 184 | 184 | |
| 185 | - // Check the group exists |
|
| 186 | - $group = $this->groupManager->get($groupId); |
|
| 187 | - if ($group !== null) { |
|
| 188 | - $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 189 | - } else { |
|
| 190 | - throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 191 | - } |
|
| 185 | + // Check the group exists |
|
| 186 | + $group = $this->groupManager->get($groupId); |
|
| 187 | + if ($group !== null) { |
|
| 188 | + $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group); |
|
| 189 | + } else { |
|
| 190 | + throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - // Check subadmin has access to this group |
|
| 194 | - if($this->groupManager->isAdmin($user->getUID()) |
|
| 195 | - || $isSubadminOfGroup) { |
|
| 196 | - $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 197 | - // Extract required number |
|
| 198 | - $users = array_slice($users, $offset, $limit); |
|
| 199 | - $users = array_keys($users); |
|
| 200 | - $usersDetails = []; |
|
| 201 | - foreach ($users as $userId) { |
|
| 202 | - $userData = $this->getUserData($userId); |
|
| 203 | - // Do not insert empty entry |
|
| 204 | - if(!empty($userData)) { |
|
| 205 | - $usersDetails[$userId] = $userData; |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - return new DataResponse(['users' => $usersDetails]); |
|
| 209 | - } |
|
| 193 | + // Check subadmin has access to this group |
|
| 194 | + if($this->groupManager->isAdmin($user->getUID()) |
|
| 195 | + || $isSubadminOfGroup) { |
|
| 196 | + $users = $this->groupManager->get($groupId)->getUsers(); |
|
| 197 | + // Extract required number |
|
| 198 | + $users = array_slice($users, $offset, $limit); |
|
| 199 | + $users = array_keys($users); |
|
| 200 | + $usersDetails = []; |
|
| 201 | + foreach ($users as $userId) { |
|
| 202 | + $userData = $this->getUserData($userId); |
|
| 203 | + // Do not insert empty entry |
|
| 204 | + if(!empty($userData)) { |
|
| 205 | + $usersDetails[$userId] = $userData; |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + return new DataResponse(['users' => $usersDetails]); |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 212 | - } |
|
| 211 | + throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * creates a new group |
|
| 216 | - * |
|
| 217 | - * @PasswordConfirmationRequired |
|
| 218 | - * |
|
| 219 | - * @param string $groupid |
|
| 220 | - * @return DataResponse |
|
| 221 | - * @throws OCSException |
|
| 222 | - */ |
|
| 223 | - public function addGroup(string $groupid): DataResponse { |
|
| 224 | - // Validate name |
|
| 225 | - if(empty($groupid)) { |
|
| 226 | - $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']); |
|
| 227 | - throw new OCSException('Invalid group name', 101); |
|
| 228 | - } |
|
| 229 | - // Check if it exists |
|
| 230 | - if($this->groupManager->groupExists($groupid)){ |
|
| 231 | - throw new OCSException('', 102); |
|
| 232 | - } |
|
| 233 | - $this->groupManager->createGroup($groupid); |
|
| 234 | - return new DataResponse(); |
|
| 235 | - } |
|
| 214 | + /** |
|
| 215 | + * creates a new group |
|
| 216 | + * |
|
| 217 | + * @PasswordConfirmationRequired |
|
| 218 | + * |
|
| 219 | + * @param string $groupid |
|
| 220 | + * @return DataResponse |
|
| 221 | + * @throws OCSException |
|
| 222 | + */ |
|
| 223 | + public function addGroup(string $groupid): DataResponse { |
|
| 224 | + // Validate name |
|
| 225 | + if(empty($groupid)) { |
|
| 226 | + $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']); |
|
| 227 | + throw new OCSException('Invalid group name', 101); |
|
| 228 | + } |
|
| 229 | + // Check if it exists |
|
| 230 | + if($this->groupManager->groupExists($groupid)){ |
|
| 231 | + throw new OCSException('', 102); |
|
| 232 | + } |
|
| 233 | + $this->groupManager->createGroup($groupid); |
|
| 234 | + return new DataResponse(); |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * @PasswordConfirmationRequired |
|
| 239 | - * |
|
| 240 | - * @param string $groupId |
|
| 241 | - * @return DataResponse |
|
| 242 | - * @throws OCSException |
|
| 243 | - */ |
|
| 244 | - public function deleteGroup(string $groupId): DataResponse { |
|
| 245 | - // Check it exists |
|
| 246 | - if(!$this->groupManager->groupExists($groupId)){ |
|
| 247 | - throw new OCSException('', 101); |
|
| 248 | - } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ |
|
| 249 | - // Cannot delete admin group |
|
| 250 | - throw new OCSException('', 102); |
|
| 251 | - } |
|
| 237 | + /** |
|
| 238 | + * @PasswordConfirmationRequired |
|
| 239 | + * |
|
| 240 | + * @param string $groupId |
|
| 241 | + * @return DataResponse |
|
| 242 | + * @throws OCSException |
|
| 243 | + */ |
|
| 244 | + public function deleteGroup(string $groupId): DataResponse { |
|
| 245 | + // Check it exists |
|
| 246 | + if(!$this->groupManager->groupExists($groupId)){ |
|
| 247 | + throw new OCSException('', 101); |
|
| 248 | + } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){ |
|
| 249 | + // Cannot delete admin group |
|
| 250 | + throw new OCSException('', 102); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - return new DataResponse(); |
|
| 254 | - } |
|
| 253 | + return new DataResponse(); |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - /** |
|
| 257 | - * @param string $groupId |
|
| 258 | - * @return DataResponse |
|
| 259 | - * @throws OCSException |
|
| 260 | - */ |
|
| 261 | - public function getSubAdminsOfGroup(string $groupId): DataResponse { |
|
| 262 | - // Check group exists |
|
| 263 | - $targetGroup = $this->groupManager->get($groupId); |
|
| 264 | - if($targetGroup === null) { |
|
| 265 | - throw new OCSException('Group does not exist', 101); |
|
| 266 | - } |
|
| 256 | + /** |
|
| 257 | + * @param string $groupId |
|
| 258 | + * @return DataResponse |
|
| 259 | + * @throws OCSException |
|
| 260 | + */ |
|
| 261 | + public function getSubAdminsOfGroup(string $groupId): DataResponse { |
|
| 262 | + // Check group exists |
|
| 263 | + $targetGroup = $this->groupManager->get($groupId); |
|
| 264 | + if($targetGroup === null) { |
|
| 265 | + throw new OCSException('Group does not exist', 101); |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - /** @var IUser[] $subadmins */ |
|
| 269 | - $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); |
|
| 270 | - // New class returns IUser[] so convert back |
|
| 271 | - $uids = []; |
|
| 272 | - foreach ($subadmins as $user) { |
|
| 273 | - $uids[] = $user->getUID(); |
|
| 274 | - } |
|
| 268 | + /** @var IUser[] $subadmins */ |
|
| 269 | + $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup); |
|
| 270 | + // New class returns IUser[] so convert back |
|
| 271 | + $uids = []; |
|
| 272 | + foreach ($subadmins as $user) { |
|
| 273 | + $uids[] = $user->getUID(); |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | - return new DataResponse($uids); |
|
| 277 | - } |
|
| 276 | + return new DataResponse($uids); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | 279 | } |
@@ -52,805 +52,805 @@ |
||
| 52 | 52 | |
| 53 | 53 | class UsersController extends AUserData { |
| 54 | 54 | |
| 55 | - /** @var IAppManager */ |
|
| 56 | - private $appManager; |
|
| 57 | - /** @var ILogger */ |
|
| 58 | - private $logger; |
|
| 59 | - /** @var IFactory */ |
|
| 60 | - private $l10nFactory; |
|
| 61 | - /** @var NewUserMailHelper */ |
|
| 62 | - private $newUserMailHelper; |
|
| 63 | - /** @var FederatedFileSharingFactory */ |
|
| 64 | - private $federatedFileSharingFactory; |
|
| 65 | - /** @var ISecureRandom */ |
|
| 66 | - private $secureRandom; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param string $appName |
|
| 70 | - * @param IRequest $request |
|
| 71 | - * @param IUserManager $userManager |
|
| 72 | - * @param IConfig $config |
|
| 73 | - * @param IAppManager $appManager |
|
| 74 | - * @param IGroupManager $groupManager |
|
| 75 | - * @param IUserSession $userSession |
|
| 76 | - * @param AccountManager $accountManager |
|
| 77 | - * @param ILogger $logger |
|
| 78 | - * @param IFactory $l10nFactory |
|
| 79 | - * @param NewUserMailHelper $newUserMailHelper |
|
| 80 | - * @param FederatedFileSharingFactory $federatedFileSharingFactory |
|
| 81 | - * @param ISecureRandom $secureRandom |
|
| 82 | - */ |
|
| 83 | - public function __construct(string $appName, |
|
| 84 | - IRequest $request, |
|
| 85 | - IUserManager $userManager, |
|
| 86 | - IConfig $config, |
|
| 87 | - IAppManager $appManager, |
|
| 88 | - IGroupManager $groupManager, |
|
| 89 | - IUserSession $userSession, |
|
| 90 | - AccountManager $accountManager, |
|
| 91 | - ILogger $logger, |
|
| 92 | - IFactory $l10nFactory, |
|
| 93 | - NewUserMailHelper $newUserMailHelper, |
|
| 94 | - FederatedFileSharingFactory $federatedFileSharingFactory, |
|
| 95 | - ISecureRandom $secureRandom) { |
|
| 96 | - parent::__construct($appName, |
|
| 97 | - $request, |
|
| 98 | - $userManager, |
|
| 99 | - $config, |
|
| 100 | - $groupManager, |
|
| 101 | - $userSession, |
|
| 102 | - $accountManager); |
|
| 103 | - |
|
| 104 | - $this->appManager = $appManager; |
|
| 105 | - $this->logger = $logger; |
|
| 106 | - $this->l10nFactory = $l10nFactory; |
|
| 107 | - $this->newUserMailHelper = $newUserMailHelper; |
|
| 108 | - $this->federatedFileSharingFactory = $federatedFileSharingFactory; |
|
| 109 | - $this->secureRandom = $secureRandom; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @NoAdminRequired |
|
| 114 | - * |
|
| 115 | - * returns a list of users |
|
| 116 | - * |
|
| 117 | - * @param string $search |
|
| 118 | - * @param int $limit |
|
| 119 | - * @param int $offset |
|
| 120 | - * @return DataResponse |
|
| 121 | - */ |
|
| 122 | - public function getUsers(string $search = '', $limit = null, $offset = 0): DataResponse { |
|
| 123 | - $user = $this->userSession->getUser(); |
|
| 124 | - $users = []; |
|
| 125 | - |
|
| 126 | - // Admin? Or SubAdmin? |
|
| 127 | - $uid = $user->getUID(); |
|
| 128 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 129 | - if($this->groupManager->isAdmin($uid)){ |
|
| 130 | - $users = $this->userManager->search($search, $limit, $offset); |
|
| 131 | - } else if ($subAdminManager->isSubAdmin($user)) { |
|
| 132 | - $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
| 133 | - foreach ($subAdminOfGroups as $key => $group) { |
|
| 134 | - $subAdminOfGroups[$key] = $group->getGID(); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $users = []; |
|
| 138 | - foreach ($subAdminOfGroups as $group) { |
|
| 139 | - $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $users = array_keys($users); |
|
| 144 | - |
|
| 145 | - return new DataResponse([ |
|
| 146 | - 'users' => $users |
|
| 147 | - ]); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @NoAdminRequired |
|
| 152 | - * |
|
| 153 | - * returns a list of users and their data |
|
| 154 | - */ |
|
| 155 | - public function getUsersDetails(string $search = '', $limit = null, $offset = 0): DataResponse { |
|
| 156 | - $user = $this->userSession->getUser(); |
|
| 157 | - $users = []; |
|
| 158 | - |
|
| 159 | - // Admin? Or SubAdmin? |
|
| 160 | - $uid = $user->getUID(); |
|
| 161 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 162 | - if($this->groupManager->isAdmin($uid)){ |
|
| 163 | - $users = $this->userManager->search($search, $limit, $offset); |
|
| 164 | - } else if ($subAdminManager->isSubAdmin($user)) { |
|
| 165 | - $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
| 166 | - foreach ($subAdminOfGroups as $key => $group) { |
|
| 167 | - $subAdminOfGroups[$key] = $group->getGID(); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $users = []; |
|
| 171 | - foreach ($subAdminOfGroups as $group) { |
|
| 172 | - $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - $users = array_keys($users); |
|
| 177 | - $usersDetails = []; |
|
| 178 | - foreach ($users as $key => $userId) { |
|
| 179 | - $userData = $this->getUserData($userId); |
|
| 180 | - // Do not insert empty entry |
|
| 181 | - if(!empty($userData)) { |
|
| 182 | - $usersDetails[$userId] = $userData; |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - return new DataResponse([ |
|
| 187 | - 'users' => $usersDetails |
|
| 188 | - ]); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @PasswordConfirmationRequired |
|
| 193 | - * @NoAdminRequired |
|
| 194 | - * |
|
| 195 | - * @param string $userid |
|
| 196 | - * @param string $password |
|
| 197 | - * @param string $email |
|
| 198 | - * @param array $groups |
|
| 199 | - * @return DataResponse |
|
| 200 | - * @throws OCSException |
|
| 201 | - */ |
|
| 202 | - public function addUser(string $userid, string $password = '', string $email='', array $groups = []): DataResponse { |
|
| 203 | - $user = $this->userSession->getUser(); |
|
| 204 | - $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
| 205 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 206 | - |
|
| 207 | - if($this->userManager->userExists($userid)) { |
|
| 208 | - $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
|
| 209 | - throw new OCSException('User already exists', 102); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - if($groups !== []) { |
|
| 213 | - foreach ($groups as $group) { |
|
| 214 | - if(!$this->groupManager->groupExists($group)) { |
|
| 215 | - throw new OCSException('group '.$group.' does not exist', 104); |
|
| 216 | - } |
|
| 217 | - if(!$isAdmin && !$subAdminManager->isSubAdminOfGroup($user, $this->groupManager->get($group))) { |
|
| 218 | - throw new OCSException('insufficient privileges for group '. $group, 105); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - } else { |
|
| 222 | - if(!$isAdmin) { |
|
| 223 | - throw new OCSException('no group specified (required for subadmins)', 106); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - $generatePasswordResetToken = false; |
|
| 228 | - if ($password === '') { |
|
| 229 | - if ($email === '') { |
|
| 230 | - throw new OCSException('To send a password link to the user an email address is required.', 108); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $password = $this->secureRandom->generate(10); |
|
| 234 | - // Make sure we pass the password_policy |
|
| 235 | - $password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()'); |
|
| 236 | - $generatePasswordResetToken = true; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - try { |
|
| 240 | - $newUser = $this->userManager->createUser($userid, $password); |
|
| 241 | - $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); |
|
| 242 | - |
|
| 243 | - foreach ($groups as $group) { |
|
| 244 | - $this->groupManager->get($group)->addUser($newUser); |
|
| 245 | - $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // Send new user mail only if a mail is set |
|
| 249 | - if ($email !== '') { |
|
| 250 | - $newUser->setEMailAddress($email); |
|
| 251 | - try { |
|
| 252 | - $emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken); |
|
| 253 | - $this->newUserMailHelper->sendMail($newUser, $emailTemplate); |
|
| 254 | - } catch (\Exception $e) { |
|
| 255 | - $this->logger->logException($e, [ |
|
| 256 | - 'message' => "Can't send new user mail to $email", |
|
| 257 | - 'level' => \OCP\Util::ERROR, |
|
| 258 | - 'app' => 'ocs_api', |
|
| 259 | - ]); |
|
| 260 | - throw new OCSException('Unable to send the invitation mail', 109); |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - return new DataResponse(); |
|
| 265 | - |
|
| 266 | - } catch (HintException $e ) { |
|
| 267 | - $this->logger->logException($e, [ |
|
| 268 | - 'message' => 'Failed addUser attempt with hint exception.', |
|
| 269 | - 'level' => \OCP\Util::WARN, |
|
| 270 | - 'app' => 'ocs_api', |
|
| 271 | - ]); |
|
| 272 | - throw new OCSException($e->getHint(), 107); |
|
| 273 | - } catch (\Exception $e) { |
|
| 274 | - $this->logger->logException($e, [ |
|
| 275 | - 'message' => 'Failed addUser attempt with exception.', |
|
| 276 | - 'level' => \OCP\Util::ERROR, |
|
| 277 | - 'app' => 'ocs_api', |
|
| 278 | - ]); |
|
| 279 | - throw new OCSException('Bad request', 101); |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * @NoAdminRequired |
|
| 285 | - * @NoSubAdminRequired |
|
| 286 | - * |
|
| 287 | - * gets user info |
|
| 288 | - * |
|
| 289 | - * @param string $userId |
|
| 290 | - * @return DataResponse |
|
| 291 | - * @throws OCSException |
|
| 292 | - */ |
|
| 293 | - public function getUser(string $userId): DataResponse { |
|
| 294 | - $data = $this->getUserData($userId); |
|
| 295 | - // getUserData returns empty array if not enough permissions |
|
| 296 | - if(empty($data)) { |
|
| 297 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 298 | - } |
|
| 299 | - return new DataResponse($data); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @NoAdminRequired |
|
| 304 | - * @NoSubAdminRequired |
|
| 305 | - * |
|
| 306 | - * gets user info from the currently logged in user |
|
| 307 | - * |
|
| 308 | - * @return DataResponse |
|
| 309 | - * @throws OCSException |
|
| 310 | - */ |
|
| 311 | - public function getCurrentUser(): DataResponse { |
|
| 312 | - $user = $this->userSession->getUser(); |
|
| 313 | - if ($user) { |
|
| 314 | - $data = $this->getUserData($user->getUID()); |
|
| 315 | - // rename "displayname" to "display-name" only for this call to keep |
|
| 316 | - // the API stable. |
|
| 317 | - $data['display-name'] = $data['displayname']; |
|
| 318 | - unset($data['displayname']); |
|
| 319 | - return new DataResponse($data); |
|
| 320 | - |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * @NoAdminRequired |
|
| 328 | - * @NoSubAdminRequired |
|
| 329 | - */ |
|
| 330 | - public function getEditableFields(): DataResponse { |
|
| 331 | - $permittedFields = []; |
|
| 332 | - |
|
| 333 | - // Editing self (display, email) |
|
| 334 | - if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 335 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 336 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 340 | - $federatedFileSharing = $this->federatedFileSharingFactory->get(); |
|
| 341 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 342 | - if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 343 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 344 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 345 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 346 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - return new DataResponse($permittedFields); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * @NoAdminRequired |
|
| 355 | - * @NoSubAdminRequired |
|
| 356 | - * @PasswordConfirmationRequired |
|
| 357 | - * |
|
| 358 | - * edit users |
|
| 359 | - * |
|
| 360 | - * @param string $userId |
|
| 361 | - * @param string $key |
|
| 362 | - * @param string $value |
|
| 363 | - * @return DataResponse |
|
| 364 | - * @throws OCSException |
|
| 365 | - */ |
|
| 366 | - public function editUser(string $userId, string $key, string $value): DataResponse { |
|
| 367 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 368 | - |
|
| 369 | - $targetUser = $this->userManager->get($userId); |
|
| 370 | - if($targetUser === null) { |
|
| 371 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - $permittedFields = []; |
|
| 375 | - if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 376 | - // Editing self (display, email) |
|
| 377 | - if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 378 | - $permittedFields[] = 'display'; |
|
| 379 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 380 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - $permittedFields[] = 'password'; |
|
| 384 | - if ($this->config->getSystemValue('force_language', false) === false || |
|
| 385 | - $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 386 | - $permittedFields[] = 'language'; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 390 | - $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 391 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 392 | - if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 393 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 394 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 395 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 396 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - // If admin they can edit their own quota |
|
| 401 | - if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 402 | - $permittedFields[] = 'quota'; |
|
| 403 | - } |
|
| 404 | - } else { |
|
| 405 | - // Check if admin / subadmin |
|
| 406 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 407 | - if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 408 | - || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 409 | - // They have permissions over the user |
|
| 410 | - $permittedFields[] = 'display'; |
|
| 411 | - $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 412 | - $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 413 | - $permittedFields[] = 'password'; |
|
| 414 | - $permittedFields[] = 'language'; |
|
| 415 | - $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 416 | - $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 417 | - $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 418 | - $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 419 | - $permittedFields[] = 'quota'; |
|
| 420 | - } else { |
|
| 421 | - // No rights |
|
| 422 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - // Check if permitted to edit this field |
|
| 426 | - if(!in_array($key, $permittedFields)) { |
|
| 427 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 428 | - } |
|
| 429 | - // Process the edit |
|
| 430 | - switch($key) { |
|
| 431 | - case 'display': |
|
| 432 | - case AccountManager::PROPERTY_DISPLAYNAME: |
|
| 433 | - $targetUser->setDisplayName($value); |
|
| 434 | - break; |
|
| 435 | - case 'quota': |
|
| 436 | - $quota = $value; |
|
| 437 | - if($quota !== 'none' && $quota !== 'default') { |
|
| 438 | - if (is_numeric($quota)) { |
|
| 439 | - $quota = (float) $quota; |
|
| 440 | - } else { |
|
| 441 | - $quota = \OCP\Util::computerFileSize($quota); |
|
| 442 | - } |
|
| 443 | - if ($quota === false) { |
|
| 444 | - throw new OCSException('Invalid quota value '.$value, 103); |
|
| 445 | - } |
|
| 446 | - if($quota === 0) { |
|
| 447 | - $quota = 'default'; |
|
| 448 | - }else if($quota === -1) { |
|
| 449 | - $quota = 'none'; |
|
| 450 | - } else { |
|
| 451 | - $quota = \OCP\Util::humanFileSize($quota); |
|
| 452 | - } |
|
| 453 | - } |
|
| 454 | - $targetUser->setQuota($quota); |
|
| 455 | - break; |
|
| 456 | - case 'password': |
|
| 457 | - $targetUser->setPassword($value); |
|
| 458 | - break; |
|
| 459 | - case 'language': |
|
| 460 | - $languagesCodes = $this->l10nFactory->findAvailableLanguages(); |
|
| 461 | - if (!in_array($value, $languagesCodes, true) && $value !== 'en') { |
|
| 462 | - throw new OCSException('Invalid language', 102); |
|
| 463 | - } |
|
| 464 | - $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
|
| 465 | - break; |
|
| 466 | - case AccountManager::PROPERTY_EMAIL: |
|
| 467 | - if(filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { |
|
| 468 | - $targetUser->setEMailAddress($value); |
|
| 469 | - } else { |
|
| 470 | - throw new OCSException('', 102); |
|
| 471 | - } |
|
| 472 | - break; |
|
| 473 | - case AccountManager::PROPERTY_PHONE: |
|
| 474 | - case AccountManager::PROPERTY_ADDRESS: |
|
| 475 | - case AccountManager::PROPERTY_WEBSITE: |
|
| 476 | - case AccountManager::PROPERTY_TWITTER: |
|
| 477 | - $userAccount = $this->accountManager->getUser($targetUser); |
|
| 478 | - if ($userAccount[$key]['value'] !== $value) { |
|
| 479 | - $userAccount[$key]['value'] = $value; |
|
| 480 | - $this->accountManager->updateUser($targetUser, $userAccount); |
|
| 481 | - } |
|
| 482 | - break; |
|
| 483 | - default: |
|
| 484 | - throw new OCSException('', 103); |
|
| 485 | - } |
|
| 486 | - return new DataResponse(); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * @PasswordConfirmationRequired |
|
| 491 | - * @NoAdminRequired |
|
| 492 | - * |
|
| 493 | - * @param string $userId |
|
| 494 | - * @return DataResponse |
|
| 495 | - * @throws OCSException |
|
| 496 | - */ |
|
| 497 | - public function deleteUser(string $userId): DataResponse { |
|
| 498 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 499 | - |
|
| 500 | - $targetUser = $this->userManager->get($userId); |
|
| 501 | - |
|
| 502 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 503 | - throw new OCSException('', 101); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - // If not permitted |
|
| 507 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 508 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 509 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - // Go ahead with the delete |
|
| 513 | - if($targetUser->delete()) { |
|
| 514 | - return new DataResponse(); |
|
| 515 | - } else { |
|
| 516 | - throw new OCSException('', 101); |
|
| 517 | - } |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * @PasswordConfirmationRequired |
|
| 522 | - * @NoAdminRequired |
|
| 523 | - * |
|
| 524 | - * @param string $userId |
|
| 525 | - * @return DataResponse |
|
| 526 | - * @throws OCSException |
|
| 527 | - * @throws OCSForbiddenException |
|
| 528 | - */ |
|
| 529 | - public function disableUser(string $userId): DataResponse { |
|
| 530 | - return $this->setEnabled($userId, false); |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * @PasswordConfirmationRequired |
|
| 535 | - * @NoAdminRequired |
|
| 536 | - * |
|
| 537 | - * @param string $userId |
|
| 538 | - * @return DataResponse |
|
| 539 | - * @throws OCSException |
|
| 540 | - * @throws OCSForbiddenException |
|
| 541 | - */ |
|
| 542 | - public function enableUser(string $userId): DataResponse { |
|
| 543 | - return $this->setEnabled($userId, true); |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * @param string $userId |
|
| 548 | - * @param bool $value |
|
| 549 | - * @return DataResponse |
|
| 550 | - * @throws OCSException |
|
| 551 | - */ |
|
| 552 | - private function setEnabled(string $userId, bool $value): DataResponse { |
|
| 553 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 554 | - |
|
| 555 | - $targetUser = $this->userManager->get($userId); |
|
| 556 | - if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 557 | - throw new OCSException('', 101); |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - // If not permitted |
|
| 561 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 562 | - if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 563 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - // enable/disable the user now |
|
| 567 | - $targetUser->setEnabled($value); |
|
| 568 | - return new DataResponse(); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * @NoAdminRequired |
|
| 573 | - * @NoSubAdminRequired |
|
| 574 | - * |
|
| 575 | - * @param string $userId |
|
| 576 | - * @return DataResponse |
|
| 577 | - * @throws OCSException |
|
| 578 | - */ |
|
| 579 | - public function getUsersGroups(string $userId): DataResponse { |
|
| 580 | - $loggedInUser = $this->userSession->getUser(); |
|
| 581 | - |
|
| 582 | - $targetUser = $this->userManager->get($userId); |
|
| 583 | - if($targetUser === null) { |
|
| 584 | - throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 588 | - // Self lookup or admin lookup |
|
| 589 | - return new DataResponse([ |
|
| 590 | - 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
|
| 591 | - ]); |
|
| 592 | - } else { |
|
| 593 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 594 | - |
|
| 595 | - // Looking up someone else |
|
| 596 | - if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 597 | - // Return the group that the method caller is subadmin of for the user in question |
|
| 598 | - /** @var IGroup[] $getSubAdminsGroups */ |
|
| 599 | - $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 600 | - foreach ($getSubAdminsGroups as $key => $group) { |
|
| 601 | - $getSubAdminsGroups[$key] = $group->getGID(); |
|
| 602 | - } |
|
| 603 | - $groups = array_intersect( |
|
| 604 | - $getSubAdminsGroups, |
|
| 605 | - $this->groupManager->getUserGroupIds($targetUser) |
|
| 606 | - ); |
|
| 607 | - return new DataResponse(['groups' => $groups]); |
|
| 608 | - } else { |
|
| 609 | - // Not permitted |
|
| 610 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 611 | - } |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * @PasswordConfirmationRequired |
|
| 618 | - * @NoAdminRequired |
|
| 619 | - * |
|
| 620 | - * @param string $userId |
|
| 621 | - * @param string $groupid |
|
| 622 | - * @return DataResponse |
|
| 623 | - * @throws OCSException |
|
| 624 | - */ |
|
| 625 | - public function addToGroup(string $userId, string $groupid = ''): DataResponse { |
|
| 626 | - if($groupid === '') { |
|
| 627 | - throw new OCSException('', 101); |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - $group = $this->groupManager->get($groupid); |
|
| 631 | - $targetUser = $this->userManager->get($userId); |
|
| 632 | - if($group === null) { |
|
| 633 | - throw new OCSException('', 102); |
|
| 634 | - } |
|
| 635 | - if($targetUser === null) { |
|
| 636 | - throw new OCSException('', 103); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - // If they're not an admin, check they are a subadmin of the group in question |
|
| 640 | - $loggedInUser = $this->userSession->getUser(); |
|
| 641 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 642 | - if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 643 | - throw new OCSException('', 104); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - // Add user to group |
|
| 647 | - $group->addUser($targetUser); |
|
| 648 | - return new DataResponse(); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * @PasswordConfirmationRequired |
|
| 653 | - * @NoAdminRequired |
|
| 654 | - * |
|
| 655 | - * @param string $userId |
|
| 656 | - * @param string $groupid |
|
| 657 | - * @return DataResponse |
|
| 658 | - * @throws OCSException |
|
| 659 | - */ |
|
| 660 | - public function removeFromGroup(string $userId, string $groupid): DataResponse { |
|
| 661 | - $loggedInUser = $this->userSession->getUser(); |
|
| 662 | - |
|
| 663 | - if($groupid === null || trim($groupid) === '') { |
|
| 664 | - throw new OCSException('', 101); |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - $group = $this->groupManager->get($groupid); |
|
| 668 | - if($group === null) { |
|
| 669 | - throw new OCSException('', 102); |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - $targetUser = $this->userManager->get($userId); |
|
| 673 | - if($targetUser === null) { |
|
| 674 | - throw new OCSException('', 103); |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - // If they're not an admin, check they are a subadmin of the group in question |
|
| 678 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 679 | - if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 680 | - throw new OCSException('', 104); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - // Check they aren't removing themselves from 'admin' or their 'subadmin; group |
|
| 684 | - if ($targetUser->getUID() === $loggedInUser->getUID()) { |
|
| 685 | - if ($this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 686 | - if ($group->getGID() === 'admin') { |
|
| 687 | - throw new OCSException('Cannot remove yourself from the admin group', 105); |
|
| 688 | - } |
|
| 689 | - } else { |
|
| 690 | - // Not an admin, so the user must be a subadmin of this group, but that is not allowed. |
|
| 691 | - throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 695 | - /** @var IGroup[] $subAdminGroups */ |
|
| 696 | - $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 697 | - $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
| 698 | - return $subAdminGroup->getGID(); |
|
| 699 | - }, $subAdminGroups); |
|
| 700 | - $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
|
| 701 | - $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups); |
|
| 702 | - |
|
| 703 | - if (count($userSubAdminGroups) <= 1) { |
|
| 704 | - // Subadmin must not be able to remove a user from all their subadmin groups. |
|
| 705 | - throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105); |
|
| 706 | - } |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - // Remove user from group |
|
| 710 | - $group->removeUser($targetUser); |
|
| 711 | - return new DataResponse(); |
|
| 712 | - } |
|
| 713 | - |
|
| 714 | - /** |
|
| 715 | - * Creates a subadmin |
|
| 716 | - * |
|
| 717 | - * @PasswordConfirmationRequired |
|
| 718 | - * |
|
| 719 | - * @param string $userId |
|
| 720 | - * @param string $groupid |
|
| 721 | - * @return DataResponse |
|
| 722 | - * @throws OCSException |
|
| 723 | - */ |
|
| 724 | - public function addSubAdmin(string $userId, string $groupid): DataResponse { |
|
| 725 | - $group = $this->groupManager->get($groupid); |
|
| 726 | - $user = $this->userManager->get($userId); |
|
| 727 | - |
|
| 728 | - // Check if the user exists |
|
| 729 | - if($user === null) { |
|
| 730 | - throw new OCSException('User does not exist', 101); |
|
| 731 | - } |
|
| 732 | - // Check if group exists |
|
| 733 | - if($group === null) { |
|
| 734 | - throw new OCSException('Group does not exist', 102); |
|
| 735 | - } |
|
| 736 | - // Check if trying to make subadmin of admin group |
|
| 737 | - if($group->getGID() === 'admin') { |
|
| 738 | - throw new OCSException('Cannot create subadmins for admin group', 103); |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 742 | - |
|
| 743 | - // We cannot be subadmin twice |
|
| 744 | - if ($subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 745 | - return new DataResponse(); |
|
| 746 | - } |
|
| 747 | - // Go |
|
| 748 | - if($subAdminManager->createSubAdmin($user, $group)) { |
|
| 749 | - return new DataResponse(); |
|
| 750 | - } else { |
|
| 751 | - throw new OCSException('Unknown error occurred', 103); |
|
| 752 | - } |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - /** |
|
| 756 | - * Removes a subadmin from a group |
|
| 757 | - * |
|
| 758 | - * @PasswordConfirmationRequired |
|
| 759 | - * |
|
| 760 | - * @param string $userId |
|
| 761 | - * @param string $groupid |
|
| 762 | - * @return DataResponse |
|
| 763 | - * @throws OCSException |
|
| 764 | - */ |
|
| 765 | - public function removeSubAdmin(string $userId, string $groupid): DataResponse { |
|
| 766 | - $group = $this->groupManager->get($groupid); |
|
| 767 | - $user = $this->userManager->get($userId); |
|
| 768 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 769 | - |
|
| 770 | - // Check if the user exists |
|
| 771 | - if($user === null) { |
|
| 772 | - throw new OCSException('User does not exist', 101); |
|
| 773 | - } |
|
| 774 | - // Check if the group exists |
|
| 775 | - if($group === null) { |
|
| 776 | - throw new OCSException('Group does not exist', 101); |
|
| 777 | - } |
|
| 778 | - // Check if they are a subadmin of this said group |
|
| 779 | - if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 780 | - throw new OCSException('User is not a subadmin of this group', 102); |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - // Go |
|
| 784 | - if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 785 | - return new DataResponse(); |
|
| 786 | - } else { |
|
| 787 | - throw new OCSException('Unknown error occurred', 103); |
|
| 788 | - } |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - /** |
|
| 792 | - * Get the groups a user is a subadmin of |
|
| 793 | - * |
|
| 794 | - * @param string $userId |
|
| 795 | - * @return DataResponse |
|
| 796 | - * @throws OCSException |
|
| 797 | - */ |
|
| 798 | - public function getUserSubAdminGroups(string $userId): DataResponse { |
|
| 799 | - $groups = $this->getUserSubAdminGroupsData($userId); |
|
| 800 | - return new DataResponse($groups); |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - /** |
|
| 804 | - * @NoAdminRequired |
|
| 805 | - * @PasswordConfirmationRequired |
|
| 806 | - * |
|
| 807 | - * resend welcome message |
|
| 808 | - * |
|
| 809 | - * @param string $userId |
|
| 810 | - * @return DataResponse |
|
| 811 | - * @throws OCSException |
|
| 812 | - */ |
|
| 813 | - public function resendWelcomeMessage(string $userId): DataResponse { |
|
| 814 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 815 | - |
|
| 816 | - $targetUser = $this->userManager->get($userId); |
|
| 817 | - if($targetUser === null) { |
|
| 818 | - throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - // Check if admin / subadmin |
|
| 822 | - $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 823 | - if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 824 | - && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 825 | - // No rights |
|
| 826 | - throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 827 | - } |
|
| 828 | - |
|
| 829 | - $email = $targetUser->getEMailAddress(); |
|
| 830 | - if ($email === '' || $email === null) { |
|
| 831 | - throw new OCSException('Email address not available', 101); |
|
| 832 | - } |
|
| 833 | - $username = $targetUser->getUID(); |
|
| 834 | - $lang = $this->config->getUserValue($username, 'core', 'lang', 'en'); |
|
| 835 | - if (!$this->l10nFactory->languageExists('settings', $lang)) { |
|
| 836 | - $lang = 'en'; |
|
| 837 | - } |
|
| 838 | - |
|
| 839 | - $l10n = $this->l10nFactory->get('settings', $lang); |
|
| 840 | - |
|
| 841 | - try { |
|
| 842 | - $this->newUserMailHelper->setL10N($l10n); |
|
| 843 | - $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
|
| 844 | - $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
|
| 845 | - } catch(\Exception $e) { |
|
| 846 | - $this->logger->logException($e, [ |
|
| 847 | - 'message' => "Can't send new user mail to $email", |
|
| 848 | - 'level' => \OCP\Util::ERROR, |
|
| 849 | - 'app' => 'settings', |
|
| 850 | - ]); |
|
| 851 | - throw new OCSException('Sending email failed', 102); |
|
| 852 | - } |
|
| 853 | - |
|
| 854 | - return new DataResponse(); |
|
| 855 | - } |
|
| 55 | + /** @var IAppManager */ |
|
| 56 | + private $appManager; |
|
| 57 | + /** @var ILogger */ |
|
| 58 | + private $logger; |
|
| 59 | + /** @var IFactory */ |
|
| 60 | + private $l10nFactory; |
|
| 61 | + /** @var NewUserMailHelper */ |
|
| 62 | + private $newUserMailHelper; |
|
| 63 | + /** @var FederatedFileSharingFactory */ |
|
| 64 | + private $federatedFileSharingFactory; |
|
| 65 | + /** @var ISecureRandom */ |
|
| 66 | + private $secureRandom; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param string $appName |
|
| 70 | + * @param IRequest $request |
|
| 71 | + * @param IUserManager $userManager |
|
| 72 | + * @param IConfig $config |
|
| 73 | + * @param IAppManager $appManager |
|
| 74 | + * @param IGroupManager $groupManager |
|
| 75 | + * @param IUserSession $userSession |
|
| 76 | + * @param AccountManager $accountManager |
|
| 77 | + * @param ILogger $logger |
|
| 78 | + * @param IFactory $l10nFactory |
|
| 79 | + * @param NewUserMailHelper $newUserMailHelper |
|
| 80 | + * @param FederatedFileSharingFactory $federatedFileSharingFactory |
|
| 81 | + * @param ISecureRandom $secureRandom |
|
| 82 | + */ |
|
| 83 | + public function __construct(string $appName, |
|
| 84 | + IRequest $request, |
|
| 85 | + IUserManager $userManager, |
|
| 86 | + IConfig $config, |
|
| 87 | + IAppManager $appManager, |
|
| 88 | + IGroupManager $groupManager, |
|
| 89 | + IUserSession $userSession, |
|
| 90 | + AccountManager $accountManager, |
|
| 91 | + ILogger $logger, |
|
| 92 | + IFactory $l10nFactory, |
|
| 93 | + NewUserMailHelper $newUserMailHelper, |
|
| 94 | + FederatedFileSharingFactory $federatedFileSharingFactory, |
|
| 95 | + ISecureRandom $secureRandom) { |
|
| 96 | + parent::__construct($appName, |
|
| 97 | + $request, |
|
| 98 | + $userManager, |
|
| 99 | + $config, |
|
| 100 | + $groupManager, |
|
| 101 | + $userSession, |
|
| 102 | + $accountManager); |
|
| 103 | + |
|
| 104 | + $this->appManager = $appManager; |
|
| 105 | + $this->logger = $logger; |
|
| 106 | + $this->l10nFactory = $l10nFactory; |
|
| 107 | + $this->newUserMailHelper = $newUserMailHelper; |
|
| 108 | + $this->federatedFileSharingFactory = $federatedFileSharingFactory; |
|
| 109 | + $this->secureRandom = $secureRandom; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @NoAdminRequired |
|
| 114 | + * |
|
| 115 | + * returns a list of users |
|
| 116 | + * |
|
| 117 | + * @param string $search |
|
| 118 | + * @param int $limit |
|
| 119 | + * @param int $offset |
|
| 120 | + * @return DataResponse |
|
| 121 | + */ |
|
| 122 | + public function getUsers(string $search = '', $limit = null, $offset = 0): DataResponse { |
|
| 123 | + $user = $this->userSession->getUser(); |
|
| 124 | + $users = []; |
|
| 125 | + |
|
| 126 | + // Admin? Or SubAdmin? |
|
| 127 | + $uid = $user->getUID(); |
|
| 128 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 129 | + if($this->groupManager->isAdmin($uid)){ |
|
| 130 | + $users = $this->userManager->search($search, $limit, $offset); |
|
| 131 | + } else if ($subAdminManager->isSubAdmin($user)) { |
|
| 132 | + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
| 133 | + foreach ($subAdminOfGroups as $key => $group) { |
|
| 134 | + $subAdminOfGroups[$key] = $group->getGID(); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $users = []; |
|
| 138 | + foreach ($subAdminOfGroups as $group) { |
|
| 139 | + $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $users = array_keys($users); |
|
| 144 | + |
|
| 145 | + return new DataResponse([ |
|
| 146 | + 'users' => $users |
|
| 147 | + ]); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @NoAdminRequired |
|
| 152 | + * |
|
| 153 | + * returns a list of users and their data |
|
| 154 | + */ |
|
| 155 | + public function getUsersDetails(string $search = '', $limit = null, $offset = 0): DataResponse { |
|
| 156 | + $user = $this->userSession->getUser(); |
|
| 157 | + $users = []; |
|
| 158 | + |
|
| 159 | + // Admin? Or SubAdmin? |
|
| 160 | + $uid = $user->getUID(); |
|
| 161 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 162 | + if($this->groupManager->isAdmin($uid)){ |
|
| 163 | + $users = $this->userManager->search($search, $limit, $offset); |
|
| 164 | + } else if ($subAdminManager->isSubAdmin($user)) { |
|
| 165 | + $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user); |
|
| 166 | + foreach ($subAdminOfGroups as $key => $group) { |
|
| 167 | + $subAdminOfGroups[$key] = $group->getGID(); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $users = []; |
|
| 171 | + foreach ($subAdminOfGroups as $group) { |
|
| 172 | + $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search, $limit, $offset)); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + $users = array_keys($users); |
|
| 177 | + $usersDetails = []; |
|
| 178 | + foreach ($users as $key => $userId) { |
|
| 179 | + $userData = $this->getUserData($userId); |
|
| 180 | + // Do not insert empty entry |
|
| 181 | + if(!empty($userData)) { |
|
| 182 | + $usersDetails[$userId] = $userData; |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + return new DataResponse([ |
|
| 187 | + 'users' => $usersDetails |
|
| 188 | + ]); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @PasswordConfirmationRequired |
|
| 193 | + * @NoAdminRequired |
|
| 194 | + * |
|
| 195 | + * @param string $userid |
|
| 196 | + * @param string $password |
|
| 197 | + * @param string $email |
|
| 198 | + * @param array $groups |
|
| 199 | + * @return DataResponse |
|
| 200 | + * @throws OCSException |
|
| 201 | + */ |
|
| 202 | + public function addUser(string $userid, string $password = '', string $email='', array $groups = []): DataResponse { |
|
| 203 | + $user = $this->userSession->getUser(); |
|
| 204 | + $isAdmin = $this->groupManager->isAdmin($user->getUID()); |
|
| 205 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 206 | + |
|
| 207 | + if($this->userManager->userExists($userid)) { |
|
| 208 | + $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']); |
|
| 209 | + throw new OCSException('User already exists', 102); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + if($groups !== []) { |
|
| 213 | + foreach ($groups as $group) { |
|
| 214 | + if(!$this->groupManager->groupExists($group)) { |
|
| 215 | + throw new OCSException('group '.$group.' does not exist', 104); |
|
| 216 | + } |
|
| 217 | + if(!$isAdmin && !$subAdminManager->isSubAdminOfGroup($user, $this->groupManager->get($group))) { |
|
| 218 | + throw new OCSException('insufficient privileges for group '. $group, 105); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + } else { |
|
| 222 | + if(!$isAdmin) { |
|
| 223 | + throw new OCSException('no group specified (required for subadmins)', 106); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + $generatePasswordResetToken = false; |
|
| 228 | + if ($password === '') { |
|
| 229 | + if ($email === '') { |
|
| 230 | + throw new OCSException('To send a password link to the user an email address is required.', 108); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $password = $this->secureRandom->generate(10); |
|
| 234 | + // Make sure we pass the password_policy |
|
| 235 | + $password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()'); |
|
| 236 | + $generatePasswordResetToken = true; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + try { |
|
| 240 | + $newUser = $this->userManager->createUser($userid, $password); |
|
| 241 | + $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); |
|
| 242 | + |
|
| 243 | + foreach ($groups as $group) { |
|
| 244 | + $this->groupManager->get($group)->addUser($newUser); |
|
| 245 | + $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // Send new user mail only if a mail is set |
|
| 249 | + if ($email !== '') { |
|
| 250 | + $newUser->setEMailAddress($email); |
|
| 251 | + try { |
|
| 252 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($newUser, $generatePasswordResetToken); |
|
| 253 | + $this->newUserMailHelper->sendMail($newUser, $emailTemplate); |
|
| 254 | + } catch (\Exception $e) { |
|
| 255 | + $this->logger->logException($e, [ |
|
| 256 | + 'message' => "Can't send new user mail to $email", |
|
| 257 | + 'level' => \OCP\Util::ERROR, |
|
| 258 | + 'app' => 'ocs_api', |
|
| 259 | + ]); |
|
| 260 | + throw new OCSException('Unable to send the invitation mail', 109); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + return new DataResponse(); |
|
| 265 | + |
|
| 266 | + } catch (HintException $e ) { |
|
| 267 | + $this->logger->logException($e, [ |
|
| 268 | + 'message' => 'Failed addUser attempt with hint exception.', |
|
| 269 | + 'level' => \OCP\Util::WARN, |
|
| 270 | + 'app' => 'ocs_api', |
|
| 271 | + ]); |
|
| 272 | + throw new OCSException($e->getHint(), 107); |
|
| 273 | + } catch (\Exception $e) { |
|
| 274 | + $this->logger->logException($e, [ |
|
| 275 | + 'message' => 'Failed addUser attempt with exception.', |
|
| 276 | + 'level' => \OCP\Util::ERROR, |
|
| 277 | + 'app' => 'ocs_api', |
|
| 278 | + ]); |
|
| 279 | + throw new OCSException('Bad request', 101); |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * @NoAdminRequired |
|
| 285 | + * @NoSubAdminRequired |
|
| 286 | + * |
|
| 287 | + * gets user info |
|
| 288 | + * |
|
| 289 | + * @param string $userId |
|
| 290 | + * @return DataResponse |
|
| 291 | + * @throws OCSException |
|
| 292 | + */ |
|
| 293 | + public function getUser(string $userId): DataResponse { |
|
| 294 | + $data = $this->getUserData($userId); |
|
| 295 | + // getUserData returns empty array if not enough permissions |
|
| 296 | + if(empty($data)) { |
|
| 297 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 298 | + } |
|
| 299 | + return new DataResponse($data); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @NoAdminRequired |
|
| 304 | + * @NoSubAdminRequired |
|
| 305 | + * |
|
| 306 | + * gets user info from the currently logged in user |
|
| 307 | + * |
|
| 308 | + * @return DataResponse |
|
| 309 | + * @throws OCSException |
|
| 310 | + */ |
|
| 311 | + public function getCurrentUser(): DataResponse { |
|
| 312 | + $user = $this->userSession->getUser(); |
|
| 313 | + if ($user) { |
|
| 314 | + $data = $this->getUserData($user->getUID()); |
|
| 315 | + // rename "displayname" to "display-name" only for this call to keep |
|
| 316 | + // the API stable. |
|
| 317 | + $data['display-name'] = $data['displayname']; |
|
| 318 | + unset($data['displayname']); |
|
| 319 | + return new DataResponse($data); |
|
| 320 | + |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * @NoAdminRequired |
|
| 328 | + * @NoSubAdminRequired |
|
| 329 | + */ |
|
| 330 | + public function getEditableFields(): DataResponse { |
|
| 331 | + $permittedFields = []; |
|
| 332 | + |
|
| 333 | + // Editing self (display, email) |
|
| 334 | + if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 335 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 336 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 340 | + $federatedFileSharing = $this->federatedFileSharingFactory->get(); |
|
| 341 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 342 | + if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 343 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 344 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 345 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 346 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + return new DataResponse($permittedFields); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * @NoAdminRequired |
|
| 355 | + * @NoSubAdminRequired |
|
| 356 | + * @PasswordConfirmationRequired |
|
| 357 | + * |
|
| 358 | + * edit users |
|
| 359 | + * |
|
| 360 | + * @param string $userId |
|
| 361 | + * @param string $key |
|
| 362 | + * @param string $value |
|
| 363 | + * @return DataResponse |
|
| 364 | + * @throws OCSException |
|
| 365 | + */ |
|
| 366 | + public function editUser(string $userId, string $key, string $value): DataResponse { |
|
| 367 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 368 | + |
|
| 369 | + $targetUser = $this->userManager->get($userId); |
|
| 370 | + if($targetUser === null) { |
|
| 371 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + $permittedFields = []; |
|
| 375 | + if($targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 376 | + // Editing self (display, email) |
|
| 377 | + if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) { |
|
| 378 | + $permittedFields[] = 'display'; |
|
| 379 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 380 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + $permittedFields[] = 'password'; |
|
| 384 | + if ($this->config->getSystemValue('force_language', false) === false || |
|
| 385 | + $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 386 | + $permittedFields[] = 'language'; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + if ($this->appManager->isEnabledForUser('federatedfilesharing')) { |
|
| 390 | + $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 391 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 392 | + if ($shareProvider->isLookupServerUploadEnabled()) { |
|
| 393 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 394 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 395 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 396 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + // If admin they can edit their own quota |
|
| 401 | + if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 402 | + $permittedFields[] = 'quota'; |
|
| 403 | + } |
|
| 404 | + } else { |
|
| 405 | + // Check if admin / subadmin |
|
| 406 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 407 | + if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 408 | + || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 409 | + // They have permissions over the user |
|
| 410 | + $permittedFields[] = 'display'; |
|
| 411 | + $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME; |
|
| 412 | + $permittedFields[] = AccountManager::PROPERTY_EMAIL; |
|
| 413 | + $permittedFields[] = 'password'; |
|
| 414 | + $permittedFields[] = 'language'; |
|
| 415 | + $permittedFields[] = AccountManager::PROPERTY_PHONE; |
|
| 416 | + $permittedFields[] = AccountManager::PROPERTY_ADDRESS; |
|
| 417 | + $permittedFields[] = AccountManager::PROPERTY_WEBSITE; |
|
| 418 | + $permittedFields[] = AccountManager::PROPERTY_TWITTER; |
|
| 419 | + $permittedFields[] = 'quota'; |
|
| 420 | + } else { |
|
| 421 | + // No rights |
|
| 422 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + // Check if permitted to edit this field |
|
| 426 | + if(!in_array($key, $permittedFields)) { |
|
| 427 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 428 | + } |
|
| 429 | + // Process the edit |
|
| 430 | + switch($key) { |
|
| 431 | + case 'display': |
|
| 432 | + case AccountManager::PROPERTY_DISPLAYNAME: |
|
| 433 | + $targetUser->setDisplayName($value); |
|
| 434 | + break; |
|
| 435 | + case 'quota': |
|
| 436 | + $quota = $value; |
|
| 437 | + if($quota !== 'none' && $quota !== 'default') { |
|
| 438 | + if (is_numeric($quota)) { |
|
| 439 | + $quota = (float) $quota; |
|
| 440 | + } else { |
|
| 441 | + $quota = \OCP\Util::computerFileSize($quota); |
|
| 442 | + } |
|
| 443 | + if ($quota === false) { |
|
| 444 | + throw new OCSException('Invalid quota value '.$value, 103); |
|
| 445 | + } |
|
| 446 | + if($quota === 0) { |
|
| 447 | + $quota = 'default'; |
|
| 448 | + }else if($quota === -1) { |
|
| 449 | + $quota = 'none'; |
|
| 450 | + } else { |
|
| 451 | + $quota = \OCP\Util::humanFileSize($quota); |
|
| 452 | + } |
|
| 453 | + } |
|
| 454 | + $targetUser->setQuota($quota); |
|
| 455 | + break; |
|
| 456 | + case 'password': |
|
| 457 | + $targetUser->setPassword($value); |
|
| 458 | + break; |
|
| 459 | + case 'language': |
|
| 460 | + $languagesCodes = $this->l10nFactory->findAvailableLanguages(); |
|
| 461 | + if (!in_array($value, $languagesCodes, true) && $value !== 'en') { |
|
| 462 | + throw new OCSException('Invalid language', 102); |
|
| 463 | + } |
|
| 464 | + $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); |
|
| 465 | + break; |
|
| 466 | + case AccountManager::PROPERTY_EMAIL: |
|
| 467 | + if(filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { |
|
| 468 | + $targetUser->setEMailAddress($value); |
|
| 469 | + } else { |
|
| 470 | + throw new OCSException('', 102); |
|
| 471 | + } |
|
| 472 | + break; |
|
| 473 | + case AccountManager::PROPERTY_PHONE: |
|
| 474 | + case AccountManager::PROPERTY_ADDRESS: |
|
| 475 | + case AccountManager::PROPERTY_WEBSITE: |
|
| 476 | + case AccountManager::PROPERTY_TWITTER: |
|
| 477 | + $userAccount = $this->accountManager->getUser($targetUser); |
|
| 478 | + if ($userAccount[$key]['value'] !== $value) { |
|
| 479 | + $userAccount[$key]['value'] = $value; |
|
| 480 | + $this->accountManager->updateUser($targetUser, $userAccount); |
|
| 481 | + } |
|
| 482 | + break; |
|
| 483 | + default: |
|
| 484 | + throw new OCSException('', 103); |
|
| 485 | + } |
|
| 486 | + return new DataResponse(); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * @PasswordConfirmationRequired |
|
| 491 | + * @NoAdminRequired |
|
| 492 | + * |
|
| 493 | + * @param string $userId |
|
| 494 | + * @return DataResponse |
|
| 495 | + * @throws OCSException |
|
| 496 | + */ |
|
| 497 | + public function deleteUser(string $userId): DataResponse { |
|
| 498 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 499 | + |
|
| 500 | + $targetUser = $this->userManager->get($userId); |
|
| 501 | + |
|
| 502 | + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 503 | + throw new OCSException('', 101); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + // If not permitted |
|
| 507 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 508 | + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 509 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + // Go ahead with the delete |
|
| 513 | + if($targetUser->delete()) { |
|
| 514 | + return new DataResponse(); |
|
| 515 | + } else { |
|
| 516 | + throw new OCSException('', 101); |
|
| 517 | + } |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * @PasswordConfirmationRequired |
|
| 522 | + * @NoAdminRequired |
|
| 523 | + * |
|
| 524 | + * @param string $userId |
|
| 525 | + * @return DataResponse |
|
| 526 | + * @throws OCSException |
|
| 527 | + * @throws OCSForbiddenException |
|
| 528 | + */ |
|
| 529 | + public function disableUser(string $userId): DataResponse { |
|
| 530 | + return $this->setEnabled($userId, false); |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * @PasswordConfirmationRequired |
|
| 535 | + * @NoAdminRequired |
|
| 536 | + * |
|
| 537 | + * @param string $userId |
|
| 538 | + * @return DataResponse |
|
| 539 | + * @throws OCSException |
|
| 540 | + * @throws OCSForbiddenException |
|
| 541 | + */ |
|
| 542 | + public function enableUser(string $userId): DataResponse { |
|
| 543 | + return $this->setEnabled($userId, true); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * @param string $userId |
|
| 548 | + * @param bool $value |
|
| 549 | + * @return DataResponse |
|
| 550 | + * @throws OCSException |
|
| 551 | + */ |
|
| 552 | + private function setEnabled(string $userId, bool $value): DataResponse { |
|
| 553 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 554 | + |
|
| 555 | + $targetUser = $this->userManager->get($userId); |
|
| 556 | + if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) { |
|
| 557 | + throw new OCSException('', 101); |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + // If not permitted |
|
| 561 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 562 | + if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) { |
|
| 563 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + // enable/disable the user now |
|
| 567 | + $targetUser->setEnabled($value); |
|
| 568 | + return new DataResponse(); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * @NoAdminRequired |
|
| 573 | + * @NoSubAdminRequired |
|
| 574 | + * |
|
| 575 | + * @param string $userId |
|
| 576 | + * @return DataResponse |
|
| 577 | + * @throws OCSException |
|
| 578 | + */ |
|
| 579 | + public function getUsersGroups(string $userId): DataResponse { |
|
| 580 | + $loggedInUser = $this->userSession->getUser(); |
|
| 581 | + |
|
| 582 | + $targetUser = $this->userManager->get($userId); |
|
| 583 | + if($targetUser === null) { |
|
| 584 | + throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 588 | + // Self lookup or admin lookup |
|
| 589 | + return new DataResponse([ |
|
| 590 | + 'groups' => $this->groupManager->getUserGroupIds($targetUser) |
|
| 591 | + ]); |
|
| 592 | + } else { |
|
| 593 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 594 | + |
|
| 595 | + // Looking up someone else |
|
| 596 | + if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) { |
|
| 597 | + // Return the group that the method caller is subadmin of for the user in question |
|
| 598 | + /** @var IGroup[] $getSubAdminsGroups */ |
|
| 599 | + $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 600 | + foreach ($getSubAdminsGroups as $key => $group) { |
|
| 601 | + $getSubAdminsGroups[$key] = $group->getGID(); |
|
| 602 | + } |
|
| 603 | + $groups = array_intersect( |
|
| 604 | + $getSubAdminsGroups, |
|
| 605 | + $this->groupManager->getUserGroupIds($targetUser) |
|
| 606 | + ); |
|
| 607 | + return new DataResponse(['groups' => $groups]); |
|
| 608 | + } else { |
|
| 609 | + // Not permitted |
|
| 610 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 611 | + } |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * @PasswordConfirmationRequired |
|
| 618 | + * @NoAdminRequired |
|
| 619 | + * |
|
| 620 | + * @param string $userId |
|
| 621 | + * @param string $groupid |
|
| 622 | + * @return DataResponse |
|
| 623 | + * @throws OCSException |
|
| 624 | + */ |
|
| 625 | + public function addToGroup(string $userId, string $groupid = ''): DataResponse { |
|
| 626 | + if($groupid === '') { |
|
| 627 | + throw new OCSException('', 101); |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + $group = $this->groupManager->get($groupid); |
|
| 631 | + $targetUser = $this->userManager->get($userId); |
|
| 632 | + if($group === null) { |
|
| 633 | + throw new OCSException('', 102); |
|
| 634 | + } |
|
| 635 | + if($targetUser === null) { |
|
| 636 | + throw new OCSException('', 103); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + // If they're not an admin, check they are a subadmin of the group in question |
|
| 640 | + $loggedInUser = $this->userSession->getUser(); |
|
| 641 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 642 | + if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 643 | + throw new OCSException('', 104); |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + // Add user to group |
|
| 647 | + $group->addUser($targetUser); |
|
| 648 | + return new DataResponse(); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * @PasswordConfirmationRequired |
|
| 653 | + * @NoAdminRequired |
|
| 654 | + * |
|
| 655 | + * @param string $userId |
|
| 656 | + * @param string $groupid |
|
| 657 | + * @return DataResponse |
|
| 658 | + * @throws OCSException |
|
| 659 | + */ |
|
| 660 | + public function removeFromGroup(string $userId, string $groupid): DataResponse { |
|
| 661 | + $loggedInUser = $this->userSession->getUser(); |
|
| 662 | + |
|
| 663 | + if($groupid === null || trim($groupid) === '') { |
|
| 664 | + throw new OCSException('', 101); |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + $group = $this->groupManager->get($groupid); |
|
| 668 | + if($group === null) { |
|
| 669 | + throw new OCSException('', 102); |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + $targetUser = $this->userManager->get($userId); |
|
| 673 | + if($targetUser === null) { |
|
| 674 | + throw new OCSException('', 103); |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + // If they're not an admin, check they are a subadmin of the group in question |
|
| 678 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 679 | + if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) { |
|
| 680 | + throw new OCSException('', 104); |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + // Check they aren't removing themselves from 'admin' or their 'subadmin; group |
|
| 684 | + if ($targetUser->getUID() === $loggedInUser->getUID()) { |
|
| 685 | + if ($this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 686 | + if ($group->getGID() === 'admin') { |
|
| 687 | + throw new OCSException('Cannot remove yourself from the admin group', 105); |
|
| 688 | + } |
|
| 689 | + } else { |
|
| 690 | + // Not an admin, so the user must be a subadmin of this group, but that is not allowed. |
|
| 691 | + throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) { |
|
| 695 | + /** @var IGroup[] $subAdminGroups */ |
|
| 696 | + $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser); |
|
| 697 | + $subAdminGroups = array_map(function (IGroup $subAdminGroup) { |
|
| 698 | + return $subAdminGroup->getGID(); |
|
| 699 | + }, $subAdminGroups); |
|
| 700 | + $userGroups = $this->groupManager->getUserGroupIds($targetUser); |
|
| 701 | + $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups); |
|
| 702 | + |
|
| 703 | + if (count($userSubAdminGroups) <= 1) { |
|
| 704 | + // Subadmin must not be able to remove a user from all their subadmin groups. |
|
| 705 | + throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105); |
|
| 706 | + } |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + // Remove user from group |
|
| 710 | + $group->removeUser($targetUser); |
|
| 711 | + return new DataResponse(); |
|
| 712 | + } |
|
| 713 | + |
|
| 714 | + /** |
|
| 715 | + * Creates a subadmin |
|
| 716 | + * |
|
| 717 | + * @PasswordConfirmationRequired |
|
| 718 | + * |
|
| 719 | + * @param string $userId |
|
| 720 | + * @param string $groupid |
|
| 721 | + * @return DataResponse |
|
| 722 | + * @throws OCSException |
|
| 723 | + */ |
|
| 724 | + public function addSubAdmin(string $userId, string $groupid): DataResponse { |
|
| 725 | + $group = $this->groupManager->get($groupid); |
|
| 726 | + $user = $this->userManager->get($userId); |
|
| 727 | + |
|
| 728 | + // Check if the user exists |
|
| 729 | + if($user === null) { |
|
| 730 | + throw new OCSException('User does not exist', 101); |
|
| 731 | + } |
|
| 732 | + // Check if group exists |
|
| 733 | + if($group === null) { |
|
| 734 | + throw new OCSException('Group does not exist', 102); |
|
| 735 | + } |
|
| 736 | + // Check if trying to make subadmin of admin group |
|
| 737 | + if($group->getGID() === 'admin') { |
|
| 738 | + throw new OCSException('Cannot create subadmins for admin group', 103); |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 742 | + |
|
| 743 | + // We cannot be subadmin twice |
|
| 744 | + if ($subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 745 | + return new DataResponse(); |
|
| 746 | + } |
|
| 747 | + // Go |
|
| 748 | + if($subAdminManager->createSubAdmin($user, $group)) { |
|
| 749 | + return new DataResponse(); |
|
| 750 | + } else { |
|
| 751 | + throw new OCSException('Unknown error occurred', 103); |
|
| 752 | + } |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + /** |
|
| 756 | + * Removes a subadmin from a group |
|
| 757 | + * |
|
| 758 | + * @PasswordConfirmationRequired |
|
| 759 | + * |
|
| 760 | + * @param string $userId |
|
| 761 | + * @param string $groupid |
|
| 762 | + * @return DataResponse |
|
| 763 | + * @throws OCSException |
|
| 764 | + */ |
|
| 765 | + public function removeSubAdmin(string $userId, string $groupid): DataResponse { |
|
| 766 | + $group = $this->groupManager->get($groupid); |
|
| 767 | + $user = $this->userManager->get($userId); |
|
| 768 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 769 | + |
|
| 770 | + // Check if the user exists |
|
| 771 | + if($user === null) { |
|
| 772 | + throw new OCSException('User does not exist', 101); |
|
| 773 | + } |
|
| 774 | + // Check if the group exists |
|
| 775 | + if($group === null) { |
|
| 776 | + throw new OCSException('Group does not exist', 101); |
|
| 777 | + } |
|
| 778 | + // Check if they are a subadmin of this said group |
|
| 779 | + if(!$subAdminManager->isSubAdminOfGroup($user, $group)) { |
|
| 780 | + throw new OCSException('User is not a subadmin of this group', 102); |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + // Go |
|
| 784 | + if($subAdminManager->deleteSubAdmin($user, $group)) { |
|
| 785 | + return new DataResponse(); |
|
| 786 | + } else { |
|
| 787 | + throw new OCSException('Unknown error occurred', 103); |
|
| 788 | + } |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + /** |
|
| 792 | + * Get the groups a user is a subadmin of |
|
| 793 | + * |
|
| 794 | + * @param string $userId |
|
| 795 | + * @return DataResponse |
|
| 796 | + * @throws OCSException |
|
| 797 | + */ |
|
| 798 | + public function getUserSubAdminGroups(string $userId): DataResponse { |
|
| 799 | + $groups = $this->getUserSubAdminGroupsData($userId); |
|
| 800 | + return new DataResponse($groups); |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + /** |
|
| 804 | + * @NoAdminRequired |
|
| 805 | + * @PasswordConfirmationRequired |
|
| 806 | + * |
|
| 807 | + * resend welcome message |
|
| 808 | + * |
|
| 809 | + * @param string $userId |
|
| 810 | + * @return DataResponse |
|
| 811 | + * @throws OCSException |
|
| 812 | + */ |
|
| 813 | + public function resendWelcomeMessage(string $userId): DataResponse { |
|
| 814 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 815 | + |
|
| 816 | + $targetUser = $this->userManager->get($userId); |
|
| 817 | + if($targetUser === null) { |
|
| 818 | + throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND); |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + // Check if admin / subadmin |
|
| 822 | + $subAdminManager = $this->groupManager->getSubAdmin(); |
|
| 823 | + if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser) |
|
| 824 | + && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) { |
|
| 825 | + // No rights |
|
| 826 | + throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED); |
|
| 827 | + } |
|
| 828 | + |
|
| 829 | + $email = $targetUser->getEMailAddress(); |
|
| 830 | + if ($email === '' || $email === null) { |
|
| 831 | + throw new OCSException('Email address not available', 101); |
|
| 832 | + } |
|
| 833 | + $username = $targetUser->getUID(); |
|
| 834 | + $lang = $this->config->getUserValue($username, 'core', 'lang', 'en'); |
|
| 835 | + if (!$this->l10nFactory->languageExists('settings', $lang)) { |
|
| 836 | + $lang = 'en'; |
|
| 837 | + } |
|
| 838 | + |
|
| 839 | + $l10n = $this->l10nFactory->get('settings', $lang); |
|
| 840 | + |
|
| 841 | + try { |
|
| 842 | + $this->newUserMailHelper->setL10N($l10n); |
|
| 843 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false); |
|
| 844 | + $this->newUserMailHelper->sendMail($targetUser, $emailTemplate); |
|
| 845 | + } catch(\Exception $e) { |
|
| 846 | + $this->logger->logException($e, [ |
|
| 847 | + 'message' => "Can't send new user mail to $email", |
|
| 848 | + 'level' => \OCP\Util::ERROR, |
|
| 849 | + 'app' => 'settings', |
|
| 850 | + ]); |
|
| 851 | + throw new OCSException('Sending email failed', 102); |
|
| 852 | + } |
|
| 853 | + |
|
| 854 | + return new DataResponse(); |
|
| 855 | + } |
|
| 856 | 856 | } |
@@ -37,143 +37,143 @@ |
||
| 37 | 37 | |
| 38 | 38 | abstract class AUserData extends OCSController { |
| 39 | 39 | |
| 40 | - /** @var IUserManager */ |
|
| 41 | - protected $userManager; |
|
| 42 | - /** @var IConfig */ |
|
| 43 | - protected $config; |
|
| 44 | - /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface |
|
| 45 | - protected $groupManager; |
|
| 46 | - /** @var IUserSession */ |
|
| 47 | - protected $userSession; |
|
| 48 | - /** @var AccountManager */ |
|
| 49 | - protected $accountManager; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param string $appName |
|
| 53 | - * @param IRequest $request |
|
| 54 | - * @param IUserManager $userManager |
|
| 55 | - * @param IConfig $config |
|
| 56 | - * @param IGroupManager $groupManager |
|
| 57 | - * @param IUserSession $userSession |
|
| 58 | - * @param AccountManager $accountManager |
|
| 59 | - */ |
|
| 60 | - public function __construct(string $appName, |
|
| 61 | - IRequest $request, |
|
| 62 | - IUserManager $userManager, |
|
| 63 | - IConfig $config, |
|
| 64 | - IGroupManager $groupManager, |
|
| 65 | - IUserSession $userSession, |
|
| 66 | - AccountManager $accountManager) { |
|
| 67 | - parent::__construct($appName, $request); |
|
| 68 | - |
|
| 69 | - $this->userManager = $userManager; |
|
| 70 | - $this->config = $config; |
|
| 71 | - $this->groupManager = $groupManager; |
|
| 72 | - $this->userSession = $userSession; |
|
| 73 | - $this->accountManager = $accountManager; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * creates a array with all user data |
|
| 78 | - * |
|
| 79 | - * @param $userId |
|
| 80 | - * @return array |
|
| 81 | - * @throws OCSException |
|
| 82 | - */ |
|
| 83 | - protected function getUserData(string $userId): array { |
|
| 84 | - $currentLoggedInUser = $this->userSession->getUser(); |
|
| 85 | - |
|
| 86 | - $data = []; |
|
| 87 | - |
|
| 88 | - // Check if the target user exists |
|
| 89 | - $targetUserObject = $this->userManager->get($userId); |
|
| 90 | - if($targetUserObject === null) { |
|
| 91 | - throw new OCSNotFoundException('User does not exist'); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - // Should be at least Admin Or SubAdmin! |
|
| 95 | - if( $this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 96 | - || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
|
| 97 | - $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
|
| 98 | - } else { |
|
| 99 | - // Check they are looking up themselves |
|
| 100 | - if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 101 | - return $data; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // Get groups data |
|
| 106 | - $userAccount = $this->accountManager->getUser($targetUserObject); |
|
| 107 | - $groups = $this->groupManager->getUserGroups($targetUserObject); |
|
| 108 | - $gids = []; |
|
| 109 | - foreach ($groups as $group) { |
|
| 110 | - $gids[] = $group->getDisplayName(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - // Find the data |
|
| 114 | - $data['id'] = $targetUserObject->getUID(); |
|
| 115 | - $data['storageLocation'] = $targetUserObject->getHome(); |
|
| 116 | - $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000; |
|
| 117 | - $data['backend'] = $targetUserObject->getBackendClassName(); |
|
| 118 | - $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID()); |
|
| 119 | - $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
|
| 120 | - $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
|
| 121 | - $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
|
| 122 | - $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value']; |
|
| 123 | - $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value']; |
|
| 124 | - $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value']; |
|
| 125 | - $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value']; |
|
| 126 | - $data['groups'] = $gids; |
|
| 127 | - $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang'); |
|
| 128 | - |
|
| 129 | - return $data; |
|
| 40 | + /** @var IUserManager */ |
|
| 41 | + protected $userManager; |
|
| 42 | + /** @var IConfig */ |
|
| 43 | + protected $config; |
|
| 44 | + /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface |
|
| 45 | + protected $groupManager; |
|
| 46 | + /** @var IUserSession */ |
|
| 47 | + protected $userSession; |
|
| 48 | + /** @var AccountManager */ |
|
| 49 | + protected $accountManager; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param string $appName |
|
| 53 | + * @param IRequest $request |
|
| 54 | + * @param IUserManager $userManager |
|
| 55 | + * @param IConfig $config |
|
| 56 | + * @param IGroupManager $groupManager |
|
| 57 | + * @param IUserSession $userSession |
|
| 58 | + * @param AccountManager $accountManager |
|
| 59 | + */ |
|
| 60 | + public function __construct(string $appName, |
|
| 61 | + IRequest $request, |
|
| 62 | + IUserManager $userManager, |
|
| 63 | + IConfig $config, |
|
| 64 | + IGroupManager $groupManager, |
|
| 65 | + IUserSession $userSession, |
|
| 66 | + AccountManager $accountManager) { |
|
| 67 | + parent::__construct($appName, $request); |
|
| 68 | + |
|
| 69 | + $this->userManager = $userManager; |
|
| 70 | + $this->config = $config; |
|
| 71 | + $this->groupManager = $groupManager; |
|
| 72 | + $this->userSession = $userSession; |
|
| 73 | + $this->accountManager = $accountManager; |
|
| 130 | 74 | } |
| 131 | 75 | |
| 132 | - /** |
|
| 133 | - * Get the groups a user is a subadmin of |
|
| 134 | - * |
|
| 135 | - * @param string $userId |
|
| 136 | - * @return array |
|
| 137 | - * @throws OCSException |
|
| 138 | - */ |
|
| 139 | - protected function getUserSubAdminGroupsData(string $userId): array { |
|
| 140 | - $user = $this->userManager->get($userId); |
|
| 141 | - // Check if the user exists |
|
| 142 | - if($user === null) { |
|
| 143 | - throw new OCSNotFoundException('User does not exist'); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // Get the subadmin groups |
|
| 147 | - $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
|
| 148 | - $groups = []; |
|
| 149 | - foreach ($subAdminGroups as $key => $group) { |
|
| 150 | - $groups[] = $group->getGID(); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $groups; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @param string $userId |
|
| 158 | - * @return array |
|
| 159 | - * @throws \OCP\Files\NotFoundException |
|
| 160 | - */ |
|
| 161 | - protected function fillStorageInfo(string $userId): array { |
|
| 162 | - try { |
|
| 163 | - \OC_Util::tearDownFS(); |
|
| 164 | - \OC_Util::setupFS($userId); |
|
| 165 | - $storage = OC_Helper::getStorageInfo('/'); |
|
| 166 | - $data = [ |
|
| 167 | - 'free' => $storage['free'], |
|
| 168 | - 'used' => $storage['used'], |
|
| 169 | - 'total' => $storage['total'], |
|
| 170 | - 'relative' => $storage['relative'], |
|
| 171 | - 'quota' => $storage['quota'], |
|
| 172 | - ]; |
|
| 173 | - } catch (NotFoundException $ex) { |
|
| 174 | - $data = []; |
|
| 175 | - } |
|
| 176 | - return $data; |
|
| 177 | - } |
|
| 76 | + /** |
|
| 77 | + * creates a array with all user data |
|
| 78 | + * |
|
| 79 | + * @param $userId |
|
| 80 | + * @return array |
|
| 81 | + * @throws OCSException |
|
| 82 | + */ |
|
| 83 | + protected function getUserData(string $userId): array { |
|
| 84 | + $currentLoggedInUser = $this->userSession->getUser(); |
|
| 85 | + |
|
| 86 | + $data = []; |
|
| 87 | + |
|
| 88 | + // Check if the target user exists |
|
| 89 | + $targetUserObject = $this->userManager->get($userId); |
|
| 90 | + if($targetUserObject === null) { |
|
| 91 | + throw new OCSNotFoundException('User does not exist'); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + // Should be at least Admin Or SubAdmin! |
|
| 95 | + if( $this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
|
| 96 | + || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
|
| 97 | + $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true'); |
|
| 98 | + } else { |
|
| 99 | + // Check they are looking up themselves |
|
| 100 | + if($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
|
| 101 | + return $data; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // Get groups data |
|
| 106 | + $userAccount = $this->accountManager->getUser($targetUserObject); |
|
| 107 | + $groups = $this->groupManager->getUserGroups($targetUserObject); |
|
| 108 | + $gids = []; |
|
| 109 | + foreach ($groups as $group) { |
|
| 110 | + $gids[] = $group->getDisplayName(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + // Find the data |
|
| 114 | + $data['id'] = $targetUserObject->getUID(); |
|
| 115 | + $data['storageLocation'] = $targetUserObject->getHome(); |
|
| 116 | + $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000; |
|
| 117 | + $data['backend'] = $targetUserObject->getBackendClassName(); |
|
| 118 | + $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID()); |
|
| 119 | + $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
|
| 120 | + $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
|
| 121 | + $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
|
| 122 | + $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value']; |
|
| 123 | + $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value']; |
|
| 124 | + $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value']; |
|
| 125 | + $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value']; |
|
| 126 | + $data['groups'] = $gids; |
|
| 127 | + $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang'); |
|
| 128 | + |
|
| 129 | + return $data; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Get the groups a user is a subadmin of |
|
| 134 | + * |
|
| 135 | + * @param string $userId |
|
| 136 | + * @return array |
|
| 137 | + * @throws OCSException |
|
| 138 | + */ |
|
| 139 | + protected function getUserSubAdminGroupsData(string $userId): array { |
|
| 140 | + $user = $this->userManager->get($userId); |
|
| 141 | + // Check if the user exists |
|
| 142 | + if($user === null) { |
|
| 143 | + throw new OCSNotFoundException('User does not exist'); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // Get the subadmin groups |
|
| 147 | + $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
|
| 148 | + $groups = []; |
|
| 149 | + foreach ($subAdminGroups as $key => $group) { |
|
| 150 | + $groups[] = $group->getGID(); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $groups; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @param string $userId |
|
| 158 | + * @return array |
|
| 159 | + * @throws \OCP\Files\NotFoundException |
|
| 160 | + */ |
|
| 161 | + protected function fillStorageInfo(string $userId): array { |
|
| 162 | + try { |
|
| 163 | + \OC_Util::tearDownFS(); |
|
| 164 | + \OC_Util::setupFS($userId); |
|
| 165 | + $storage = OC_Helper::getStorageInfo('/'); |
|
| 166 | + $data = [ |
|
| 167 | + 'free' => $storage['free'], |
|
| 168 | + 'used' => $storage['used'], |
|
| 169 | + 'total' => $storage['total'], |
|
| 170 | + 'relative' => $storage['relative'], |
|
| 171 | + 'quota' => $storage['quota'], |
|
| 172 | + ]; |
|
| 173 | + } catch (NotFoundException $ex) { |
|
| 174 | + $data = []; |
|
| 175 | + } |
|
| 176 | + return $data; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | 179 | } |