Skip to content

Commit 9d8f56c

Browse files
authored
Adding roles and admin command role permissions (#307)
# Changes * Config `Roles` * rolename => role permissions * Permissions can be widely permissive or narrow depending on specificity * Got rid of old "permission" property on users * Added a couple of "example" roles. * User/Admin are immutable. Users have no elevated permissions, admins have all. * Added `teleport` command.
1 parent 8bd9c33 commit 9d8f56c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+551
-295
lines changed

_datafiles/config.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,26 @@ Validation:
518518
- "join"
519519
- "register"
520520

521+
################################################################################
522+
#
523+
# Roles
524+
# Roles are string identifiers that map to a number of command names.
525+
# They are used to determine what admin commands a player has access to.
526+
# Two roles are build in and cannot be changed:
527+
# - "user" - Has no admin commands
528+
# - "admin" - Has all admin commands
529+
# Role sub-commands should be separated by a dot: room.info
530+
# Roles are more permissive the more they prefix.
531+
# Example: room prefixes room.info so would permit that action as well.
532+
# Role checks must be implemented wherever role-based restriction is desired:
533+
# if user.HasRolePermission(`room`) { /* Do something */ }
534+
#
535+
################################################################################
536+
Roles:
537+
builder: ["room.info", "build"]
538+
helper: ["paz", "teleport.playername", "locate"]
539+
540+
521541
################################################################################
522542
#
523543
# Modules
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
The <ansi fg="command">modify</ansi> command modifies player records:
22

3-
<ansi fg="command">modify permission [username] [user/mod/admin]</ansi> - Changes a users permission level.
4-
Note: Cannot downgrade an admin to a lower permission.
3+
<ansi fg="command">modify role [username] [newrole]</ansi> - Changes a users role.
4+
Note: Cannot downgrade an admin to a lower role.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The <ansi fg="command">teleport</ansi> command can be used in the following ways:
2+
3+
<ansi fg="command">teleport [room_id]</ansi> - e.g. <ansi fg="command">teleport 1</ansi>
4+
Move to a specific room id
5+
6+
<ansi fg="command">teleport [direction]</ansi> - e.g. <ansi fg="command">teleport east</ansi>
7+
Move through walls, across gaps, etc. to the first room found to the direction specified (if any).
8+
9+
<ansi fg="command">teleport [name]</ansi> - e.g. <ansi fg="command">teleport davey</ansi>
10+
Move to the room of a player matching the specified name.
11+

_datafiles/world/default/users/1.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
userid: 1
2-
permission: admin
2+
role: admin
33
username: admin
44
password: password
55
joined: 2024-10-31T13:28:45.395873-07:00
@@ -15,6 +15,8 @@ character:
1515
other adventurers, granting them strength, wisdom, or fortune beyond their wildest
1616
dreams. Conversely, he can also mete out justice to those who would seek to disrupt
1717
the balance of the world, swiftly and decisively.
18+
adjectives:
19+
- zombie
1820
roomid: 1
1921
zone: Frostfang
2022
raceid: 2
@@ -61,17 +63,16 @@ character:
6163
- buffid: 28
6264
onstartevent: true
6365
permabuff: true
64-
roundcounter: 20
66+
roundcounter: 3
6567
triggersleft: 1000000000
6668
- buffid: 29
6769
onstartevent: true
6870
permabuff: true
69-
roundcounter: 43
71+
roundcounter: 11
7072
triggersleft: 1000000000
7173
- buffid: 39
7274
onstartevent: false
7375
permabuff: true
74-
roundcounter: 2
7576
triggersleft: 1000000000
7677
equipment:
7778
weapon:
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
The <ansi fg="command">modify</ansi> command modifies player records:
22

3-
<ansi fg="command">modify permission [username] [user/mod/admin]</ansi> - Changes a users permission level.
4-
Note: Cannot downgrade an admin to a lower permission.
3+
<ansi fg="command">modify role [username] [newrole]</ansi> - Changes a users role.
4+
Note: Cannot downgrade an admin to a lower role.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The <ansi fg="command">teleport</ansi> command can be used in the following ways:
2+
3+
<ansi fg="command">teleport [room_id]</ansi> - e.g. <ansi fg="command">teleport 1</ansi>
4+
Move to a specific room id
5+
6+
<ansi fg="command">teleport [direction]</ansi> - e.g. <ansi fg="command">teleport east</ansi>
7+
Move through walls, across gaps, etc. to the first room found to the direction specified (if any).
8+
9+
<ansi fg="command">teleport [name]</ansi> - e.g. <ansi fg="command">teleport davey</ansi>
10+
Move to the room of a player matching the specified name.
11+

_datafiles/world/empty/users/1.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
userid: 1
22
permission: admin
3+
role: admin
34
username: admin
45
password: password
56
joined: 2024-10-31T13:28:45.395873-07:00

internal/configs/config.roles.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package configs
2+
3+
type Roles map[string]ConfigSliceString
4+
5+
func (m *Roles) Validate() {
6+
}
7+
8+
func GetRolesConfig() Roles {
9+
configDataLock.RLock()
10+
defer configDataLock.RUnlock()
11+
12+
if !configData.validated {
13+
configData.Validate()
14+
}
15+
16+
return configData.Roles
17+
}

internal/configs/configs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Config struct {
4747
Scripting Scripting `yaml:"Scripting"`
4848
SpecialRooms SpecialRooms `yaml:"SpecialRooms"`
4949
Validation Validation `yaml:"Validation"`
50-
50+
Roles Roles `yaml:"Roles"`
5151
// Plugins is a special case
5252
Modules Modules `yaml:"Modules"`
5353

@@ -198,6 +198,7 @@ func (c *Config) Validate() {
198198
c.SpecialRooms.Validate()
199199
c.Validation.Validate()
200200
c.Modules.Validate()
201+
c.Roles.Validate()
201202

202203
// nothing to do with LootGoblinIncludeRecentRooms
203204

internal/hooks/NewRound_InactivePlayers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ func InactivePlayers(e events.Event) events.ListenerReturn {
3232

3333
for _, user := range users.GetAllActiveUsers() {
3434

35-
if !kickMods && user.Permission == users.PermissionAdmin || user.Permission == users.PermissionMod {
35+
// If don't kick mods and they aren't a regular user, skip
36+
if !kickMods && user.Role != users.RoleUser {
3637
continue
3738
}
3839

0 commit comments

Comments
 (0)