Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature flags (WIP) #47

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
4 changes: 4 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions db/featureflags.go
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions defs/featureflags.go
Original file line number Diff line number Diff line change
@@ -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
}