Merge pull request #4492 from humhub/enh/user-cli

Enh #4475: Add Console UserAdmin Command
This commit is contained in:
Lucas Bartholemy 2020-10-15 12:08:34 +02:00 committed by GitHub
commit 4ef4fa4128
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 125 additions and 4 deletions

View File

@ -58,6 +58,8 @@ HumHub Changelog
- Fix #4473: Catch all Throwables when loading module configuration
- Fix #4474: Loader not removed from event trigger
- Enh #4476: Reworked TimeAgo widget
- Chng #4482: Removed old legacy richtext editor which is deprecated since v1.3
- Enh #4475: Add Console UserAdmin Command
- Chg #4482: Removed old legacy richtext editor which is deprecated since v1.3
- Enh #3851: Migrate components from ZendFramework to Laminas
- Fix #4354: Set `about` as target url of space invitation notification

View File

@ -0,0 +1,109 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\user\commands;
use yii\console\Controller;
use yii\console\ExitCode;
use humhub\modules\user\models\User;
use humhub\modules\user\models\Profile;
use humhub\modules\user\models\Password;
use humhub\modules\user\models\Group;
/**
* Console User management
*
* Example usage:
* php yii user/create "john.doe" "jd@example.com" "John" "Doe"
* php yii user/set-password "john.doe" "secret"
* php yii user/make-admin "john.doe"
*
* @since 1.7
* @author Luke
* @author Michael Riedmann
* @author Mathieu Brunot
*/
class UserController extends Controller
{
/**
* Creates a new user account.
*/
public function actionCreate(string $userName, string $email, string $firstName, string $lastName)
{
$user = new User();
$user->scenario = User::SCENARIO_EDIT_ADMIN;
$user->load(['username' => $userName, 'email' => $email], '');
$user->validate();
$profile = new Profile();
$profile->scenario = Profile::SCENARIO_EDIT_ADMIN;
$profile->load(['firstname' => $firstName, 'lastname' => $lastName], '');
$profile->validate();
if ($user->hasErrors() || $profile->hasErrors()) {
$this->stderr("Could not create user!\n\n");
$this->stderr("Validation errors:\n");
print_r($user->getErrors());
print_r($profile->getErrors());
return ExitCode::UNSPECIFIED_ERROR;
}
if ($user->save()) {
$profile->user_id = $user->id;
if ($profile->save()) {
$this->stdout('User with ID ' . $user->id . ' successfully created!');
return ExitCode::OK;
}
}
$this->stderr("Could not create user!\n\n");
return ExitCode::UNSPECIFIED_ERROR;
}
/**
* Sets the password for a user account
*/
public function actionSetPassword(string $username, string $password)
{
/** @var User $user */
$user = User::find()->where(['username' => $username])->one();
if ($user === null) {
$this->stderr("Could not find user!\n\n");
return ExitCode::UNSPECIFIED_ERROR;
}
$passwordModel = new Password();
$passwordModel->user_id = $user->id;
$passwordModel->setPassword($password);
$passwordModel->save();
$this->stdout("Password for user with ID " . $user->id . " successfully created!\n\n");
return ExitCode::OK;
}
/**
* Add user to the admin group
*/
public function actionMakeAdmin(string $username)
{
/** @var User $user */
$user = User::find()->where(['username' => $username])->one();
if ($user === null) {
$this->stderr("Could not find user!\n\n");
return ExitCode::UNSPECIFIED_ERROR;
}
Group::getAdminGroup()->addUser($user);
$this->stdout("User with ID " . $user->id . " successfully added to the administrator group!\n\n");
return ExitCode::OK;
}
}

View File

@ -14,6 +14,9 @@ return [
'urlManagerRules' => [
['class' => 'humhub\modules\user\components\UrlRule']
],
'consoleControllerMap' => [
'user' => 'humhub\modules\user\commands\UserController'
],
'events' => [
['class' => Search::class, 'event' => Search::EVENT_ON_REBUILD, 'callback' => [Events::class, 'onSearchRebuild']],
['class' => ContentActiveRecord::class, 'event' => ContentActiveRecord::EVENT_BEFORE_DELETE, 'callback' => [Events::class, 'onContentDelete']],
@ -22,4 +25,4 @@ return [
['class' => CronController::class, 'event' => CronController::EVENT_ON_HOURLY_RUN, 'callback' => [Events::class, 'onHourlyCron']],
]
];
?>
?>

View File

@ -87,6 +87,13 @@ class User extends ContentContainerActiveRecord implements IdentityInterface, Se
const USERGROUP_USER = 'u_user';
const USERGROUP_GUEST = 'u_guest';
/**
* Scenarios
*/
const SCENARIO_EDIT_ADMIN = 'editAdmin';
const SCENARIO_LOGIN = 'login';
const SCENARIO_REGISTRATION = 'registration';
/**
* @event Event an event that is triggered when the user visibility is checked via [[isVisible()]].
*/
@ -367,8 +374,8 @@ class User extends ContentContainerActiveRecord implements IdentityInterface, Se
/**
* Specifies whether the user should appear in user lists or in the search.
*
* @since 1.2.3
* @return boolean is visible
* @since 1.2.3
*/
public function isVisible()
{
@ -398,8 +405,8 @@ class User extends ContentContainerActiveRecord implements IdentityInterface, Se
/**
*
* @since 1.3
* @throws Exception
* @since 1.3
*/
public function softDelete()
{
@ -739,7 +746,7 @@ class User extends ContentContainerActiveRecord implements IdentityInterface, Se
return true;
}
if((new PermissionManager(['subject' => $this]))->can([ManageUsers::class, ManageGroups::class])) {
if ((new PermissionManager(['subject' => $this]))->can([ManageUsers::class, ManageGroups::class])) {
return true;
}