mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-04-03 03:27:13 +08:00
Merge 4c3a42418818faf537be4897d919c564857b75f0 into 9b771cbac632197c117f248972706893f35084b1
This commit is contained in:
commit
6de626149b
@ -37,6 +37,11 @@ const (
|
||||
|
||||
UUIDSize = 16
|
||||
TokenSize = 32
|
||||
|
||||
// feature access groups
|
||||
DEV_STAFF = "DEV_STAFF"
|
||||
CONTRIBUTOR = "CONTRIBUTOR"
|
||||
EVERYONE = "EVERYONE"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -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 ""
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
4
db/db.go
4
db/db.go
@ -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
48
db/featureflags.go
Normal 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
23
defs/featureflags.go
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user