Skip to content

Commit

Permalink
refactor: Simplify, hardcode (empty) access groups
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmontag committed Jul 30, 2024
1 parent 5c8b071 commit 4c3a424
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 85 deletions.
5 changes: 5 additions & 0 deletions api/account/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const (

UUIDSize = 16
TokenSize = 32

// feature access groups
DEV_STAFF = "DEV_STAFF"
CONTRIBUTOR = "CONTRIBUTOR"
EVERYONE = "EVERYONE"
)

var (
Expand Down
16 changes: 16 additions & 0 deletions api/account/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,19 @@ func RetrieveDiscordId(code string) (string, error) {

return user.Id, nil
}

// TODO: fetch these instead of hardcoding them
var devsAndStaff = map[string]bool{}
var contributors = map[string]bool{}

func GetAccessGroupByDiscordRole(discordId string) (group string) {
if devsAndStaff[discordId] {
return DEV_STAFF
}

if contributors[discordId] {
return CONTRIBUTOR
}

return ""
}
42 changes: 38 additions & 4 deletions api/account/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,54 @@ import (
)

type InfoResponse struct {
Username string `json:"username"`
DiscordId string `json:"discordId"`
GoogleId string `json:"googleId"`
LastSessionSlot int `json:"lastSessionSlot"`
Username string `json:"username"`
DiscordId string `json:"discordId"`
GoogleId string `json:"googleId"`
LastSessionSlot int `json:"lastSessionSlot"`
FeatureFlags []string `json:"featureFlags"`
}

// /account/info - get account info
func Info(username string, discordId string, googleId string, uuid []byte) (InfoResponse, error) {
slot, _ := db.GetLatestSessionSaveDataSlot(uuid)
featureFlags := getFeatureFlags(discordId)
response := InfoResponse{
Username: username,
LastSessionSlot: slot,
DiscordId: discordId,
GoogleId: googleId,
FeatureFlags: featureFlags,
}
return response, nil
}

func getFeatureFlags(discordId string) []string {
var flags []string

enabledFlags, err := db.GetEnabledFeatureFlags()
if err != nil {
return flags
}

for _, flag := range enabledFlags {
var hasAccess = false

if flag.AccessLevel == EVERYONE {
hasAccess = true
} else {
accessGroup := GetAccessGroupByDiscordRole(discordId)

if flag.AccessLevel == DEV_STAFF {
hasAccess = accessGroup == DEV_STAFF
} else if flag.AccessLevel == CONTRIBUTOR {
hasAccess = accessGroup == CONTRIBUTOR || accessGroup == DEV_STAFF
}
}

if hasAccess {
flags = append(flags, flag.Name)
}
}

return flags
}
44 changes: 0 additions & 44 deletions api/featureflags.go

This file was deleted.

3 changes: 1 addition & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ func setupDb(tx *sql.Tx) error {

// ----------------------------------
// MIGRATION 004
`CREATE TABLE IF NOT EXISTS featureFlags (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(64) UNIQUE NOT NULL, enabled TINYINT(1) NOT NULL, percentage TINYINT(3) NOT NULL DEFAULT 0, CONSTRAINT chk_featureFlagsPercentage_withinRange CHECK(percentage >= 0 AND percentage <= 100))`,
`CREATE TABLE IF NOT EXISTS accountFeatureFlagOverrides (accountId BINARY(16) NOT NULL, flagId INT(11) NOT NULL, enabled TINYINT(1) NOT NULL, PRIMARY KEY (accountId, flagId), FOREIGN KEY (accountId) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (flagId) REFERENCES featureFlags (id) ON DELETE CASCADE ON UPDATE CASCADE)`,
`CREATE TABLE IF NOT EXISTS featureFlags (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(64) UNIQUE NOT NULL, accessLevel VARCHAR(16), CONSTRAINT chk_featureFlags_accessLevel CHECK(accessLevel IN ('DEV_STAFF', 'CONTRIBUTOR', 'EVERYONE')))`,
}

for _, q := range queries {
Expand Down
34 changes: 6 additions & 28 deletions db/featureflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package db

import "github.com/pagefaultgames/rogueserver/defs"
import (
"github.com/pagefaultgames/rogueserver/defs"
)

func GetEnabledFeatureFlags() ([]defs.FeatureFlag, error) {
var activeFlags []defs.FeatureFlag

results, err := handle.Query("SELECT name, percentage FROM featureFlags WHERE enabled = 1")
results, err := handle.Query("SELECT name, accessLevel FROM featureFlags WHERE enabled = 1")

if err != nil {
return activeFlags, err
Expand All @@ -33,7 +35,8 @@ func GetEnabledFeatureFlags() ([]defs.FeatureFlag, error) {
for results.Next() {
var flag defs.FeatureFlag

err = results.Scan(&flag.Name, &flag.Percentage)
err = results.Scan(&flag.Name, &flag.AccessLevel)

if err != nil {
return activeFlags, err
}
Expand All @@ -43,28 +46,3 @@ func GetEnabledFeatureFlags() ([]defs.FeatureFlag, error) {

return activeFlags, nil
}

func GetFeatureFlagOverrides(accountId []byte) ([]defs.FeatureFlagOverride, error) {
var overrides []defs.FeatureFlagOverride

results, err := handle.Query("SELECT ff.name, o.enabled FROM accoutFeatureFlagOverrides o JOIN featureFlags ff ON o.flagId = ff.id WHERE accountId = ?", accountId)

if err != nil {
return overrides, err
}

defer results.Close()

for results.Next() {
var override defs.FeatureFlagOverride

err = results.Scan(&override.FlagName, &override.Enabled)
if err != nil {
return overrides, err
}

overrides = append(overrides, override)
}

return overrides, nil
}
9 changes: 2 additions & 7 deletions defs/featureflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
package defs

type FeatureFlag struct {
Name string
Percentage int
}

type FeatureFlagOverride struct {
FlagName string
Enabled bool
Name string
AccessLevel string // DEV_STAFF | CONTRIBUTOR | EVERYONE
}

0 comments on commit 4c3a424

Please sign in to comment.