protected function execute(InputInterface $input, OutputInterface $output) { $logger = $this->getService('logger'); $path = $input->getOption('path'); $user_name = $input->getArgument('username'); $password = $input->getOption('password'); $admin = $input->getOption('admin'); $active = $input->getOption('active'); if ($input->getOption('ask-password')) { $helper = $this->getHelper('question'); $question = new Question("Please enter the new password for user {$user_name}: ", null); defined('PHPUNIT_SUGARCLI_TESTSUITE') || $question->setHidden(true); defined('PHPUNIT_SUGARCLI_TESTSUITE') || $question->setHiddenFallback(true); $password = $helper->ask($input, $output, $question); } $additionnal_fields = array(); foreach ($this->fields_mapping as $option => $field_name) { $value = $input->getOption($option); if (!is_null($value)) { $additionnal_fields[$field_name] = $value; } } try { $um = new UsersManager($this->getService('sugarcrm.entrypoint')); if ($this->isCreate($input)) { $um->createUser($user_name, $additionnal_fields); // Users are active by default. if (is_null($active)) { $active = true; } } else { $um->updateUser($user_name, $additionnal_fields); } if (!is_null($admin)) { $um->setAdmin($user_name, $this->getBoolean($admin)); } if (!is_null($active)) { $um->setActive($user_name, $this->getBoolean($active)); } if (!is_null($password)) { $um->setPassword($user_name, $password); } } catch (BeanNotFoundException $e) { $logger->error("User '{$user_name}' doesn't exists on the SugarCRM located at '{$path}'."); return ExitCode::EXIT_USER_NOT_FOUND; } }
public function testGetUsers() { // Create User $user_name = 'test_user'; $sugar = $this->getEntryPointInstance(); $this->cleanUsers($user_name); $um = new UsersManager($sugar); $user_id = $um->createUser($user_name, array('is_admin' => 1)); $this->assertRegExp('/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/', $user_id); // Fetch user $user_bean = $um->getUserBeanByName($user_name); $this->assertEquals($user_id, $user_bean->id); $this->assertEquals(1, $user_bean->is_admin); $this->assertEquals($user_id, $um->getUserIdByName($user_name)); // Deactivate $um->deactivate($user_name); $user_bean = $um->getUserBeanByName($user_name); $this->assertEquals('Inactive', $user_bean->status); // Activate $um->activate($user_name); $user_bean = $um->getUserBeanByName($user_name); $this->assertEquals('Active', $user_bean->status); //Unset admin $um->setAdmin($user_name, false); $user_bean = $um->getUserBeanByName($user_name); $this->assertEquals(0, $user_bean->is_admin); // Set email $email = '*****@*****.**'; $um->updateUser($user_name, array('email1' => $email)); $user_bean = $um->getUserBeanByName($user_name); // Test a second time because it might fail sometimes. $email = '*****@*****.**'; $um->updateUser($user_name, array('email1' => $email)); $user_bean = $um->getUserBeanByName($user_name); $this->assertEquals($email, $user_bean->email1); //Set password $um->setPassword($user_name, 'test_password'); $user_bean = $um->getUserBeanByName($user_name); $this->assertTrue($user_bean->authenticate_user(md5('test_password'))); $mock = $this->getMock('Inet\\SugarCRM\\UsersManager', array('getUserBeanByName'), array($sugar)); $mock->method('getUserBeanByName')->willReturn((object) array('id' => $user_id, 'table_name' => 'users')); $mock->setPassword($user_name, 'test_password2'); $user_bean = $um->getUserBeanByName($user_name); $this->assertEquals(md5('test_password2'), $user_bean->user_hash); $this->assertTrue($user_bean->authenticate_user(md5('test_password2'))); }