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