Exemplo n.º 1
0
 public function testGetApps()
 {
     $user = $this->generateUsers();
     $this->groupManager->get('admin')->addUser($user);
     $this->userSession->setUser($user);
     $result = $this->api->getApps([]);
     $this->assertTrue($result->succeeded());
     $data = $result->getData();
     $this->assertEquals(count(\OC_App::listAllApps()), count($data['apps']));
 }
Exemplo n.º 2
0
 /**
  * become another user
  * @UseSession
  */
 public function impersonate($userid)
 {
     $users = $this->userManager->search($userid, 1, 0);
     if (count($users) > 0) {
         $user = array_shift($users);
         if (strcasecmp($user->getUID(), $userid) === 0) {
             $this->userSession->setUser($user);
         }
     }
     return new JSONResponse();
 }
Exemplo n.º 3
0
 public function testGetApps()
 {
     $this->ocsClient->expects($this->any())->method($this->anything())->will($this->returnValue(null));
     $user = $this->generateUsers();
     $this->groupManager->get('admin')->addUser($user);
     $this->userSession->setUser($user);
     $result = $this->api->getApps([]);
     $this->assertTrue($result->succeeded());
     $data = $result->getData();
     $this->assertEquals(count(\OC_App::listAllApps(false, true, $this->ocsClient)), count($data['apps']));
 }
Exemplo n.º 4
0
 /**
  * become another user
  * @param string $userid
  * @UseSession
  * @return JSONResponse
  */
 public function impersonate($userid)
 {
     $oldUserId = $this->userSession->getUser()->getUID();
     $this->logger->warning("User {$oldUserId} trying to impersonate user {$userid}", ['app' => 'impersonate']);
     $user = $this->userManager->get($userid);
     if ($user === null) {
         return new JSONResponse("No user found for {$userid}", Http::STATUS_NOT_FOUND);
     } else {
         $this->logger->warning("changing to user {$userid}", ['app' => 'impersonate']);
         $this->userSession->setUser($user);
     }
     return new JSONResponse();
 }
Exemplo n.º 5
0
 /**
  * @param string $userId
  * @return \OCP\Files\Folder
  * @throws SetUpException
  */
 public function setUpUserHome($userId = null)
 {
     if (is_null($userId)) {
         $user = $this->userSession->getUser();
     } else {
         $user = $this->userManager->get($userId);
     }
     if (is_null($user) || !$this->userManager->userExists($user->getUID())) {
         throw new SetUpException('could not set up user home for ' . json_encode($user));
     }
     if ($user !== $this->userSession->getUser()) {
         \OC_Util::tearDownFS();
         $this->userSession->setUser($user);
     }
     \OC_Util::setupFS($user->getUID());
     return $this->getOrCreateSubFolder($this->rootFolder, '/' . $user->getUID());
 }
Exemplo n.º 6
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $userId = $input->getArgument('user_id');
     if (!empty($userId)) {
         $user = $this->userManager->get($userId);
         if (is_null($user)) {
             $output->writeln("<error>user {$userId} not found</error>");
             return;
         }
         $this->userSession->setUser($user);
         $storageService = $this->userService;
     } else {
         $storageService = $this->globalService;
     }
     /** @var  $mounts StorageConfig[] */
     $mounts = $storageService->getAllStorages();
     $this->listMounts($userId, $mounts, $input, $output);
 }
Exemplo n.º 7
0
 public function setUp()
 {
     $app = new Application();
     $container = $app->getContainer();
     // reset backend
     $um = $container->getServer()->getUserManager();
     $this->userSession = $container->getServer()->getUserSession();
     $um->clearBackends();
     $um->registerBackend(new \OC_User_Database());
     // create test user
     $this->userName = '******';
     \OC_User::deleteUser($this->userName);
     $um->createUser($this->userName, $this->userName);
     \OC_Util::tearDownFS();
     $this->userSession->setUser(null);
     Filesystem::tearDown();
     \OC_Util::setupFS($this->userName);
     $this->userSession->setUser($um->get($this->userName));
     $view = new \OC\Files\View('/' . $this->userName . '/files');
     // setup files
     $filesToCopy = array('documents' => array('document.pdf', 'document.docx', 'document.odt', 'document.txt'));
     $count = 0;
     foreach ($filesToCopy as $folder => $files) {
         foreach ($files as $file) {
             $imgData = file_get_contents(__DIR__ . '/data/' . $file);
             $view->mkdir($folder);
             $path = $folder . '/' . $file;
             $view->file_put_contents($path, $imgData);
             // set mtime to get fixed sorting with respect to recentFiles
             $count++;
             $view->touch($path, 1000 + $count);
         }
     }
     list($storage, ) = $view->resolvePath('');
     /** @var $storage Storage */
     $this->storage = $storage;
     $this->scanner = $storage->getScanner();
     $this->scanner->scan('');
 }
Exemplo n.º 8
0
 protected function getStorageService($userId)
 {
     if (!empty($userId)) {
         $user = $this->userManager->get($userId);
         if (is_null($user)) {
             throw new NoUserException("user {$userId} not found");
         }
         $this->userSession->setUser($user);
         return $this->userService;
     } else {
         return $this->globalService;
     }
 }
Exemplo n.º 9
0
 public function testSubAdminOfGroupAlreadySubAdmin()
 {
     $user1 = $this->generateUsers();
     $user2 = $this->generateUsers();
     $this->userSession->setUser($user1);
     $this->groupManager->get('admin')->addUser($user1);
     $group1 = $this->groupManager->createGroup($this->getUniqueID());
     //Make user2 subadmin of group1
     $_POST['groupid'] = $group1->getGID();
     $result = $this->api->addSubAdmin(['userid' => $user2->getUID()]);
     $this->assertInstanceOf('OC_OCS_Result', $result);
     $this->assertTrue($result->succeeded());
     //Make user2 subadmin of group1 again
     $_POST['groupid'] = $group1->getGID();
     $result = $this->api->addSubAdmin(['userid' => $user2->getUID()]);
     $this->assertInstanceOf('OC_OCS_Result', $result);
     $this->assertTrue($result->succeeded());
     $group1->delete();
 }
