Restrict key managment to the user itself

This commit is contained in:
Jonas Lochmann 2022-08-01 02:00:00 +02:00
parent b557f58083
commit 050da14087
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
4 changed files with 54 additions and 5 deletions

View file

@ -1,5 +1,5 @@
/* /*
* TimeLimit Copyright <C> 2019 - 2020 Jonas Lochmann * TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -46,17 +46,25 @@ object ManageUserKeyView {
view.addKeyButton.setOnClickListener { view.addKeyButton.setOnClickListener {
if (auth.requestAuthenticationOrReturnTrue()) { if (auth.requestAuthenticationOrReturnTrue()) {
if (auth.authenticatedUser.value?.second?.id == userId) {
AddUserKeyDialogFragment.newInstance(userId).show(fragmentManager) AddUserKeyDialogFragment.newInstance(userId).show(fragmentManager)
} else {
ParentKeyWrongUserDialogFragment.newInstance().show(fragmentManager)
}
} }
} }
view.removeKeyButton.setOnClickListener { view.removeKeyButton.setOnClickListener {
if (auth.requestAuthenticationOrReturnTrue()) { if (auth.requestAuthenticationOrReturnTrue()) {
if (auth.authenticatedUser.value?.second?.id == userId) {
val database = auth.database val database = auth.database
Threads.database.execute { Threads.database.execute {
database.userKey().deleteUserKeySync(userId) database.userKey().deleteUserKeySync(userId)
} }
} else {
ParentKeyWrongUserDialogFragment.newInstance().show(fragmentManager)
}
} }
} }

View file

@ -0,0 +1,39 @@
/*
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.timelimit.android.ui.manage.parent.key
import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import io.timelimit.android.R
import io.timelimit.android.extensions.showSafe
class ParentKeyWrongUserDialogFragment: DialogFragment() {
companion object {
private const val DIALOG_TAG = "ParentKeyWrongUserDialogFragment"
fun newInstance() = ParentKeyWrongUserDialogFragment()
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = AlertDialog.Builder(requireContext(), theme)
.setMessage(R.string.manage_user_key_wrong_user)
.setPositiveButton(R.string.generic_ok, null)
.create()
fun show(fragmentManager: FragmentManager) = showSafe(fragmentManager, DIALOG_TAG)
}

View file

@ -1086,6 +1086,7 @@
<string name="manage_user_key_added">Der Schlüssel wurde hinzugefügt</string> <string name="manage_user_key_added">Der Schlüssel wurde hinzugefügt</string>
<string name="manage_user_key_invalid">Dies ist kein gültiger TimeLimit-Schlüssel</string> <string name="manage_user_key_invalid">Dies ist kein gültiger TimeLimit-Schlüssel</string>
<string name="manage_user_key_other_user">Dieser Schlüssel ist schon bei einem anderen Benutzer hinterlegt</string> <string name="manage_user_key_other_user">Dieser Schlüssel ist schon bei einem anderen Benutzer hinterlegt</string>
<string name="manage_user_key_wrong_user">Nur der Benutzer selbst kann seine Schlüssel verwalten</string>
<string name="manage_parent_link_mail_title">E-Mail-Adresse verknüpfen</string> <string name="manage_parent_link_mail_title">E-Mail-Adresse verknüpfen</string>
<string name="manage_parent_link_mail_status_disabled">Es wurde keine E-Mail-Adresse verknüpft</string> <string name="manage_parent_link_mail_status_disabled">Es wurde keine E-Mail-Adresse verknüpft</string>

View file

@ -1134,6 +1134,7 @@
<string name="manage_user_key_added">The key was added</string> <string name="manage_user_key_added">The key was added</string>
<string name="manage_user_key_invalid">This was no valid TimeLimit key</string> <string name="manage_user_key_invalid">This was no valid TimeLimit key</string>
<string name="manage_user_key_other_user">This key is already linked to an other user</string> <string name="manage_user_key_other_user">This key is already linked to an other user</string>
<string name="manage_user_key_wrong_user">Only the user itself can manage his keys</string>
<string name="manage_parent_link_mail_title">Link mail address</string> <string name="manage_parent_link_mail_title">Link mail address</string>
<string name="manage_parent_link_mail_status_disabled">No mail address was linked</string> <string name="manage_parent_link_mail_status_disabled">No mail address was linked</string>