Skip to content

Commit

Permalink
Merge pull request #11807 from jredrejo/small_validation_errors
Browse files Browse the repository at this point in the history
Capture more errors from the backend and show error messages for them
  • Loading branch information
marcellamaki authored Jan 31, 2024
2 parents 1123395 + 5b47241 commit 6008a25
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
8 changes: 7 additions & 1 deletion kolibri/plugins/setup_wizard/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
from django.urls import reverse
from rest_framework import decorators
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.exceptions import NotFound
from rest_framework.exceptions import PermissionDenied
from rest_framework.exceptions import ValidationError
Expand Down Expand Up @@ -154,7 +155,12 @@ def listfacilitylearners(self, request):
baseurl = request.data.get("baseurl")
password = request.data.get("password")
username = request.data.get("username")
facility_info = get_remote_users_info(baseurl, facility_id, username, password)
try:
facility_info = get_remote_users_info(
baseurl, facility_id, username, password
)
except AuthenticationFailed:
raise PermissionDenied()
user_info = facility_info["user"]
roles = user_info["roles"]
admin_roles = (user_kinds.ADMIN, user_kinds.SUPERUSER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
:disabled="formSubmitted"
:label="coreString('usernameLabel')"
:autofocus="$attrs.autofocus"
@blur="blurred = true"
:invalid="Boolean(invalidText)"
:invalidText="invalidText"
@blur="shouldValidate = true"
/>
<PasswordTextbox
v-if="!facility.learner_can_login_with_no_password"
Expand Down Expand Up @@ -79,7 +81,9 @@
v-model.trim="adminUsername"
:label="coreString('usernameLabel')"
:autofocus="$attrs.autofocus"
@blur="blurred = true"
:invalid="Boolean(invalidText)"
:invalidText="invalidText"
@blur="shouldValidate = true"
/>
<PasswordTextbox
ref="adminPasswordTextbox"
Expand Down Expand Up @@ -135,6 +139,7 @@
facilities: [],
selectedFacilityId: 'selectedFacilityId',
formSubmitted: false,
shouldValidate: false,
};
},
inject: ['wizardService'],
Expand Down Expand Up @@ -191,6 +196,18 @@
adminModalMessage() {
return this.$tr('enterAdminCredentials', { facility: this.facility.name });
},
invalidText() {
if (!this.shouldValidate) {
return '';
}
if (
!this.useAdmin & (this.username.trim() === '') ||
this.useAdmin & (this.adminUsername === null || this.adminUsername.trim() === '')
) {
return this.coreString('requiredFieldError');
}
return '';
},
},
beforeMount() {
this.fetchNetworkLocation(this.deviceId).then(() => {
Expand Down Expand Up @@ -225,12 +242,23 @@
this.useAdmin = false;
},
openAdminCredentialsForm() {
// clean error from non-admin form:
this.shouldValidate = false;
this.error = false;
this.useAdmin = true;
this.$nextTick(function() {
this.$refs.adminUsernameTextbox.focus();
});
},
handleSubmit() {
if (this.wizardService.state.context.importedUsers.length === 0) {
this.shouldValidate = true;
if (this.invalidText) {
this.$refs.usernameTextbox.focus();
return;
}
}
this.formSubmitted = true;
const task_name = 'kolibri.core.auth.tasks.peeruserimport';
const password = this.password === '' ? DemographicConstants.NOT_SPECIFIED : this.password;
Expand Down Expand Up @@ -314,6 +342,11 @@
this.handleSubmit();
},
moveAdmin() {
this.shouldValidate = true;
if (this.invalidText) {
this.$refs.adminUsernameTextbox.focus();
return;
}
const params = {
baseurl: this.device.baseurl,
username: this.adminUsername,
Expand All @@ -337,6 +370,9 @@
ERROR_CONSTANTS.INVALID_CREDENTIALS,
ERROR_CONSTANTS.AUTHENTICATION_FAILED,
ERROR_CONSTANTS.PERMISSION_DENIED,
ERROR_CONSTANTS.MISSING_PASSWORD,
ERROR_CONSTANTS.PASSWORD_NOT_SPECIFIED,
ERROR_CONSTANTS.INVALID_USERNAME,
]);
if (errorsCaught) {
this.error = true;
Expand Down

0 comments on commit 6008a25

Please sign in to comment.