Skip to content

Commit

Permalink
Merge pull request #12310 from LianaHarris360/username-exists-string
Browse files Browse the repository at this point in the history
Add string prompt to log in with existing username or create an account
  • Loading branch information
rtibbles authored Jul 2, 2024
2 parents 2692958 + b6d0d2d commit c65262f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
21 changes: 17 additions & 4 deletions kolibri/core/auth/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,13 @@ def annotate_queryset(self, queryset):
return annotate_array_aggregate(queryset, user_ids="membership__user__id")


@method_decorator(csrf_protect, name="dispatch")
class SignUpViewSet(viewsets.GenericViewSet, CreateModelMixin):

class BaseSignUpViewSet(viewsets.GenericViewSet, CreateModelMixin):
serializer_class = FacilityUserSerializer

def check_can_signup(self, serializer):
"""
Check if the user can sign up to the specified facility.
"""
facility = serializer.validated_data["facility"]
if (
not facility.dataset.learner_can_sign_up
Expand All @@ -793,6 +794,9 @@ def check_can_signup(self, serializer):
raise PermissionDenied("Cannot sign up to this facility")

def perform_create(self, serializer):
"""
Handle the creation of a new user, including validation and logging in.
"""
self.check_can_signup(serializer)
serializer.save()
data = serializer.validated_data
Expand All @@ -804,8 +808,17 @@ def perform_create(self, serializer):
login(self.request, authenticated_user)


@method_decorator(csrf_protect, name="dispatch")
class SignUpViewSet(BaseSignUpViewSet):
"""
Viewset for signing up a user with CSRF protection.
"""

pass


@method_decorator(csrf_exempt, name="dispatch")
class PublicSignUpViewSet(SignUpViewSet):
class PublicSignUpViewSet(BaseSignUpViewSet):
"""
Identical to the SignUpViewset except that it does not login the user.
This endpoint is intended to allow a FacilityUser in a different facility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ export const wizardMachine = createMachine(
on: {
BACK: 'selectLodSetupType',
CONTINUE: 'lodJoinLoading',
SIGN_IN_INSTEAD: {
target: 'lodProceedJoinOrNew',
actions: ['setLodType', 'setSelectedImportDeviceFacility'],
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
:noBackAction="false"
:errors.sync="caughtErrors"
@submit="handleClickNext"
@signInInstead="goToSignIn"
/>

</template>
Expand All @@ -24,7 +25,7 @@
import { ERROR_CONSTANTS } from 'kolibri.coreVue.vuex.constants';
import commonSyncElements from 'kolibri.coreVue.mixins.commonSyncElements';
import { SetupWizardResource } from '../api';
import { FooterMessageTypes } from '../constants';
import { FooterMessageTypes, LodTypePresets as Options } from '../constants';
import UserCredentialsForm from './onboarding-forms/UserCredentialsForm';
export default {
Expand Down Expand Up @@ -90,6 +91,18 @@
}
});
},
goToSignIn() {
this.wizardService.send({
type: 'SIGN_IN_INSTEAD',
value: {
importDeviceId: this.wizardService.state.context.importDeviceId,
importOrJoin: Options.IMPORT,
selectedFacility: this.facility,
importDevice: this.wizardService.state.context.importDevice,
facilitiesCount: this.wizardService.state.context.facilitiesOnDeviceCount,
},
});
},
},
$trs: {
header: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
:isUniqueValidator="!selectedUser ? uniqueUsernameValidator : () => true"
/>

<KButton
v-if="usernameNotUnique"
:text="$tr('signInInstead')"
class="link"
appearance="basic-link"
@click="handleSignIn"
/>

<PasswordTextbox
ref="passwordTextbox"
:value.sync="password"
Expand Down Expand Up @@ -83,6 +91,7 @@
import PasswordTextbox from 'kolibri.coreVue.components.PasswordTextbox';
import PrivacyLinkAndModal from 'kolibri.coreVue.components.PrivacyLinkAndModal';
import commonSyncElements from 'kolibri.coreVue.mixins.commonSyncElements';
import { ERROR_CONSTANTS } from 'kolibri.coreVue.vuex.constants';
import OnboardingStepBase from '../OnboardingStepBase';
export default {
Expand Down Expand Up @@ -191,6 +200,9 @@
return every([this.usernameValid, this.fullNameValid, this.passwordValid]);
}
},
usernameNotUnique() {
return this.caughtErrors.includes(ERROR_CONSTANTS.USERNAME_ALREADY_EXISTS);
},
},
watch: {
selectedUser(user) {
Expand Down Expand Up @@ -270,6 +282,9 @@
}
});
},
handleSignIn() {
this.$emit('signInInstead');
},
},
$trs: {
adminAccountCreationHeader: {
Expand All @@ -286,12 +301,10 @@
context:
'The learner is creating their account for an existing facility and is told what that is',
},
/* eslint-disable kolibri/vue-no-unused-translations */
signInInstead: {
message: 'Sign in instead?',
context: 'Text prompting user to sign in with existing username.',
},
/* eslint-enable kolibri/vue-no-unused-translations */
},
};
Expand Down Expand Up @@ -322,4 +335,8 @@
margin: 18px 0 36px;
}
.link {
padding-bottom: 15px;
}
</style>

0 comments on commit c65262f

Please sign in to comment.