Make privilege methods peer independent (#52)

master
HimbeerserverDE 2021-03-20 18:46:32 +01:00
parent 4e5b424297
commit ac1ce7e31d
No known key found for this signature in database
GPG Key ID: 1A651504791E6A8B
2 changed files with 33 additions and 43 deletions

View File

@ -247,35 +247,23 @@ func init() {
var r string
name := param
var p2 *Peer
if name == "" {
p2 = p
name = p.Username()
r += "Your privileges: "
} else {
p2 = PeerByUsername(name)
r += name + "'s privileges: "
}
if name != "" && !IsOnline(name) {
p.SendChatMsg(name + " is not online.")
return
}
privs, err := p2.Privs()
privs, err := Privs(name)
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to get the privileges.")
return
}
var privnames []string
for k, v := range privs {
if v {
privnames = append(privnames, k)
}
}
eprivs := encodePrivs(privs)
p.SendChatMsg(r + strings.Join(privnames, " "))
p.SendChatMsg(r + strings.Replace(eprivs, "|", " ", -1))
})
RegisterChatCommand("grant",
@ -285,34 +273,30 @@ func init() {
func(p *Peer, param string) {
name := strings.Split(param, " ")[0]
var privnames string
var p2 *Peer
if len(strings.Split(param, " ")) < 2 {
p2 = p
privnames = name
name = p.Username()
} else {
p2 = PeerByUsername(name)
privnames = strings.Split(param, " ")[1]
}
if len(strings.Split(param, " ")) >= 2 && !IsOnline(name) {
p.SendChatMsg(name + " is not online.")
return
}
privs, err := p2.Privs()
privs, err := Privs(name)
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to get the privileges.")
return
}
splitprivs := strings.Split(strings.Replace(privnames, " ", "", -1), ",")
for i := range splitprivs {
privs[splitprivs[i]] = true
}
err = p2.SetPrivs(privs)
err = SetPrivs(name, privs)
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to get the privileges.")
p.SendChatMsg("An internal error occured while attempting to set the privileges.")
return
}
@ -326,31 +310,27 @@ func init() {
func(p *Peer, param string) {
name := strings.Split(param, " ")[0]
var privnames string
var p2 *Peer
if len(strings.Split(param, " ")) < 2 {
p2 = p
privnames = name
name = p.Username()
} else {
p2 = PeerByUsername(name)
privnames = strings.Split(param, " ")[1]
}
if len(strings.Split(param, " ")) >= 2 && !IsOnline(name) {
p.SendChatMsg(name + " is not online.")
return
}
privs, err := p2.Privs()
privs, err := Privs(name)
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to get the privileges.")
return
}
splitprivs := strings.Split(strings.Replace(privnames, " ", "", -1), ",")
for i := range splitprivs {
privs[splitprivs[i]] = false
}
err = p2.SetPrivs(privs)
err = SetPrivs(name, privs)
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to set the privileges.")

View File

@ -115,15 +115,15 @@ func readPrivItem(db *sql.DB, name string) (string, error) {
return r, err
}
// Privs returns the privileges of the Peer
func (p *Peer) Privs() (map[string]bool, error) {
// Privs returns the privileges of a player
func Privs(name string) (map[string]bool, error) {
db, err := initAuthDB()
if err != nil {
return nil, err
}
defer db.Close()
eprivs, err := readPrivItem(db, p.Username())
eprivs, err := readPrivItem(db, name)
if err != nil {
return nil, err
}
@ -131,15 +131,20 @@ func (p *Peer) Privs() (map[string]bool, error) {
return decodePrivs(eprivs), nil
}
// SetPrivs sets the privileges for the Peer
func (p *Peer) SetPrivs(privs map[string]bool) error {
// Privs returns the privileges of the Peer
func (p *Peer) Privs() (map[string]bool, error) {
return Privs(p.Username())
}
// SetPrivs sets the privileges for a player
func SetPrivs(name string, privs map[string]bool) error {
db, err := initAuthDB()
if err != nil {
return err
}
defer db.Close()
err = modPrivItem(db, p.Username(), encodePrivs(privs))
err = modPrivItem(db, name, encodePrivs(privs))
if err != nil {
return err
}
@ -147,6 +152,11 @@ func (p *Peer) SetPrivs(privs map[string]bool) error {
return nil
}
// SetPrivs sets the privileges for the Peer
func (p *Peer) SetPrivs(privs map[string]bool) error {
return SetPrivs(p.Username(), privs)
}
// CheckPrivs reports if the Peer has all ofthe specified privileges
func (p *Peer) CheckPrivs(req map[string]bool) (bool, error) {
privs, err := p.Privs()