I’m messing with the docker container and found the following code:
There is a function create_default_users() in suitecrm/install/install_utils.php:
function create_default_users()
{
global $setup_site_admin_password;
global $setup_site_admin_user_name;
global $create_default_user;
global $sugar_config;
require_once('install/UserDemoData.php');
//Create default admin user
$user = BeanFactory::newBean('Users');
$user->id = 1;
$user->new_with_id = true;
$user->last_name = 'Administrator';
$user->user_name = $setup_site_admin_user_name;
$user->title = "Administrator";
$user->status = 'Active';
$user->is_admin = true;
$user->employee_status = 'Active';
$user->user_hash = User::getPasswordHash($setup_site_admin_password);
$GLOBALS['current_user'] = $user;
$GLOBALS['current_user']->save();
if ($create_default_user) {
$default_user = BeanFactory::newBean('Users');
$default_user->last_name = $sugar_config['default_user_name'];
$default_user->user_name = $sugar_config['default_user_name'];
$default_user->status = 'Active';
if (isset($sugar_config['default_user_is_admin']) && $sugar_config['default_user_is_admin']) {
$default_user->is_admin = true;
}
$default_user->user_hash = User::getPasswordHash($sugar_config['default_password']);
$default_user->save();
}
}
That makes sense to me. Is there any way I can call that from the CLI? I’m trying to be able to programatically create users without creating any new code snippets. Is there any way to do that, or should I be writing my own PHP script?
If that’s the case, what type of framework would be needed? Can I just insert those lines or do I need various import or class definition or… something?
Sorry, I deal more with bash and ansible and python scripting than PHP application code, so while I can read the code, it’s hard for me to understand the framework and setting up that I need to do.
You might have a look at the way Robo tasks are structured, that’s the best way to add a command-line function to SuiteCRM. If ti works well we can add it in core.
There’s a couple of roadblocks so far. For one, the docker container doesn’t seem to have robo set up. For another, the robo.li documentation is giving me a 404.
I’m still trying to work through it, but those are some pretty tall roadblocks.
This is with this file in lib/Robo/Plugin/Commands/CreateUserCommands.php:
<?php
/**
*
* SugarCRM Community Edition is a customer relationship management program developed by
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
*
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
* Copyright (C) 2011 - 2019 SalesAgility Ltd.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
* SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
* reasonably feasible for technical reasons, the Appropriate Legal Notices must
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
*/
namespace SuiteCRM\Robo\Plugin\Commands;
use DBManager;
use Robo\Tasks;
use SuiteCRM\Robo\Traits\RoboTrait;
use SuiteCRM\Robo\Traits\CliRunnerTrait;
use Api\V8\BeanDecorator\BeanManager;
use DBManagerFactory;
use User;
class CreateUserCommands extends Tasks
{
use RoboTrait;
use CliRunnerTrait;
/**
* @var DBManager
*/
protected $db;
/**
* @var BeanManager
*/
protected $beanManager;
/**
* @var array
*/
protected static $beanAliases = [
User::class => 'Users',
OAuth2Clients::class => 'OAuth2Clients',
];
/**
* CreateUserCommands constructor
*/
public function __construct()
{
$this->bootstrap();
$this->db = DBManagerFactory::getInstance();
$this->beanManager = new BeanManager($this->db, static::$beanAliases);
}
/**
* Creates a SuiteCRM user
* @param string $username
* @param string $password
* @param string $is_admin
* @return void
*/
public function createUser($username, $password, $is_admin)
{
$count = $this->getNameCount($username, 'users', 'user_name');
if ($count > 1) {
$this->io()->title('User Already Exists');
return;
};
global $current_user;
$current_user->is_admin = '1';
$userBean = $this->beanManager->newBeanSafe(
User::class
);
$userBean->user_name = $username;
$userBean->first_name = $username;
$userBean->last_name = $username;
$userBean->status = 'Active';
$userBean->title = "Administrator";
$userBean->employee_status = 'Active';
$userBean->is_admin = filter_var($is_admin, FILTER_VALIDATE_BOOLEAN);
$userBean->save();
$userBean->setNewPassword($password, 1);
$this->io()->title('Successfully Created User');
}
/**
* Returns the number of duplicate name records from a table
* @param string $name
* @param string $table
* @param string $row
* @return int
*/
private function getNameCount($name, $table, $row)
{
$nameQuoted = $this->db->quoted($name);
$query = <<<SQL
SELECT
count(`id`) AS `count`
FROM
`$table`
WHERE
`$row` LIKE '$nameQuoted %'
SQL;
$result = $this->db->fetchOne($query);
$count = $result
? (int)$result['count']
: 0;
$count++;
return $count;
}
}
And the following files ammended next to ApiCommands to put CreateUserCommands:
FWIW, this is a really ugly script, and single-purpose. Feel free to take this and modify it, but I don’t think I’ll be submitting a PR for this unless there’s other use-cases or some serious cleanup done to make it nice and more extensible.