This commit is contained in:
Izlydov 2025-02-19 18:29:52 +03:00
parent c66e737bc6
commit 99ee6fbaac
4 changed files with 21 additions and 13 deletions

View File

@ -53,7 +53,10 @@ class MainActivity : AppCompatActivity() {
Log.d("Navigate", "Navigate to " + destination.label) Log.d("Navigate", "Navigate to " + destination.label)
navView.visibility = if (destination.id == R.id.nav_auth) View.GONE else View.VISIBLE navView.visibility = if (destination.id == R.id.nav_auth) View.GONE else View.VISIBLE
val userDTO = UserServiceST.getInstance().getUserDTO() val userDTO = UserServiceST.getInstance().getUserDTO()
if (!userDTO.roles.any {it.name == "ROLE_ADMIN"}) navView.menu.findItem(R.id.nav_admin).setVisible(false) else navView.menu.findItem(R.id.nav_admin).setVisible(true) if (userDTO != null) {
if (!userDTO.roles.any { it.name == "ROLE_ADMIN" }) navView.menu.findItem(R.id.nav_admin)
.setVisible(false) else navView.menu.findItem(R.id.nav_admin).setVisible(true)
}
} }
return navController return navController
} }
@ -63,8 +66,10 @@ class MainActivity : AppCompatActivity() {
} }
private fun checkForAdmin() { private fun checkForAdmin() {
val userDTO = UserServiceST.getInstance().getUserDTO() val userDTO = UserServiceST.getInstance().getUserDTO()
if (userDTO.roles.any {it.name == "ROLE_ADMIN"}){ if (userDTO != null){
Log.d("adminlog", "i'm admin") if (userDTO.roles.any {it.name == "ROLE_ADMIN"}) {
Log.d("adminlog", "i'm admin")
}
} }
} }
} }

View File

@ -3,6 +3,7 @@ package com.displaynone.acss.components.auth.internal_utils;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import androidx.annotation.Nullable;
import androidx.security.crypto.EncryptedSharedPreferences; import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKeys; import androidx.security.crypto.MasterKeys;
@ -45,7 +46,7 @@ public class UserManager {
.putString(ACCESS_KEY,toJson(userDTO)) .putString(ACCESS_KEY,toJson(userDTO))
.apply(); .apply();
} }
public UserDTO getDto() { public @Nullable UserDTO getDto() {
if (this.userDTO != null) return this.userDTO; if (this.userDTO != null) return this.userDTO;
UserDTO userDTO = fromJson( _preferences.getString(ACCESS_KEY, null)); UserDTO userDTO = fromJson( _preferences.getString(ACCESS_KEY, null));
return userDTO; return userDTO;

View File

@ -84,7 +84,7 @@ class UserServiceST(
suspend fun openDoor(code: String): Result<Int> { suspend fun openDoor(code: String): Result<Int> {
return userRepository.openDoor(tokenManager.authTokenPair!!.accessToken, code = code) return userRepository.openDoor(tokenManager.authTokenPair!!.accessToken, code = code)
} }
fun getUserDTO(): UserDTO { fun getUserDTO(): UserDTO? {
return userManager.dto return userManager.dto
} }
fun saveUserDTO(userDTO: UserDTO){ fun saveUserDTO(userDTO: UserDTO){

View File

@ -75,15 +75,17 @@ class ProfileFragment: Fragment(R.layout.fragment_profile) {
Log.d("check", "cheking for roles") Log.d("check", "cheking for roles")
val userDTO = UserServiceST.getInstance().getUserDTO() val userDTO = UserServiceST.getInstance().getUserDTO()
if (userDTO.roles.any {it.name == "ROLE_ADMIN"}){ if (userDTO != null) {
Log.d("adminlog", "i'm admin") if (userDTO.roles.any { it.name == "ROLE_ADMIN" }) {
binding.buttonSearch.visibility = View.VISIBLE Log.d("adminlog", "i'm admin")
binding.rightsUsingSmartphone.text = "Пропуск действителен" binding.buttonSearch.visibility = View.VISIBLE
} binding.rightsUsingSmartphone.text = "Пропуск действителен"
if (userDTO.roles.any {it.name == "ROLE_USER"}){ }
Log.d("userlog", "i'm user") if (userDTO.roles.any { it.name == "ROLE_USER" }) {
Log.d("userlog", "i'm user")
binding.rightsUsingSmartphone.text = "Пропуск действителен" binding.rightsUsingSmartphone.text = "Пропуск действителен"
}
} }
} }
private fun hideButtons() { private fun hideButtons() {