Разграничение ролей

This commit is contained in:
ityurner02@mail.ru 2023-12-25 20:52:32 +04:00
parent ef72894515
commit ef4140f273
8 changed files with 71 additions and 15 deletions

View File

@ -18,7 +18,7 @@
"id": 3, "id": 3,
"login": "User2", "login": "User2",
"password": "123", "password": "123",
"email": "user2@gmail.ru", "email": "user2@mail.ru",
"role": "USER" "role": "USER"
} }
], ],

View File

@ -117,7 +117,7 @@ interface ServerService{
@Path("id") id: Int, @Path("id") id: Int,
): UserRemote ): UserRemote
companion object { companion object {
private const val BASE_URL = "http://89.239.174.67:8079/" private const val BASE_URL = "http://100.74.118.221:8079/"
@Volatile @Volatile
private var INSTANCE: ServerService? = null private var INSTANCE: ServerService? = null

View File

@ -59,8 +59,14 @@ fun AuthorCell(navController: NavController?, author: Author, viewModel: AuthorL
modifier = Modifier modifier = Modifier
.padding(all = 5.dp), .padding(all = 5.dp),
onClick = { onClick = {
val route = Screen.AuthorEdit.route.replace("{id}", author.id.toString()) if(LiveStore.getRole() == "ADMIN") {
navController!!.navigate(route) val route = Screen.AuthorEdit.route.replace("{id}", author.id.toString())
navController!!.navigate(route)
}
else{
val toast = Toast.makeText(MainActivity.appContext, "У вас нет соответствующих прав", Toast.LENGTH_SHORT)
toast.show()
}
}, },
) { ) {
Icon( Icon(
@ -73,8 +79,14 @@ fun AuthorCell(navController: NavController?, author: Author, viewModel: AuthorL
modifier = Modifier modifier = Modifier
.padding(all = 5.dp), .padding(all = 5.dp),
onClick = { onClick = {
scope.launch { if(LiveStore.getRole() == "ADMIN") {
scope.launch {
viewModel.deleteAuthor(author) viewModel.deleteAuthor(author)
}
}
else{
val toast = Toast.makeText(MainActivity.appContext, "У вас нет соответствующих прав", Toast.LENGTH_SHORT)
toast.show()
} }
}, },
colors = ButtonDefaults.buttonColors(containerColor = Color.Red), colors = ButtonDefaults.buttonColors(containerColor = Color.Red),

View File

@ -1,5 +1,6 @@
package com.example.myapplication.composeui package com.example.myapplication.composeui
import android.widget.Toast
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
@ -29,9 +30,11 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController import androidx.navigation.NavController
import com.example.myapplication.MainActivity
import com.example.myapplication.composeui.ViewModel.BookListViewModel import com.example.myapplication.composeui.ViewModel.BookListViewModel
import com.example.myapplication.composeui.navigation.Screen import com.example.myapplication.composeui.navigation.Screen
import com.example.myapplication.db.model.Book import com.example.myapplication.db.model.Book
import com.example.myapplication.store.LiveStore
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@Composable @Composable
@ -79,8 +82,14 @@ fun BookCell(navController: NavController?, book: Book, viewModel: BookListViewM
modifier = Modifier modifier = Modifier
.padding(all = 5.dp), .padding(all = 5.dp),
onClick = { onClick = {
val route = Screen.BookEdit.route.replace("{id}", book.id.toString()) if((LiveStore.getUserId() == book.userId) || (LiveStore.getRole() == "ADMIN")) {
navController!!.navigate(route) val route = Screen.BookEdit.route.replace("{id}", book.id.toString())
navController!!.navigate(route)
}
else{
val toast = Toast.makeText(MainActivity.appContext, "У вас нет соответствующих прав", Toast.LENGTH_SHORT)
toast.show()
}
}, },
) { ) {
Icon( Icon(
@ -93,8 +102,14 @@ fun BookCell(navController: NavController?, book: Book, viewModel: BookListViewM
modifier = Modifier modifier = Modifier
.padding(all = 5.dp), .padding(all = 5.dp),
onClick = { onClick = {
scope.launch { if((LiveStore.getUserId() == book.userId) || (LiveStore.getRole() == "ADMIN")) {
viewModel.deleteBook(book) scope.launch {
viewModel.deleteBook(book)
}
}
else{
val toast = Toast.makeText(MainActivity.appContext, "У вас нет соответствующих прав", Toast.LENGTH_SHORT)
toast.show()
} }
}, },
colors = ButtonDefaults.buttonColors(containerColor = Color.Red), colors = ButtonDefaults.buttonColors(containerColor = Color.Red),

View File

@ -8,12 +8,16 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
@ -24,10 +28,15 @@ import androidx.navigation.compose.rememberNavController
import com.example.myapplication.composeui.navigation.Screen import com.example.myapplication.composeui.navigation.Screen
import com.example.myapplication.R import com.example.myapplication.R
import com.example.myapplication.store.LiveStore import com.example.myapplication.store.LiveStore
import com.example.myapplication.store.PreferencesStore
import com.example.myapplication.ui.theme.MyApplicationTheme import com.example.myapplication.ui.theme.MyApplicationTheme
import kotlinx.coroutines.launch
@Composable @Composable
fun Profile(navController: NavController) { fun Profile(navController: NavController) {
val scope = rememberCoroutineScope()
val context = LocalContext.current
val store = PreferencesStore(context)
Column(Modifier.padding(all = 40.dp)) { Column(Modifier.padding(all = 40.dp)) {
Image( Image(
painter = painterResource(id = R.drawable.user), painter = painterResource(id = R.drawable.user),
@ -53,6 +62,21 @@ fun Profile(navController: NavController) {
}) { }) {
Text(stringResource(id = R.string.my_books)) Text(stringResource(id = R.string.my_books))
} }
Spacer(Modifier.padding(bottom = 10.dp))
Button(
onClick = {
scope.launch {
store.setUid("")
}
navController?.navigate(Screen.Main.route)
},
modifier = Modifier
.fillMaxWidth()
.padding(all = 10.dp),
colors = ButtonDefaults.buttonColors(containerColor = Color.LightGray, contentColor = Color.Black))
{
Text(stringResource(id = R.string.exit))
}
} }
} }

View File

@ -12,10 +12,12 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController import androidx.navigation.NavController
import com.example.myapplication.R
import com.example.myapplication.composeui.ViewModel.UserDetails import com.example.myapplication.composeui.ViewModel.UserDetails
import com.example.myapplication.composeui.ViewModel.UserEditViewModel import com.example.myapplication.composeui.ViewModel.UserEditViewModel
import com.example.myapplication.composeui.ViewModel.UserUiState import com.example.myapplication.composeui.ViewModel.UserUiState
@ -52,7 +54,7 @@ private fun ProfileEdit(
value = userUiState.userDetails.email, value = userUiState.userDetails.email,
onValueChange = { onUpdate(userUiState.userDetails.copy(email = it)) }, onValueChange = { onUpdate(userUiState.userDetails.copy(email = it)) },
singleLine = true, singleLine = true,
label = { Text("Почта", fontSize = 20.sp) }, label = { Text(stringResource(id = R.string.email), fontSize = 20.sp) },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(10.dp, 5.dp) .padding(10.dp, 5.dp)
@ -60,7 +62,7 @@ private fun ProfileEdit(
OutlinedTextField( OutlinedTextField(
value = userUiState.userDetails.login, value = userUiState.userDetails.login,
onValueChange = { onUpdate(userUiState.userDetails.copy(login = it)) }, onValueChange = { onUpdate(userUiState.userDetails.copy(login = it)) },
label = { Text("Логин", fontSize = 20.sp) }, label = { Text(stringResource(id = R.string.login), fontSize = 20.sp) },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(10.dp, 5.dp) .padding(10.dp, 5.dp)
@ -69,7 +71,7 @@ private fun ProfileEdit(
value = userUiState.userDetails.password, value = userUiState.userDetails.password,
onValueChange = { onUpdate(userUiState.userDetails.copy(password = it)) }, onValueChange = { onUpdate(userUiState.userDetails.copy(password = it)) },
singleLine = true, singleLine = true,
label = { Text("Пароль", fontSize = 20.sp) }, label = { Text(stringResource(id = R.string.password), fontSize = 20.sp) },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(10.dp, 5.dp) .padding(10.dp, 5.dp)
@ -89,9 +91,8 @@ private fun ProfileEdit(
.padding(10.dp, 0.dp), .padding(10.dp, 0.dp),
colors = ButtonDefaults.buttonColors(containerColor = Color.LightGray, contentColor = Color.Black)) colors = ButtonDefaults.buttonColors(containerColor = Color.LightGray, contentColor = Color.Black))
{ {
Text("Назад") Text(stringResource(id = R.string.back))
} }
} }
} }
} }

View File

@ -16,6 +16,8 @@
<string name="my_books">Мои книги</string> <string name="my_books">Мои книги</string>
<string name="authors_books">Книги автора</string> <string name="authors_books">Книги автора</string>
<string name="enter">Войти</string> <string name="enter">Войти</string>
<string name="exit">Выйти</string>
<string name="delete_acc">Удалить аккаунт</string>
<string name="create_acc">Создать аккаунт</string> <string name="create_acc">Создать аккаунт</string>
<string name="email">Почта</string> <string name="email">Почта</string>
<string name="login">Логин</string> <string name="login">Логин</string>
@ -30,4 +32,6 @@
<string name="load_book">Загрузить</string> <string name="load_book">Загрузить</string>
<string name="add_book">Добавить</string> <string name="add_book">Добавить</string>
<string name="read_book">Читать</string> <string name="read_book">Читать</string>
<string name="edit">Изменить</string>
<string name="back">Назад</string>
</resources> </resources>

View File

@ -3,6 +3,6 @@
<domain-config cleartextTrafficPermitted="true"> <domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">100.87.48.148</domain> <domain includeSubdomains="true">100.87.48.148</domain>
<domain includeSubdomains="true">192.168.43.198</domain> <domain includeSubdomains="true">192.168.43.198</domain>
<domain includeSubdomains="true">89.239.174.67</domain> <domain includeSubdomains="true">100.74.118.221</domain>
</domain-config> </domain-config>
</network-security-config> </network-security-config>