Check if user have enough power level to change settings.

This commit is contained in:
onurays 2020-06-23 10:59:08 +03:00 committed by Benoit Marty
parent 762dd1d0a5
commit 1f30cf468a
5 changed files with 88 additions and 7 deletions

View file

@ -17,6 +17,7 @@
package im.vector.matrix.android.api.session.room.powerlevels package im.vector.matrix.android.api.session.room.powerlevels
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.room.model.PowerLevelsContent import im.vector.matrix.android.api.session.room.model.PowerLevelsContent
/** /**
@ -123,4 +124,59 @@ class PowerLevelsHelper(private val powerLevelsContent: PowerLevelsContent) {
else -> Role.Moderator.value else -> Role.Moderator.value
} }
} }
/**
* Check if user have the necessary power level to change room name
* @param userId the id of the user to check for.
* @return true if able to change room name
*/
fun isUserAbleToChangeRoomName(userId: String): Boolean {
val powerLevel = getUserPowerLevelValue(userId)
val minPowerLevel = powerLevelsContent.events[EventType.STATE_ROOM_NAME] ?: powerLevelsContent.stateDefault
return powerLevel >= minPowerLevel
}
/**
* Check if user have the necessary power level to change room topic
* @param userId the id of the user to check for.
* @return true if able to change room topic
*/
fun isUserAbleToChangeRoomTopic(userId: String): Boolean {
val powerLevel = getUserPowerLevelValue(userId)
val minPowerLevel = powerLevelsContent.events[EventType.STATE_ROOM_TOPIC] ?: powerLevelsContent.stateDefault
return powerLevel >= minPowerLevel
}
/**
* Check if user have the necessary power level to change room canonical alias
* @param userId the id of the user to check for.
* @return true if able to change room canonical alias
*/
fun isUserAbleToChangeRoomCanonicalAlias(userId: String): Boolean {
val powerLevel = getUserPowerLevelValue(userId)
val minPowerLevel = powerLevelsContent.events[EventType.STATE_ROOM_CANONICAL_ALIAS] ?: powerLevelsContent.stateDefault
return powerLevel >= minPowerLevel
}
/**
* Check if user have the necessary power level to change room history readability
* @param userId the id of the user to check for.
* @return true if able to change room history readability
*/
fun isUserAbleToChangeRoomHistoryReadability(userId: String): Boolean {
val powerLevel = getUserPowerLevelValue(userId)
val minPowerLevel = powerLevelsContent.events[EventType.STATE_ROOM_HISTORY_VISIBILITY] ?: powerLevelsContent.stateDefault
return powerLevel >= minPowerLevel
}
/**
* Check if user have the necessary power level to change room avatar
* @param userId the id of the user to check for.
* @return true if able to change room avatar
*/
fun isUserAbleToChangeRoomAvatar(userId: String): Boolean {
val powerLevel = getUserPowerLevelValue(userId)
val minPowerLevel = powerLevelsContent.events[EventType.STATE_ROOM_AVATAR] ?: powerLevelsContent.stateDefault
return powerLevel >= minPowerLevel
}
} }

View file

