This commit is contained in:
Serafim_Pankin 2025-02-19 18:34:20 +03:00
commit 1cb797f307
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 != null){
if (userDTO.roles.any {it.name == "ROLE_ADMIN"}) { if (userDTO.roles.any {it.name == "ROLE_ADMIN"}) {
Log.d("adminlog", "i'm 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,6 +75,7 @@ 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 != null) {
if (userDTO.roles.any { it.name == "ROLE_ADMIN" }) { if (userDTO.roles.any { it.name == "ROLE_ADMIN" }) {
Log.d("adminlog", "i'm admin") Log.d("adminlog", "i'm admin")
binding.buttonSearch.visibility = View.VISIBLE binding.buttonSearch.visibility = View.VISIBLE
@ -86,6 +87,7 @@ class ProfileFragment: Fragment(R.layout.fragment_profile) {
binding.rightsUsingSmartphone.text = "Пропуск действителен" binding.rightsUsingSmartphone.text = "Пропуск действителен"
} }
} }
}
private fun hideButtons() { private fun hideButtons() {
binding.logout.visibility = View.GONE binding.logout.visibility = View.GONE
binding.scan.visibility = View.GONE binding.scan.visibility = View.GONE