Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
UserCreateCommand
and use from Web UI
Fixes #23837
- Loading branch information
Showing
3 changed files
with
222 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php | ||
# MantisBT - A PHP based bugtracking system | ||
|
||
# MantisBT is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 2 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# MantisBT 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 General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
require_api( 'authentication_api.php' ); | ||
require_api( 'constant_inc.php' ); | ||
require_api( 'config_api.php' ); | ||
require_api( 'helper_api.php' ); | ||
require_api( 'user_api.php' ); | ||
|
||
use Mantis\Exceptions\ClientException; | ||
|
||
/** | ||
* A command that creates a user account. | ||
* | ||
* Sample: | ||
* { | ||
* "data": { | ||
* "username": "vboctor", | ||
* "password": "p@ssw0rd", | ||
* "real_name": "Victor Boctor", | ||
* "email": "vboctor@example.com", | ||
* "access_level": { "name" => "developer" }, | ||
* "enabled": true, | ||
* "protected": false | ||
* } | ||
* } | ||
*/ | ||
class UserCreateCommand extends Command { | ||
/** | ||
* @var string user name | ||
*/ | ||
private $username; | ||
|
||
/** | ||
* @var string user real name | ||
*/ | ||
private $realname; | ||
|
||
/** | ||
* @var string user email address | ||
*/ | ||
private $email; | ||
|
||
/** | ||
* @var string user password | ||
*/ | ||
private $password; | ||
|
||
/** | ||
* @var boolean user can edit their information. Usually, true for shared accounts. | ||
*/ | ||
private $protected; | ||
|
||
/** | ||
* @var boolean user account enabled for login. | ||
*/ | ||
private $enabled; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param array $p_data The command data. | ||
*/ | ||
function __construct( array $p_data ) { | ||
parent::__construct( $p_data ); | ||
} | ||
|
||
/** | ||
* Validate the data. | ||
*/ | ||
function validate() { | ||
# Ensure user has access level to create users | ||
access_ensure_global_level( config_get_global( 'manage_user_threshold' ) ); | ||
|
||
# Access Level | ||
$this->access_level = access_parse_array( | ||
$this->payload( | ||
'access_level', | ||
array( 'id' => config_get_global( 'default_new_account_access_level' ) ) ) ); | ||
|
||
# Don't allow the creation of accounts with access levels higher than that of | ||
# the user creating the account. | ||
access_ensure_global_level( $this->access_level ); | ||
|
||
# Username and Real Name | ||
$this->username = trim( $this->payload( 'username', '' ) ); | ||
if( is_blank( $this->username ) ) { | ||
throw new ClientException( 'username must be specified', ERROR_EMPTY_FIELD ); | ||
} | ||
|
||
$this->realname = string_normalize( $this->payload( 'real_name', '' ) ); | ||
user_ensure_name_valid( $this->username ); | ||
user_ensure_realname_unique( $this->username, $this->realname ); | ||
|
||
# Protected and Enabled Flags | ||
$this->protected = $this->payload( 'protected', false ); | ||
$this->enabled = $this->payload( 'enabled', true ); | ||
|
||
$this->email = trim( $this->payload( 'email', '' ) ); | ||
email_ensure_not_disposable( $this->email ); | ||
|
||
# Password | ||
$this->password = $this->payload( 'password', '' ); | ||
if( ( ON == config_get( 'send_reset_password' ) ) && | ||
( ON == config_get( 'enable_email_notification' ) ) ) { | ||
# Check code will be sent to the user directly via email. Dummy password set to random | ||
# Create random password | ||
$this->password = auth_generate_random_password(); | ||
} else { | ||
$this->password = $this->payload( 'password', auth_generate_random_password() ); | ||
} | ||
} | ||
|
||
/** | ||
* Process the command. | ||
* | ||
* @returns array Command response | ||
*/ | ||
protected function process() { | ||
# Need to send the user creation mail in the tracker language, not in the creating admin's language | ||
# Park the current language name until the user has been created | ||
lang_push( config_get_global( 'default_language' ) ); | ||
|
||
# create the user | ||
$t_admin_name = user_get_name( auth_get_current_user_id() ); | ||
user_create( | ||
$this->username, | ||
$this->password, | ||
$this->email, | ||
$this->access_level, | ||
$this->protected, | ||
$this->enabled, | ||
$this->realname, | ||
$t_admin_name ); | ||
|
||
# set language back to user language | ||
lang_pop(); | ||
|
||
return array( | ||
'id' => user_get_id_by_name( $this->username ) | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters