Commit 47b9b897 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Checking for permissions before showing the Admin Panel view instead of...

Checking for permissions before showing the Admin Panel view instead of checking wheater the user is admin.
parent ec2f0497
...@@ -13,12 +13,12 @@ import androidx.annotation.IdRes ...@@ -13,12 +13,12 @@ import androidx.annotation.IdRes
import androidx.drawerlayout.widget.DrawerLayout import androidx.drawerlayout.widget.DrawerLayout
import chat.rocket.android.BuildConfig import chat.rocket.android.BuildConfig
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.helper.UserHelper
import chat.rocket.android.main.adapter.AccountsAdapter import chat.rocket.android.main.adapter.AccountsAdapter
import chat.rocket.android.main.adapter.Selector import chat.rocket.android.main.adapter.Selector
import chat.rocket.android.main.presentation.MainPresenter import chat.rocket.android.main.presentation.MainPresenter
import chat.rocket.android.main.presentation.MainView import chat.rocket.android.main.presentation.MainView
import chat.rocket.android.main.uimodel.NavHeaderUiModel import chat.rocket.android.main.uimodel.NavHeaderUiModel
import chat.rocket.android.server.domain.PermissionsInteractor
import chat.rocket.android.server.domain.model.Account import chat.rocket.android.server.domain.model.Account
import chat.rocket.android.server.ui.INTENT_CHAT_ROOM_ID import chat.rocket.android.server.ui.INTENT_CHAT_ROOM_ID
import chat.rocket.android.util.extensions.fadeIn import chat.rocket.android.util.extensions.fadeIn
...@@ -52,7 +52,7 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector, ...@@ -52,7 +52,7 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
@Inject @Inject
lateinit var presenter: MainPresenter lateinit var presenter: MainPresenter
@Inject @Inject
lateinit var userHelper: UserHelper lateinit var permissions: PermissionsInteractor
private var isFragmentAdded: Boolean = false private var isFragmentAdded: Boolean = false
private var expanded = false private var expanded = false
private val headerLayout by lazy { view_navigation.getHeaderView(0) } private val headerLayout by lazy { view_navigation.getHeaderView(0) }
......
...@@ -45,7 +45,7 @@ internal fun MainActivity.setupMenu(menu: Menu) { ...@@ -45,7 +45,7 @@ internal fun MainActivity.setupMenu(menu: Menu) {
R.string.title_settings R.string.title_settings
).setIcon(R.drawable.ic_settings_black_24dp) ).setIcon(R.drawable.ic_settings_black_24dp)
if (userHelper.isAdmin()) { if (permissions.canSeeTheAdminChannel()) {
menu.add( menu.add(
MENU_SECTION_TWO, MENU_SECTION_TWO,
MENU_ACTION_ADMIN_PANEL, MENU_ACTION_ADMIN_PANEL,
......
package chat.rocket.android.server.domain package chat.rocket.android.server.domain
import chat.rocket.android.helper.UserHelper import chat.rocket.android.helper.UserHelper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.core.model.Permission
import javax.inject.Inject import javax.inject.Inject
// Creating rooms // Creating rooms
const val CREATE_PUBLIC_CHANNELS = "create-c" private const val CREATE_PUBLIC_CHANNELS = "create-c"
const val CREATE_DIRECT_MESSAGES = "create-d" private const val CREATE_DIRECT_MESSAGES = "create-d"
const val CREATE_PRIVATE_CHANNELS = "create-p" private const val CREATE_PRIVATE_CHANNELS = "create-p"
// Messages // Messages
const val DELETE_MESSAGE = "delete-message" private const val DELETE_MESSAGE = "delete-message"
const val FORCE_DELETE_MESSAGE = "force-delete-message" private const val FORCE_DELETE_MESSAGE = "force-delete-message"
const val EDIT_MESSAGE = "edit-message" private const val EDIT_MESSAGE = "edit-message"
const val PIN_MESSAGE = "pin-message" private const val PIN_MESSAGE = "pin-message"
const val POST_READONLY = "post-readonly" private const val POST_READONLY = "post-readonly"
private const val VIEW_STATISTICS = "view-statistics"
private const val VIEW_ROOM_ADMINISTRATION = "view-room-administration"
private const val VIEW_USER_ADMINISTRATION = "view-user-administration"
private const val VIEW_PRIVILEGED_SETTING = "view-privileged-setting"
class PermissionsInteractor @Inject constructor( class PermissionsInteractor @Inject constructor(
private val settingsRepository: SettingsRepository, private val settingsRepository: SettingsRepository,
...@@ -23,14 +26,8 @@ class PermissionsInteractor @Inject constructor( ...@@ -23,14 +26,8 @@ class PermissionsInteractor @Inject constructor(
private val getCurrentServerInteractor: GetCurrentServerInteractor, private val getCurrentServerInteractor: GetCurrentServerInteractor,
private val userHelper: UserHelper private val userHelper: UserHelper
) { ) {
private fun publicSettings(): PublicSettings? = settingsRepository.get(currentServerUrl()!!) private fun publicSettings(): PublicSettings? = settingsRepository.get(currentServerUrl()!!)
fun saveAll(permissions: List<Permission>) {
val url = currentServerUrl()!!
permissions.forEach { permissionsRepository.save(url, it) }
}
/** /**
* Check whether the user is allowed to delete a message. * Check whether the user is allowed to delete a message.
*/ */
...@@ -71,6 +68,29 @@ class PermissionsInteractor @Inject constructor( ...@@ -71,6 +68,29 @@ class PermissionsInteractor @Inject constructor(
} == true || userHelper.isAdmin() } == true || userHelper.isAdmin()
} }
fun canSeeTheAdminChannel(): Boolean {
currentServerUrl()?.let { serverUrl ->
val test = permissionsRepository.get(serverUrl, POST_READONLY)
val viewStatistics =
permissionsRepository.get(serverUrl, VIEW_STATISTICS)
val viewRoomAdministration =
permissionsRepository.get(serverUrl, VIEW_ROOM_ADMINISTRATION)
val viewUserAdministration =
permissionsRepository.get(serverUrl, VIEW_USER_ADMINISTRATION)
val viewPrivilegedSetting =
permissionsRepository.get(serverUrl, VIEW_PRIVILEGED_SETTING)
userHelper.user()?.roles?.let { userRolesList ->
return viewStatistics?.roles?.any { userRolesList.contains(it) } == true ||
viewRoomAdministration?.roles?.any { userRolesList.contains(it) } == true ||
viewUserAdministration?.roles?.any { userRolesList.contains(it) } == true ||
viewPrivilegedSetting?.roles?.any { userRolesList.contains(it) } == true
}
}
return false
}
private fun currentServerUrl(): String? { private fun currentServerUrl(): String? {
return getCurrentServerInteractor.get() return getCurrentServerInteractor.get()
} }
......
...@@ -5,20 +5,20 @@ import chat.rocket.core.model.Permission ...@@ -5,20 +5,20 @@ import chat.rocket.core.model.Permission
interface PermissionsRepository { interface PermissionsRepository {
/** /**
* Store [permission] locally. * Stores [permission] locally.
* *
* @param url The server url from where we're interest to store the permission. * @param url The server url to store the permission.
* @param permission The permission to store. * @param permission The permission to store.
*/ */
fun save(url: String, permission: Permission) fun save(url: String, permission: Permission)
/** /**
* Get permission given by the [permissionId] and for the server [url]. * Gets permission given by the [permissionId] and for the server [url].
* *
* @param url The server url from where we're interested on getting the permissions. * @param url The server url to get the permissions from.
* @param permissionId the id of the permission to get. * @param permissionId the ID of the permission to get.
* *
* @return The interested [Permission] or null if not found. * @return The [Permission] or null if not found.
*/ */
fun get(url: String, permissionId: String): Permission? fun get(url: String, permissionId: String): Permission?
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment