Change auth level checking to be on command level

The server sends new auth level to user when modded
closes #60
This commit is contained in:
joeyak 2019-03-24 09:24:57 -04:00
parent 5c87d70d3e
commit cd34480bba
7 changed files with 59 additions and 51 deletions

View File

@ -15,8 +15,7 @@ type Client struct {
conn *chatConnection
belongsTo *ChatRoom
color string
IsMod bool
IsAdmin bool
CmdLevel common.CommandLevel
IsColorForced bool
IsNameForced bool
}
@ -24,6 +23,12 @@ type Client struct {
//Client has a new message to broadcast
func (cl *Client) NewMsg(data common.ClientData) {
switch data.Type {
case common.CdAuth:
fmt.Printf("[chat|hidden] <%s> get auth level\n", cl.name)
err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, cl.CmdLevel))
if err != nil {
fmt.Printf("Error sending auth level to client: %v\n", err)
}
case common.CdUsers:
fmt.Printf("[chat|hidden] <%s> get list of users\n", cl.name)
@ -37,7 +42,7 @@ func (cl *Client) NewMsg(data common.ClientData) {
err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, append(names[:idx], names[idx+1:]...)))
if err != nil {
fmt.Printf("Error sending chat data: %v\n", err)
fmt.Printf("Error sending users to client: %v\n", err)
}
case common.CdMessage:
msg := html.EscapeString(data.Message)
@ -60,7 +65,7 @@ func (cl *Client) NewMsg(data common.ClientData) {
if response != "" {
err := cl.SendChatData(common.NewChatMessage("", "",
common.ParseEmotes(response),
common.CmdUser,
common.CmdlUser,
common.MsgCommandResponse))
if err != nil {
fmt.Printf("Error command results %v\n", err)
@ -77,7 +82,7 @@ func (cl *Client) NewMsg(data common.ClientData) {
fmt.Printf("[chat] <%s> %q\n", cl.name, msg)
// Enable links for mods and admins
if cl.IsMod || cl.IsAdmin {
if cl.CmdLevel >= common.CmdlMod {
msg = formatLinks(msg)
}
@ -108,7 +113,7 @@ func (cl *Client) Send(data common.ChatDataJSON) error {
}
func (cl *Client) SendServerMessage(s string) error {
err := cl.SendChatData(common.NewChatMessage("", ColorServerMessage, s, common.CmdUser, common.MsgServer))
err := cl.SendChatData(common.NewChatMessage("", ColorServerMessage, s, common.CmdlUser, common.MsgServer))
if err != nil {
return fmt.Errorf("could send server message to %s: message - %#v: %v", cl.name, s, err)
}
@ -146,11 +151,13 @@ func (cl *Client) Me(msg string) {
}
func (cl *Client) Mod() {
cl.IsMod = true
if cl.CmdLevel < common.CmdlMod {
cl.CmdLevel = common.CmdlMod
}
}
func (cl *Client) Unmod() {
cl.IsMod = false
cl.CmdLevel = common.CmdlUser
}
func (cl *Client) Host() string {

View File

@ -52,22 +52,21 @@ var commands = &CommandControl{
common.CNAuth.String(): Command{
HelpText: "Authenticate to admin",
Function: func(cl *Client, args []string) string {
if cl.IsAdmin {
if cl.CmdLevel == common.CmdlAdmin {
return "You are already authenticated."
}
pw := html.UnescapeString(strings.Join(args, " "))
if settings.AdminPassword == pw {
cl.IsMod = true
cl.IsAdmin = true
cl.CmdLevel = common.CmdlAdmin
cl.belongsTo.AddModNotice(cl.name + " used the admin password")
fmt.Printf("[auth] %s used the admin password\n", cl.name)
return "Admin rights granted."
}
if cl.belongsTo.redeemModPass(pw) {
cl.IsMod = true
cl.CmdLevel = common.CmdlMod
cl.belongsTo.AddModNotice(cl.name + " used a mod password")
fmt.Printf("[auth] %s used a mod password\n", cl.name)
return "Moderator privileges granted."
@ -100,7 +99,7 @@ var commands = &CommandControl{
// Two arguments to force a name change on another user: `/nick OldName NewName`
if len(args) == 2 {
if !cl.IsAdmin {
if cl.CmdLevel != common.CmdlAdmin {
return "Only admins can do that PeepoSus"
}
@ -109,7 +108,7 @@ var commands = &CommandControl{
forced = true
}
if len(args) == 1 && cl.IsNameForced && !cl.IsAdmin {
if len(args) == 1 && cl.IsNameForced && cl.CmdLevel != common.CmdlAdmin {
return "You cannot change your name once it has been changed by an admin."
}
@ -180,7 +179,7 @@ var commands = &CommandControl{
common.CNUnmod.String(): Command{
HelpText: "Revoke a user's moderator privilages. Moderators can only unmod themselves.",
Function: func(cl *Client, args []string) string {
if len(args) > 0 && !cl.IsAdmin && cl.name != args[0] {
if len(args) > 0 && cl.CmdLevel != common.CmdlAdmin && cl.name != args[0] {
return "You can only unmod yourself, not others."
}
@ -334,7 +333,7 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie
// Look for mod command
if modCmd, ok := cc.mod[cmd]; ok {
if sender.IsMod || sender.IsAdmin {
if sender.CmdLevel >= common.CmdlMod {
fmt.Printf("[mod] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return modCmd.Function(sender, args)
}
@ -345,7 +344,7 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie
// Look for admin command
if adminCmd, ok := cc.admin[cmd]; ok {
if sender.IsAdmin {
if sender.CmdLevel == common.CmdlAdmin {
fmt.Printf("[admin] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return adminCmd.Function(sender, args)
}
@ -360,12 +359,13 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie
func cmdHelp(cl *Client, args []string) string {
url := "/help"
if cl.IsMod {
url = "/help?mod=1"
if cl.CmdLevel >= common.CmdlMod {
url += "?mod=1"
}
if cl.IsAdmin {
url = "/help?mod=1&admin=1"
if cl.CmdLevel == common.CmdlAdmin {
url += "&admin=1"
}
cl.SendChatData(common.NewChatCommand(common.CmdHelp, []string{url}))
@ -375,11 +375,11 @@ func cmdHelp(cl *Client, args []string) string {
func getHelp(lvl common.CommandLevel) map[string]string {
var cmdList map[string]Command
switch lvl {
case common.CmdUser:
case common.CmdlUser:
cmdList = commands.user
case common.CmdMod:
case common.CmdlMod:
cmdList = commands.mod
case common.CmdAdmin:
case common.CmdlAdmin:
cmdList = commands.admin
}
@ -401,7 +401,7 @@ var cmdColor = Command{
// If the caller is priviledged enough, they can change the color of another user
if len(args) == 2 {
if !(cl.IsMod || cl.IsAdmin) {
if cl.CmdLevel == common.CmdlUser {
return "You cannot change someone else's color. PeepoSus"
}
@ -492,6 +492,9 @@ var cmdColor = Command{
var cmdWhoAmI = Command{
HelpText: "Shows debug user info",
Function: func(cl *Client, args []string) string {
return fmt.Sprintf("Name: %s IsMod: %t IsAdmin: %t", cl.name, cl.IsMod, cl.IsAdmin)
return fmt.Sprintf("Name: %s IsMod: %t IsAdmin: %t",
cl.name,
cl.CmdLevel >= common.CmdlMod,
cl.CmdLevel == common.CmdlAdmin)
},
}

View File

@ -154,11 +154,11 @@ func (cr *ChatRoom) Kick(name string) string {
return "Unable to get client for name " + name
}
if client.IsMod {
if client.CmdLevel == common.CmdlMod {
return "You cannot kick another mod."
}
if client.IsAdmin {
if client.CmdLevel == common.CmdlAdmin {
return "Jebaited No."
}
@ -182,7 +182,7 @@ func (cr *ChatRoom) Ban(name string) string {
return "Cannot find that name"
}
if client.IsAdmin {
if client.CmdLevel == common.CmdlAdmin {
return "You cannot ban an admin Jebaited"
}
@ -226,16 +226,8 @@ func (cr *ChatRoom) AddMsg(from *Client, isAction, isServer bool, msg string) {
t = common.MsgServer
}
lvl := common.CmdUser
if from.IsMod {
lvl = common.CmdMod
}
if from.IsAdmin {
lvl = common.CmdAdmin
}
select {
case cr.queue <- common.NewChatMessage(from.name, from.color, msg, lvl, t):
case cr.queue <- common.NewChatMessage(from.name, from.color, msg, from.CmdLevel, t):
default:
fmt.Println("Unable to queue chat message. Channel full.")
}
@ -251,7 +243,7 @@ func (cr *ChatRoom) AddCmdMsg(command common.CommandType, args []string) {
func (cr *ChatRoom) AddModNotice(message string) {
select {
case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdUser, common.MsgNotice):
case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdlUser, common.MsgNotice):
default:
fmt.Println("Unable to queue notice. Channel full.")
}
@ -288,8 +280,10 @@ func (cr *ChatRoom) Mod(name string) error {
return err
}
client.IsMod = true
client.SendServerMessage(`You have been modded.`)
if client.CmdLevel < common.CmdlMod {
client.CmdLevel = common.CmdlMod
client.SendServerMessage(`You have been modded.`)
}
return nil
}
@ -357,7 +351,7 @@ func (cr *ChatRoom) Broadcast() {
case msg := <-cr.modqueue:
cr.clientsMtx.Lock()
for _, client := range cr.clients {
if client.IsMod || client.IsAdmin {
if client.CmdLevel >= common.CmdlMod {
send(msg, client)
}
}

View File

@ -111,9 +111,9 @@ func (dc DataMessage) HTML() string {
default:
badge := ""
switch dc.Level {
case CmdMod:
case CmdlMod:
badge = `<img src="/static/img/mod.png" class="badge" />`
case CmdAdmin:
case CmdlAdmin:
badge = `<img src="/static/img/admin.png" class="badge" />`
}
return `<div>` + badge + `<span class="name" style="color:` + dc.Color + `">` + dc.From +

View File

@ -7,7 +7,7 @@ const (
CdMessage ClientDataType = iota // a normal message from the client meant to be broadcast
CdUsers // get a list of users
CdPing // ping the server to keep the connection alive
CdHelp // tells server to send help data again for buttons
CdAuth // get the auth levels of the user
)
type DataType int
@ -36,9 +36,9 @@ type CommandLevel int
// Command access levels
const (
CmdUser CommandLevel = iota
CmdMod
CmdAdmin
CmdlUser CommandLevel = iota
CmdlMod
CmdlAdmin
)
type EventType int

View File

@ -169,15 +169,15 @@ func handleHelpTemplate(w http.ResponseWriter, r *http.Request) {
data := Data{
Title: "Help",
Commands: getHelp(common.CmdUser),
Commands: getHelp(common.CmdlUser),
}
if len(r.URL.Query().Get("mod")) > 0 {
data.ModCommands = getHelp(common.CmdMod)
data.ModCommands = getHelp(common.CmdlMod)
}
if len(r.URL.Query().Get("admin")) > 0 {
data.AdminCommands = getHelp(common.CmdAdmin)
data.AdminCommands = getHelp(common.CmdlAdmin)
}
err = t.Execute(w, data)

View File

@ -19,6 +19,7 @@ const (
var (
currentName string
auth common.CommandLevel
names []string
filteredNames []string
)
@ -182,6 +183,8 @@ func recieve(v []js.Value) {
for _, i := range h.Data.([]interface{}) {
names = append(names, i.(string))
}
case common.CdAuth:
auth = h.Data.(common.CommandLevel)
}
case common.DTEvent:
d := chat.Data.(common.DataEvent)
@ -277,6 +280,7 @@ func isValidName(this js.Value, v []js.Value) interface{} {
func debugValues(v []js.Value) {
fmt.Printf("currentName %#v\n", currentName)
fmt.Printf("auth %#v\n", auth)
fmt.Printf("names %#v\n", names)
fmt.Printf("filteredNames %#v\n", filteredNames)
}