mirror of
https://github.com/humhub/humhub.git
synced 2025-03-14 12:09:44 +01:00
Update Console Commands
This commit is contained in:
parent
b6d52676b5
commit
fac9732815
@ -107,6 +107,7 @@ class CronController extends Controller
|
||||
* Runs the daily cron jobs
|
||||
*
|
||||
* @param bool $force
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function runDaily($force = false)
|
||||
{
|
||||
|
@ -20,7 +20,7 @@ use humhub\modules\user\models\ProfileField;
|
||||
use humhub\modules\user\models\User;
|
||||
use Yii;
|
||||
use yii\db\Expression;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\helpers\VarDumper;
|
||||
use Zend\Ldap\Exception\LdapException;
|
||||
use Zend\Ldap\Ldap;
|
||||
use Zend\Ldap\Node;
|
||||
@ -382,11 +382,12 @@ class LdapAuth extends BaseFormAuth implements AutoSyncUsers, SyncAttributes, Ap
|
||||
|
||||
try {
|
||||
$this->getLdap()->bind($userName, $this->login->password);
|
||||
$dn = $this->getLdap()->getCanonicalAccountName($userName, Ldap::ACCTNAME_FORM_DN);
|
||||
|
||||
// Rebind with administrative DN
|
||||
$this->getLdap()->bind();
|
||||
|
||||
$dn = $this->getLdap()->getCanonicalAccountName($userName, Ldap::ACCTNAME_FORM_DN);
|
||||
|
||||
return $dn;
|
||||
} catch (LdapException $ex) {
|
||||
// User not found in LDAP
|
||||
@ -469,8 +470,15 @@ class LdapAuth extends BaseFormAuth implements AutoSyncUsers, SyncAttributes, Ap
|
||||
$authClient = $this->getAuthClientInstance($ldapEntry);
|
||||
$user = AuthClientHelpers::getUserByAuthClient($authClient);
|
||||
if ($user === null) {
|
||||
if (!AuthClientHelpers::createUser($authClient)) {
|
||||
Yii::warning('Could not automatically create LDAP user - check required attributes! (' . print_r($attributes, 1) . ')');
|
||||
$registration = AuthClientHelpers::createRegistration($authClient);
|
||||
if ($registration === null) {
|
||||
Yii::warning('Could not automatically create LDAP user - No ID attribute!', 'ldap');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$registration->register($authClient)) {
|
||||
Yii::warning('Could not create LDAP user (' . $ldapEntry['dn'] . '). Error: '
|
||||
. VarDumper::dumpAsString($registration->getErrors()), 'ldap');
|
||||
}
|
||||
} else {
|
||||
AuthClientHelpers::updateUser($authClient, $user);
|
||||
@ -491,19 +499,19 @@ class LdapAuth extends BaseFormAuth implements AutoSyncUsers, SyncAttributes, Ap
|
||||
// Enable disabled users that have been found in ldap
|
||||
$user->status = User::STATUS_ENABLED;
|
||||
$user->save();
|
||||
Yii::info('Enabled user' . $user->username . ' (' . $user->id . ') - found in LDAP!');
|
||||
Yii::info('Enabled user' . $user->username . ' (' . $user->id . ') - found in LDAP!', 'ldap');
|
||||
} elseif (!$foundInLdap && $user->status !== User::STATUS_DISABLED) {
|
||||
// Disable users that were not found in ldap
|
||||
$user->status = User::STATUS_DISABLED;
|
||||
$user->save();
|
||||
Yii::warning('Disabled user' . $user->username . ' (' . $user->id . ') - not found in LDAP!');
|
||||
Yii::warning('Disabled user' . $user->username . ' (' . $user->id . ') - not found in LDAP!', 'ldap');
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (\Zend\Ldap\Exception\LdapException $ex) {
|
||||
Yii::error('Could not connect to LDAP instance: ' . $ex->getMessage());
|
||||
Yii::error('Could not connect to LDAP instance: ' . $ex->getMessage(), 'ldap');
|
||||
} catch (\Exception $ex) {
|
||||
Yii::error('An error occurred while user sync: ' . $ex->getMessage());
|
||||
Yii::error('An error occurred while user sync: ' . $ex->getMessage(), 'ldap');
|
||||
}
|
||||
}
|
||||
|
||||
@ -531,7 +539,8 @@ class LdapAuth extends BaseFormAuth implements AutoSyncUsers, SyncAttributes, Ap
|
||||
* @param $ldapEntry array
|
||||
* @return LdapAuth
|
||||
*/
|
||||
public function getAuthClientInstance($ldapEntry) {
|
||||
public function getAuthClientInstance($ldapEntry)
|
||||
{
|
||||
$authClient = clone $this;
|
||||
$authClient->init();
|
||||
$authClient->setUserAttributes($ldapEntry);
|
||||
|
@ -24,6 +24,39 @@ use Zend\Ldap\Ldap;
|
||||
class LdapController extends \yii\console\Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public $defaultAction = 'list';
|
||||
|
||||
/**
|
||||
* Lists configured LDAP auth clients
|
||||
*
|
||||
* @return int the exit code
|
||||
*/
|
||||
public function actionList()
|
||||
{
|
||||
$this->stdout("*** Configured LDAP AuthClients \n\n");
|
||||
|
||||
$clients = [];
|
||||
foreach (Yii::$app->authClientCollection->getClients(true) as $id => $client) {
|
||||
if ($client instanceof LdapAuth) {
|
||||
/** @var LdapAuth $client */
|
||||
$clients[] = [$id, $client->getName() . ' (' . $client->getId() . ')', $client->hostname, $client->port, $client->baseDn];
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
echo Table::widget(['headers' => ['AuthClient ID', 'Name (ClientId)', 'Host', 'Port', 'Base DN'], 'rows' => $clients]);
|
||||
} catch (Exception $e) {
|
||||
$this->stderr("Error: " . $e->getMessage() . "\n\n");
|
||||
return ExitCode::UNSPECIFIED_ERROR;
|
||||
}
|
||||
|
||||
print "\n\n";
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns status information
|
||||
*
|
||||
@ -62,14 +95,14 @@ class LdapController extends \yii\console\Controller
|
||||
|
||||
|
||||
/**
|
||||
* Synchronizes all ldap users if autoRefresh is enabled
|
||||
* Synchronizes all ldap users (if autoRefresh is enabled)
|
||||
*
|
||||
* @param string $id the auth client id (default: ldap)
|
||||
* @return int status code
|
||||
*/
|
||||
public function actionSync($id = 'ldap')
|
||||
{
|
||||
$this->stdout("*** LDAP User List for AuthClient ID: " . $id . "\n\n");
|
||||
$this->stdout("*** LDAP Sync for AuthClient ID: " . $id . "\n\n");
|
||||
|
||||
try {
|
||||
$ldapAuthClient = $this->getAuthClient($id);
|
||||
@ -122,6 +155,50 @@ class LdapController extends \yii\console\Controller
|
||||
return ExitCode::OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map found users to given auth client.
|
||||
*
|
||||
* Useful if an existing authclient was renamed.
|
||||
*
|
||||
* @param string $id the auth client id (default: ldap)
|
||||
* @return int status code
|
||||
*/
|
||||
public function actionRemapAuthid($id)
|
||||
{
|
||||
$this->stdout("*** LDAP ReMap Users for AuthClient ID: " . $id . "\n\n");
|
||||
|
||||
$i = 0;
|
||||
$m = 0;
|
||||
|
||||
try {
|
||||
$newAuthClient = $this->getAuthClient($id);
|
||||
|
||||
foreach ($newAuthClient->getUserCollection() as $userEntry) {
|
||||
$i++;
|
||||
|
||||
$authClient = $newAuthClient->getAuthClientInstance($userEntry);
|
||||
$attributes = $authClient->getUserAttributes();
|
||||
|
||||
if (isset($attributes['id'])) {
|
||||
$user = User::findOne(['authclient_id' => $attributes['id']]);
|
||||
if ($user !== null) {
|
||||
$user->updateAttributes(['auth_mode' => $newAuthClient->getId()]);
|
||||
$m++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->stdout("Checked:\t" . $i . " users.\n");
|
||||
$this->stdout("Remapped:\t" . $m . " users.\n");
|
||||
|
||||
} catch (Exception $ex) {
|
||||
$this->stderr("Error: " . $ex->getMessage() . "\n\n");
|
||||
return ExitCode::UNSPECIFIED_ERROR;
|
||||
}
|
||||
|
||||
return ExitCode::OK;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
|
@ -248,7 +248,7 @@ class LdapSettings extends Model
|
||||
'baseDn' => $this->baseDn,
|
||||
'loginFilter' => $this->loginFilter,
|
||||
'userFilter' => $this->userFilter,
|
||||
'autoRefreshUsers' => ($this->refreshUsers),
|
||||
'autoRefreshUsers' => (boolean) $this->refreshUsers,
|
||||
'emailAttribute' => $this->emailAttribute,
|
||||
'usernameAttribute' => $this->usernameAttribute,
|
||||
'idAttribute' => $this->idAttribute
|
||||
|
@ -8,10 +8,11 @@
|
||||
|
||||
namespace humhub\modules\user\authclient;
|
||||
|
||||
use Yii;
|
||||
use yii\authclient\ClientInterface;
|
||||
use humhub\modules\user\models\Auth;
|
||||
use humhub\modules\user\models\User;
|
||||
use Yii;
|
||||
use yii\authclient\ClientInterface;
|
||||
use yii\helpers\VarDumper;
|
||||
|
||||
/**
|
||||
* AuthClientHelper provides helper functions fo auth clients
|
||||
@ -71,8 +72,8 @@ class AuthClientHelpers
|
||||
if ($auth === null) {
|
||||
$auth = new \humhub\modules\user\models\Auth([
|
||||
'user_id' => $user->id,
|
||||
'source' => (string) $authClient->getId(),
|
||||
'source_id' => (string) $attributes['id'],
|
||||
'source' => (string)$authClient->getId(),
|
||||
'source_id' => (string)$attributes['id'],
|
||||
]);
|
||||
|
||||
$auth->save();
|
||||
@ -90,7 +91,7 @@ class AuthClientHelpers
|
||||
{
|
||||
Auth::deleteAll([
|
||||
'user_id' => $user->id,
|
||||
'source' => (string) $authClient->getId()
|
||||
'source' => (string)$authClient->getId()
|
||||
]);
|
||||
}
|
||||
|
||||
@ -130,12 +131,16 @@ class AuthClientHelpers
|
||||
}
|
||||
|
||||
if (count($user->getDirtyAttributes()) !== 0 && !$user->save()) {
|
||||
Yii::error('Could not update user attributes by AuthClient (UserId: ' . $user->id . ") - Error: " . print_r($user->getErrors(), 1));
|
||||
|
||||
Yii::warning('Could not update user (' . $user->id . '). Error: '
|
||||
. VarDumper::dumpAsString($user->getErrors()), 'user');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (count($user->profile->getDirtyAttributes()) !== 0 && !$user->profile->save()) {
|
||||
Yii::error('Could not update user attributes by AuthClient (UserId: ' . $user->id . ") - Error: " . print_r($user->profile->getErrors(), 1));
|
||||
Yii::warning('Could not update user profile (' . $user->id . '). Error: '
|
||||
. VarDumper::dumpAsString($user->profile->getErrors()), 'user');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -144,17 +149,17 @@ class AuthClientHelpers
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatically creates user by auth client attributes
|
||||
* Populates a Registration model with the information provided by the given AuthClient
|
||||
*
|
||||
* @param \yii\authclient\BaseClient $authClient
|
||||
* @return boolean success status
|
||||
* @param ClientInterface $authClient
|
||||
* @return bool|\humhub\modules\user\models\forms\Registration|null
|
||||
*/
|
||||
public static function createUser(ClientInterface $authClient)
|
||||
public static function createRegistration(ClientInterface $authClient)
|
||||
{
|
||||
$attributes = $authClient->getUserAttributes();
|
||||
|
||||
if (!isset($attributes['id'])) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
$registration = new \humhub\modules\user\models\forms\Registration();
|
||||
@ -170,7 +175,20 @@ class AuthClientHelpers
|
||||
$registration->getProfile()->setAttributes($attributes, false);
|
||||
$registration->getGroupUser()->setAttributes($attributes, false);
|
||||
|
||||
if ($registration->validate() && $registration->register($authClient)) {
|
||||
return $registration;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically creates user by auth client attributes
|
||||
*
|
||||
* @param \yii\authclient\BaseClient $authClient
|
||||
* @return User the created user
|
||||
*/
|
||||
public static function createUser(ClientInterface $authClient)
|
||||
{
|
||||
$registration = static::createRegistration($authClient);
|
||||
if ($registration !== null && $registration->validate() && $registration->register($authClient)) {
|
||||
return $registration->getUser();
|
||||
}
|
||||
|
||||
|
@ -243,6 +243,10 @@ class Registration extends HForm
|
||||
*/
|
||||
public function register(\yii\authclient\ClientInterface $authClient = null)
|
||||
{
|
||||
if (!$this->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->models['User']->language = Yii::$app->language;
|
||||
if ($this->enableUserApproval) {
|
||||
$this->models['User']->status = User::STATUS_NEED_APPROVAL;
|
||||
@ -352,4 +356,19 @@ class Registration extends HForm
|
||||
return $this->_groupUser;
|
||||
}
|
||||
|
||||
|
||||
public function getErrors()
|
||||
{
|
||||
$errors = [];
|
||||
|
||||
if ($this->models['User']->hasErrors()) {
|
||||
$errors = array_merge($errors, $this->models['User']->getErrors());
|
||||
}
|
||||
|
||||
if ($this->models['Profile']->hasErrors()) {
|
||||
$errors = array_merge($errors, $this->models['Profile']->getErrors());
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user