Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added user autocomplete from ldap in create view and a parameter to enable it #543

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/User/Dictionary/UserSourceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Da\User\Dictionary;

use yii\helpers\ArrayHelper;

class UserSourceType
{
const LOCAL = 'LOCAL';
const LDAP = 'LDAP';

/**
* Returns an array that contains the codes for the dictionary and their common name. It's useful to be used for
* dropdowns and selects.
* @return array
*/
public static function all()
{
return [
static::LOCAL => \Yii::t('usuario', 'Local'),
static::LDAP => \Yii::t('usuario', 'LDAP'),
];
}

/**
* Returns the dictionary value for the given code
* @param $key
* @return string|null
* @throws \Exception
*/
public static function get($key)
{
return ArrayHelper::getValue(static::all(), $key);
}

AndreScara11 marked this conversation as resolved.
Show resolved Hide resolved

}
4 changes: 4 additions & 0 deletions src/User/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class Module extends BaseModule
* if equals false records will not be deleted
*/
public $numberSessionHistory = false;
/**
* @var bool Enable LDAP to sync local users with LDAP users
AndreScara11 marked this conversation as resolved.
Show resolved Hide resolved
*/
public $searchUsersInLdap = false;
/**
* @var int|bool The time after which the expired 'session history' will be deleted
* if equals false records will not be deleted
Expand Down
58 changes: 55 additions & 3 deletions src/User/resources/views/admin/_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,60 @@
* @var yii\widgets\ActiveForm $form
* @var \Da\User\Model\User $user
*/

use Da\User\Dictionary\UserSourceType;
use kartik\typeahead\Typeahead;
AndreScara11 marked this conversation as resolved.
Show resolved Hide resolved
use yii\helpers\Html;
use yii\helpers\Url;

?>
<?php $source = Yii::$app->request->get('source') ?: $user->source ?>
AndreScara11 marked this conversation as resolved.
Show resolved Hide resolved

<?= $form->field($user, 'email')->textInput(['maxlength' => 255]) ?>
<?= $form->field($user, 'username')->textInput(['maxlength' => 255]) ?>
<?= $form->field($user, 'password')->passwordInput() ?>
<?php
$emailInputId = Html::getInputId($user, 'email');
$usernameInputId = Html::getInputId($user, 'username');
$sourceId = Html::getInputId($user, 'source');
$this->registerJs(<<<JS
function updateFromLdap(event, data) {
$("#$emailInputId").val(data.value).change();
$("#$usernameInputId").val(data.username).change();
}
$('#$sourceId').change(function() {
var source = $(this).val();
$.pjax.reload({container: '#pjax-user-create', data: {source: source}})
})
JS);
?>
<?php if ($user->isNewRecord): ?>
<?php if (Yii::$app->getModule('user')->searchUsersInLdap && $source==UserSourceType::LDAP): ?>
<?= $form->field($user, 'source')->dropDownList(UserSourceType::all(), ['value' => $source]); ?>
<?= $form->field($user, 'email')->widget(Typeahead::class, [
'options' => ['placeholder' => Yii::t('mis', 'Filter as you type ...')],
AndreScara11 marked this conversation as resolved.
Show resolved Hide resolved
AndreScara11 marked this conversation as resolved.
Show resolved Hide resolved
'pluginOptions' => ['highlight' => true],
'dataset' => [
[
'display' => 'value',
'remote' => [
'url' => Url::to(['/usuario-ldap/ldap/search']) . '?q=%QUERY', // You can add &limit to set a results limit, 20 to default
'wildcard' => '%QUERY'
]
]
],
// When the email is selected, get the username and change the source from local to ldap
'pluginEvents' => [
'typeahead:select' => 'updateFromLdap',
],
])
?>
<?= $form->field($user, 'username')->hiddenInput()->label(false) ?>
<?php else: ?>
<?= $form->field($user, 'source')->dropDownList(UserSourceType::all(), ['value' => $source]); ?>
AndreScara11 marked this conversation as resolved.
Show resolved Hide resolved
<?= $form->field($user, 'email')->textInput(['maxlength' => 255]) ?>
<?= $form->field($user, 'username')->textInput(['maxlength' => 255]) ?>
<?= $form->field($user, 'password')->passwordInput() ?>
<?php endif; ?>
<?php else: ?>
<?= $form->field($user, 'email')->textInput(['maxlength' => 255]) ?>
<?= $form->field($user, 'username')->textInput(['maxlength' => 255]) ?>
<?= $form->field($user, 'password')->passwordInput() ?>
<?php endif; ?>
8 changes: 6 additions & 2 deletions src/User/resources/views/admin/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Nav;
use yii\helpers\Html;
use yii\widgets\Pjax;

/**
* @var yii\web\View $this
Expand Down Expand Up @@ -83,7 +84,9 @@
'A password will be generated automatically if not provided'
) ?>.
</div>
<?php $form = ActiveForm::begin(
<?php
Pjax::begin(['id' => 'pjax-user-create']);
$form = ActiveForm::begin(
[
'layout' => 'horizontal',
'enableAjaxValidation' => true,
Expand All @@ -107,7 +110,8 @@
</div>
</div>

<?php ActiveForm::end(); ?>
<?php ActiveForm::end();
Pjax::end() ?>
</div>
</div>
</div>
Expand Down