@ -71,7 +71,7 @@ abstract class ProfileActionItem : VectorEpoxyModel<ProfileActionItem.Holder>()
override fun bind(holder: Holder) { override fun bind(holder: Holder) {
super.bind(holder) super.bind(holder)
holder.view.setOnClickListener(listener) holder.view.setOnClickListener(listener)
if (listener == null) { if (listener == null || !editable) {
holder.view.isClickable = false holder.view.isClickable = false
} }
holder.title.text = title holder.title.text = title

View file

@ -67,7 +67,7 @@ class RoomSettingsController @Inject constructor(
formEditTextItem { formEditTextItem {
id("name") id("name")
/*enabled(enableFormElement)*/ enabled(data.actionPermissions.canChangeName)
value(data.newName ?: roomSummary.displayName) value(data.newName ?: roomSummary.displayName)
hint(stringProvider.getString(R.string.room_settings_name_hint)) hint(stringProvider.getString(R.string.room_settings_name_hint))
@ -78,7 +78,7 @@ class RoomSettingsController @Inject constructor(
formEditTextItem { formEditTextItem {
id("topic") id("topic")
/*enabled(enableFormElement)*/ enabled(data.actionPermissions.canChangeTopic)
value(data.newTopic ?: roomSummary.topic) value(data.newTopic ?: roomSummary.topic)
hint(stringProvider.getString(R.string.room_settings_topic_hint)) hint(stringProvider.getString(R.string.room_settings_topic_hint))
@ -89,7 +89,7 @@ class RoomSettingsController @Inject constructor(
formEditTextItem { formEditTextItem {
id("alias") id("alias")
/*enabled(enableFormElement)*/ enabled(data.actionPermissions.canChangeCanonicalAlias)
value(data.newAlias ?: roomSummary.canonicalAlias) value(data.newAlias ?: roomSummary.canonicalAlias)
hint(stringProvider.getString(R.string.room_settings_addresses_add_new_address)) hint(stringProvider.getString(R.string.room_settings_addresses_add_new_address))
@ -104,7 +104,7 @@ class RoomSettingsController @Inject constructor(
subtitle = newHistoryVisibility ?: historyVisibility, subtitle = newHistoryVisibility ?: historyVisibility,
dividerColor = dividerColor, dividerColor = dividerColor,
divider = false, divider = false,
editable = true, editable = data.actionPermissions.canChangeHistoryReadability,
action = { callback?.onHistoryVisibilityClicked() } action = { callback?.onHistoryVisibilityClicked() }
) )
@ -114,7 +114,7 @@ class RoomSettingsController @Inject constructor(
subtitle = "", subtitle = "",
dividerColor = dividerColor, dividerColor = dividerColor,
divider = true, divider = true,
editable = true, editable = data.actionPermissions.canChangeAvatar,
accessoryMatrixItem = roomSummary.toMatrixItem(), accessoryMatrixItem = roomSummary.toMatrixItem(),
avatarRenderer = avatarRenderer, avatarRenderer = avatarRenderer,
action = { callback?.onPhotoClicked() } action = { callback?.onPhotoClicked() }

View file

@ -24,9 +24,11 @@ import com.squareup.inject.assisted.AssistedInject
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.room.powerlevels.PowerLevelsHelper
import im.vector.matrix.rx.rx import im.vector.matrix.rx.rx
import im.vector.matrix.rx.unwrap import im.vector.matrix.rx.unwrap
import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.features.powerlevel.PowerLevelsObservableFactory
import io.reactivex.Completable import io.reactivex.Completable
import io.reactivex.Observable import io.reactivex.Observable
import java.util.Locale import java.util.Locale
@ -68,6 +70,20 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState:
newTopic = roomSummary?.topic newTopic = roomSummary?.topic
) )
} }
val powerLevelsContentLive = PowerLevelsObservableFactory(room).createObservable()
powerLevelsContentLive.subscribe {
val powerLevelsHelper = PowerLevelsHelper(it)
val permissions = RoomSettingsViewState.ActionPermissions(
canChangeName = powerLevelsHelper.isUserAbleToChangeRoomName(session.myUserId),
canChangeTopic = powerLevelsHelper.isUserAbleToChangeRoomTopic(session.myUserId),
canChangeCanonicalAlias = powerLevelsHelper.isUserAbleToChangeRoomCanonicalAlias(session.myUserId),
canChangeAvatar = powerLevelsHelper.isUserAbleToChangeRoomAvatar(session.myUserId),
canChangeHistoryReadability = powerLevelsHelper.isUserAbleToChangeRoomHistoryReadability(session.myUserId)
)
setState { copy(actionPermissions = permissions) }
}.disposeOnClear()
} }
override fun handle(action: RoomSettingsAction) { override fun handle(action: RoomSettingsAction) {

View file

@ -35,8 +35,17 @@ data class RoomSettingsViewState(
val newAvatar: MultiPickerImageType? = null, val newAvatar: MultiPickerImageType? = null,
val newHistoryVisibility: RoomHistoryVisibility? = null, val newHistoryVisibility: RoomHistoryVisibility? = null,
val newAlias: String? = null, val newAlias: String? = null,
val showSaveAction: Boolean = false val showSaveAction: Boolean = false,
val actionPermissions: ActionPermissions = ActionPermissions()
) : MvRxState { ) : MvRxState {
constructor(args: RoomProfileArgs) : this(roomId = args.roomId) constructor(args: RoomProfileArgs) : this(roomId = args.roomId)
data class ActionPermissions(
val canChangeName: Boolean = false,
val canChangeTopic: Boolean = false,
val canChangeCanonicalAlias: Boolean = false,
val canChangeAvatar: Boolean = false,
val canChangeHistoryReadability: Boolean = false
)
} }