Exemplo n.º 10
0
 public function testGetSubAdminsOfGroup()
 {
     $user1 = $this->generateUsers();
     $user2 = $this->generateUsers();
     $this->userSession->setUser($user1);
     $this->groupManager->get('admin')->addUser($user1);
     $group1 = $this->groupManager->createGroup($this->getUniqueID());
     \OC_SubAdmin::createSubAdmin($user2->getUID(), $group1->getGID());
     $result = $this->api->getSubAdminsOfGroup(['groupid' => $group1->getGID()]);
     $this->assertInstanceOf('OC_OCS_Result', $result);
     $this->assertTrue($result->succeeded());
     $data = $result->getData();
     $this->assertEquals($user2->getUID(), reset($data));
     $group1->delete();
     $user1 = $this->generateUsers();
     $this->userSession->setUser($user1);
     $this->groupManager->get('admin')->addUser($user1);
     $result = $this->api->getSubAdminsOfGroup(['groupid' => $this->getUniqueID()]);
     $this->assertInstanceOf('OC_OCS_Result', $result);
     $this->assertFalse($result->succeeded());
     $this->assertEquals(101, $result->getStatusCode());
 }
Exemplo n.º 11
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $userId = $input->getArgument('user_id');
     if (!empty($userId)) {
         $user = $this->userManager->get($userId);
         if (is_null($user)) {
             $output->writeln("<error>user {$userId} not found</error>");
             return;
         }
         $this->userSession->setUser($user);
         $storageService = $this->userService;
     } else {
         $storageService = $this->globalService;
     }
     /** @var  $mounts StorageConfig[] */
     $mounts = $storageService->getAllStorages();
     if (count($mounts) === 0) {
         if ($userId) {
             $output->writeln("<info>No mounts configured by {$userId}</info>");
         } else {
             $output->writeln("<info>No admin mounts configured</info>");
         }
         return;
     }
     $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options'];
     if (!$userId) {
         $headers[] = 'Applicable Users';
         $headers[] = 'Applicable Groups';
     }
     if (!$input->getOption('show-password')) {
         $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key'];
         foreach ($mounts as $mount) {
             $config = $mount->getBackendOptions();
             foreach ($config as $key => $value) {
                 if (in_array($key, $hideKeys)) {
                     $mount->setBackendOption($key, '***');
                 }
             }
         }
     }
     $outputType = $input->getOption('output');
     if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
         $keys = array_map(function ($header) {
             return strtolower(str_replace(' ', '_', $header));
         }, $headers);
         $pairs = array_map(function (StorageConfig $config) use($keys, $userId) {
             $values = [$config->getId(), $config->getMountPoint(), $config->getBackend()->getStorageClass(), $config->getAuthMechanism()->getScheme(), $config->getBackendOptions(), $config->getMountOptions()];
             if (!$userId) {
                 $values[] = $config->getApplicableUsers();
                 $values[] = $config->getApplicableGroups();
             }
             return array_combine($keys, $values);
         }, $mounts);
         if ($outputType === self::OUTPUT_FORMAT_JSON) {
             $output->writeln(json_encode(array_values($pairs)));
         } else {
             $output->writeln(json_encode(array_values($pairs), JSON_PRETTY_PRINT));
         }
     } else {
         $full = $input->getOption('full');
         $defaultMountOptions = ['encrypt' => true, 'previews' => true, 'filesystem_check_changes' => 1];
         $rows = array_map(function (StorageConfig $config) use($userId, $defaultMountOptions, $full) {
             $storageConfig = $config->getBackendOptions();
             $keys = array_keys($storageConfig);
             $values = array_values($storageConfig);
             if (!$full) {
                 $values = array_map(function ($value) {
                     if (is_string($value) && strlen($value) > 32) {
                         return substr($value, 0, 6) . '...' . substr($value, -6, 6);
                     } else {
                         return $value;
                     }
                 }, $values);
             }
             $configStrings = array_map(function ($key, $value) {
                 return $key . ': ' . json_encode($value);
             }, $keys, $values);
             $configString = implode(', ', $configStrings);
             $mountOptions = $config->getMountOptions();
             // hide defaults
             foreach ($mountOptions as $key => $value) {
                 if ($value === $defaultMountOptions[$key]) {
                     unset($mountOptions[$key]);
                 }
             }
             $keys = array_keys($mountOptions);
             $values = array_values($mountOptions);
             $optionsStrings = array_map(function ($key, $value) {
                 return $key . ': ' . json_encode($value);
             }, $keys, $values);
             $optionsString = implode(', ', $optionsStrings);
             $values = [$config->getId(), $config->getMountPoint(), $config->getBackend()->getText(), $config->getAuthMechanism()->getText(), $configString, $optionsString];
             if (!$userId) {
                 $applicableUsers = implode(', ', $config->getApplicableUsers());
                 $applicableGroups = implode(', ', $config->getApplicableGroups());
                 if ($applicableUsers === '' && $applicableGroups === '') {
                     $applicableUsers = 'All';
                 }
                 $values[] = $applicableUsers;
                 $values[] = $applicableGroups;
             }
             return $values;
         }, $mounts);
         $table = new Table($output);
         $table->setHeaders($headers);
         $table->setRows($rows);
         $table->render();
     }
 }