Fixed feature flag handling #393

This commit is contained in:
Sven Heidemann 2023-10-03 10:51:40 +02:00
parent 9e12d84ba0
commit 75adc2285e
27 changed files with 243 additions and 232 deletions

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "",
"AuthorEmail": "",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -26,7 +26,7 @@ class FeatureFlagsSettings(ConfigurationModelABC):
FeatureFlagsEnum.game_server.value: False, # 25.09.2023 #366
FeatureFlagsEnum.sync_xp.value: False, # 25.09.2023 #366
FeatureFlagsEnum.short_role_name.value: False, # 28.09.2023 #378
FeatureFlagsEnum.technician_full_access.value: False, # 28.09.2023 #393
FeatureFlagsEnum.technician_full_access.value: False, # 03.10.2023 #393
}
def __init__(self, **kwargs: dict):

View File

@ -7,7 +7,6 @@ from bot_data.abc.technician_config_repository_abc import TechnicianConfigReposi
from bot_data.model.server import Server
from bot_data.model.technician_config import TechnicianConfig
from bot_data.service.server_config_seeder import ServerConfigSeeder
from bot_data.service.technician_config_seeder import TechnicianConfigSeeder
class ConfigService:
@ -17,7 +16,6 @@ class ConfigService:
services: ServiceProviderABC,
technician_config_repo: TechnicianConfigRepositoryABC,
server_config_repo: ServerConfigRepositoryABC,
tech_seeder: TechnicianConfigSeeder,
server_seeder: ServerConfigSeeder,
):
self._config = config
@ -25,13 +23,9 @@ class ConfigService:
self._technician_config_repo = technician_config_repo
self._server_config_repo = server_config_repo
self._tech_seeder = tech_seeder
self._server_seeder = server_seeder
async def reload_technician_config(self):
if not self._technician_config_repo.does_technician_config_exists():
await self._tech_seeder.seed()
def reload_technician_config(self):
technician_config = self._technician_config_repo.get_technician_config()
self._config.add_configuration(TechnicianConfig, technician_config)
self._config.add_configuration(FeatureFlagsSettings, FeatureFlagsSettings(**technician_config.feature_flags))

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -11,6 +11,7 @@ from bot_data.model.technician_config import TechnicianConfig
from bot_data.model.technician_id_config import TechnicianIdConfig
from bot_data.model.technician_ping_url_config import TechnicianPingUrlConfig
from bot_data.model.user_role_enum import UserRoleEnum
from bot_data.service.technician_config_seeder import TechnicianConfigSeeder
from bot_graphql.abc.query_abc import QueryABC
@ -23,6 +24,7 @@ class TechnicianConfigMutation(QueryABC):
technician_configs: TechnicianConfigRepositoryABC,
db: DatabaseContextABC,
config_service: ConfigService,
tech_seeder: TechnicianConfigSeeder,
):
QueryABC.__init__(self, "TechnicianConfigMutation")
@ -32,10 +34,14 @@ class TechnicianConfigMutation(QueryABC):
self._technician_configs = technician_configs
self._db = db
self._config_service = config_service
self._tech_seeder = tech_seeder
self.set_field("updateTechnicianConfig", self.resolve_update_technician_config)
def resolve_update_technician_config(self, *_, input: dict):
if not self._technician_configs.does_technician_config_exists():
self._bot.loop.create_task(self._tech_seeder.seed())
technician_config = self._technician_configs.get_technician_config()
self._can_user_mutate_data(Route.get_user().users[0].server, UserRoleEnum.technician)
@ -53,11 +59,16 @@ class TechnicianConfigMutation(QueryABC):
technician_config.cache_max_messages = (
input["cacheMaxMessages"] if "cacheMaxMessages" in input else technician_config.cache_max_messages
)
old_feature_flags = technician_config.feature_flags
technician_config.feature_flags = (
dict(zip([x["key"] for x in input["featureFlags"]], [x["value"] for x in input["featureFlags"]]))
if "featureFlags" in input
else technician_config.feature_flags
)
for old_flag in old_feature_flags:
if old_flag not in technician_config.feature_flags:
technician_config.feature_flags[old_flag] = False
technician_config.ping_urls = (
List(str, input["pingURLs"]) if "pingURLs" in input else technician_config.ping_urls
)
@ -75,6 +86,7 @@ class TechnicianConfigMutation(QueryABC):
self._update_technician_ids(technician_config)
self._db.save_changes()
self._config_service.reload_technician_config()
return technician_config
def _update_ping_urls(self, new_config: TechnicianConfig):
@ -109,5 +121,3 @@ class TechnicianConfigMutation(QueryABC):
continue
self._technician_configs.add_technician_id_config(TechnicianIdConfig(technician_id))
self._bot.loop.create_task(self._config_service.reload_technician_config())

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "",
"AuthorEmail": "",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "",
"AuthorEmail": "",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "",
"AuthorEmail": "",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "",
"AuthorEmail": "",

View File

@ -19,4 +19,4 @@ class ConfigExtension(ApplicationExtensionABC):
logger: LoggerABC = services.get_service(LoggerABC)
logger.debug(__name__, "Config extension started")
config: ConfigService = services.get_service(ConfigService)
await config.reload_technician_config()
config.reload_technician_config()

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "",
"AuthorEmail": "",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "",
"AuthorEmail": "",

