Skip to content

Commit

Permalink
added different role requirements for chat commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Jun 23, 2015
1 parent a33abe1 commit 0ac0335
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main/groovy/net/zomis/duga/tasks/ChatCommandDelegate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,49 @@ abstract class ChatCommandDelegate extends Script {
}

Closure ping = {
allowAll()
message.reply('pong!')
}

void allowAll() {
assert message.user_id > 0
}

Map say(String text) {
requireAdmin()
[inRoom: {int id ->
bean.chatBot.postSingle(WebhookParameters.toRoom(Integer.toString(id)), text)
}, default: {
message.reply(text)
}]
}

void requireUser() {
assert message.fetchUser()
}

void requireRole(String role) {
User user = message.fetchUser()
if (!user) {
message.reply('You are not registered with Duga. Please go to http://stats.zomis.net/GithubHookSEChatService/registration/index for registration instructions')
assert false
}
boolean userHasRole = false
User.withNewSession {status ->
userHasRole = message.fetchUser().getAuthorities().stream().anyMatch({auth ->
auth.authority.equals(role)
})
if (!userHasRole) {
message.reply('Unauthorized, requires ' + role)
}
}
assert userHasRole
}

void requireAdmin() {
requireRole('ROLE_ADMIN')
}

ChatMessageIncoming getMessage() {
message
}
Expand All @@ -49,6 +81,7 @@ abstract class ChatCommandDelegate extends Script {
}

def register(String githubKey) {
allowAll()
User.withNewSession {status ->
User user = User.findByPingExpect(githubKey)
if (user == null) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/groovy/net/zomis/duga/tasks/ChatMessageIncoming.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.zomis.duga.tasks

import net.zomis.duga.DugaBot
import net.zomis.duga.User
import net.zomis.duga.chat.WebhookParameters
import org.apache.commons.lang.StringEscapeUtils

Expand All @@ -22,6 +23,18 @@ class ChatMessageIncoming {

DugaBot bot
WebhookParameters params
private User dugaUser

User fetchUser() {
if (dugaUser) {
return dugaUser
} else {
User.withNewSession {status ->
dugaUser = User.findByChatId(user_id)
}
return dugaUser
}
}

void reply(String message) {
bot.postSingle(params, ":$message_id $message")
Expand Down

0 comments on commit 0ac0335

Please sign in to comment.