added roles logic, some fixes
This commit is contained in:
parent
58def7160d
commit
8bcf56d905
@ -26,8 +26,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||||
insets
|
insets
|
||||||
}
|
}
|
||||||
Log.d("123", R.id.action_nav_main_to_nav_profile.toString())
|
|
||||||
Log.d("123", R.id.action_nav_main_to_nav_auth.toString())
|
|
||||||
|
|
||||||
val navController: NavController = this.setupNavigation()
|
val navController: NavController = this.setupNavigation()
|
||||||
|
|
||||||
@ -54,12 +52,19 @@ class MainActivity : AppCompatActivity() {
|
|||||||
navController.addOnDestinationChangedListener { _, destination, _ ->
|
navController.addOnDestinationChangedListener { _, destination, _ ->
|
||||||
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()
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isUserAuthenticated(): Boolean {
|
private fun isUserAuthenticated(): Boolean {
|
||||||
return UserServiceST.getInstance().hasTokens()
|
return UserServiceST.getInstance().hasTokens()
|
||||||
}
|
}
|
||||||
|
private fun checkForAdmin() {
|
||||||
|
val userDTO = UserServiceST.getInstance().getUserDTO()
|
||||||
|
if (userDTO.roles.any {it.name == "ROLE_ADMIN"}){
|
||||||
|
Log.d("adminlog", "i'm admin")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -14,7 +14,6 @@ import java.io.IOException;
|
|||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
|
|
||||||
public class AuthTokenManager {
|
public class AuthTokenManager {
|
||||||
// Preferences
|
|
||||||
private static final String _PREFERENCES_FILENAME = "authData";
|
private static final String _PREFERENCES_FILENAME = "authData";
|
||||||
private final SharedPreferences _preferences;
|
private final SharedPreferences _preferences;
|
||||||
|
|
||||||
@ -46,6 +45,7 @@ public class AuthTokenManager {
|
|||||||
|
|
||||||
public void clear() {
|
public void clear() {
|
||||||
_preferences.edit().clear().apply();
|
_preferences.edit().clear().apply();
|
||||||
|
this._tokenPair = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasTokens() {
|
public boolean hasTokens() {
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
package com.displaynone.acss.components.auth.internal_utils;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
|
import androidx.security.crypto.EncryptedSharedPreferences;
|
||||||
|
import androidx.security.crypto.MasterKeys;
|
||||||
|
|
||||||
|
import com.displaynone.acss.components.auth.models.AuthTokenPair;
|
||||||
|
import com.displaynone.acss.components.auth.models.user.repository.dto.UserDTO;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class UserManager {
|
||||||
|
private static final String _PREFERENCES_FILENAME = "userData";
|
||||||
|
private final SharedPreferences _preferences;
|
||||||
|
private UserDTO userDTO;
|
||||||
|
private static final String ACCESS_KEY = "user";
|
||||||
|
private Gson gson = new Gson();
|
||||||
|
|
||||||
|
public UserManager(Context context) {
|
||||||
|
this._preferences = this._createEncryptedPreferences(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SharedPreferences _createEncryptedPreferences(Context ctx) {
|
||||||
|
try {
|
||||||
|
return EncryptedSharedPreferences.create(
|
||||||
|
_PREFERENCES_FILENAME,
|
||||||
|
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),
|
||||||
|
ctx,
|
||||||
|
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
|
||||||
|
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
|
||||||
|
);
|
||||||
|
} catch (GeneralSecurityException | IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void saveDto(UserDTO userDTO) {
|
||||||
|
this.userDTO = userDTO;
|
||||||
|
_preferences.edit()
|
||||||
|
.putString(ACCESS_KEY,toJson(userDTO))
|
||||||
|
.apply();
|
||||||
|
}
|
||||||
|
public UserDTO getDto() {
|
||||||
|
if (this.userDTO != null) return this.userDTO;
|
||||||
|
UserDTO userDTO = fromJson( _preferences.getString(ACCESS_KEY, null));
|
||||||
|
return userDTO;
|
||||||
|
}
|
||||||
|
private UserDTO fromJson(String userJSON){
|
||||||
|
UserDTO userDTO = gson.fromJson(userJSON, UserDTO.class);
|
||||||
|
return userDTO;
|
||||||
|
}
|
||||||
|
private String toJson(UserDTO userDTO){
|
||||||
|
return gson.toJson(userDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -22,6 +22,7 @@ class UserMapper {
|
|||||||
name = userEntity.name,
|
name = userEntity.name,
|
||||||
photo = userEntity.photo,
|
photo = userEntity.photo,
|
||||||
position = userEntity.position,
|
position = userEntity.position,
|
||||||
|
roles = emptyList(),
|
||||||
// lastVisit = userEntity.lastVisit,
|
// lastVisit = userEntity.lastVisit,
|
||||||
)
|
)
|
||||||
return userDto
|
return userDto
|
||||||
|
@ -1,15 +1,19 @@
|
|||||||
package com.displaynone.acss.components.auth.models.user
|
package com.displaynone.acss.components.auth.models.user
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
import com.displaynone.acss.components.auth.internal_utils.AuthTokenManager
|
import com.displaynone.acss.components.auth.internal_utils.AuthTokenManager
|
||||||
|
import com.displaynone.acss.components.auth.internal_utils.UserManager
|
||||||
import com.displaynone.acss.components.auth.models.user.repository.UserRepository
|
import com.displaynone.acss.components.auth.models.user.repository.UserRepository
|
||||||
import com.displaynone.acss.components.auth.models.user.repository.dto.LastVisitsDto
|
import com.displaynone.acss.components.auth.models.user.repository.dto.LastVisitsDto
|
||||||
import com.displaynone.acss.components.auth.models.user.repository.dto.UserDTO
|
import com.displaynone.acss.components.auth.models.user.repository.dto.UserDTO
|
||||||
import com.displaynone.acss.components.auth.models.user.repository.dto.VisitDto
|
import com.displaynone.acss.components.auth.models.user.repository.dto.VisitDto
|
||||||
|
import java.util.Optional
|
||||||
|
|
||||||
|
|
||||||
class UserServiceST(
|
class UserServiceST(
|
||||||
private val tokenManager: AuthTokenManager,
|
private val tokenManager: AuthTokenManager,
|
||||||
|
private val userManager: UserManager,
|
||||||
) {
|
) {
|
||||||
private val userRepository: UserRepository = UserRepository()
|
private val userRepository: UserRepository = UserRepository()
|
||||||
|
|
||||||
@ -22,7 +26,9 @@ class UserServiceST(
|
|||||||
AuthTokenManager(
|
AuthTokenManager(
|
||||||
context
|
context
|
||||||
)
|
)
|
||||||
instance = UserServiceST(tokenManager)
|
val userManager =
|
||||||
|
UserManager(context)
|
||||||
|
instance = UserServiceST(tokenManager, userManager)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +56,7 @@ class UserServiceST(
|
|||||||
pageNum = pageNum,
|
pageNum = pageNum,
|
||||||
pageSize = pageSize,
|
pageSize = pageSize,
|
||||||
token = tokenManager.authTokenPair!!.accessToken
|
token = tokenManager.authTokenPair!!.accessToken
|
||||||
).map { pagingDto -> pagingDto.content
|
).map { pagingDto -> pagingDto.content }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
suspend fun getLastVisitsByLogin(pageNum: Int,
|
suspend fun getLastVisitsByLogin(pageNum: Int,
|
||||||
pageSize: Int,
|
pageSize: Int,
|
||||||
@ -79,4 +84,11 @@ 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 {
|
||||||
|
return userManager.dto
|
||||||
|
}
|
||||||
|
fun saveUserDTO(userDTO: UserDTO){
|
||||||
|
return userManager.saveDto(userDTO)
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.displaynone.acss.components.auth.models.user.repository.dto
|
||||||
|
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class AuthorityDTO (
|
||||||
|
@SerialName("id")
|
||||||
|
val id: Long,
|
||||||
|
@SerialName("name")
|
||||||
|
val name: String
|
||||||
|
)
|
@ -20,6 +20,8 @@ data class UserDTO (
|
|||||||
|
|
||||||
@SerialName("position")
|
@SerialName("position")
|
||||||
val position: String,
|
val position: String,
|
||||||
|
@SerialName("roles")
|
||||||
|
val roles: List<AuthorityDTO>,
|
||||||
|
|
||||||
// @SerialName("lastVisit")
|
// @SerialName("lastVisit")
|
||||||
// val lastVisit: String,
|
// val lastVisit: String,
|
||||||
|
@ -110,6 +110,7 @@ class AuthFragment: Fragment(R.layout.fragment_auth) {
|
|||||||
return password.isNotEmpty() &&
|
return password.isNotEmpty() &&
|
||||||
password.length >= 8
|
password.length >= 8
|
||||||
}
|
}
|
||||||
|
|
||||||
// private fun subscribe() {
|
// private fun subscribe() {
|
||||||
// viewModel.state.collectWhenStarted(this) { state ->
|
// viewModel.state.collectWhenStarted(this) { state ->
|
||||||
// binding.login.setOnClickListener(this::onLoginButtonClicked)
|
// binding.login.setOnClickListener(this::onLoginButtonClicked)
|
||||||
|
@ -11,6 +11,7 @@ import androidx.navigation.fragment.findNavController
|
|||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import com.bumptech.glide.Glide
|
import com.bumptech.glide.Glide
|
||||||
import com.displaynone.acss.R
|
import com.displaynone.acss.R
|
||||||
|
import com.displaynone.acss.components.auth.models.user.UserServiceST
|
||||||
import com.displaynone.acss.components.auth.models.user.repository.VisitAdapter
|
import com.displaynone.acss.components.auth.models.user.repository.VisitAdapter
|
||||||
import com.displaynone.acss.components.auth.models.user.repository.dto.UserDTO
|
import com.displaynone.acss.components.auth.models.user.repository.dto.UserDTO
|
||||||
import com.displaynone.acss.databinding.FragmentProfileBinding
|
import com.displaynone.acss.databinding.FragmentProfileBinding
|
||||||
@ -29,6 +30,7 @@ class ProfileFragment: Fragment(R.layout.fragment_profile) {
|
|||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
_binding = FragmentProfileBinding.bind(view)
|
_binding = FragmentProfileBinding.bind(view)
|
||||||
|
checkForAdmin()
|
||||||
|
|
||||||
binding.swipeRefresh.setOnRefreshListener {
|
binding.swipeRefresh.setOnRefreshListener {
|
||||||
if (getIsMe()){
|
if (getIsMe()){
|
||||||
@ -61,8 +63,6 @@ class ProfileFragment: Fragment(R.layout.fragment_profile) {
|
|||||||
Log.d("ProfileFragment", "adapter submitted data")
|
Log.d("ProfileFragment", "adapter submitted data")
|
||||||
}
|
}
|
||||||
hideButtons()
|
hideButtons()
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
subscribe()
|
subscribe()
|
||||||
binding.recyclerViewLogs.layoutManager = LinearLayoutManager(requireContext())
|
binding.recyclerViewLogs.layoutManager = LinearLayoutManager(requireContext())
|
||||||
@ -71,11 +71,25 @@ class ProfileFragment: Fragment(R.layout.fragment_profile) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
private fun checkForAdmin() {
|
||||||
|
Log.d("check", "cheking for roles")
|
||||||
|
|
||||||
|
val userDTO = UserServiceST.getInstance().getUserDTO()
|
||||||
|
if (userDTO.roles.any {it.name == "ROLE_ADMIN"}){
|
||||||
|
Log.d("adminlog", "i'm admin")
|
||||||
|
binding.buttonSearch.visibility = View.VISIBLE
|
||||||
|
binding.rightsUsingSmartphone.text = "Пропуск действителен"
|
||||||
|
}
|
||||||
|
if (userDTO.roles.any {it.name == "ROLE_USER"}){
|
||||||
|
Log.d("userlog", "i'm user")
|
||||||
|
|
||||||
|
binding.rightsUsingSmartphone.text = "Пропуск действителен"
|
||||||
|
}
|
||||||
|
}
|
||||||
private fun hideButtons() {
|
private fun hideButtons() {
|
||||||
binding.logout.visibility = View.INVISIBLE
|
binding.logout.visibility = View.GONE
|
||||||
binding.scan.visibility = View.INVISIBLE
|
binding.scan.visibility = View.GONE
|
||||||
binding.buttonSearch.visibility = View.INVISIBLE
|
binding.buttonSearch.visibility = View.GONE
|
||||||
}
|
}
|
||||||
fun showMyData(userDTO: UserDTO){
|
fun showMyData(userDTO: UserDTO){
|
||||||
binding.fio.text = userDTO.name
|
binding.fio.text = userDTO.name
|
||||||
|
@ -22,7 +22,7 @@ class ProfileViewModel(): ViewModel() {
|
|||||||
capacity = Channel.BUFFERED,
|
capacity = Channel.BUFFERED,
|
||||||
onBufferOverflow = BufferOverflow.DROP_OLDEST,
|
onBufferOverflow = BufferOverflow.DROP_OLDEST,
|
||||||
)
|
)
|
||||||
private var login: String = "" // FIXME()
|
private var login: String = ""
|
||||||
fun setLogin(login1: String){
|
fun setLogin(login1: String){
|
||||||
login = login1
|
login = login1
|
||||||
}
|
}
|
||||||
@ -56,6 +56,8 @@ class ProfileViewModel(): ViewModel() {
|
|||||||
UserServiceST.getInstance().getInfo().fold(
|
UserServiceST.getInstance().getInfo().fold(
|
||||||
onSuccess = { data ->
|
onSuccess = { data ->
|
||||||
_state.emit(State.Show(data))
|
_state.emit(State.Show(data))
|
||||||
|
UserServiceST.getInstance().saveUserDTO(data)
|
||||||
|
Log.d("Pvm", data.login)
|
||||||
},
|
},
|
||||||
onFailure = { error ->
|
onFailure = { error ->
|
||||||
error.message?.let { error(it) }
|
error.message?.let { error(it) }
|
||||||
|
@ -138,7 +138,7 @@
|
|||||||
android:id="@+id/button_search"
|
android:id="@+id/button_search"
|
||||||
app:cardElevation="8dp"
|
app:cardElevation="8dp"
|
||||||
android:maxHeight="200dp"
|
android:maxHeight="200dp"
|
||||||
|
android:visibility="gone"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/administrator_searc_button_search"
|
android:text="@string/administrator_searc_button_search"
|
||||||
@ -175,9 +175,10 @@
|
|||||||
app:backgroundTint="@color/primary"
|
app:backgroundTint="@color/primary"
|
||||||
app:rippleColor="@color/white"/>
|
app:rippleColor="@color/white"/>
|
||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.button.MaterialButton
|
||||||
android:id="@+id/сhange_rights"
|
android:id="@+id/change_rights"
|
||||||
android:layout_margin="16dp"
|
android:layout_margin="16dp"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
android:visibility="gone"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/сhange_rights"
|
android:text="@string/сhange_rights"
|
||||||
android:maxHeight="200dp"
|
android:maxHeight="200dp"
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<fragment
|
<fragment
|
||||||
android:id="@+id/nav_profile"
|
android:id="@+id/nav_profile"
|
||||||
android:name="com.displaynone.acss.ui.profile.ProfileFragment"
|
android:name="com.displaynone.acss.ui.profile.ProfileFragment"
|
||||||
android:label="ProfileFragment"
|
android:label="@string/profile"
|
||||||
tools:layout="@layout/fragment_profile">
|
tools:layout="@layout/fragment_profile">
|
||||||
<action
|
<action
|
||||||
android:id="@+id/action_profileFragment_to_authFragment"
|
android:id="@+id/action_profileFragment_to_authFragment"
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
<string name="title_profile">Your profile</string>
|
<string name="title_profile">Your profile</string>
|
||||||
<string name="сhange_rights">Change rights using smartphone</string>
|
<string name="сhange_rights">Change rights using smartphone</string>
|
||||||
<string name="admin">Admin panel</string>
|
<string name="admin">Admin panel</string>
|
||||||
|
<string name="profile">Profile</string>
|
||||||
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
Loading…
x
Reference in New Issue
Block a user