Merge 4c3a42418818faf537be4897d919c564857b75f0 into 9b771cbac632197c117f248972706893f35084b1

This commit is contained in:
mcmontag 2024-07-30 06:19:26 +00:00 committed by GitHub
commit 6de626149b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 134 additions and 4 deletions

View File

@ -37,6 +37,11 @@ const (
UUIDSize = 16
TokenSize = 32
// feature access groups
DEV_STAFF = "DEV_STAFF"
CONTRIBUTOR = "CONTRIBUTOR"
EVERYONE = "EVERYONE"
)
var (

View File

@ -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 ""
}

View File

@ -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
}

View File

@ -103,6 +103,10 @@ func setupDb(tx *sql.Tx) error {
`ALTER TABLE accounts ADD COLUMN IF NOT EXISTS discordId VARCHAR(32) UNIQUE DEFAULT NULL`,
`ALTER TABLE accounts ADD COLUMN IF NOT EXISTS googleId VARCHAR(32) UNIQUE DEFAULT NULL`,
// ----------------------------------
// MIGRATION 004
`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 {

48
db/featureflags.go Normal file
View File

@ -0,0 +1,48 @@
/*
Copyright (C) 2024 Pagefault Games
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package db
import (
"github.com/pagefaultgames/rogueserver/defs"
)
func GetEnabledFeatureFlags() ([]defs.FeatureFlag, error) {
var activeFlags []defs.FeatureFlag
results, err := handle.Query("SELECT name, accessLevel FROM featureFlags WHERE enabled = 1")
if err != nil {
return activeFlags, err
}
defer results.Close()
for results.Next() {
var flag defs.FeatureFlag
err = results.Scan(&flag.Name, &flag.AccessLevel)
if err != nil {
return activeFlags, err
}
activeFlags = append(activeFlags, flag)
}
return activeFlags, nil
}

23
defs/featureflags.go Normal file
View File

@ -0,0 +1,23 @@
/*
Copyright (C) 2024 Pagefault Games
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package defs
type FeatureFlag struct {
Name string
AccessLevel string // DEV_STAFF | CONTRIBUTOR | EVERYONE
}