1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-09-20 00:49:10 -05:00

Fix issues related to not authenticated eduOne students (#2501)

This commit is contained in:
Mikołaj Pich 2024-03-24 23:22:07 +01:00 committed by GitHub
parent 76d038eefa
commit 3881678208
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 6 deletions

View File

@ -12,6 +12,7 @@ import io.github.wulkanowy.data.db.entities.StudentName
import io.github.wulkanowy.data.db.entities.StudentNickAndAvatar import io.github.wulkanowy.data.db.entities.StudentNickAndAvatar
import io.github.wulkanowy.data.db.entities.StudentWithSemesters import io.github.wulkanowy.data.db.entities.StudentWithSemesters
import io.github.wulkanowy.data.exceptions.NoCurrentStudentException import io.github.wulkanowy.data.exceptions.NoCurrentStudentException
import io.github.wulkanowy.data.mappers.mapToEntities
import io.github.wulkanowy.data.mappers.mapToPojo import io.github.wulkanowy.data.mappers.mapToPojo
import io.github.wulkanowy.data.pojos.RegisterUser import io.github.wulkanowy.data.pojos.RegisterUser
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
@ -110,7 +111,22 @@ class StudentRepository @Inject constructor(
} }
val initializedSdk = wulkanowySdkFactory.create(student) val initializedSdk = wulkanowySdkFactory.create(student)
val newCurrentStudent = initializedSdk.getCurrentStudent() ?: return val newCurrentStudent = runCatching { initializedSdk.getCurrentStudent() }
.onFailure { Timber.e(it, "Check isAuthorized: error occurred") }
.getOrNull()
if (newCurrentStudent == null) {
Timber.d("Check isAuthorized: current user is null")
return
}
val currentStudentSemesters = semesterDb.loadAll(student.studentId, student.classId)
if (currentStudentSemesters.isEmpty()) {
Timber.d("Check isAuthorized: apply empty semesters workaround")
semesterDb.insertSemesters(
items = newCurrentStudent.semesters.mapToEntities(student.studentId),
)
}
if (!newCurrentStudent.isAuthorized) { if (!newCurrentStudent.isAuthorized) {
Timber.i("Check isAuthorized: authorization required") Timber.i("Check isAuthorized: authorization required")
@ -178,15 +194,21 @@ class StudentRepository @Inject constructor(
wulkanowySdkFactory.create(student, semester) wulkanowySdkFactory.create(student, semester)
.authorizePermission(pesel) .authorizePermission(pesel)
suspend fun refreshStudentName(student: Student, semester: Semester) { suspend fun refreshStudentAfterAuthorize(student: Student, semester: Semester) {
val newCurrentApiStudent = wulkanowySdkFactory.create(student, semester) val newCurrentApiStudent = wulkanowySdkFactory
.getCurrentStudent() ?: return .create(student, semester)
.getCurrentStudent()
?: return Timber.d("Can't find student with id ${student.studentId}")
val studentName = StudentName( val studentName = StudentName(
studentName = "${newCurrentApiStudent.studentName} ${newCurrentApiStudent.studentSurname}" studentName = "${newCurrentApiStudent.studentName} ${newCurrentApiStudent.studentSurname}"
).apply { id = student.id } ).apply { id = student.id }
studentDb.update(studentName) studentDb.update(studentName)
semesterDb.removeOldAndSaveNew(
oldItems = semesterDb.loadAll(student.studentId, semester.classId),
newItems = newCurrentApiStudent.semesters.mapToEntities(newCurrentApiStudent.studentId)
)
} }
suspend fun deleteStudentsAssociatedWithAccount(student: Student) { suspend fun deleteStudentsAssociatedWithAccount(student: Student) {
@ -202,4 +224,3 @@ class StudentRepository @Inject constructor(
} }
class NoAuthorizationException : Exception() class NoAuthorizationException : Exception()

View File

@ -5,6 +5,7 @@ import io.github.wulkanowy.data.repositories.StudentRepository
import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.BasePresenter
import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.base.ErrorHandler
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
class AuthPresenter @Inject constructor( class AuthPresenter @Inject constructor(
@ -57,8 +58,9 @@ class AuthPresenter @Inject constructor(
val semester = semesterRepository.getCurrentSemester(student) val semester = semesterRepository.getCurrentSemester(student)
val isSuccess = studentRepository.authorizePermission(student, semester, pesel) val isSuccess = studentRepository.authorizePermission(student, semester, pesel)
Timber.d("Auth succeed: $isSuccess")
if (isSuccess) { if (isSuccess) {
studentRepository.refreshStudentName(student, semester) studentRepository.refreshStudentAfterAuthorize(student, semester)
} }
isSuccess isSuccess
} }
@ -68,6 +70,7 @@ class AuthPresenter @Inject constructor(
view?.showContent(true) view?.showContent(true)
} }
.onSuccess { .onSuccess {
Timber.d("Auth fully succeed: $it")
if (it) { if (it) {
view?.showSuccess(true) view?.showSuccess(true)
view?.showContent(false) view?.showContent(false)