View File

@ -29,10 +29,11 @@ class PermissionService(PermissionServiceABC):
self._technician_configs = technician_configs
def _has_member_role(self, member: discord.Member, team_member_type: TeamMemberTypeEnum) -> bool:
self._logger.debug(__name__, f"Checking permissions for {member.name}")
if member is None or member.guild is None:
return False
self._logger.debug(__name__, f"Checking is member {member.name} {team_member_type.value}")
try:
server = self._servers.get_server_by_discord_id(member.guild.id)
config = self._server_configs.get_server_config_by_server(server.id)
@ -58,10 +59,11 @@ class PermissionService(PermissionServiceABC):
)
def is_member_technician(self, member: discord.Member) -> bool:
self._logger.debug(__name__, f"Checking is member {member.name} technician")
if member is None or member.guild is None:
return False
self._logger.debug(__name__, f"Checking is member {member.name} technician")
try:
tech_config = self._technician_configs.get_technician_config()
if member.id in tech_config.technician_ids:

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "",
"AuthorEmail": "",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -1,6 +1,6 @@
{
"name": "kdb-web",
"version": "1.1.9",
"version": "1.1.10",
"scripts": {
"ng": "ng",
"update-version": "ts-node update-version.ts",

View File

@ -34,11 +34,11 @@ export class AuthGuard implements CanActivate {
const role = route.data["role"];
const memberRole = route.data["memberRole"];
const authUser = await this.authService.getLoggedInUser();
const isTechnician = (authUser?.users?.map(u => u.isTechnician).filter(u => u) ?? []).length > 0;
if (role !== undefined) {
this.authService.hasUserPermission(role).then(async hasPermission => {
let authUser = await this.authService.getLoggedInUser();
let isTechnician = authUser?.users?.map(u => u.isTechnician).filter(u => u) ?? [];
if (!hasPermission && !isTechnician) {
this.router.navigate(["/dashboard"]);
return false;
@ -49,12 +49,15 @@ export class AuthGuard implements CanActivate {
if (memberRole !== undefined) {
let userHasAccess = false;
let authUser = await this.authService.getLoggedInUser();
let server = route.params["serverId"];
if (!authUser || !authUser.users) {
if (this.sidebarService.hasFeature("TechnicianFullAccess") && isTechnician) {
return true;
}
if (!authUser || !authUser.users) {
return false;
}
authUser?.users?.forEach(u => {
if (u.server === +(server ?? 0)) {
if (

View File

@ -192,21 +192,23 @@ export class SidebarService {
this.authService.hasUserPermission(AuthRoles.Admin).then(async hasPermission => {
let authUser = await this.authService.getLoggedInUser();
let user: UserDTO | null = authUser?.users?.find(u => u.server == this.server?.id) ?? null;
let isTechnician = authUser?.users?.map(u => u.isTechnician).filter(u => u) ?? [];
let isTechnician = (authUser?.users?.map(u => u.isTechnician).filter(u => u) ?? []).length > 0;
let isTechnicianAndFullAccessActive = this.hasFeature("TechnicianFullAccess") && isTechnician;
console.log(this.hasFeature("TechnicianFullAccess"))
if (build || this.menuItems$.value.length == 0) {
await this.buildMenu(user, hasPermission, isTechnician.length > 0);
await this.buildMenu(user, hasPermission, isTechnician);
}
if (this.server) {
this.serverMenu.visible = true;
this.serverMembers.visible = !!user?.isModerator;
this.serverAutoRoles.visible = this.hasFeature("AutoRoleModule") ? !!user?.isModerator : false;
this.serverLevels.visible = this.hasFeature("LevelModule") ? !!user?.isModerator : false;
this.serverAchievements.visible = this.hasFeature("AchievementsModule") ? !!user?.isModerator : false;
this.serverShortRoleNames.visible = this.hasFeature("ShortRoleName") ? !!user?.isAdmin : false;
this.serverMembers.visible = isTechnicianAndFullAccessActive || user?.isModerator;
this.serverAutoRoles.visible = isTechnicianAndFullAccessActive || this.hasFeature("AutoRoleModule") && user?.isModerator;
this.serverLevels.visible = isTechnicianAndFullAccessActive || this.hasFeature("LevelModule") && user?.isModerator;
this.serverAchievements.visible = isTechnicianAndFullAccessActive || this.hasFeature("AchievementsModule") && user?.isModerator;
this.serverShortRoleNames.visible = isTechnicianAndFullAccessActive || this.hasFeature("ShortRoleName") && user?.isAdmin;
this.serverConfig.visible = !!user?.isAdmin || isTechnician.length > 0;
this.serverConfig.visible = isTechnicianAndFullAccessActive || user?.isAdmin;
} else {
this.serverMenu.visible = false;
}
@ -220,7 +222,7 @@ export class SidebarService {
});
}
private hasFeature(key: string): boolean {
public hasFeature(key: string): boolean {
const flag = this.featureFlags.filter(flag => flag.key == key);
if (flag.length == 0) {
return false;

View File

@ -2,6 +2,6 @@
"WebVersion": {
"Major": "1",
"Minor": "1",
"Micro": "9"
"Micro": "10"
}
}