Skip to content

Commit

Permalink
Merge pull request #205 from vincentmigot/fix-3.2
Browse files Browse the repository at this point in the history
Add user creation error message
  • Loading branch information
Gouga34 authored May 16, 2019
2 parents b75a79f + ec89d54 commit d7c6edf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 22 deletions.
66 changes: 46 additions & 20 deletions controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,35 +137,61 @@ public function actionCreate() {

$dataToSend[] = $userModel->attributesToArray();

$requestRes = $userModel->insert($sessionToken, $dataToSend);
$requestRes = $userModel->insert($sessionToken, $dataToSend);

if (is_string($requestRes) && $requestRes === "token") { //user must log in
return $this->redirect(Yii::$app->urlManager->createUrl("site/login"));
} else {
} else if (count($requestRes->metadata->status) == 0) {
return $this->redirect(['view', 'id' => $userModel->email]);
}
} else {
$searchGroupModel = new GroupSearch();
$groups = $searchGroupModel->find($sessionToken,[]);

if (is_string($groups)) {
return $this->render('/site/error', [
'name' => Yii::t('app/messages','Internal error'),
'message' => $groups]);
} else if (is_array ($groups) && isset($groups["token"])) {
return $this->redirect(Yii::$app->urlManager->createUrl("site/login"));
} else {
$groups = $this->groupsToMap($groups);
$this->view->params['listGroups'] = $groups;
$userModel->isNewRecord = true;

return $this->render('create', [
'model' => $userModel,
]);
$errors = [];
foreach ($requestRes->metadata->status as $error) {
$matches = [];
// Message format returned by the webservice is like "[cryptic error parameter message]real error message part"
// This preg_match regex is used to only get the "real error message part"
if (preg_match('/\[.*\](.*)/', $error->exception->details, $matches)) {
$errors[] = $matches[1];
} else {
$errors[] = $error->message;
}
}

return $this->displayUserCreationForm($userModel, $errors);
}
} else {
return $this->displayUserCreationForm($userModel);
}
}

/**
* Return user creation form view for given model with errors (optional)
* @param type $userModel
* @param type $errors
* @return type
*/
private function displayUserCreationForm($userModel, $errors = []) {
$sessionToken = Yii::$app->session['access_token'];
$searchGroupModel = new GroupSearch();
$groups = $searchGroupModel->find($sessionToken,[]);

if (is_string($groups)) {
return $this->render('/site/error', [
'name' => Yii::t('app/messages','Internal error'),
'message' => $groups]);
} else if (is_array ($groups) && isset($groups["token"])) {
return $this->redirect(Yii::$app->urlManager->createUrl("site/login"));
} else {
$groups = $this->groupsToMap($groups);
$this->view->params['listGroups'] = $groups;
$userModel->isNewRecord = true;

return $this->render('create', [
'model' => $userModel,
'errors' => $errors
]);
}
}

/**
* update a user
* @return mixed
Expand Down
3 changes: 2 additions & 1 deletion models/yiiModels/YiiUserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public function rules() {
[['address', 'password', 'phone', 'orcid', 'affiliation', 'uri'], 'string', 'max' => 255],
['email', 'email'],
[['firstName', 'familyName'], 'string', 'max' => 50],
[['isAdmin', 'available'], 'boolean']
[['isAdmin', 'available'], 'boolean'],
[['groups'], 'safe']
];
}

Expand Down
1 change: 1 addition & 0 deletions translations/fr/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
EventGridViewWidget::NO_EVENT_LABEL => "Pas d'événement",

'Error' => 'Erreur',
'Errors while creating user' => 'Erreurs lors de la création de l\'utilisateur',
'Experimental Organization' => 'Organisation expérimentale',
'Experiment Modalities' => 'Modalités Expérimentales',
'File Extension' => 'Extension du Fichier',
Expand Down
17 changes: 16 additions & 1 deletion views/user/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// Last modification date: April, 2017
// Subject: creation or update user's form
//***********************************************************************************************

use Yii;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use borales\extensions\phoneInput\PhoneInput;
Expand All @@ -23,6 +23,21 @@
?>

<div class="user-form well">

<?php if (count($errors) > 0): ?>
<div class="alert alert-danger" >
<h3 style="margin:3%;"><?= Yii::t('app/messages', 'Errors while creating user')?> :</h3>
<ul>
<?php
$errorMessages = array_unique($errors);
foreach ($errorMessages as $errorMessage) {
echo '<li>' . str_replace("|", ",", $errorMessage) . '</li>';
}
?>
</ul>
</div>
<?php endif; ?>

<?php $form = ActiveForm::begin(); ?>

<?php
Expand Down
1 change: 1 addition & 0 deletions views/user/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@

<?= $this->render('_form', [
'model' => $model,
'errors' => $errors
]) ?>

0 comments on commit d7c6edf

Please sign in to comment.