Commit 2dad9429 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Add permission checks to edit and pin

parent a0869f09
......@@ -23,7 +23,8 @@ class ServerPresenter @Inject constructor(private val view: ServerView,
private var settingsFilter = arrayOf(SITE_URL, SITE_NAME, FAVICON_512, USE_REALNAME, ALLOW_ROOM_NAME_SPECIAL_CHARS, FAVORITE_ROOMS,
ACCOUNT_LOGIN_FORM, ACCOUNT_GOOGLE, ACCOUNT_FACEBOOK, ACCOUNT_GITHUB, ACCOUNT_GITLAB, ACCOUNT_LINKEDIN, ACCOUNT_METEOR,
ACCOUNT_TWITTER, ACCOUNT_WORDPRESS, LDAP_ENABLE, ACCOUNT_REGISTRATION, STORAGE_TYPE, HIDE_USER_JOIN, HIDE_USER_LEAVE, HIDE_TYPE_AU,
HIDE_MUTE_UNMUTE, HIDE_TYPE_RU, ACCOUNT_CUSTOM_FIELDS, ALLOW_MESSAGE_DELETING, ALLOW_MESSAGE_EDITING, SHOW_DELETED_STATUS, SHOW_EDITED_STATUS)
HIDE_MUTE_UNMUTE, HIDE_TYPE_RU, ACCOUNT_CUSTOM_FIELDS, ALLOW_MESSAGE_DELETING, ALLOW_MESSAGE_EDITING, ALLOW_MESSAGE_PINNING,
SHOW_DELETED_STATUS, SHOW_EDITED_STATUS)
fun connect(server: String) {
if (!UrlHelper.isValidUrl(server)) {
......
......@@ -18,7 +18,6 @@ import chat.rocket.core.internal.rest.*
import chat.rocket.core.model.Message
import chat.rocket.core.model.Value
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.cancel
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.launch
import timber.log.Timber
......@@ -28,7 +27,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
private val strategy: CancelStrategy,
getSettingsInteractor: GetSettingsInteractor,
private val serverInteractor: GetCurrentServerInteractor,
private val getPermissionsInteractor: GetPermissionsInteractor,
private val permissions: GetPermissionsInteractor,
private val messagesRepository: MessagesRepository,
factory: RocketChatClientFactory,
private val mapper: MessageViewModelMapper) {
......@@ -149,17 +148,16 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
*/
fun deleteMessage(roomId: String, id: String) {
launchUI(strategy) {
if (!getPermissionsInteractor.isMessageDeletingAllowed()) {
if (!permissions.allowedMessageDeleting()) {
return@launchUI
}
//TODO: Default delete message always to true. Until we have the permissions system
//implemented, a user will only be able to delete his own messages.
try {
//TODO: Should honor permission 'Message_ShowDeletedStatus'
client.deleteMessage(roomId, id, true)
// if Message_ShowDeletedStatus == true an update to that message will be dispatched.
// Otherwise we signalize that we just want the message removed.
if (!getPermissionsInteractor.showDeletedStatus()) {
if (!permissions.showDeletedStatus()) {
view.dispatchDeleteMessage(id)
}
} catch (e: RocketChatException) {
......@@ -225,8 +223,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
*/
fun editMessage(roomId: String, messageId: String, text: String) {
launchUI(strategy) {
if (!getPermissionsInteractor.isMessageEditingAllowed()) {
coroutineContext.cancel()
if (!permissions.allowedMessageEditing()) {
view.showMessage(R.string.permission_editing_not_allowed)
return@launchUI
}
......@@ -236,7 +233,10 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
fun pinMessage(messageId: String) {
launchUI(strategy) {
//TODO: Check permissions.
if (!permissions.allowedMessagePinning()) {
view.showMessage(R.string.permission_pinning_not_allowed)
return@launchUI
}
try {
client.pinMessage(messageId)
} catch (e: RocketChatException) {
......@@ -247,7 +247,10 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
fun unpinMessage(messageId: String) {
launchUI(strategy) {
//TODO: Check permissions.
if (!permissions.allowedMessagePinning()) {
view.showMessage(R.string.permission_pinning_not_allowed)
return@launchUI
}
try {
client.unpinMessage(messageId)
} catch (e: RocketChatException) {
......
......@@ -10,12 +10,17 @@ class GetPermissionsInteractor @Inject constructor(private val settingsRepositor
/**
* Check whether user is allowed to delete a message.
*/
fun isMessageDeletingAllowed() = publicSettings()?.deleteMessageAllowed() ?: false
fun allowedMessageDeleting() = publicSettings()?.allowedMessageDeleting() ?: false
/**
* Checks whether user is allowed to edit a message.
*/
fun isMessageEditingAllowed() = publicSettings()?.deleteMessageAllowed() ?: false
fun allowedMessageEditing() = publicSettings()?.allowedMessageEditing() ?: false
/**
* Checks whether user is allowed to pin a message to a channel.
*/
fun allowedMessagePinning() = publicSettings()?.allowedMessagePinning() ?: false
/**
* Checks whether should show deleted message status.
......
......@@ -38,6 +38,7 @@ const val ALLOW_MESSAGE_DELETING = "Message_AllowDeleting"
const val ALLOW_MESSAGE_EDITING = "Message_AllowEditing"
const val SHOW_DELETED_STATUS = "Message_ShowDeletedStatus"
const val SHOW_EDITED_STATUS = "Message_ShowEditedStatus"
const val ALLOW_MESSAGE_PINNING = "Message_AllowPinning"
/*
* Extension functions for Public Settings.
*
......@@ -56,10 +57,11 @@ fun Map<String, Value<Any>>.wordpressEnabled(): Boolean = this[ACCOUNT_WORDPRESS
fun Map<String, Value<Any>>.useRealName(): Boolean = this[USE_REALNAME]?.value == true
// Message settings
fun Map<String, Value<Any>>.deleteMessageAllowed(): Boolean = this[ALLOW_MESSAGE_DELETING]?.value == true
fun Map<String, Value<Any>>.editingMessageAllowed(): Boolean = this[ALLOW_MESSAGE_EDITING]?.value == true
fun Map<String, Value<Any>>.showDeletedStatus(): Boolean = this[SHOW_DELETED_STATUS]?.value == true
fun Map<String, Value<Any>>.showEditedStatus(): Boolean = this[SHOW_EDITED_STATUS]?.value == true
fun Map<String, Value<Any>>.allowedMessagePinning(): Boolean = this[ALLOW_MESSAGE_PINNING]?.value == true
fun Map<String, Value<Any>>.allowedMessageEditing(): Boolean = this[ALLOW_MESSAGE_EDITING]?.value == true
fun Map<String, Value<Any>>.allowedMessageDeleting(): Boolean = this[ALLOW_MESSAGE_DELETING]?.value == true
fun Map<String, Value<Any>>.registrationEnabled(): Boolean {
val value = this[ACCOUNT_REGISTRATION]
......
......@@ -71,6 +71,8 @@
<!-- Permission messages -->
<string name="permission_editing_not_allowed">Edição não permitida</string>
<string name="permission_deleting_not_allowed">Remoção não permitida</string>
<string name="permission_pinning_not_allowed">Fixar não permitido</string>
<!-- Pinned Messages -->
<string name="title_pinned_messages">Mensagens Pinadas</string>
......
......@@ -72,7 +72,9 @@
<string name="action_title_editing">Editing Message</string>
<!-- Permission messages -->
<string name="permission_editing_not_allowed">Editing not allowed</string>
<string name="permission_editing_not_allowed">Editing is not allowed</string>
<string name="permission_deleting_not_allowed">Deleting is not allowed</string>
<string name="permission_pinning_not_allowed">Pinning is not allowed</string>
<!-- Pinned Messages -->
<string name="title_pinned_messages">Pinned Messages</string>
......
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